-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeCheckVisitor.java
629 lines (511 loc) · 15 KB
/
TypeCheckVisitor.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import java.util.StringTokenizer;
import syntaxtree.*;
import visitor.*;
import symbol.*;
public class TypeCheckVisitor extends GJDepthFirst<String, String[]> {
private final SymbolTable symbols;
public TypeCheckVisitor(SymbolTable symbols) {
this.symbols = symbols;
}
/**
* 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 {
// Names of class and main method (only one method in the main class), so we can include it here
String className = n.f1.accept(this, argu);
String[] names = new String[] { className, "main" };
n.f14.accept(this, names);
n.f15.accept(this, names);
return null;
}
/**
* f0 -> "class"
* f1 -> Identifier()
* f2 -> "{"
* f3 -> ( VarDeclaration() )*
* f4 -> ( MethodDeclaration() )*
* f5 -> "}"
*/
@Override
public String visit(ClassDeclaration n, String[] argu) throws Exception {
String[] names = new String[2];
names[0] = n.f1.accept(this, names);
n.f3.accept(this, names);
n.f4.accept(this, names);
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[] names = new String[2];
names[0] = n.f1.accept(this, names);
n.f5.accept(this, names);
n.f6.accept(this, names);
return null;
}
/**
* f0 -> Type()
* f1 -> Identifier()
* f2 -> ";"
*/
@Override
public String visit(VarDeclaration n, String[] argu) throws Exception {
String varType = n.f0.accept(this, argu);
String varName = n.f1.accept(this, argu);
if (!symbols.isValidType(varType))
throw new TypeCheckException(argu, "Type " + varType + " of variable " + varName + " is not a basic type or a defined class");
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 {
/* Declared method return type */
String methodType = n.f1.accept(this, argu);
/* add function name to array passed from Class Declaration */
argu[1] = n.f2.accept(this, argu);
n.f4.accept(this, argu); // Method parameters
n.f7.accept(this, argu); // Method variables
n.f8.accept(this, argu); // Method body
/* Return type of final expression */
String exprType = n.f10.accept(this, argu);
// Check if expression return type matched expected function signature
if (!symbols.typesMatch(exprType, methodType))
throw new TypeCheckException(argu, "Return type mismatch (Expected " + methodType + ", got " + exprType + ')');
return null;
}
/**
* f0 -> Type()
* f1 -> Identifier()
*/
@Override
public String visit(FormalParameter n, String[] argu) throws Exception {
String paramType = n.f0.accept(this, argu);
String paramName = n.f1.accept(this, argu);
if (!symbols.isValidType(paramType))
throw new TypeCheckException(argu, "Type " + paramType + " of parameter " + paramName + " is not a basic type or a defined class");
return null;
}
/**
* f0 -> "int"
* f1 -> "["
* f2 -> "]"
*/
@Override
public String visit(ArrayType n, String[] argu) throws Exception {
return n.f0.toString() + n.f1.toString() + n.f2.toString();
}
/**
* f0 -> "boolean"
*/
@Override
public String visit(BooleanType n, String[] argu) throws Exception {
return n.f0.toString();
}
/**
* f0 -> "int"
*/
@Override
public String visit(IntegerType n, String[] argu) throws Exception {
return n.f0.toString();
}
/**
* f0 -> <IDENTIFIER>
*/
@Override
public String visit(Identifier n, String[] argu) throws Exception {
return n.f0.toString();
}
/* Statement Family */
/**
* f0 -> Identifier()
* f1 -> "="
* f2 -> Expression()
* f3 -> ";"
*/
@Override
public String visit(AssignmentStatement n, String[] argu) throws Exception {
String idName = n.f0.accept(this, argu);
String idType = symbols.getFieldType(argu[0], argu[1], idName);
if (idType == null)
throw new TypeCheckException(argu, "Identifier " + idName + " is undefined");
String exprType = n.f2.accept(this, argu);
if (!symbols.typesMatch(exprType, idType))
throw new TypeCheckException(argu, "Type mismatch in assignment (" + exprType + " cannot be assigned to " + idType + ')');
return null;
}
/**
* f0 -> Identifier()
* f1 -> "["
* f2 -> Expression()
* f3 -> "]"
* f4 -> "="
* f5 -> Expression()
* f6 -> ";"
*/
@Override
public String visit(ArrayAssignmentStatement n, String[] argu) throws Exception {
String idName = n.f0.accept(this, argu);
String idType = symbols.getFieldType(argu[0], argu[1], idName);
if (!idType.equals("int[]"))
throw new TypeCheckException(argu, "Identifier " + idName + " is not of type int[]");
String indexType = n.f2.accept(this, argu);
if (!indexType.equals("int"))
throw new TypeCheckException(argu, "Indexing expression for array " + idName + "[] is not of type int");
String exprType = n.f5.accept(this, argu);
if (!exprType.equals("int"))
throw new TypeCheckException(argu, "Expression assigned to member of array " + idName + "[] is not of type int");
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 exprType = n.f2.accept(this, argu);
if (!exprType.equals("boolean"))
throw new TypeCheckException(argu, "Expression used as \"if\" condition is not of type boolean");
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 exprType = n.f2.accept(this, argu);
if (!exprType.equals("boolean"))
throw new TypeCheckException(argu, "Expression used as \"while\" condition is not of type boolean");
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 exprType = n.f2.accept(this, argu);
if (!exprType.equals("int"))
throw new TypeCheckException(argu, "Expression to be printed is not of type int");
return null;
}
/* Expression Family */
/**
* f0 -> Clause()
* f1 -> "&&"
* f2 -> Clause()
*/
@Override
public String visit(AndExpression n, String[] argu) throws Exception {
String exprType1 = n.f0.accept(this, argu);
if (!exprType1.equals("boolean"))
throw new TypeCheckException(argu, "Expression on left side of && is not of type boolean");
String exprType2 = n.f2.accept(this, argu);
if (!exprType2.equals("boolean"))
throw new TypeCheckException(argu, "Expression on right side of && is not of type boolean");
return "boolean";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "<"
* f2 -> PrimaryExpression()
*/
@Override
public String visit(CompareExpression n, String[] argu) throws Exception {
String exprType1 = n.f0.accept(this, argu);
if (!exprType1.equals("int"))
throw new TypeCheckException(argu, "Expression on left side of < is not of type int");
String exprType2 = n.f2.accept(this, argu);
if (!exprType2.equals("int"))
throw new TypeCheckException(argu, "Expression on right side of < is not of type int");
return "boolean";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "+"
* f2 -> PrimaryExpression()
*/
@Override
public String visit(PlusExpression n, String[] argu) throws Exception {
String exprType1 = n.f0.accept(this, argu);
if (!exprType1.equals("int"))
throw new TypeCheckException(argu, "Expression on left side of + is not of type int");
String exprType2 = n.f2.accept(this, argu);
if (!exprType2.equals("int"))
throw new TypeCheckException(argu, "Expression on right side of + is not of type int");
return "int";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "-"
* f2 -> PrimaryExpression()
*/
@Override
public String visit(MinusExpression n, String[] argu) throws Exception {
String exprType1 = n.f0.accept(this, argu);
if (!exprType1.equals("int"))
throw new TypeCheckException(argu, "Expression on left side of - is not of type int");
String exprType2 = n.f2.accept(this, argu);
if (!exprType2.equals("int"))
throw new TypeCheckException(argu, "Expression on right side of - is not of type int");
return "int";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "*"
* f2 -> PrimaryExpression()
*/
@Override
public String visit(TimesExpression n, String[] argu) throws Exception {
String exprType1 = n.f0.accept(this, argu);
if (!exprType1.equals("int"))
throw new TypeCheckException(argu, "Expression on left side of * is not of type int");
String exprType2 = n.f2.accept(this, argu);
if (!exprType2.equals("int"))
throw new TypeCheckException(argu, "Expression on right side of * is not of type int");
return "int";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "["
* f2 -> PrimaryExpression()
* f3 -> "]"
*/
@Override
public String visit(ArrayLookup n, String[] argu) throws Exception {
String exprType1 = n.f0.accept(this, argu);
if (!exprType1.equals("int[]"))
throw new TypeCheckException(argu, "Expression on left side of [] is not of type int[]");
String exprType2 = n.f2.accept(this, argu);
if (!exprType2.equals("int"))
throw new TypeCheckException(argu, "Expression inside [] is not of type int");
return "int";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "."
* f2 -> "length"
*/
@Override
public String visit(ArrayLength n, String[] argu) throws Exception {
String exprType = n.f0.accept(this, argu);
if (!exprType.equals("int[]"))
throw new TypeCheckException(argu, "Expression on left side of array.length is not of type int[]");
return "int";
}
/**
* f0 -> PrimaryExpression()
* f1 -> "."
* f2 -> Identifier()
* f3 -> "("
* f4 -> ( ExpressionList() )?
* f5 -> ")"
*/
@Override
public String visit(MessageSend n, String[] argu) throws Exception {
// Class name
String exprType = n.f0.accept(this, argu);
ClassSymbol callClass = symbols.getClass(exprType);
if (callClass == null)
throw new TypeCheckException(argu, "Attempt to call method on non-class (" + exprType + ')');
// Method name
String idName = n.f2.accept(this, argu);
// Pick up arguments
String argList = null;
if (n.f4.present())
argList = n.f4.accept(this, argu);
MethodSymbol classMethod = callClass.getMethod(idName);
if (classMethod == null)
throw new TypeCheckException(argu, "class " + callClass.getName() + " has no method named " + idName + "()");
// Compare temporary method construct with signature of normal class method
MethodSymbol callMethod = new MethodSymbol(classMethod.getType(), idName);
if (argList != null) {
StringTokenizer args = new StringTokenizer(argList, ",");
int counter = 0; /* Used to get hashing to work */
while (args.hasMoreTokens()) {
String argType = args.nextToken();
callMethod.addParameter(new Symbol(argType, "a" + counter++));
}
}
if (!symbols.compatibleCall(classMethod, callMethod))
throw new TypeCheckException(argu, "Incorrect use of method " + callClass.getName() + '.' + callMethod.getName() + "()");
return callMethod.getType();
}
/**
* f0 -> Expression()
* f1 -> ExpressionTail()
*/
@Override
public String visit(ExpressionList n, String[] argu) throws Exception {
String[] argList = new String[3];
/* Carry class and method names to lowe levels of recursion as normal */
argList[0] = argu[0];
argList[1] = argu[1];
/* Append arguments types from expressions */
argList[2] = n.f0.accept(this, argList);
n.f1.accept(this, argList);
return argList[2];
}
/**
* f0 -> ","
* f1 -> Expression()
*/
@Override
public String visit(ExpressionTerm n, String[] argu) throws Exception {
argu[2] += "," + n.f1.accept(this, argu);
return null;
}
/**
* f0 -> IntegerLiteral()
* | TrueLiteral()
* | FalseLiteral()
* | Identifier()
* | ThisExpression()
* | ArrayAllocationExpression()
* | AllocationExpression()
* | BracketExpression()
*/
@Override
public String visit(PrimaryExpression n, String[] argu) throws Exception {
String exprType = n.f0.accept(this, argu);
if (symbols.isValidType(exprType))
return exprType;
if (exprType.equals("this"))
return argu[0]; /* The class name */
/* At this point, exprType is an identifier */
String idType = symbols.getFieldType(argu[0], argu[1], exprType);
if (idType == null)
throw new TypeCheckException(argu, "Identifier " + exprType + " is undefined");
return idType;
}
/**
* f0 -> <INTEGER_LITERAL>
*/
@Override
public String visit(IntegerLiteral n, String[] argu) throws Exception {
return "int";
}
/**
* f0 -> "true"
*/
@Override
public String visit(TrueLiteral n, String[] argu) throws Exception {
return "boolean";
}
/**
* f0 -> "false"
*/
@Override
public String visit(FalseLiteral n, String[] argu) throws Exception {
return "boolean";
}
/**
* f0 -> "this"
*/
@Override
public String visit(ThisExpression n, String[] argu) throws Exception {
return "this";
}
/**
* f0 -> "new"
* f1 -> "int"
* f2 -> "["
* f3 -> Expression()
* f4 -> "]"
*/
@Override
public String visit(ArrayAllocationExpression n, String[] argu) throws Exception {
String exprType = n.f3.accept(this, argu);
if (!exprType.equals("int"))
throw new TypeCheckException(argu, "Expression used for size of array allocation must be int");
return "int[]";
}
/**
* f0 -> "new"
* f1 -> Identifier()
* f2 -> "("
* f3 -> ")"
*/
@Override
public String visit(AllocationExpression n, String[] argu) throws Exception {
String className = n.f1.accept(this, argu);
if (!symbols.hasClass(className))
throw new TypeCheckException(argu, "Type " + className + "is not a defined class");
return className;
}
/**
* f0 -> "!"
* f1 -> Clause()
*/
@Override
public String visit(NotExpression n, String[] argu) throws Exception {
String exprType = n.f1.accept(this, argu);
if (!exprType.equals("boolean"))
throw new TypeCheckException(argu, "Expression used in logical \"not\" is not of type boolean");
return "boolean";
}
/**
* f0 -> "("
* f1 -> Expression()
* f2 -> ")"
*/
@Override
public String visit(BracketExpression n, String[] argu) throws Exception {
return n.f1.accept(this, argu);
}
}