|
1 |
| package edu.rice.cs.dynamicjava.interpreter; |
|
2 |
| |
|
3 |
| import java.util.Map; |
|
4 |
| import java.util.HashMap; |
|
5 |
| import java.util.Collections; |
|
6 |
| import edu.rice.cs.plt.tuple.Pair; |
|
7 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
8 |
| |
|
9 |
| import edu.rice.cs.dynamicjava.symbol.*; |
|
10 |
| import edu.rice.cs.dynamicjava.symbol.type.Type; |
|
11 |
| import edu.rice.cs.dynamicjava.symbol.type.VariableType; |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| public class RuntimeBindings { |
|
21 |
| |
|
22 |
| public static final RuntimeBindings EMPTY = new RuntimeBindings(); |
|
23 |
| |
|
24 |
| private final RuntimeBindings _parent; |
|
25 |
| private final Map<LocalVariable, Object> _vars; |
|
26 |
| private final Map<VariableType, Type> _tvars; |
|
27 |
| private final Map<DJClass, Object> _thisVals; |
|
28 |
| |
|
29 |
564
| public RuntimeBindings(RuntimeBindings parent, Map<LocalVariable, Object> vars,
|
|
30 |
| Map<VariableType, Type> tvars, Map<DJClass, Object> thisVals) { |
|
31 |
564
| _parent = parent;
|
|
32 |
564
| _vars = new HashMap<LocalVariable, Object>(vars);
|
|
33 |
564
| _tvars = new HashMap<VariableType, Type>(tvars);
|
|
34 |
564
| _thisVals = new HashMap<DJClass, Object>(thisVals);
|
|
35 |
| } |
|
36 |
| |
|
37 |
1
| private RuntimeBindings() {
|
|
38 |
1
| this(null, Collections.<LocalVariable, Object>emptyMap(),
|
|
39 |
| Collections.<VariableType, Type>emptyMap(), Collections.<DJClass, Object>emptyMap()); |
|
40 |
| } |
|
41 |
| |
|
42 |
335
| public RuntimeBindings(RuntimeBindings parent, LocalVariable var, Object val) {
|
|
43 |
335
| this(parent, Collections.singletonMap(var, val), Collections.<VariableType, Type>emptyMap(),
|
|
44 |
| Collections.<DJClass, Object>emptyMap()); |
|
45 |
| } |
|
46 |
| |
|
47 |
179
| public RuntimeBindings(RuntimeBindings parent, Iterable<LocalVariable> vars, Iterable<Object> vals) {
|
|
48 |
179
| this(parent, makeMap(vars, vals), Collections.<VariableType, Type>emptyMap(),
|
|
49 |
| Collections.<DJClass, Object>emptyMap()); |
|
50 |
| } |
|
51 |
| |
|
52 |
179
| private static <K, V> Map<K, V> makeMap(Iterable<? extends K> keys, Iterable<? extends V> vals) {
|
|
53 |
179
| Map<K, V> result = new HashMap<K, V>();
|
|
54 |
171
| for (Pair<K, V> entry : IterUtil.zip(keys, vals)) { result.put(entry.first(), entry.second()); }
|
|
55 |
179
| return result;
|
|
56 |
| } |
|
57 |
| |
|
58 |
49
| public RuntimeBindings(RuntimeBindings parent, DJClass thisClass, Object thisObj) {
|
|
59 |
49
| this(parent, Collections.<LocalVariable, Object>emptyMap(),
|
|
60 |
| Collections.<VariableType, Type>emptyMap(), Collections.singletonMap(thisClass, thisObj)); |
|
61 |
| } |
|
62 |
| |
|
63 |
1371
| public Object get(LocalVariable v) {
|
|
64 |
785
| if (_vars.containsKey(v)) { return _vars.get(v); }
|
|
65 |
586
| else if (_parent != null) { return _parent.get(v); }
|
|
66 |
0
| else { throw new IllegalArgumentException("Variable " + v + " is undefined"); }
|
|
67 |
| } |
|
68 |
| |
|
69 |
740
| public void set(LocalVariable v, Object val) {
|
|
70 |
563
| if (_vars.containsKey(v)) { _vars.put(v, val); }
|
|
71 |
177
| else if (_parent != null) { _parent.set(v, val); }
|
|
72 |
0
| else { throw new IllegalArgumentException("Variable " + v + " is undefined"); }
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| public Type get(VariableType v) {
|
|
76 |
0
| if (_tvars.containsKey(v)) { return _tvars.get(v); }
|
|
77 |
0
| else if (_parent != null) { return _parent.get(v); }
|
|
78 |
0
| else { throw new IllegalArgumentException("Type variable " + v + " is undefined"); }
|
|
79 |
| } |
|
80 |
| |
|
81 |
66
| public Object getThis(DJClass c) {
|
|
82 |
33
| if (_thisVals.containsKey(c)) { return _thisVals.get(c); }
|
|
83 |
33
| else if (_parent != null) { return _parent.getThis(c); }
|
|
84 |
0
| else { throw new IllegalArgumentException("This value " + c + " is undefined"); }
|
|
85 |
| } |
|
86 |
| |
|
87 |
| } |