-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.java
207 lines (199 loc) · 5.33 KB
/
SymbolTable.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import xtc.tree.Location;
abstract class Symbol {
Scope _scope;
Symbol(Scope scope) { _scope = scope;}
abstract Location loc();
abstract String name();
abstract Type type();
}
class FieldSym extends Symbol {
AstNode _def;
FieldSym(Scope scope, AstNode def) {
super(scope);
_def = def;
if (def instanceof FieldLit)
((FieldLit)def)._sym = this;
else
((FieldType)def)._sym = this;
}
Location loc() { return _def._loc; }
static String name(AstNode def) {
if (def instanceof FieldLit)
return ((FieldLit)def)._field._id;
return ((FieldType)def)._field._id;
}
String name() { return name(_def); }
static FieldType type(AstNode def) {
if (def instanceof FieldLit)
return ((FieldLit)def)._type;
return (FieldType)def;
}
FieldType type() { return type(_def); }
}
class FunSym extends Symbol {
FunDef _def;
List<Instruction> _instructions;
Map<String, Address> _addresses;
FunSym(Scope scope, FunDef def) {
super(scope);
_def = def;
_instructions = null;
_addresses = null;
def._sym = this;
}
Location loc() { return _def._loc; }
static String name(FunDef def) { return def._name._id; }
String name() { return name(_def); }
FunType type() { return _def._type; }
}
class VarSym extends Symbol {
AstNode _def;
NameAddr _addr;
VarSym(Scope scope, AstNode def) {
super(scope);
if (def instanceof FieldType) {
FieldType f = (FieldType)def;
f._sym = this;
f._field._sym = this;
} else if (def instanceof ForStmt) {
ForStmt f = (ForStmt)def;
f._sym = this;
f._var._sym = this;
} else {
VarDef v = (VarDef)def;
v._sym = this;
v._var._sym = this;
}
_def = def;
_addr = null;
}
Location loc() { return _def._loc; }
static String name(AstNode def) {
if (def instanceof FieldType)
return ((FieldType)def)._field._id;
if (def instanceof ForStmt)
return ((ForStmt)def)._var._id;
else
return ((VarDef)def)._var._id;
}
String name() { return name(_def); }
static Type type(AstNode def) {
if (def instanceof FieldType) {
return ((FieldType)def)._type;
} else if (def instanceof ForStmt) {
Type arrayType = ((ForStmt)def)._expr._type;
if (null != arrayType && arrayType instanceof ArrayType)
return ((ArrayType)arrayType)._elem;
return null;
} else {
return ((VarDef)def)._rhs._type;
}
}
Type type() { return type(_def); }
}
class SymbolTable {
Scope _topLevel;
Scope _current;
SymbolTable() {
_topLevel = new Scope(null, null);
_current = _topLevel;
}
Scope pop(Scope scope) {
assert _current == scope && _current != _topLevel;
_current = scope._parent;
return _current;
}
void push(Scope scope) {
assert _current == scope._parent : _current + " not parent of " + scope;
_current = scope;
}
boolean contains(String key) { return _current.contains(key); }
void def(Symbol sym) { _current.def(sym); }
Symbol get(String key) { return _current.get(key); }
Symbol lookup(String key) {
for (Scope s = _current; null != s; s = s._parent)
if (s.contains(key))
return s.get(key);
return null;
}
PrintWriter print(PrintWriter w) {
_topLevel.print(w, 0);
w.println();
return w;
}
}
class Scope implements Comparable<Scope> {
AstNode _owner;
Scope _parent;
Set<Scope> _children;
Map<String, Symbol> _symbols;
List<ConstantAddr> _rodatas; // for assembly code
Scope(AstNode owner, Scope parent) {
_owner = owner;
_parent = parent;
if (null != parent)
_parent._children.add(this);
_children = new TreeSet<Scope>();
_symbols = new TreeMap<String, Symbol>();
_rodatas = new ArrayList<ConstantAddr>();
}
public int compareTo(Scope other) {
if (null == _owner)
if (null == other._owner)
return hashCode() - other.hashCode();
else
return -1;
else
if (null == other._owner)
return +1;
else
return _owner._loc.compareTo(other._owner._loc);
}
boolean contains(String key) { return _symbols.containsKey(key); }
void def(Symbol sym) {
String key = sym.name();
assert !_symbols.containsKey(key);
_symbols.put(key, sym);
}
Symbol get(String key) { return _symbols.get(key); }
PrintWriter print(PrintWriter w, int indent) {
for (int i=0; i<indent; i++)
w.print(" ");
w.print("Scope(");
if (null != _owner) {
w.print("owner " + _owner.getClass().getSimpleName());
if (_owner instanceof FunDef)
w.print(" " + ((FunDef)_owner)._name._id);
else if (_owner instanceof ForStmt)
w.print(" " + ((ForStmt)_owner)._var._id);
w.print(", ");
}
w.print("symbols (");
boolean first = true;
for (Symbol s : _symbols.values()) {
if (first) first = false;
else w.print(", ");
w.print(s.name());
}
w.print(")");
for (Scope s : _children) {
w.println(",");
s.print(w, indent + 1);
}
w.print(")");
return w;
}
public String toString() {
StringWriter w = new StringWriter();
print(new PrintWriter(w), 0);
return w.toString();
}
}