49 lines
1.3 KiB
Java
49 lines
1.3 KiB
Java
package serene.simple;
|
|
|
|
import java.util.HashMap;
|
|
import serene.simple.builtin.PrintlnFn;
|
|
import serene.simple.builtin.QuitFn;
|
|
import serene.simple.builtin.PlusFn;
|
|
import serene.simple.builtin.NewFn;
|
|
|
|
public class RootScope extends BaseScope {
|
|
private final BaseScope parent;
|
|
private final HashMap<String, Object> symbolsMapping = new HashMap<String, Object>() {{
|
|
put("println", new PrintlnFn());
|
|
put("quit", new QuitFn());
|
|
put("+", new PlusFn());
|
|
put("System", System.class);
|
|
put("Boolean", Boolean.class);
|
|
put("String", String.class);
|
|
put("new", new NewFn());
|
|
}};
|
|
// "+", PlusFn,
|
|
// "-", MinusFn,
|
|
// "*", TimesFn,
|
|
// "/", ObelusFn,
|
|
// "mod", ModFn,
|
|
// "now", NowFn,
|
|
|
|
|
|
public RootScope() {
|
|
this.parent = null;
|
|
System.out.println(this.symbolsMapping.get("println"));
|
|
|
|
}
|
|
|
|
@Override
|
|
public Object lookupSymbol(String symbolName) {
|
|
if (this.symbolsMapping.containsKey(symbolName)) {
|
|
return this.symbolsMapping.get(symbolName);
|
|
}
|
|
else {
|
|
throw new RuntimeException(String.format("Symbol '%s' is not defined in this scope.",
|
|
symbolName));
|
|
}
|
|
}
|
|
|
|
public void insertSymbol(String symbolName, Object symbolValue) {
|
|
this.symbolsMapping.put(symbolName, symbolValue);
|
|
}
|
|
}
|