-
Notifications
You must be signed in to change notification settings - Fork 1
/
SCVisitor.java
530 lines (417 loc) · 13.3 KB
/
SCVisitor.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
import syntaxtree.*;
import symbol_table.*;
import visitor.GJDepthFirst;
public class SCVisitor extends GJDepthFirst<String, String> {
private SymbolTable st;
// Scope is represented as a string of the form "ClassId:MethodId"
private String getScopeClass(String scope) {
String[] tokens = scope.split(":");
return tokens[0];
}
private String getScopeMethod(String scope) {
String[] tokens = scope.split(":");
return tokens.length == 2 ? tokens[1] : null;
}
private VarInfo searchVar(String target_id, String scope) {
if (scope == null)
return null; // We didn't find the var we were looking for
String class_id = getScopeClass(scope);
String method_id = getScopeMethod(scope);
// Assumes that the class contained in 'scope' is declared -- no null checks
ClassInfo classinfo = st.getClassInfo(class_id);
if (method_id != null) {
MethodInfo methodinfo = classinfo.getMethodInfo(method_id);
VarInfo targetinfo = methodinfo.getInfo(target_id);
if (targetinfo != null)
return targetinfo; // Found a matching local var (or arg) inside the method
}
// Didn't find the target var in the method's scope, search class fields upwards
VarInfo targetinfo = classinfo.getFieldInfo(target_id);
return targetinfo != null ? targetinfo
: searchVar(target_id, st.getSuperClassId(class_id));
}
private MethodInfo searchMethod(String target_id, String scope) {
if (scope == null)
return null; // We didn't find the method we were looking for
String class_id = getScopeClass(scope);
// Assumes that the class contained in 'scope' is defined -- no null checks
ClassInfo classinfo = st.getClassInfo(class_id);
MethodInfo targetinfo = classinfo.getMethodInfo(target_id);
return targetinfo != null ? targetinfo
: searchMethod(target_id, st.getSuperClassId(class_id));
}
private boolean isCompatible(String t1, String t2) throws Exception {
if (t1.equals("int")) return t2.equals("int");
if (t1.equals("boolean")) return t2.equals("boolean");
if (t1.equals("int[]")) return t2.equals("int[]");
if (st.getClassInfo(t1) == null || st.getClassInfo(t2) == null)
throw new Exception("Invalid type (undefined symbol)");
// t1 is compatible with t2 if (a) they're the same or (b) t1 inherits from t2
return t1.equals(t2) || st.isDerivedFrom(t1, t2);
}
public SCVisitor(SymbolTable st) {
this.st = st;
}
// f0 -> "class"
// f1 -> Identifier()
// f2 -> "{"
// f3 -> "public"
// f4 -> "static"
// f5 -> "void"
// f6 -> "main"
// f7 -> "("
// f8 -> "String"
// f9 -> "["
// f10 -> "]"
// f11 -> Identifier()
// f12 -> ")"
// f13 -> "{"
// f14 -> ( VarDeclaration() )*
// f15 -> ( Statement() )*
// f16 -> "}"
// f17 -> "}"
@Override
public String visit(MainClass n, String argu) throws Exception {
String classname = n.f1.f0.tokenImage;
n.f15.accept(this, classname + ":main");
return null;
}
// f0 -> "class"
// f1 -> Identifier()
// f2 -> "{"
// f3 -> ( VarDeclaration() )*
// f4 -> ( MethodDeclaration() )*
// f5 -> "}"
@Override
public String visit(ClassDeclaration n, String argu) throws Exception {
String classname = n.f1.f0.tokenImage;
n.f4.accept(this, classname);
return null;
}
// f0 -> "class"
// f1 -> Identifier()
// f2 -> "extends"
// f3 -> Identifier()
// f4 -> "{"
// f5 -> ( VarDeclaration() )*
// f6 -> ( MethodDeclaration() )*
// f7 -> "}"
@Override
public String visit(ClassExtendsDeclaration n, String argu) throws Exception {
String classname = n.f1.f0.tokenImage;
n.f6.accept(this, classname);
return null;
}
// f0 -> "public"
// f1 -> Type()
// f2 -> Identifier()
// f3 -> "("
// f4 -> ( FormalParameterList() )?
// f5 -> ")"
// f6 -> "{"
// f7 -> ( VarDeclaration() )*
// f8 -> ( Statement() )*
// f9 -> "return"
// f10 -> Expression()
// f11 -> ";"
// f12 -> "}"
@Override
public String visit(MethodDeclaration n, String argu) throws Exception {
String methodtype = n.f1.accept(this, null);
String methodname = n.f2.f0.tokenImage;
String scope = getScopeClass(argu) + ":" + methodname;
n.f8.accept(this, scope);
String returntype = n.f10.accept(this, scope);
if (!isCompatible(returntype, methodtype))
throw new Exception("Incompatible return type");
return null;
}
@Override
public String visit(ArrayType n, String argu) {
return "int[]";
}
@Override
public String visit(BooleanType n, String argu) {
return "boolean";
}
@Override
public String visit(IntegerType n, String argu) {
return "int";
}
// f0 -> Identifier()
// f1 -> "="
// f2 -> Expression()
// f3 -> ";"
@Override
public String visit(AssignmentStatement n, String argu) throws Exception {
String lvalue_type = n.f0.accept(this, argu);
String rvalue_type = n.f2.accept(this, argu);
if (!isCompatible(rvalue_type, lvalue_type))
throw new Exception("Incompatible types in assignment");
return null;
}
// f0 -> Identifier()
// f1 -> "["
// f2 -> Expression()
// f3 -> "]"
// f4 -> "="
// f5 -> Expression()
// f6 -> ";"
@Override
public String visit(ArrayAssignmentStatement n, String argu) throws Exception {
String lvalue_type = n.f0.accept(this, argu);
if (!lvalue_type.equals("int[]"))
throw new Exception("Invalid lvalue type in array assignment");
String idx_type = n.f2.accept(this, argu);
if (!idx_type.equals("int"))
throw new Exception("Invalid array index type");
String rvalue_type = n.f5.accept(this, argu);
if (!rvalue_type.equals("int"))
throw new Exception("Incompatible types in array assignment");
return null;
}
// f0 -> "if"
// f1 -> "("
// f2 -> Expression()
// f3 -> ")"
// f4 -> Statement()
// f5 -> "else"
// f6 -> Statement()
@Override
public String visit(IfStatement n, String argu) throws Exception {
String condition_type = n.f2.accept(this, argu);
if (!condition_type.equals("boolean"))
throw new Exception("Invalid condition expression type");
n.f4.accept(this, argu);
n.f6.accept(this, argu);
return null;
}
// f0 -> "while"
// f1 -> "("
// f2 -> Expression()
// f3 -> ")"
// f4 -> Statement()
@Override
public String visit(WhileStatement n, String argu) throws Exception {
String condition_type = n.f2.accept(this, argu);
if (!condition_type.equals("boolean"))
throw new Exception("Invalid condition expression type");
n.f4.accept(this, argu);
return null;
}
// f0 -> "System.out.println"
// f1 -> "("
// f2 -> Expression()
// f3 -> ")"
// f4 -> ";"
@Override
public String visit(PrintStatement n, String argu) throws Exception {
String print_arg_type = n.f2.accept(this, argu);
if (!print_arg_type.equals("int"))
throw new Exception("Invalid print statement argument type");
return null;
}
// f0 -> Clause()
// f1 -> "&&"
// f2 -> Clause()
@Override
public String visit(AndExpression n, String argu) throws Exception {
String clause1_type = n.f0.accept(this, argu);
String clause2_type = n.f2.accept(this, argu);
if (!clause1_type.equals("boolean") || !clause2_type.equals("boolean"))
throw new Exception("Invalid operand type in conjunction");
return "boolean";
}
// f0 -> PrimaryExpression()
// f1 -> "<"
// f2 -> PrimaryExpression()
@Override
public String visit(CompareExpression n, String argu) throws Exception {
String expr1_type = n.f0.accept(this, argu);
String expr2_type = n.f2.accept(this, argu);
if (!expr1_type.equals("int") || !expr2_type.equals("int"))
throw new Exception("Invalid operand type in comparison");
return "boolean";
}
// f0 -> PrimaryExpression()
// f1 -> "+"
// f2 -> PrimaryExpression()
@Override
public String visit(PlusExpression n, String argu) throws Exception {
String expr1_type = n.f0.accept(this, argu);
String expr2_type = n.f2.accept(this, argu);
if (!expr1_type.equals("int") || !expr2_type.equals("int"))
throw new Exception("Invalid operand type in addition");
return "int";
}
// f0 -> PrimaryExpression()
// f1 -> "-"
// f2 -> PrimaryExpression()
@Override
public String visit(MinusExpression n, String argu) throws Exception {
String expr1_type = n.f0.accept(this, argu);
String expr2_type = n.f2.accept(this, argu);
if (!expr1_type.equals("int") || !expr2_type.equals("int"))
throw new Exception("Invalid operand type in subtraction");
return "int";
}
// f0 -> PrimaryExpression()
// f1 -> "*"
// f2 -> PrimaryExpression()
@Override
public String visit(TimesExpression n, String argu) throws Exception {
String expr1_type = n.f0.accept(this, argu);
String expr2_type = n.f2.accept(this, argu);
if (!expr1_type.equals("int") || !expr2_type.equals("int"))
throw new Exception("Invalid operand type in multiplication");
return "int";
}
// f0 -> PrimaryExpression()
// f1 -> "["
// f2 -> PrimaryExpression()
// f3 -> "]"
@Override
public String visit(ArrayLookup n, String argu) throws Exception {
String expr_type = n.f0.accept(this, argu);
String idx_type = n.f2.accept(this, argu);
if (!expr_type.equals("int[]"))
throw new Exception("Non-array type in lookup expression");
if (!idx_type.equals("int"))
throw new Exception("Invalid array index type");
return "int";
}
// f0 -> PrimaryExpression()
// f1 -> "."
// f2 -> "length"
@Override
public String visit(ArrayLength n, String argu) throws Exception {
String expr_type = n.f0.accept(this, argu);
if (!expr_type.equals("int[]"))
throw new Exception("Non-array type in length expression");
return "int";
}
// f0 -> PrimaryExpression()
// f1 -> "."
// f2 -> Identifier()
// f3 -> "("
// f4 -> ( ExpressionList() )?
// f5 -> ")"
@Override
public String visit(MessageSend n, String argu) throws Exception {
String expr_type = n.f0.accept(this, argu);
ClassInfo classinfo = st.getClassInfo(expr_type);
if (classinfo == null)
throw new Exception("Method called on object of invalid type");
String target_method = n.f2.f0.tokenImage;
MethodInfo methodinfo = searchMethod(target_method, expr_type);
if (methodinfo == null)
throw new Exception("Invalid method (undefined symbol)");
// Calling n.f4.accept will give us a string like "int,boolean,T"
String[] argtypes = n.f4.present() ? n.f4.accept(this, argu).split(",")
: new String[0]; // Ensures .length == 0
if (argtypes.length != methodinfo.getNumArgs())
throw new Exception("Invalid number of arguments in method call");
for (int i = 0; i < argtypes.length; i++)
if (!isCompatible(argtypes[i], methodinfo.getArgType(i)))
throw new Exception("Incompatible method argument type");
return methodinfo.getType();
}
// f0 -> Expression()
// f1 -> ExpressionTail()
@Override
public String visit(ExpressionList n, String argu) throws Exception {
String ret = n.f0.accept(this, argu);
if (n.f1 != null)
ret += n.f1.accept(this, argu);
return ret;
}
// f0 -> ( ExpressionTerm() )*
@Override
public String visit(ExpressionTail n, String argu) throws Exception {
String ret = "";
for (Node node : n.f0.nodes)
ret += "," + node.accept(this, argu);
return ret;
}
// f0 -> ","
// f1 -> Expression()
@Override
public String visit(ExpressionTerm n, String argu) throws Exception {
return n.f1.accept(this, argu);
}
@Override
public String visit(IntegerLiteral n, String argu) {
return "int";
}
@Override
public String visit(TrueLiteral n, String argu) {
return "boolean";
}
@Override
public String visit(FalseLiteral n, String argu) {
return "boolean";
}
@Override
public String visit(Identifier n, String argu) throws Exception {
String target_id = n.f0.toString();
// Try to find the identifier's type in the scope designated by argu
VarInfo targetinfo = searchVar(target_id, argu);
if (targetinfo == null)
throw new Exception("Invalid identifier (undefined symbol)");
return targetinfo.getType();
}
@Override
public String visit(Type n, String argu) throws Exception {
// .which = 0 -> ArrayType
// = 1 -> BooleanType
// = 2 -> IntegerType
// = 3 -> Identifier
//
// (See minijava-jtb.jj, lines: 421-448)
return n.f0.which == 3 ? ((Identifier) n.f0.choice).f0.tokenImage
: super.visit(n, argu);
}
@Override
public String visit(ThisExpression n, String argu) {
return getScopeClass(argu); // Return type is just the surrounding class' id
}
// f0 -> "new"
// f1 -> "int"
// f2 -> "["
// f3 -> Expression()
// f4 -> "]"
@Override
public String visit(ArrayAllocationExpression n, String argu) throws Exception {
String idx_type = n.f3.accept(this, argu);
if (!idx_type.equals("int"))
throw new Exception("Invalid array index type");
return "int[]";
}
// f0 -> "new"
// f1 -> Identifier()
// f2 -> "("
// f3 -> ")"
@Override
public String visit(AllocationExpression n, String argu) throws Exception {
String typename = n.f1.f0.tokenImage;
if (st.getClassInfo(typename) == null)
throw new Exception("Invalid type (undefined symbol)");
return typename; // Name of the class corresponding to Identifier()
}
// f0 -> "!"
// f1 -> Clause()
@Override
public String visit(NotExpression n, String argu) throws Exception {
String clause_type = n.f1.accept(this, argu);
if (!clause_type.equals("boolean"))
throw new Exception("Invalid operand type in negation");
return "boolean";
}
// f0 -> "("
// f1 -> Expression()
// f2 -> ")"
@Override
public String visit(BracketExpression n, String argu) throws Exception {
return n.f1.accept(this, argu);
}
}