Skip to content

Commit 9d38093

Browse files
committed
backend: Allow the initialization of symbols before compilation
1 parent 6f8320c commit 9d38093

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/main/java/org/piccode/backend/Compiler.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.PrintStream;
44
import java.util.ArrayList;
5+
import java.util.HashMap;
56
import java.util.List;
67
import org.antlr.v4.runtime.BaseErrorListener;
78
import org.antlr.v4.runtime.CharStreams;
@@ -50,8 +51,9 @@ public class Compiler {
5051
public static PrintStream out = System.out;
5152
public static ErrorAsciiKind errorKind = ErrorAsciiKind.GLEAM_STYLE;
5253
public static boolean exitOnError = true;
53-
private static List<Runnable> nativeFunctions = new ArrayList<>();
54-
54+
private final static List<Runnable> nativeFunctions = new ArrayList<>();
55+
private final static HashMap<String, PiccodeValue> symbols = new HashMap<>();
56+
5557
public static PiccodeValue compile(String file, String code) {
5658
return compile(file, code, List.of());
5759
}
@@ -194,9 +196,20 @@ public static void prepareGlobalScope(String file, String globalScopeName) {
194196
Context.top.pushStackFrame(scope_id);
195197
Context.top.putLocal("true", new PiccodeBoolean("true"));
196198
Context.top.putLocal("false", new PiccodeBoolean("false"));
199+
200+
for (var kv: symbols.entrySet()) {
201+
var key = kv.getKey();
202+
var value = kv.getValue();
203+
Context.top.putLocal(key, value);
204+
}
205+
197206
addSystemFunctions();
198207
}
199208

209+
public static void addSymbol(String name, PiccodeValue value) {
210+
symbols.put(name, value);
211+
}
212+
200213
public static void addNativeFunctions(Runnable funcs) {
201214
funcs.run();
202215
}

0 commit comments

Comments
 (0)