Skip to content

Commit 6e7172e

Browse files
committed
ast: General improvements
1 parent f979f2a commit 6e7172e

File tree

8 files changed

+162
-227
lines changed

8 files changed

+162
-227
lines changed

src/main/java/org/piccode/ast/IdentifierAst.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public PiccodeValue execute(Integer frame) {
6464
var note = new PiccodeSimpleNote("Track size: " + ctx.getFramesCount());
6565
err.addNote(note);
6666

67-
note = new PiccodeSimpleNote("Symbol table dump: " + sb.toString());
67+
note = new PiccodeSimpleNote("Symbol table dump: \n" + sb.toString());
6868
err.addNote(note);
6969
throw err;
7070
}

src/main/java/org/piccode/ast/ImportAst.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void executeLifted(List<Ast> moduleNodes, List<Ast> nestedLifted, Intege
122122
}
123123
}
124124

125-
private List<Ast> loadModuleFromStdLib(String module, Integer frame) {
125+
public List<Ast> loadModuleFromStdLib(String module, Integer frame) {
126126
var storage = getAppStorage();
127127
var paths = List.of(storage, "./");
128128
var nodes = new ArrayList<Ast>();

src/main/java/org/piccode/ast/ImportModuleCreateAst.java

Lines changed: 62 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,97 @@
1818
import org.piccode.rt.PiccodeBoolean;
1919
import org.piccode.rt.PiccodeException;
2020
import org.piccode.rt.PiccodeInfo;
21+
import org.piccode.rt.PiccodeModule;
2122
import org.piccode.rt.PiccodeUnit;
2223
import org.piccode.rt.PiccodeValue;
2324

2425
/**
2526
*
2627
* @author hexaredecimal
2728
*/
28-
public class ImportAst extends Ast {
29+
public class ImportModuleCreateAst extends Ast {
2930

30-
public String path;
31-
public List<Ast> lifted;
31+
public String name;
32+
public ImportAst import_;
33+
private String path;
3234

33-
public ImportAst(String path) {
34-
this.path = path;
35-
this.lifted = new ArrayList<>();
36-
}
37-
38-
public ImportAst(String path, List<Ast> lifted) {
39-
this.path = path;
40-
this.lifted = lifted;
41-
}
42-
43-
@Override
44-
public String toString() {
45-
return "import " + path;
35+
public ImportModuleCreateAst(String name, ImportAst import_) {
36+
this.name = name;
37+
this.import_ = import_;
38+
this.path = import_.path;
4639
}
4740

4841
@Override
4942
public PiccodeValue execute(Integer frame) {
50-
var nodes = loadModuleFromStdLib(path, frame);
43+
var nodes = import_.loadModuleFromStdLib(path, frame);
44+
45+
var final_nodes = new ArrayList<Ast>();
46+
var lifted = import_.lifted;
5147

5248
if (lifted.isEmpty()) {
53-
nodes.forEach(node -> node.execute(frame));
54-
return new PiccodeUnit();
49+
nodes.forEach(node -> {
50+
if (node instanceof ModuleAst mod && mod.name.equals(name)) {
51+
final_nodes.addAll(mod.nodes);
52+
} else {
53+
final_nodes.add(node);
54+
}
55+
});
56+
57+
var top = Context.top.getValue(name);
58+
if (top != null && top instanceof PiccodeModule module) {
59+
final_nodes.addAll(module.nodes);
60+
var mod = new ModuleAst(name, final_nodes);
61+
var result = mod.execute(frame);
62+
Context.top.putLocal(name, result);
63+
return result;
64+
} else {
65+
var mod = new ModuleAst(name, final_nodes);
66+
var result = mod.execute(frame);
67+
return result;
68+
}
5569
}
5670

5771
for (var liftedNode : lifted) {
58-
executeLifted(nodes, liftedNode, frame);
72+
executeLifted(nodes, liftedNode, frame, final_nodes);
73+
}
74+
75+
var top = Context.top.getValue(name);
76+
if (top != null && top instanceof PiccodeModule module) {
77+
final_nodes.addAll(module.nodes);
78+
var mod = new ModuleAst(name, final_nodes);
79+
var result = mod.execute(frame);
80+
Context.top.putLocal(name, result);
81+
return result;
82+
} else {
83+
var mod = new ModuleAst(name, final_nodes);
84+
var result = mod.execute(frame);
85+
return result;
5986
}
6087

61-
return new PiccodeUnit();
6288
}
6389

64-
private void executeLifted(List<Ast> moduleNodes, Ast liftedNode, Integer frame) {
90+
private void executeLifted(List<Ast> moduleNodes, Ast liftedNode, Integer frame, List<Ast> final_nodes) {
6591
if (liftedNode instanceof ImportId id) {
6692
for (var node : moduleNodes) {
6793
if (node instanceof ImportAst imp) {
6894
imp.execute(frame);
6995
}
7096
if (node instanceof StatementList sts) {
71-
executeLifted(sts.nodes, id, frame);
97+
executeLifted(sts.nodes, id, frame, final_nodes);
7298
return;
7399
}
74100
if (node instanceof FunctionAst func && func.name.equals(id.text)) {
75-
func.execute(frame);
101+
final_nodes.add(node);
76102
return;
77103
}
78104
if (node instanceof ModuleAst mod && mod.name.equals(id.text)) {
79-
mod.execute(frame);
105+
if (id.text.equals(name)) {
106+
final_nodes.addAll(mod.nodes);
107+
return;
108+
}
109+
110+
mod.createSymbol = false;
111+
final_nodes.add(node);
80112
return;
81113
}
82114
}
@@ -91,12 +123,12 @@ private void executeLifted(List<Ast> moduleNodes, Ast liftedNode, Integer frame)
91123
imp.execute(frame);
92124
}
93125
if (node instanceof StatementList sts) {
94-
executeLifted(sts.nodes, lift, frame);
126+
executeLifted(sts.nodes, lift, frame, final_nodes);
95127
return;
96128
}
97129
if (node instanceof ModuleAst mod && mod.name.equals(lift.text)) {
98130
// Recursively execute lifted symbols from inside the module
99-
executeLifted(mod.nodes, lift.nodes, frame);
131+
executeLifted(mod.nodes, lift.nodes, frame, final_nodes);
100132
return;
101133
}
102134
if (node instanceof FunctionAst func && func.name.equals(lift.text)) {
@@ -116,103 +148,14 @@ private void executeLifted(List<Ast> moduleNodes, Ast liftedNode, Integer frame)
116148
}
117149
}
118150

119-
private void executeLifted(List<Ast> moduleNodes, List<Ast> nestedLifted, Integer frame) {
151+
private void executeLifted(List<Ast> moduleNodes, List<Ast> nestedLifted, Integer frame, List<Ast> final_nodes) {
120152
for (var lifted : nestedLifted) {
121-
executeLifted(moduleNodes, lifted, frame);
122-
}
123-
}
124-
125-
private List<Ast> loadModuleFromStdLib(String module, Integer frame) {
126-
var storage = getAppStorage();
127-
var paths = List.of(storage, "./");
128-
var nodes = new ArrayList<Ast>();
129-
var _file = findModule(module, paths);
130-
if (_file == null) {
131-
var err = new PiccodeException(file, line, column, "Invalid module " + module.replaceAll("/", "."));
132-
err.frame = frame;
133-
throw err;
134-
}
135-
136-
var path_ = _file.getPath();
137-
if (Context.isImportCached(path_)) {
138-
return Context.getCached(path_);
139-
}
140-
141-
var files = _file.listFiles();
142-
143-
for (var fp : files) {
144-
if (fp.getName().endsWith(".pics")) {
145-
var code = readFile(fp);
146-
if (code == null) {
147-
var err = new PiccodeException(file, line, column, "Invalid module " + module.replaceAll("/", "."));
148-
err.frame = frame;
149-
throw err;
150-
}
151-
var program = (StatementList) _import(fp.getAbsolutePath(), code);
152-
var noImports = program.nodes
153-
.stream()
154-
.filter((value) -> {
155-
if (value instanceof ImportAst) {
156-
value.execute(frame);
157-
}
158-
return !(value instanceof ImportAst);
159-
})
160-
.toList();
161-
nodes.addAll(noImports);
162-
}
163-
}
164-
if (!Context.isImportCached(path)) {
165-
Context.cacheImport(path, lifted);
166-
}
167-
return nodes;
168-
}
169-
170-
private Ast _import(String file, String code) {
171-
Context.top.putLocal("true", new PiccodeBoolean("true"));
172-
Context.top.putLocal("false", new PiccodeBoolean("false"));
173-
174-
try {
175-
return Compiler.program(file, code);
176-
} catch (PiccodeException e) {
177-
throw e;
178-
}
179-
}
180-
181-
private String readFile(File fp) {
182-
StringBuilder sb = new StringBuilder();
183-
Scanner sc;
184-
try {
185-
sc = new Scanner(fp);
186-
while (sc.hasNext()) {
187-
sb.append(sc.nextLine()).append("\n");
188-
}
189-
return sb.toString();
190-
} catch (FileNotFoundException ex) {
191-
return null;
153+
executeLifted(moduleNodes, lifted, frame, final_nodes);
192154
}
193155
}
194156

195157
@Override
196158
public String codeGen(TargetEnvironment target) {
197-
return "";
198-
}
199-
200-
private String getAppStorage() {
201-
try {
202-
String path = ImportAst.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
203-
return new File(path).getParentFile().getPath();
204-
} catch (URISyntaxException ex) {
205-
return "./";
206-
}
207-
}
208-
209-
private File findModule(String module, List<String> of) {
210-
for (var dir : of) {
211-
var fp = new File(dir + "/" + module);
212-
if (fp.isDirectory()) {
213-
return fp;
214-
}
215-
}
216-
return null;
159+
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
217160
}
218161
}

src/main/java/org/piccode/ast/ModuleAst.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
public class ModuleAst extends Ast {
1414
public String name;
1515
public List<Ast> nodes;
16+
public boolean createSymbol;
1617

1718
public ModuleAst(String name, List<Ast> nodes) {
1819
this.name = name;
1920
this.nodes = nodes;
21+
this.createSymbol = true;
2022
}
2123

2224
@Override
@@ -33,15 +35,20 @@ public String toString() {
3335

3436
@Override
3537
public PiccodeValue execute(Integer frame) {
36-
if (Context.modules.containsKey(name)) {
37-
var mod = Context.modules.get(name);
38-
mod.nodes.addAll(nodes);
38+
var ctx = Context.top;
39+
var module = ctx.getValue(name);
40+
41+
if ((module == null) || !(module instanceof PiccodeModule)) {
42+
var mod = new PiccodeModule(name, nodes);
43+
if (createSymbol) {
44+
ctx.putLocal(name, mod);
45+
}
3946
return mod;
40-
} else {
41-
var module = new PiccodeModule(name, nodes);
42-
Context.modules.put(name, module);
43-
return module;
4447
}
48+
49+
var mod = (PiccodeModule) module;
50+
mod.nodes.addAll(nodes);
51+
return mod;
4552
}
4653

4754
@Override

src/main/java/org/piccode/ast/PiccodeVisitor.java

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -112,45 +112,15 @@ public Ast visitArg(ArgContext ctx) {
112112
var tok = ctx.ID().getSymbol();
113113
var name = ctx.ID().getText();
114114
if (ctx.ASSIGN() != null) {
115-
var value = visitLiteral_expr(ctx.literal_expr());
115+
var value = visitExpr(ctx.expr());
116116
var result = new Arg(name, value);
117-
result.line = tok.getLine();
118-
result.column = tok.getCharPositionInLine();
119-
result.file = fileName;
120-
return result;
117+
result.export = ctx.USE() != null;
118+
return finalizeAstNode(result, tok);
121119
}
122120

123121
var result = new Arg(name);
124-
result.line = tok.getLine();
125-
result.column = tok.getCharPositionInLine();
126-
result.file = fileName;
127-
return result;
128-
}
129-
130-
@Override
131-
public Ast visitLiteral_expr(Literal_exprContext ctx) {
132-
if (ctx.NUMBER() != null) {
133-
return visitNumber(ctx.NUMBER());
134-
}
135-
136-
if (ctx.STRING() != null) {
137-
return visitString(ctx.STRING());
138-
}
139-
140-
if (ctx.array() != null) {
141-
return visitArray(ctx.array());
142-
}
143-
if (ctx.tuple() != null) {
144-
return visitTuple(ctx.tuple());
145-
}
146-
if (ctx.object() != null) {
147-
return visitObject(ctx.object());
148-
}
149-
150-
var tok = ctx.getStart();
151-
var err = new PiccodeException(fileName, tok.getLine(), tok.getCharPositionInLine(), "Invalid literal expression");
152-
err.frame = null;
153-
throw err;
122+
result.export = ctx.USE() != null;
123+
return finalizeAstNode(result, tok);
154124
}
155125

156126
@Override

0 commit comments

Comments
 (0)