-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNormalizer.java
More file actions
294 lines (259 loc) · 7.65 KB
/
TreeNormalizer.java
File metadata and controls
294 lines (259 loc) · 7.65 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.util.*;
//see aho_et_al_2007 Figure 5.13 (Page 321)
class TreeNormalizer extends Visitor {
// ---------------- top-level ----------------
Object visit(Program ast) {
@SuppressWarnings("unchecked")
List<FunDef> functions = (List<FunDef>)ast._raw.accept(this);
ast._functions = functions;
ast._raw = null;
return ast;
}
Object visit(FunDef ast) {
ast._name = (FunId)ast._name.accept(this);
ast._type = (FunType)ast._type.accept(this);
ast._body = (BlockStmt)ast._body.accept(this);
return ast;
}
Object visit(FunDefListHead ast) {
List<FunDef> result = new ArrayList<FunDef>();
if (null == ast._first)
return result;
result.add((FunDef)ast._first.accept(this));
ast._tail._inh = result;
return ast._tail.accept(this);
}
Object visit(FunDefListTail ast) {
if (null == ast._next)
return ast._inh;
ast._inh.add((FunDef)ast._next.accept(this));
ast._tail._inh = ast._inh;
return ast._tail.accept(this);
}
// ---------------- types ----------------
Object visit(ArrayType ast) {
ast._elem = (Type)ast._elem.accept(this);
return ast;
}
Object visit(RecordType ast) {
if (null != ast._raw) {
@SuppressWarnings("unchecked")
List<FieldType> fields = (List<FieldType>)ast._raw.accept(this);
ast._fields = fields;
ast._raw = null;
}
return ast;
}
Object visit(FieldTypeListHead ast) {
List<FieldType> result = new ArrayList<FieldType>();
if (null == ast._first)
return result;
result.add((FieldType)ast._first.accept(this));
ast._tail._inh = result;
return ast._tail.accept(this);
}
Object visit(FieldTypeListTail ast) {
if (null == ast._next)
return ast._inh;
ast._inh.add((FieldType)ast._next.accept(this));
ast._tail._inh = ast._inh;
return ast._tail.accept(this);
}
Object visit(FieldType ast) {
ast._field = (FieldId)ast._field.accept(this);
ast._type = (Type)ast._type.accept(this);
return ast;
}
Object visit(PrimitiveType ast) {
return ast;
}
Object visit(FunType ast) {
ast._formals = (RecordType)ast._formals.accept(this);
ast._returnType = (Type)ast._returnType.accept(this);
return ast;
}
// ---------------- statements ----------------
Object visit(VarDef ast) {
ast._var = (VarId)ast._var.accept(this);
ast._rhs = (Expr)ast._rhs.accept(this);
return ast;
}
Object visit(AssignStmt ast) {
ast._lhs = (Expr)ast._lhs.accept(this);
ast._rhs = (Expr)ast._rhs.accept(this);
return ast;
}
Object visit(BlockStmt ast) {
@SuppressWarnings("unchecked")
List<Stmt> stmts = (List<Stmt>)ast._raw.accept(this);
ast._stmts = stmts;
ast._raw = null;
return ast;
}
Object visit(CallStmt ast) {
ast._expr = (Expr)ast._expr.accept(this);
return ast;
}
Object visit(ForStmt ast) {
ast._var = (VarId)ast._var.accept(this);
ast._expr = (Expr)ast._expr.accept(this);
ast._body = (BlockStmt)ast._body.accept(this);
return ast;
}
Object visit(IfStmt ast) {
ast._cond = (Expr)ast._cond.accept(this);
ast._thenBranch = (BlockStmt)ast._thenBranch.accept(this);
if (null != ast._elseBranch)
ast._elseBranch = (BlockStmt)ast._elseBranch.accept(this);
return ast;
}
Object visit(WhileStmt ast) {
ast._cond = (Expr)ast._cond.accept(this);
ast._body = (BlockStmt)ast._body.accept(this);
return ast;
}
Object visit(ReturnStmt ast) {
if (null != ast._expr)
ast._expr = (Expr)ast._expr.accept(this);
return ast;
}
Object visit(StmtListHead ast) {
List<Stmt> result = new ArrayList<Stmt>();
if (null == ast._first)
return result;
result.add((Stmt)ast._first.accept(this));
ast._tail._inh = result;
return ast._tail.accept(this);
}
Object visit(StmtListTail ast) {
if (null == ast._next)
return ast._inh;
ast._inh.add((Stmt)ast._next.accept(this));
ast._tail._inh = ast._inh;
return ast._tail.accept(this);
}
// ---------------- expressions ----------------
Object visit(InfixExprHead ast) {
ast._tail._inh = (Expr)ast._lhs.accept(this);
return ast._tail.accept(this);
}
Object visit(InfixExprTail ast) {
if (null == ast._rhs)
return ast._inh;
Expr rhs = (Expr)ast._rhs.accept(this);
ast._tail._inh = new InfixExpr(ast._inh._loc, ast._op, ast._inh, rhs);
return ast._tail.accept(this);
}
Object visit(PrefixExpr ast) {
ast._base = (Expr)ast._base.accept(this);
return ast;
}
Object visit(PostfixExprHead ast) {
ast._tail._inh = (Expr)ast._base.accept(this);
return ast._tail.accept(this);
}
Object visit(PostfixExprTail ast) {
assert PostfixExprTail.class == ast.getClass() : "must not be subtype";
return ast._inh;
}
Object visit(CallExprTail ast) {
@SuppressWarnings("unchecked")
List<Expr> actuals = (List<Expr>)ast._actuals.accept(this);
Expr callee = ast._inh;
if (callee instanceof VarId)
callee = new FunId(callee._loc, ((VarId)callee)._id);
ast._tail._inh = new CallExpr(callee._loc, callee, actuals);
return ast._tail.accept(this);
}
Object visit(CastExprTail ast) {
Type type = (Type)ast._targetType.accept(this);
ast._tail._inh = new CastExpr(ast._inh._loc, ast._inh, type);
return ast._tail.accept(this);
}
Object visit(FieldExprTail ast) {
FieldId field = (FieldId)ast._field.accept(this);
ast._tail._inh = new FieldExpr(ast._inh._loc, ast._inh, field);
return ast._tail.accept(this);
}
Object visit(SubscriptExprTail ast) {
Expr subscript = (Expr)ast._subscript.accept(this);
ast._tail._inh = new SubscriptExpr(ast._inh._loc, ast._inh, subscript);
return ast._tail.accept(this);
}
Object visit(ExprListHead ast) {
List<Expr> result = new ArrayList<Expr>();
if (null == ast._first)
return result;
result.add((Expr)ast._first.accept(this));
ast._tail._inh = result;
return ast._tail.accept(this);
}
Object visit(ExprListTail ast) {
if (null == ast._next)
return ast._inh;
ast._inh.add((Expr)ast._next.accept(this));
ast._tail._inh = ast._inh;
return ast._tail.accept(this);
}
Object visit(ParenExpr ast) {
ast._base = (Expr)ast._base.accept(this);
return ast;
}
// ---------------- identifiers ----------------
Object visit(FieldId ast) {
return ast;
}
Object visit(FunId ast) {
return ast;
}
Object visit(VarId ast) {
return ast;
}
// ---------------- literals ----------------
Object visit(ArrayLit ast) {
@SuppressWarnings("unchecked")
List<Expr> elems = (List<Expr>)ast._raw.accept(this);
ast._elems = elems;
ast._raw = null;
return ast;
}
Object visit(RecordLit ast) {
@SuppressWarnings("unchecked")
List<FieldLit> fields = (List<FieldLit>)ast._raw.accept(this);
ast._fields = fields;
ast._raw = null;
return ast;
}
Object visit(FieldLitListHead ast) {
List<FieldLit> result = new ArrayList<FieldLit>();
if (null == ast._first)
return result;
result.add((FieldLit)ast._first.accept(this));
ast._tail._inh = result;
return ast._tail.accept(this);
}
Object visit(FieldLitListTail ast) {
if (null == ast._next)
return ast._inh;
ast._inh.add((FieldLit)ast._next.accept(this));
ast._tail._inh = ast._inh;
return ast._tail.accept(this);
}
Object visit(FieldLit ast) {
ast._field = (FieldId)ast._field.accept(this);
ast._expr = (Expr)ast._expr.accept(this);
return ast;
}
Object visit(BoolLit ast) {
return ast;
}
Object visit(IntLit ast) {
return ast;
}
Object visit(NullLit ast) {
return ast;
}
Object visit(StringLit ast) {
return ast;
}
}