-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxTreePrinter.java
312 lines (272 loc) · 6.91 KB
/
SyntaxTreePrinter.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import java.io.PrintWriter;
import java.util.Stack;
class SyntaxTreePrinter extends Visitor {
PrintWriter _writer;
Stack<AstNode> _stack;
SyntaxTreePrinter(PrintWriter writer) {
_writer = writer;
_stack = new Stack<AstNode>();
}
SyntaxTreePrinter begin(AstNode ast) {
return begin(ast, null);
}
SyntaxTreePrinter begin(AstNode ast, String attr) {
for (int i=0, n=_stack.size(); i<n; i++)
p(" ");
_stack.push(ast);
p(ast.getClass().getSimpleName());
if (null != attr)
p(" ").p(attr);
return pln();
}
SyntaxTreePrinter end(AstNode ast) {
AstNode top = _stack.pop();
assert top == ast;
return this;
}
SyntaxTreePrinter p(AstNode ast) {
ast.accept(this);
return this;
}
SyntaxTreePrinter p(String s) {
assert(-1 == s.indexOf('\n'));
_writer.print(s);
return this;
}
SyntaxTreePrinter pln() {
_writer.println();
return this;
}
// ---------------- top-level ----------------
Object visit(Program ast) {
begin(ast);
if (null != ast._raw)
p(ast._raw);
else
for (FunDef f : ast._functions)
p(f);
return end(ast);
}
Object visit(FunDef ast) {
return begin(ast).p(ast._name).p(ast._type).p(ast._body).end(ast);
}
Object visit(FunDefListHead ast) {
begin(ast);
if (null != ast._first)
p(ast._first).p(ast._tail);
return end(ast);
}
Object visit(FunDefListTail ast) {
begin(ast);
if (null != ast._next)
p(ast._next).p(ast._tail);
return end(ast);
}
// ---------------- types ----------------
Object visit(ArrayType ast) {
return begin(ast).p(ast._elem).end(ast);
}
Object visit(RecordType ast) {
begin(ast);
if (null != ast._raw)
p(ast._raw);
else
for (FieldType f : ast._fields)
p(f);
return end(ast);
}
Object visit(FieldTypeListHead ast) {
begin(ast);
if (null != ast._first)
p(ast._first).p(ast._tail);
return end(ast);
}
Object visit(FieldTypeListTail ast) {
begin(ast);
if (null != ast._next)
p(ast._next).p(ast._tail);
return end(ast);
}
Object visit(FieldType ast) {
return begin(ast).p(ast._field).p(ast._type).end(ast);
}
Object visit(PrimitiveType ast) {
return begin(ast, ast._name).end(ast);
}
Object visit(NullType ast) {
return begin(ast).end(ast);
}
Object visit(FunType ast) {
return begin(ast).p(ast._formals).p(ast._returnType).end(ast);
}
// ---------------- statements ----------------
Object visit(VarDef ast) {
return begin(ast).p(ast._var).p(ast._rhs).end(ast);
}
Object visit(AssignStmt ast) {
return begin(ast).p(ast._lhs).p(ast._rhs).end(ast);
}
Object visit(BlockStmt ast) {
begin(ast);
if (null != ast._raw)
p(ast._raw);
else
for (Stmt s : ast._stmts)
p(s);
return end(ast);
}
Object visit(CallStmt ast) {
return begin(ast).p(ast._expr).end(ast);
}
Object visit(ForStmt ast) {
return begin(ast).p(ast._var).p(ast._expr).p(ast._body).end(ast);
}
Object visit(IfStmt ast) {
begin(ast).p(ast._cond).p(ast._thenBranch);
if (null != ast._elseBranch)
p(ast._elseBranch);
return end(ast);
}
Object visit(WhileStmt ast) {
return begin(ast).p(ast._cond).p(ast._body).end(ast);
}
Object visit(ReturnStmt ast) {
begin(ast);
if (null != ast._expr)
p(ast._expr);
return end(ast);
}
Object visit(StmtListHead ast) {
begin(ast);
if (null != ast._first)
p(ast._first).p(ast._tail);
return end(ast);
}
Object visit(StmtListTail ast) {
begin(ast);
if (null != ast._next)
p(ast._next).p(ast._tail);
return end(ast);
}
// ---------------- expressions ----------------
Object visit(InfixExpr ast) {
return begin(ast, ast._op).p(ast._lhs).p(ast._rhs).end(ast);
}
Object visit(InfixExprHead ast) {
return begin(ast).p(ast._lhs).p(ast._tail).end(ast);
}
Object visit(InfixExprTail ast) {
begin(ast);
if (null != ast._rhs)
p(ast._op).p(ast._rhs).p(ast._tail);
return end(ast);
}
Object visit(PrefixExpr ast) {
return begin(ast, ast._op).p(ast._base).end(ast);
}
Object visit(PostfixExprHead ast) {
return begin(ast).p(ast._base).p(ast._tail).end(ast);
}
Object visit(PostfixExprTail ast) {
assert PostfixExprTail.class == ast.getClass() : "must not be subtype";
return begin(ast).end(ast);
}
Object visit(CallExpr ast) {
begin(ast).p(ast._base);
for (Expr e : ast._actuals)
p(e);
return end(ast);
}
Object visit(CallExprTail ast) {
return begin(ast).p(ast._actuals).p(ast._tail).end(ast);
}
Object visit(CastExpr ast) {
return begin(ast).p(ast._base).p(ast._targetType).end(ast);
}
Object visit(CastExprTail ast) {
return begin(ast).p(ast._targetType).p(ast._tail).end(ast);
}
Object visit(FieldExpr ast) {
return begin(ast).p(ast._base).p(ast._field).end(ast);
}
Object visit(FieldExprTail ast) {
return begin(ast).p(ast._field).p(ast._tail).end(ast);
}
Object visit(SubscriptExpr ast) {
return begin(ast).p(ast._base).p(ast._subscript).end(ast);
}
Object visit(SubscriptExprTail ast) {
return begin(ast).p(ast._subscript).p(ast._tail).end(ast);
}
Object visit(ExprListHead ast) {
begin(ast);
if (null != ast._first)
p(ast._first).p(ast._tail);
return end(ast);
}
Object visit(ExprListTail ast) {
begin(ast);
if (null != ast._next)
p(ast._next).p(ast._tail);
return end(ast);
}
Object visit(ParenExpr ast) {
return begin(ast).p(ast._base).end(ast);
}
// ---------------- identifiers ----------------
Object visit(FieldId ast) {
return begin(ast, ast._id).end(ast);
}
Object visit(FunId ast) {
return begin(ast, ast._id).end(ast);
}
Object visit(VarId ast) {
return begin(ast, ast._id).end(ast);
}
// ---------------- literals ----------------
Object visit(ArrayLit ast) {
begin(ast);
if (null != ast._raw)
p(ast._raw);
else
for (Expr e : ast._elems)
p(e);
return end(ast);
}
Object visit(RecordLit ast) {
begin(ast);
if (null != ast._raw)
p(ast._raw);
else
for (FieldLit f : ast._fields)
p(f);
return end(ast);
}
Object visit(FieldLitListHead ast) {
begin(ast);
if (null != ast._first)
p(ast._first).p(ast._tail);
return end(ast);
}
Object visit(FieldLitListTail ast) {
begin(ast);
if (null != ast._next)
p(ast._next).p(ast._tail);
return end(ast);
}
Object visit(FieldLit ast) {
return begin(ast).p(ast._field).p(ast._expr).end(ast);
}
Object visit(BoolLit ast) {
return begin(ast, Boolean.toString(ast._value)).end(ast);
}
Object visit(IntLit ast) {
return begin(ast, Integer.toString(ast._value)).end(ast);
}
Object visit(NullLit ast) {
return begin(ast).end(ast);
}
Object visit(StringLit ast) {
return begin(ast, ast._token).end(ast);
}
}