serene-simple/src/main/java/serene/simple/BaseScope.java

35 lines
897 B
Java

package serene.simple;
import java.util.HashMap;
import java.util.Map;
public abstract class BaseScope {
private final Map<String, Object> symbolsMapping = new HashMap<String, Object>();
private final BaseScope parent;
public BaseScope() {
this(null);
}
public BaseScope(BaseScope parent) {
this.parent = parent;
}
public Object lookupSymbol(String symbolName) {
if (this.symbolsMapping.containsKey(symbolName)) {
return this.symbolsMapping.get(symbolName);
}
else if (this.parent != null) {
return this.parent.lookupSymbol(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);
}
}