blob: 93d3af968adc7b045fbb5b9b64acec4dc65d1578 (
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
package st;
import java.util.*;
/**
* Class which provides methods for interacting with and managing
* the symbol table. Maintains context-awareness to keep the state
* of each symbol consistent.
*/
public class SymbolTable {
private HashMap<String,AbstractInstance> symt; // the mapping of ids to Instances
private HashMap<TypeEnum,String> active; // the current scope of the visitor (class, method)
public SymbolTable() {
this.symt = new HashMap<>();
this.active = new HashMap<>();
}
public String toString() {
StringBuilder mapAsString = new StringBuilder("{");
for (String key : this.symt.keySet()) {
mapAsString.append(key + ":" + this.symt.get(key).getType() + ", ");
}
mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
return mapAsString.toString();
}
/**
* Methods intended to be used during the first pass
*/
public void put(String id, AbstractInstance symbol) {
this.symt.put(id, symbol);
}
/**
* Methods intended to be used during the second pass
*/
public void setExtend(String arg) {
String str = this.active.get(TypeEnum.classname);
ClassInstance cls = this.getClass(str);
ClassInstance ext = this.getClass(arg);
cls.setExtend(ext);
// for (TypeInstance : ext.attrs) //FIXME add the extended classes' stuff
// cls.add
}
public void addLocal(String lvar) {
TypeInstance var = this.getType(lvar);
if (this.active.get(TypeEnum.method) != null) { // we are in a method
String str = this.active.get(TypeEnum.method);
MethodInstance mtd = this.getMethod(str);
mtd.addLocal(var);
var.setScope(mtd);
} else {
String str = this.active.get(TypeEnum.classname);
ClassInstance cls = this.getClass(str);
cls.addLocal(var);
var.setScope(cls);
}
}
public void addMethod(String mtd) {
String str = this.active.get(TypeEnum.classname);
ClassInstance cls = this.getClass(str);
MethodInstance lmtd = this.getMethod(mtd);
lmtd.setScope(cls);
cls.addMethod(lmtd);
}
public void addParameter(String arg) {
String str = this.active.get(TypeEnum.method);
MethodInstance mtd = this.getMethod(str);
TypeInstance para = this.getType(arg);
para.setScope(mtd);
mtd.addArgument(para); // also adds to local vars
}
public void addClassInstance(String t, String c) {
TypeInstance type = this.getType(t);
ClassInstance cls = (c != null) ?
this.getClass(c) :
null;
type.addClassInstance(cls);
}
/**
* Methods to safely retrieve differentiable types
* in `typecheck', `vaporize' libraries
*/
public void setActive(TypeEnum type, String id) {
this.active.put(type, id);
}
public void removeActive(TypeEnum type) {
this.active.remove(type);
}
public TypeInstance getType(String id) {
AbstractInstance symbol;
return ((symbol = this.symt.get(id)) !=
null && symbol instanceof TypeInstance) ?
(TypeInstance) symbol : null;
}
public MethodInstance getMethod(String id) {
AbstractInstance symbol;
return ((symbol = this.symt.get(id)) !=
null && symbol instanceof MethodInstance) ?
(MethodInstance) symbol : null;
}
public ClassInstance getClass(String id) {
AbstractInstance symbol;
return ((symbol = this.symt.get(id)) !=
null && symbol instanceof ClassInstance) ?
(ClassInstance) symbol : null;
}
public String getActive(TypeEnum type) {
return this.active.get(type);
}
}
|