-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstNode.java
562 lines (519 loc) · 16.9 KB
/
AstNode.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import xtc.tree.Location;
abstract class AstNode {
Location _loc;
AstNode(Location loc) { _loc = loc; }
abstract Object accept(Visitor visitor);
public String toString() {
StringWriter w = new StringWriter();
PrettyPrinter prettyPrinter = new PrettyPrinter(new PrintWriter(w));
this.accept(prettyPrinter);
return w.toString();
}
}
// ---------------- top-level ----------------
class Program extends AstNode {
FunDefListHead _raw;
List<FunDef> _functions;
Program(Location loc, FunDefListHead raw) {
super(loc); _raw = raw; _functions = null;
}
Program(Location loc, List<FunDef> functions) {
super(loc); _raw = null; _functions = functions;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
Object accept(IRVisitor visitor) { return visitor.visit(this); }
}
class FunDef extends AstNode {
FunId _name;
FunType _type;
BlockStmt _body;
Scope _heldScope;
FunSym _sym;
//int _frameSize;
CCStack _stack;
FunDef(Location loc, FunId name, FunType type, BlockStmt body) {
super(loc); _name = name; _type = type; _body = body;
_heldScope = null; _sym = null;
}
FunDef(Location loc, FunId name, FunType type, List<Instruction> instrs) {
super(loc); _name = name; _type = type; _body = null;
_sym = new FunSym(null, this);
_sym._instructions = instrs;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
Object accept(IRVisitor visitor) { return visitor.visit(this); }
}
class FunDefListHead extends AstNode {
FunDef _first;
FunDefListTail _tail;
FunDefListHead(Location loc) { super(loc); _first = null; _tail = null; }
FunDefListHead(Location loc,FunDef first,FunDefListTail tail) {
super(loc); _first = first; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FunDefListTail extends AstNode {
List<FunDef> _inh;
FunDef _next;
FunDefListTail _tail;
FunDefListTail(Location loc) { super(loc); _next = null; _tail = null; }
FunDefListTail(Location loc,FunDef next,FunDefListTail tail) {
super(loc); _next = next; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
// ---------------- types ----------------
abstract class Type extends AstNode {
Type(Location loc) { super(loc); }
public boolean equals(Object t2) {
assert false : "subclass of Type must override equals() method: "
+ t2.getClass().getSimpleName();
return false;
}
}
class ArrayType extends Type {
Type _elem;
ArrayType(Location loc, Type elem) { super(loc); _elem = elem; }
Object accept(Visitor visitor) { return visitor.visit(this); }
public boolean equals(Object t2) {
return t2 instanceof ArrayType && _elem.equals(((ArrayType)t2)._elem);
}
}
class RecordType extends Type {
FieldTypeListHead _raw;
List<FieldType> _fields;
Scope _heldScope;
RecordType(Location loc, FieldTypeListHead raw) {
super(loc); _raw = raw; _fields = null; _heldScope = null;
}
RecordType(Location loc, List<FieldType> fields) {
super(loc); _raw = null; _fields = fields; _heldScope = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
public boolean equals(Object t2) {
if (!(t2 instanceof RecordType))
return false;
RecordType r2 = (RecordType)t2;
if (_fields.size() != r2._fields.size())
return false;
for (int i=0, n=_fields.size(); i<n; i++)
if (!_fields.get(i).equals(r2._fields.get(i)))
return false;
return true;
}
}
class FieldTypeListHead extends AstNode {
FieldType _first;
FieldTypeListTail _tail;
FieldTypeListHead(Location loc) { super(loc); _first = null; _tail = null; }
FieldTypeListHead(Location loc, FieldType first, FieldTypeListTail tail) {
super(loc); _first = first; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldTypeListTail extends AstNode {
List<FieldType> _inh;
FieldType _next;
FieldTypeListTail _tail;
FieldTypeListTail(Location loc) { super(loc); _next = null; _tail = null; }
FieldTypeListTail(Location loc, FieldType next, FieldTypeListTail tail) {
super(loc); _next = next; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldType extends Type {
FieldId _field;
Type _type;
Symbol _sym;
FieldType(Location loc, FieldId field, Type type) {
super(loc); _field = field; _type = type; _sym = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
public boolean equals(Object t2) {
if (!(t2 instanceof FieldType))
return false;
FieldType f2 = (FieldType)t2;
return _field._id.equals(f2._field._id) && _type.equals(f2._type);
}
}
class NullType extends Type {
NullType() { super(null); }
Object accept(Visitor visitor) { return visitor.visit(this); }
public boolean equals(Object t2) { return t2 instanceof NullType; }
}
class PrimitiveType extends Type {
static final String BOOL = "bool".intern();
static final PrimitiveType BOOLT = new PrimitiveType(null, BOOL);
static final String INT = "int".intern();
static final PrimitiveType INTT = new PrimitiveType(null, INT);
static final String STRING = "string".intern();
static final PrimitiveType STRINGT = new PrimitiveType(null, STRING);
static final String VOID = "void".intern();
static final PrimitiveType VOIDT = new PrimitiveType(null, VOID);
String _name;
PrimitiveType(Location loc, String name) {
super(loc);
assert BOOL == name || INT == name || STRING == name || VOID == name;
_name = name;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
public boolean equals(Object t2) {
if (!(t2 instanceof PrimitiveType))
return false;
return _name.equals(((PrimitiveType)t2)._name);
}
}
class FunType extends Type {
RecordType _formals;
Type _returnType;
FunType(Location loc, RecordType formals, Type returnType) {
super(loc); _formals = formals; _returnType = returnType;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
public boolean equals(Object t2) {
if (!(t2 instanceof FunType))
return false;
FunType f2 = (FunType)t2;
return _formals.equals(f2._formals) && _returnType.equals(f2._returnType);
}
}
// ---------------- statements ----------------
abstract class Stmt extends AstNode {
Label _nextLabel;
Stmt(Location loc) { super(loc); _nextLabel = null; }
}
class VarDef extends Stmt {
VarId _var;
Expr _rhs;
VarSym _sym;
VarDef(Location loc, VarId var, Expr rhs) {
super(loc); _var = var; _rhs = rhs; _sym = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class AssignStmt extends Stmt {
Expr _lhs;
Expr _rhs;
AssignStmt(Location loc, Expr lhs, Expr rhs) {
super(loc); _lhs = lhs; _rhs = rhs;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class BlockStmt extends Stmt {
StmtListHead _raw;
List<Stmt> _stmts;
boolean _needsScope;
Scope _heldScope;
BlockStmt(Location loc, StmtListHead raw) {
super(loc); _raw=raw; _stmts=null; _needsScope=true; _heldScope=null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class CallStmt extends Stmt {
Expr _expr;
CallStmt(Location loc, Expr expr) { super(loc); _expr = expr; }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class ForStmt extends Stmt {
VarId _var;
Expr _expr;
BlockStmt _body;
Scope _heldScope;
VarSym _sym;
ForStmt(Location loc, VarId var, Expr expr, BlockStmt body) {
super(loc); _var = var; _expr = expr; _body = body;
_heldScope = null; _sym = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class IfStmt extends Stmt {
Expr _cond;
BlockStmt _thenBranch;
BlockStmt _elseBranch;
IfStmt(Location loc, Expr cond, BlockStmt thenBranch, BlockStmt elseBranch) {
super(loc); _cond=cond; _thenBranch=thenBranch; _elseBranch=elseBranch;
}
IfStmt(Location loc, Expr cond, BlockStmt thenBranch) {
super(loc); _cond=cond; _thenBranch=thenBranch; _elseBranch=null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class ReturnStmt extends Stmt {
Expr _expr;
ReturnStmt(Location loc, Expr expr) { super(loc); _expr = expr; }
ReturnStmt(Location loc) { super(loc); _expr = null; }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class WhileStmt extends Stmt {
Expr _cond;
BlockStmt _body;
WhileStmt(Location loc, Expr cond, BlockStmt body) {
super(loc); _cond = cond; _body = body;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class StmtListHead extends AstNode {
Stmt _first;
StmtListTail _tail;
StmtListHead(Location loc) { super(loc); _first = null; _tail = null; }
StmtListHead(Location loc, Stmt first, StmtListTail tail) {
super(loc); _first = first; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class StmtListTail extends AstNode {
List<Stmt> _inh;
Stmt _next;
StmtListTail _tail;
StmtListTail(Location loc) { super(loc); _next = null; _tail = null; }
StmtListTail(Location loc, Stmt next, StmtListTail tail) {
super(loc); _next = next; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
// ---------------- expressions ----------------
abstract class Expr extends AstNode {
Type _type;
Label _trueLabel, _falseLabel;
Expr(Location loc) {
super(loc); _type = null; _trueLabel = null; _falseLabel = null;
}
}
class InfixExpr extends Expr {
String _op;
Expr _lhs;
Expr _rhs;
InfixExpr(Location loc, String op, Expr lhs, Expr rhs) {
super(loc); _op = op; _lhs = lhs; _rhs = rhs;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class InfixExprHead extends Expr {
Expr _lhs;
InfixExprTail _tail;
InfixExprHead(Location loc, Expr lhs, InfixExprTail tail) {
super(loc); _lhs = lhs; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class InfixExprTail extends AstNode {
Expr _inh;
String _op;
Expr _rhs;
InfixExprTail _tail;
InfixExprTail(Location loc) {
super(loc); _inh = null; _op = null; _rhs = null; _tail = null;
}
InfixExprTail(Location loc, String op, Expr rhs, InfixExprTail tail) {
super(loc); _inh = null; _op = op; _rhs = rhs; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class PrefixExpr extends Expr {
String _op;
Expr _base;
PrefixExpr(Location loc, String op, Expr base) {
super(loc); _op = op; _base = base;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class PostfixExprHead extends Expr {
Expr _base;
PostfixExprTail _tail;
PostfixExprHead(Location loc, Expr base, PostfixExprTail tail) {
super(loc); _base = base; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class PostfixExprTail extends AstNode {
Expr _inh;
PostfixExprTail(Location loc) { super(loc); _inh = null; }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class CallExpr extends Expr {
Expr _base;
List<Expr> _actuals;
CallExpr(Location loc, Expr base, List<Expr> actuals) {
super(loc); _base = base; _actuals = actuals;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class CallExprTail extends PostfixExprTail {
ExprListHead _actuals;
PostfixExprTail _tail;
CallExprTail(Location loc, ExprListHead actuals, PostfixExprTail tail) {
super(loc); _actuals = actuals; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class CastExpr extends Expr {
Expr _base;
Type _targetType;
CastExpr(Location loc, Expr base, Type type) {
super(loc); _base = base; _targetType = type;
}
CastExpr(Expr base, Type type) {
this(base._loc, base, type);
_type = _targetType;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class CastExprTail extends PostfixExprTail {
Type _targetType;
PostfixExprTail _tail;
CastExprTail(Location loc, Type type, PostfixExprTail tail) {
super(loc); _targetType = type; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldExpr extends Expr {
Expr _base;
FieldId _field;
FieldExpr(Location loc, Expr base, FieldId field) {
super(loc); _base = base; _field = field;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldExprTail extends PostfixExprTail {
FieldId _field;
PostfixExprTail _tail;
FieldExprTail(Location loc, FieldId field, PostfixExprTail tail) {
super(loc); _field = field; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class SubscriptExpr extends Expr {
Expr _base;
Expr _subscript;
SubscriptExpr(Location loc, Expr base, Expr subscript) {
super(loc); _base = base; _subscript = subscript;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class SubscriptExprTail extends PostfixExprTail {
Expr _subscript;
PostfixExprTail _tail;
SubscriptExprTail(Location loc, Expr subscript, PostfixExprTail tail) {
super(loc); _subscript = subscript; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class ExprListHead extends AstNode {
Expr _first;
ExprListTail _tail;
ExprListHead(Location loc) { super(loc); _first = null; _tail = null; }
ExprListHead(Location loc, Expr first, ExprListTail tail) {
super(loc); _first = first; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class ExprListTail extends AstNode {
List<Expr> _inh;
Expr _next;
ExprListTail _tail;
ExprListTail(Location loc) { super(loc); _next = null; _tail = null; }
ExprListTail(Location loc, Expr next, ExprListTail tail) {
super(loc); _next = next; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class ParenExpr extends Expr {
Expr _base;
ParenExpr(Location loc, Expr base) { super(loc); _base = base; }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
// ---------------- identifiers ----------------
class FieldId extends AstNode {
String _id;
Symbol _sym;
int _offset;
FieldId(Location loc, String id) { super(loc); _id = id; _sym = null; _offset = -1;}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FunId extends Expr {
String _id;
FunSym _sym;
FunId(Location loc, String id) { super(loc); _id = id; _sym = null; }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class VarId extends Expr {
String _id;
VarSym _sym;
VarId(Location loc, String id) { super(loc); _id = id; }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
// ---------------- literals ----------------
class ArrayLit extends Expr {
ExprListHead _raw;
List<Expr> _elems;
ArrayLit(Location loc, ExprListHead raw) {
super(loc); _raw = raw; _elems = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class RecordLit extends Expr {
FieldLitListHead _raw;
List<FieldLit> _fields;
Scope _heldScope;
RecordLit(Location loc, FieldLitListHead raw) {
super(loc); _raw = raw; _fields = null; _heldScope = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldLitListHead extends AstNode {
FieldLit _first;
FieldLitListTail _tail;
FieldLitListHead(Location loc) { super(loc); _first = null; _tail = null; }
FieldLitListHead(Location loc, FieldLit first, FieldLitListTail tail) {
super(loc); _first = first; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldLitListTail extends AstNode {
List<FieldLit> _inh;
FieldLit _next;
FieldLitListTail _tail;
FieldLitListTail(Location loc) { super(loc); _next = null; _tail = null; }
FieldLitListTail(Location loc, FieldLit next, FieldLitListTail tail) {
super(loc); _next = next; _tail = tail;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class FieldLit extends AstNode {
FieldId _field;
Expr _expr;
FieldSym _sym;
FieldType _type;
FieldLit(Location loc, FieldId field, Expr expr) {
super(loc); _field = field; _expr = expr; _sym = null; _type = null;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class BoolLit extends Expr {
boolean _value;
BoolLit(Location loc, String token) {
super(loc); _value = "true".equals(token);
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class IntLit extends Expr {
int _value;
IntLit(Location loc, String token) {
super(loc); _value = Integer.parseInt(token);
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class NullLit extends Expr {
NullLit(Location loc) { super(loc); }
Object accept(Visitor visitor) { return visitor.visit(this); }
}
class StringLit extends Expr {
String _token;
StringLit(Location loc, String token) {
super(loc); _token = token;
}
Object accept(Visitor visitor) { return visitor.visit(this); }
}