Skip to content

Commit 176982e

Browse files
committed
ast: Delete temporary symbols created from executing modules
1 parent 79cc55e commit 176982e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
8181
if (rhs instanceof IdentifierAst _id) {
8282
for (var node : mod.nodes) {
8383
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
84-
return node.execute(frame);
84+
var result = node.execute(frame);
85+
ctx.deleteLocal(vd.name);
86+
return result;
8587
}
8688
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
8789
node.execute(frame);
8890
var result = ctx.getValue(_id.text);
91+
ctx.deleteLocal(func.name);
8992
if (result == null) {
9093
var err = new PiccodeException(func.file, func.line, func.column, "Function `" + Chalk.on(_id.text).red() + "` is not defined");
9194
err.frame = frame;
@@ -99,7 +102,9 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
99102
return result;
100103
}
101104
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
102-
return node.execute(frame);
105+
var result = node.execute(frame);
106+
ctx.deleteLocal(_mod.name);
107+
return result;
103108
}
104109
}
105110

@@ -119,17 +124,23 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
119124
var _id = (IdentifierAst) call.expr;
120125
for (var node : mod.nodes) {
121126
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
122-
return node.execute(frame);
127+
var result = node.execute(frame);
128+
ctx.deleteLocal(vd.name);
129+
return result;
123130
}
124131
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
125-
return Ast.safeExecute(frame, func, (expr) -> {
132+
var result = Ast.safeExecute(frame, func, (expr) -> {
126133
node.execute(frame);
127134
return call.execute(frame);
128135
});
136+
ctx.deleteLocal(func.name);
137+
return result;
129138
}
130139
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
131140
node.execute(frame);
132-
return ctx.getValue(_id.text);
141+
var result = ctx.getValue(_id.text);
142+
ctx.deleteLocal(_id.text);
143+
return result;
133144
}
134145
}
135146

0 commit comments

Comments
 (0)