blob: 72a268b5bc82ab920171f264d6b7501d453b6a95 (
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
|
package com.jantuomi.interpreter.main.core.runtime;
import com.jantuomi.interpreter.main.core.parser.datatype.DataContainer;
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 */
scopes.push(new Scope());
}
private DataContainer resolveSymbol(String symbol) {
return scopes.peek().resolveSymbol(symbol, Arrays.asList());
}
public DataContainer getSymbolValue(String symbol) {
DataContainer d = resolveSymbol(symbol);
return d;
}
public DataContainer getSymbolValue(String symbol, List<DataContainer> parameters) {
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();
}
}
|