blob: 8563a77005aa29d3abae7d034d47a2adae9b6ac1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package com.jantuomi.interpreter.main.core.runtime;
import com.jantuomi.interpreter.main.core.parser.datatype.DataContainer;
import com.jantuomi.interpreter.main.core.runtime.builtins.BuiltinManager;
import com.jantuomi.interpreter.main.exception.InterpreterException;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
/**
* Created by jan on 11.6.2016.
*/
public class State {
private static final State instance = new State();
public static State getInstance() {
return instance;
}
private Stack<Scope> scopes = new Stack<>();
private State() {
/* Push global scope onto the stack */
Scope globalScope = new Scope();
for (Function builtin : BuiltinManager.getInstance().getBuiltins()) {
globalScope.addFunction(builtin.getName(), builtin);
}
scopes.push(globalScope);
}
private DataContainer resolveSymbol(String symbol) throws InterpreterException {
return scopes.peek().resolveSymbol(symbol, Arrays.asList());
}
public DataContainer getSymbolValue(String symbol) throws InterpreterException {
DataContainer d = resolveSymbol(symbol);
return d;
}
public DataContainer getSymbolValue(String symbol, List<DataContainer> parameters) throws InterpreterException {
return scopes.peek().resolveSymbol(symbol, parameters);
}
public Scope createScope() {
Scope scope = new Scope();
if (scopes.size() > 0) {
scope.setParent(scopes.peek());
} else {
scope.setParent(null);
}
scopes.push(scope);
return scope;
}
public void addSymbolToScope(String symbol) {
scopes.peek().addVariable(symbol);
}
public void addFunctionToScope(String symbol, Function func) {
scopes.peek().addFunction(symbol, func);
}
public void setSymbolValueToScope(String symbol, DataContainer value) {
scopes.peek().setVariableValue(symbol, value);
}
public Scope popScope() {
return scopes.pop();
}
}
|