-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractSyntax.java
418 lines (327 loc) · 10.9 KB
/
AbstractSyntax.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
// Abstract syntax for the language C++Lite,
// exactly as it appears in Appendix B.
import java.util.*;
class Program {
// Program = Declarations decpart ; Block body
Declarations decpart;
Block body;
Program (Declarations d, Block b) {
decpart = d;
body = b;
}
void display() {
// student exercise
}
}
class Declarations extends ArrayList<Declaration> {
// Declarations = Declaration*
// (a list of declarations d1, d2, ..., dn)
}
class Declaration {
// Declaration = Variable v; Type t
Variable v;
Type t;
Declaration (Variable var, Type type) {
v = var; t = type;
} // declaration */
}
class Type {
// Type = int | bool | char | float
final static Type INT = new Type("int");
final static Type BOOL = new Type("bool");
final static Type CHAR = new Type("char");
final static Type FLOAT = new Type("float");
// final static Type UNDEFINED = new Type("undef");
private String id;
private Type (String t) { id = t; }
public String toString ( ) { return id; }
}
abstract class Statement {
// Statement = Skip | Block | Assignment | Conditional | Loop
}
class Skip extends Statement {
}
class Block extends Statement {
// Block = Statement*
// (a Vector of members)
public ArrayList<Statement> members = new ArrayList<Statement>();
}
class Assignment extends Statement {
// Assignment = Variable target; Expression source
Variable target;
Expression source;
Assignment (Variable t, Expression e) {
target = t;
source = e;
}
}
class Conditional extends Statement {
// Conditional = Expression test; Statement thenbranch, elsebranch
Expression test;
Statement thenbranch, elsebranch;
// elsebranch == null means "if... then"
Conditional (Expression t, Statement tp) {
test = t; thenbranch = tp; elsebranch = new Skip( );
}
Conditional (Expression t, Statement tp, Statement ep) {
test = t; thenbranch = tp; elsebranch = ep;
}
}
class Loop extends Statement {
// Loop = Expression test; Statement body
Expression test;
Statement body;
Loop (Expression t, Statement b) {
test = t; body = b;
}
}
abstract class Expression {
// Expression = Variable | Value | Binary | Unary
}
class Variable extends Expression {
// Variable = String id
private String id;
Variable (String s) { id = s; }
public String toString( ) { return id; }
public boolean equals (Object obj) {
String s = ((Variable) obj).id;
return id.equals(s); // case-sensitive identifiers
}
public int hashCode ( ) { return id.hashCode( ); }
}
abstract class Value extends Expression {
// Value = IntValue | BoolValue |
// CharValue | FloatValue
protected Type type;
protected boolean undef = true;
int intValue ( ) {
assert false : "should never reach here";
return 0;
}
boolean boolValue ( ) {
assert false : "should never reach here";
return false;
}
char charValue ( ) {
assert false : "should never reach here";
return ' ';
}
float floatValue ( ) {
assert false : "should never reach here";
return 0.0f;
}
boolean isUndef( ) { return undef; }
Type type ( ) { return type; }
static Value mkValue (Type type) {
if (type == Type.INT) return new IntValue( );
if (type == Type.BOOL) return new BoolValue( );
if (type == Type.CHAR) return new CharValue( );
if (type == Type.FLOAT) return new FloatValue( );
throw new IllegalArgumentException("Illegal type in mkValue");
}
}
class IntValue extends Value {
private int value = 0;
IntValue ( ) { type = Type.INT; }
IntValue (int v) { this( ); value = v; undef = false; }
int intValue ( ) {
assert !undef : "reference to undefined int value";
return value;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
}
class BoolValue extends Value {
private boolean value = false;
BoolValue ( ) { type = Type.BOOL; }
BoolValue (boolean v) { this( ); value = v; undef = false; }
boolean boolValue ( ) {
assert !undef : "reference to undefined bool value";
return value;
}
int intValue ( ) {
assert !undef : "reference to undefined bool value";
return value ? 1 : 0;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
}
class CharValue extends Value {
private char value = ' ';
CharValue ( ) { type = Type.CHAR; }
CharValue (char v) { this( ); value = v; undef = false; }
char charValue ( ) {
assert !undef : "reference to undefined char value";
return value;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
}
class FloatValue extends Value {
private float value = 0;
FloatValue ( ) { type = Type.FLOAT; }
FloatValue (float v) { this( ); value = v; undef = false; }
float floatValue ( ) {
assert !undef : "reference to undefined float value";
return value;
}
public String toString( ) {
if (undef) return "undef";
return "" + value;
}
}
class Binary extends Expression {
// Binary = Operator op; Expression term1, term2
Operator op;
Expression term1, term2;
Binary (Operator o, Expression l, Expression r) {
op = o; term1 = l; term2 = r;
} // binary
}
class Unary extends Expression {
// Unary = Operator op; Expression term
Operator op;
Expression term;
Unary (Operator o, Expression e) {
op = o; term = e;
} // unary
}
class Operator {
// Operator = BooleanOp | RelationalOp | ArithmeticOp | UnaryOp
// BooleanOp = && | ||
final static String AND = "&&";
final static String OR = "||";
// RelationalOp = < | <= | == | != | >= | >
final static String LT = "<";
final static String LE = "<=";
final static String EQ = "==";
final static String NE = "!=";
final static String GT = ">";
final static String GE = ">=";
// ArithmeticOp = + | - | * | /
final static String PLUS = "+";
final static String MINUS = "-";
final static String TIMES = "*";
final static String DIV = "/";
// UnaryOp = !
final static String NOT = "!";
final static String NEG = "-";
// CastOp = int | float | char
final static String INT = "int";
final static String FLOAT = "float";
final static String CHAR = "char";
// Typed Operators
// RelationalOp = < | <= | == | != | >= | >
final static String INT_LT = "INT<";
final static String INT_LE = "INT<=";
final static String INT_EQ = "INT==";
final static String INT_NE = "INT!=";
final static String INT_GT = "INT>";
final static String INT_GE = "INT>=";
// ArithmeticOp = + | - | * | /
final static String INT_PLUS = "INT+";
final static String INT_MINUS = "INT-";
final static String INT_TIMES = "INT*";
final static String INT_DIV = "INT/";
// UnaryOp = !
final static String INT_NEG = "-";
// RelationalOp = < | <= | == | != | >= | >
final static String FLOAT_LT = "FLOAT<";
final static String FLOAT_LE = "FLOAT<=";
final static String FLOAT_EQ = "FLOAT==";
final static String FLOAT_NE = "FLOAT!=";
final static String FLOAT_GT = "FLOAT>";
final static String FLOAT_GE = "FLOAT>=";
// ArithmeticOp = + | - | * | /
final static String FLOAT_PLUS = "FLOAT+";
final static String FLOAT_MINUS = "FLOAT-";
final static String FLOAT_TIMES = "FLOAT*";
final static String FLOAT_DIV = "FLOAT/";
// UnaryOp = !
final static String FLOAT_NEG = "-";
// RelationalOp = < | <= | == | != | >= | >
final static String CHAR_LT = "CHAR<";
final static String CHAR_LE = "CHAR<=";
final static String CHAR_EQ = "CHAR==";
final static String CHAR_NE = "CHAR!=";
final static String CHAR_GT = "CHAR>";
final static String CHAR_GE = "CHAR>=";
// RelationalOp = < | <= | == | != | >= | >
final static String BOOL_LT = "BOOL<";
final static String BOOL_LE = "BOOL<=";
final static String BOOL_EQ = "BOOL==";
final static String BOOL_NE = "BOOL!=";
final static String BOOL_GT = "BOOL>";
final static String BOOL_GE = "BOOL>=";
// Type specific cast
final static String I2F = "I2F";
final static String F2I = "F2I";
final static String C2I = "C2I";
final static String I2C = "I2C";
String val;
Operator (String s) { val = s; }
public String toString( ) { return val; }
public boolean equals(Object obj) { return val.equals(obj); }
boolean BooleanOp ( ) { return val.equals(AND) || val.equals(OR); }
boolean RelationalOp ( ) {
return val.equals(LT) || val.equals(LE) || val.equals(EQ)
|| val.equals(NE) || val.equals(GT) || val.equals(GE);
}
boolean ArithmeticOp ( ) {
return val.equals(PLUS) || val.equals(MINUS)
|| val.equals(TIMES) || val.equals(DIV);
}
boolean NotOp ( ) { return val.equals(NOT) ; }
boolean NegateOp ( ) { return val.equals(NEG) ; }
boolean intOp ( ) { return val.equals(INT); }
boolean floatOp ( ) { return val.equals(FLOAT); }
boolean charOp ( ) { return val.equals(CHAR); }
final static String intMap[ ] [ ] = {
{PLUS, INT_PLUS}, {MINUS, INT_MINUS},
{TIMES, INT_TIMES}, {DIV, INT_DIV},
{EQ, INT_EQ}, {NE, INT_NE}, {LT, INT_LT},
{LE, INT_LE}, {GT, INT_GT}, {GE, INT_GE},
{NEG, INT_NEG}, {FLOAT, I2F}, {CHAR, I2C}
};
final static String floatMap[ ] [ ] = {
{PLUS, FLOAT_PLUS}, {MINUS, FLOAT_MINUS},
{TIMES, FLOAT_TIMES}, {DIV, FLOAT_DIV},
{EQ, FLOAT_EQ}, {NE, FLOAT_NE}, {LT, FLOAT_LT},
{LE, FLOAT_LE}, {GT, FLOAT_GT}, {GE, FLOAT_GE},
{NEG, FLOAT_NEG}, {INT, F2I}
};
final static String charMap[ ] [ ] = {
{EQ, CHAR_EQ}, {NE, CHAR_NE}, {LT, CHAR_LT},
{LE, CHAR_LE}, {GT, CHAR_GT}, {GE, CHAR_GE},
{INT, C2I}
};
final static String boolMap[ ] [ ] = {
{EQ, BOOL_EQ}, {NE, BOOL_NE}, {LT, BOOL_LT},
{LE, BOOL_LE}, {GT, BOOL_GT}, {GE, BOOL_GE},
};
final static private Operator map (String[][] tmap, String op) {
for (int i = 0; i < tmap.length; i++)
if (tmap[i][0].equals(op))
return new Operator(tmap[i][1]);
assert false : "should never reach here";
return null;
}
final static public Operator intMap (String op) {
return map (intMap, op);
}
final static public Operator floatMap (String op) {
return map (floatMap, op);
}
final static public Operator charMap (String op) {
return map (charMap, op);
}
final static public Operator boolMap (String op) {
return map (boolMap, op);
}
}