-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.java
More file actions
84 lines (71 loc) · 3.22 KB
/
Scope.java
File metadata and controls
84 lines (71 loc) · 3.22 KB
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
75
76
77
78
79
80
81
82
83
84
package plc.project;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public final class Scope {
private final Scope parent;
private final Map<String, Environment.Variable> variables = new HashMap<>();
private final Map<String, Environment.Function> functions = new HashMap<>();
public Scope(Scope parent) {
this.parent = parent;
}
public Scope getParent() {
return parent;
}
public void defineVariable(String name, Environment.PlcObject value) {
defineVariable(name, name, Environment.Type.ANY, value);
}
public Environment.Variable defineVariable(String name, String jvmName, Environment.Type type, Environment.PlcObject value) {
if (variables.containsKey(name)) {
throw new RuntimeException("The variable " + name + " is already defined in this scope.");
} else {
Environment.Variable variable = new Environment.Variable(name, jvmName, type, value);
variables.put(variable.getName(), variable);
return variables.get(name);
}
}
public Environment.Variable lookupVariable(String name) {
if (variables.containsKey(name)) {
return variables.get(name);
} else if (parent != null) {
return parent.lookupVariable(name);
} else {
throw new RuntimeException("The variable " + name + " is not defined in this scope.");
}
}
public void defineFunction(String name, int arity, Function<List<Environment.PlcObject>, Environment.PlcObject> function) {
List<Environment.Type> parameterTypes = new ArrayList<>();
for (int i = 0; i < arity; i++) {
parameterTypes.add(Environment.Type.ANY);
}
defineFunction(name, name, parameterTypes, Environment.Type.ANY, function);
}
public Environment.Function defineFunction(String name, String jvmName, List<Environment.Type> parameterTypes, Environment.Type returnType, java.util.function.Function<List<Environment.PlcObject>, Environment.PlcObject> function) {
if (functions.containsKey(name + "/" + parameterTypes.size())) {
throw new RuntimeException("The function " + name + "/" + parameterTypes.size() + " is already defined in this scope.");
} else {
Environment.Function func = new Environment.Function(name, jvmName, parameterTypes, returnType, function);
functions.put(func.getName() + "/" + func.getParameterTypes().size(), func);
return func;
}
}
public Environment.Function lookupFunction(String name, int arity) {
if (functions.containsKey(name + "/" + arity)) {
return functions.get(name + "/" + arity);
} else if (parent != null) {
return parent.lookupFunction(name, arity);
} else {
throw new RuntimeException("The function " + name + "/" + arity + " is not defined in this scope.");
}
}
@Override
public String toString() {
return "Scope{" +
"parent=" + parent +
", variables=" + variables.keySet() +
", functions=" + functions.keySet() +
'}';
}
}