-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCheckingVisitor.java
More file actions
694 lines (516 loc) · 19.9 KB
/
Copy pathTypeCheckingVisitor.java
File metadata and controls
694 lines (516 loc) · 19.9 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import syntaxtree.*;
import java.util.Arrays;
import java.util.HashMap;
/*
* TypeCheckingVisitor
* This currently only checks miniJava programs for type errors.
* This is the visitor that will be used to check for type errors.
* It has the method to traverse the syntax tree for each node type,
* The constructor will take the symbol table as a parameter.
* The data parameter will be the current prefix, initially the empty string ""
* It will use the symbol table to check the types of the expressions and statements.
* Errors will be reported to the standard output and the program will continue
* as if there was no error, looking for more errors
*
* The visit methods will return the type of the node as a string
* and will use "*void" for statements and nodes that don't have a type
*/
/*
* Video 1:https://brandeis.zoom.us/rec/share/H5qY_cVuTlH2FQoRPFJ15Xx_k5D6qJMurU1chXfksARoIf9Ms31Vb-RGGHe9Asji.M4vLe79OLPboS6P0?startTime=1711384592000
* Video 2:https://brandeis.zoom.us/rec/share/H5qY_cVuTlH2FQoRPFJ15Xx_k5D6qJMurU1chXfksARoIf9Ms31Vb-RGGHe9Asji.M4vLe79OLPboS6P0?startTime=1711376496000
* Team: Xin Chen
*/
public class TypeCheckingVisitor implements Visitor {
public static SymbolTableModified st;
public int num_errors=0;
public static PP_Visitor miniJava = new PP_Visitor();
public TypeCheckingVisitor(SymbolTableModified st) {
this.st = st;
}
public static String getTypeName(Object node){
// we only recognize 3 types in miniJava int, boolean, and *void
if (node.getClass().equals(syntaxtree.BooleanType.class)){
return "boolean";
}else if (node.getClass().equals(syntaxtree.IntegerType.class)){
return "int";
} else if (node.getClass().equals(syntaxtree.IntArrayType.class)){
return "int[]";
}else if (node.getClass().equals(syntaxtree.IdentifierType.class)){
String class_name = ((IdentifierType) node).s;
if (st.classes.get("$" + class_name) != null){
return class_name;
}
return "*class";
}
else {return "*void";}
}
public Object visit(And node, Object data){
// not in miniJava
Exp e1=node.e1;
Exp e2=node.e2;
if (!e1.accept(this, data).equals("boolean") || !e2.accept(this, data).equals("boolean")) {
System.out.println("Type error: " + e1 + " != " + e2+" in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
}
return "boolean";
}
public Object visit(ArrayAssign node, Object data){
// not in miniJava
Identifier i = node.i;
Exp e1=node.e1;
Exp e2=node.e2;
node.i.accept(this,data);
node.e1.accept(this, data);
node.e2.accept(this, data);
String result = st.typeName.get(data+"$"+i.s);
String location = (String) data;
if(result == null ||!result.equals("int[]") ){
result = st.typeName.get("$" + location.split("\\$")[1]+"$"+i.s);
if(result == null || !result.equals("int[]") ){
System.out.println("Identifier error: " + i.s + " does not exist as an int[]");
System.out.println("in " + node.accept(miniJava,0));
num_errors ++;
}
}
if (!e1.accept(this, data).equals("int")) {
System.out.println("Type error: " + e1 + " != int in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
}
if (!e2.accept(this, data).equals("int")) {
System.out.println("Type error: " + e2 + " != int in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
}
return "*void";
}
public Object visit(ArrayLength node, Object data){
// not in miniJava
Exp e=node.e;
if (!e.accept(this, data).equals("int[]")) {
System.out.println("Type error: " + e + " != " + "int[]"+" in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors ++;
}
return "int";
}
public Object visit(ArrayLookup node, Object data){
// not in miniJava
Exp e1=node.e1;
Exp e2=node.e2;
if (!e2.accept(this, data).equals("int") || !e1.accept(this, data).equals("int[]")) {
System.out.println("Type error: " + e2 + " != " + "int[]"+" in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors ++;
}
return "int";
}
public Object visit(Assign node, Object data){
Identifier i=node.i;
Exp e=node.e;
String t1 = (String) node.i.accept(this, data);
String t2 = (String) node.e.accept(this, data);
if (!t1.equals(t2)) {
System.out.println("Assign Type error: " + t1 + " != " + t2+" in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
}
return "*void";
}
public Object visit(Block node, Object data){
StatementList slist=node.slist;
if (node.slist != null){
node.slist.accept(this, data);
}
return "*void";
}
public Object visit(BooleanType node, Object data){
return "boolean";
}
public Object visit(Call node, Object data){
// have to check that the method exists and that
// the types of the formal parameters are the same as
// the types of the corresponding arguments.
Exp e1 = node.e1; // in miniJava there is no e1 for a call
Identifier i = node.i;
ExpList e2=node.e2;
String class_name = (String) e1.accept(this, data);
// check that the method exists
if(st.methods.get("$" + class_name + "$" + i.s) == null){
System.out.println("Method " + i.s + " does not exist in class " + class_name);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
return "*void";
}
String paramTypes = st.methods.get("$" + class_name + "$" + i.s);
String returnType = paramTypes.split(" ")[0].trim();
paramTypes = paramTypes.substring(paramTypes.indexOf(" ") + 1).trim();
// paramTypes is the type of the parameters of the method
// e.g. "int int boolean int"
// we get the parameter types by "typing" the formals
// this is somewhat inefficient, we should do this
// when we construct the symbol table....
String argTypes = "";
if (node.e2 != null){
argTypes = ((String) node.e2.accept(this, data)).trim();
}
if (!paramTypes.equals(argTypes)) {
System.out.println("Call Type error: " + paramTypes + " != " + argTypes+" in method "+i.s);
System.out.println("in \n"+node.accept(miniJava,0));
num_errors++;
}
return returnType;
}
public Object visit(ClassDecl node, Object data){
// not in miniJava
Identifier i = node.i;
VarDeclList v=node.v;
MethodDeclList m=node.m;
node.i.accept(this, data);
if (node.v != null){
node.v.accept(this, data);
}
if (node.m != null){
node.m.accept(this, "$" + i.s);
}
return data;
}
public Object visit(ClassDeclList node, Object data){
// not in miniJava
ClassDecl c=node.c;
ClassDeclList clist=node.clist;
node.c.accept(this, data);
if (node.clist != null){
node.clist.accept(this, data);
}
return data;
}
public Object visit(ExpGroup node, Object data){
Exp e=node.e;
String result = (String) node.e.accept(this, data);
return result;
}
public Object visit(ExpList node, Object data){
// this return a list of the types of the expressions!
Exp e=node.e;
ExpList elist=node.elist;
String t1 = (String) node.e.accept(this, data);
String t2 = "";
if (node.elist != null){
t2 = (String) node.elist.accept(this, data);
}
return t1+" "+t2;
}
public Object visit(False node, Object data){
return "boolean";
}
public Object visit(Formal node, Object data){
Identifier i=node.i;
Type t=node.t;
//node.i.accept(this, data);
//node.t.accept(this, data);
if (node.t instanceof BooleanType) {
return "boolean";
} else if (node.t instanceof IntegerType) {
return "int";
} else if (node.t instanceof IntArrayType){
return "int[]";
} else if (node.t instanceof IdentifierType) {
if (st.classes.get("$" + ((IdentifierType) node.t).s.toString()) == null){
System.out.println("Identifier Type error: " + node.t + " is not a valid type");
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
return "*void";
}
return ((IdentifierType) node.t).s.toString();
} else {
System.out.println("Formal Type error: " + node.t + " is not a valid type");
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
return "*void";
}
}
public Object visit(FormalList node, Object data){
Formal f=node.f;
FormalList flist=node.flist;
String t1 = (String) node.f.accept(this, data);
String t2 = "";
if (node.flist != null) {
t2 = (String) node.flist.accept(this, data);
}
return t1+" "+t2;
}
public Object visit(Identifier node, Object data){
String location = (String) data;
String s=node.s;
String result = st.typeName.get(data+"$"+s);
if(result == null ){
result = st.typeName.get("$" + location.split("\\$")[1]+"$"+s);
if(result == null){
System.out.println("Identifier error: " + s + " does not exist");
System.out.println("in " + node.accept(miniJava,0));
num_errors ++;
return "*void";
}
}
return result;
}
public Object visit(IdentifierExp node, Object data){
String location = (String) data;
String s=node.s;
String result = st.typeName.get(location+"$"+s);
if(result == null ){
result = st.typeName.get("$" + location.split("\\$")[1]+"$"+s);
if(result == null){
System.out.println("Identifier error: " + s + " does not exist");
System.out.println("in " + node.accept(miniJava,0));
num_errors ++;
return "*void";
}
}
return result;
}
public Object visit(IdentifierType node, Object data){
String location = (String) data;
String s=node.s;
String result = st.typeName.get(location+"$"+s);
if(result == null ){
result = st.typeName.get("$" + location.split("\\$")[1]+"$"+s);
if(result == null){
System.out.println("Identifier error: " + s + " does not exist");
System.out.println("in " + node.accept(miniJava,0));
num_errors ++;
return "*void";
}
}
return result;
}
public Object visit(If node, Object data){
Exp e=node.e;
Statement s1=node.s1;
Statement s2=node.s2;
node.e.accept(this, data);
node.s1.accept(this, data);
node.s2.accept(this, data);
if(((String)node.e.accept(this, data)) != "boolean"){
System.out.println("Type error: " + node.e + " is not a boolean expression");
System.out.println("in "+node.accept(miniJava,0));
num_errors ++;
}
return "*void";
}
public Object visit(IntArrayType node, Object data){
return "int[]";
}
public Object visit(IntegerLiteral node, Object data){
int i=node.i;
return "int";
}
public Object visit(IntegerType node, Object data){
return "int";
}
public Object visit(LessThan node, Object data){
// not in miniJava
Exp e1=node.e1;
Exp e2=node.e2;
String t1 = (String) node.e1.accept(this, data);
String t2 = (String) node.e2.accept(this, data);
if (!t1.equals("int") || !t2.equals("int")) {
System.out.println("Comparison Type error: " + t1 + " != " + t2+" in node"+node);
System.out.println("in" + node.accept(miniJava, 0));
num_errors++;
}
return "boolean";
}
public Object visit(MainClass node, Object data){
// not in miniJava
Identifier i=node.i;
Statement s=node.s;
node.i.accept(this, data);
node.s.accept(this, data);
return data;
}
public Object visit(MethodDecl node, Object data){
Type t=node.t;
Identifier i=node.i;
FormalList f=node.f;
VarDeclList v=node.v;
StatementList s=node.s;
Exp e=node.e;
//node.t.accept(this, data);
//node.i.accept(this, data);
if (node.f != null){
node.f.accept(this, data);
}
if (node.v != null){
node.v.accept(this, data);
}
if (node.s != null){
node.s.accept(this, data+"$"+i.s);
}
String returnType = (String) node.e.accept(this, data+"$"+i.s);
if (!returnType.equals(getTypeName(node.t))) {
System.out.println("Method Return Type error: " + returnType + " != " + getTypeName(node.t)+" in method "+i.s);
System.out.print("in " + node.accept(miniJava, 0));
num_errors++;
}
return "*void";
}
public Object visit(MethodDeclList node, Object data){
MethodDecl m=node.m;
MethodDeclList mlist=node.mlist;
node.m.accept(this, data);
if (node.mlist != null) {
node.mlist.accept(this, data);
}
return "*void";
}
public Object visit(Minus node, Object data){
Exp e1=node.e1;
Exp e2=node.e2;
String t1 = (String) node.e1.accept(this, data);
String t2 = (String) node.e2.accept(this, data);
if (!t1.equals("int") || !t2.equals("int")) {
System.out.println("Type error: " + t1 + " != " + t2+" in node"+node);
System.out.println("in " + node.accept(miniJava, 0));
num_errors++;
}
return "int";
}
public Object visit(NewArray node, Object data){
// not in miniJava
Exp e=node.e;
if (!((String) node.e.accept(this, data)).equals("int")) {
System.out.println("Type error: " + ((String) node.e.accept(this, data)) + " != int in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
}
return "int[]";
}
public Object visit(NewObject node, Object data){
// not in miniJava
Identifier i=node.i;
if (st.classes.get("$" + i.s) == null) {
System.out.println("Type error: " + "$" + i.s + " does not exist in node "+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors++;
return "*void";
}
return i.s;
}
public Object visit(Not node, Object data){
// not in miniJava
Exp e=node.e;
node.e.accept(this, data);
if(node.e.accept(this, data) != "boolean"){
System.out.println("Type error: " + node.e.accept(this, data) + " != boolean in node"+node);
System.out.println("in "+node.accept(miniJava,0));
num_errors ++;
}
return "boolean";
}
public Object visit(Plus node, Object data){
Exp e1=node.e1;
Exp e2=node.e2;
String t1 = (String) node.e1.accept(this, data);
String t2 = (String) node.e2.accept(this, data);
if (!t1.equals("int") || !t2.equals("int")) {
System.out.println("Type error: " + e1.accept(miniJava, 0) + " or " + e2.accept(miniJava, 0) +" != int in node"+node);
System.out.println("in " + node.accept(miniJava, 0));
num_errors++;
}
return "int";
}
public Object visit(Print node, Object data){
Exp e=node.e;
String t1 = (String) node.e.accept(this, data);
if (!t1.equals("int")&&!t1.equals("boolean")&&!t1.equals("int[]")) {
System.out.println("Print Type error: " + t1 + " is not a valid type for print");
System.out.println("in " + node.accept(miniJava, 0));
num_errors++;
}
return "*void";
}
public Object visit(Program node, Object data){
// not in miniJava
MainClass m=node.m;
ClassDeclList c=node.c;
node.m.accept(this, data);
if (node.c != null){
node.c.accept(this, data);
}
return "*void";
}
public Object visit(StatementList node, Object data){
Statement s=node.s;
StatementList slist=node.slist;
node.s.accept(this, data);
if (node.slist != null){
node.slist.accept(this, data);
}
return "*void";
}
public Object visit(This node, Object data){
// not in miniJava
String variable_name = (String) data;
return variable_name.split("\\$")[1].trim();
}
public Object visit(Times node, Object data){
Exp e1=node.e1;
Exp e2=node.e2;
String t1 = (String) node.e1.accept(this, data);
String t2 = (String) node.e2.accept(this, data);
if (!t1.equals("int") || !t2.equals("int")) {
System.out.println("Type error: " + t1 + " != " + t2+" in node"+node);
num_errors++;
}
return "int";
}
public Object visit(True node, Object data){
return "boolean";
}
public Object visit(VarDecl node, Object data){
Type t=node.t;
Identifier i=node.i;
//node.t.accept(this, data);
//node.i.accept(this, data);
if (node.t instanceof BooleanType) {
return "boolean";
} else if (node.t instanceof IntegerType) {
return "int";
} else if (node.t instanceof IntArrayType) {
return "int[]";
}
else if(st.typeName.get("$" + ((IdentifierType) t).s) == "*class"){
return "*class";
}
else{
System.out.println("Unknown Type, Type error: " + node.t + " is not a valid type");
num_errors++;
}
return "*void";
}
public Object visit(VarDeclList node, Object data){
VarDecl v=node.v;
VarDeclList vlist=node.vlist;
node.v.accept(this, data);
if (node.vlist != null) {
node.vlist.accept(this, data);
}
return "*void";
}
public Object visit(While node, Object data){
// not in miniJava
Exp e=node.e;
Statement s=node.s;
node.e.accept(this, data);
node.s.accept(this, data);
if(((String) node.e.accept(this, data)) != "boolean"){
System.out.println("Type error: " + node.e + " is not a boolean expression");
System.out.println("in "+node.accept(miniJava,0));
num_errors ++;
}
return "*void";
}
}