-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCheckingVisitor.java
More file actions
718 lines (601 loc) · 18.3 KB
/
Copy pathTypeCheckingVisitor.java
File metadata and controls
718 lines (601 loc) · 18.3 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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.List;
import syntaxtree.*;
import visitor.GJDepthFirst;
/*This visitor checks: Inheritance, Circular Dependency,
Type checking in expressions, statements, methods,conditions */
public class TypeCheckingVisitor extends GJDepthFirst <String, Argu>{
LinkedHashMap<String,ClassInfo> symbolTable;
/*getting the symbol table from v1 */
public TypeCheckingVisitor(LinkedHashMap<String, ClassInfo> symbolTable){
this.symbolTable = symbolTable;
}
/*HELPER FUNCTIONS */
/*Helper that checks whether var1 is subtype of var2 or of the same type*/
boolean checkType(String var1, String var2){
//same type
if(var1.equals(var2)){
return true;
}
ClassInfo cInfo=symbolTable.get(var1);
//no such class
if (cInfo==null){
return false;
}
//doesnt have a parent so type doesnt exist for sure
if(cInfo.parent==null){
return false;
}
//go up through the parents to check
return checkType(cInfo.parent, var2);
}
/*Helper that checks whether a method was declared in a class or any of its parents */
/*Returns the MethodInfo list or null if Method doesnt exist anywhere*/
List<MethodInfo> checkMethod(String classname, String methodname){
//in the class
if(symbolTable.get(classname).methods.containsKey(methodname)){
return symbolTable.get(classname).methods.get(methodname);
}
//in parent
String parentname=symbolTable.get(classname).parent;
if(parentname!=null){
return checkMethod(parentname, methodname);
}
return null;
}
//returns the current method from the MethodInfo list
// last added is the current one
MethodInfo getCurrentMethod(String className, String methodName){
List<MethodInfo> list = symbolTable.get(className).methods.get(methodName);
return list.get(list.size() - 1);
}
/*VISIT FUNCTIONS */
/*Main Class */
/**
* Grammar production:
* 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 mc, Argu argu){
String classname=mc.f1.f0.toString();
Argu argu2=new Argu(classname,"main");
mc.f15.accept(this, argu2);
return null;
}
/*CLASS DECLARATION */
/**
* Grammar production:
* f0 -> "class"
* f1 -> Identifier()
* f2 -> "{"
* f3 -> ( VarDeclaration() )*
* f4 -> ( MethodDeclaration() )*
* f5 -> "}"
*/
@Override
public String visit(ClassDeclaration cd, Argu argu){
String classname=cd.f1.f0.toString();
Argu argu2=new Argu(classname, null);
cd.f4.accept(this, argu2);
return null;
}
/*CLASS EXTENDS DECLARATION */
/**
* Grammar production:
* f0 -> "class"
* f1 -> Identifier()
* f2 -> "extends"
* f3 -> Identifier()
* f4 -> "{"
* f5 -> ( VarDeclaration() )*
* f6 -> ( MethodDeclaration() )*
* f7 -> "}"
*/
@Override
public String visit(ClassExtendsDeclaration ced, Argu argu){
String classname=ced.f1.f0.toString();
String parentclassname=ced.f3.f0.toString();
//Check that Parent was actually declared before
if(!symbolTable.containsKey(parentclassname)){
throw new RuntimeException("Undefined class: "+parentclassname+"\n");
}
// Check for circular dependency
String current= classname;
while(current!=null){
current= symbolTable.get(current).parent;
if(current!=null && current.equals(classname) ){
throw new RuntimeException("Circular dependency between: "+classname+" "+parentclassname+"\n");
}
}
Argu argu2=new Argu(classname,null);
ced.f5.accept(this,argu2);
ced.f6.accept(this,argu2);
return null;
}
/*METHOD DECLARATION */
/**
* Grammar production:
* 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 md, Argu argu){
String methodname=md.f2.f0.toString();
String methodtype=md.f1.accept(this,argu);
//type validity
if(!methodtype.equals("int")&& !methodtype.equals("boolean") && !methodtype.equals("int[]")){
//doesnt exist as a class either
if(!symbolTable.containsKey(methodtype)){
throw new RuntimeException("No such type: "+methodtype+"\n");
}
}
//retur type check
Argu argu2= new Argu(argu.className, methodname);
md.f4.accept(this, argu2);
md.f8.accept(this,argu2);
String returntype= md.f10.accept(this,argu2);
if(!checkType(returntype,methodtype)){
throw new RuntimeException("In method: "+methodname+": Method Type != Return Type: "+methodtype+" "+returntype+"\n");
}
return null;
}
/*Var Declaration */
/**
* Grammar production:
* f0 -> Type()
* f1 -> Identifier()
* f2 -> ";"
*/
@Override
public String visit(VarDeclaration vd, Argu argu){
String type=vd.f0.accept(this, argu);
//type validity
if(!type.equals("int")&& !type.equals("boolean") && !type.equals("int[]")){
//doesnt exist as a class either
if(!symbolTable.containsKey(type)){
throw new RuntimeException("No such type: "+type+"\n");
}
}
return null;
}
/*FORMAL PARAMETER */
/**
* Grammar production:
* f0 -> Type()
* f1 -> Identifier()
*/
@Override
public String visit(FormalParameter fp, Argu argu){
String parametertype=fp.f0.accept(this,argu);
if(!parametertype.equals("int")&& !parametertype.equals("boolean")&& !parametertype.equals("int[]")){
if(!symbolTable.containsKey(parametertype)){
throw new RuntimeException("No such type: "+parametertype+"\n");
}
}
return null;
}
/*STATEMENTS */
/*Assignment Statement */
/**
* Grammar production:
* f0 -> Identifier()
* f1 -> "="
* f2 -> Expression()
* f3 -> ";"
*/
@Override
public String visit(AssignmentStatement as, Argu argu){
String leftType=as.f0.accept(this,argu);
String rightType=as.f2.accept(this,argu);
if(!checkType(rightType, leftType )){
throw new RuntimeException("Type mismatch Id=Expr: "+leftType+" = "+rightType+"\n Expected: Expr=subtype of Id\n ");
}
return null;
}
/*Array Assignment Statemnt */
/**
* Grammar production:
* f0 -> Identifier()
* f1 -> "["
* f2 -> Expression()
* f3 -> "]"
* f4 -> "="
* f5 -> Expression()
* f6 -> ";"
*/
@Override
public String visit(ArrayAssignmentStatement aas, Argu argu){
String arrayType=aas.f0.accept(this,argu);
String insideBracketType= aas.f2.accept(this,argu);
String rightType=aas.f5.accept(this,argu);
if(!(arrayType.equals("int[]") && insideBracketType.equals("int") && rightType.equals("int"))){
throw new RuntimeException("Type error, arrays are always: int[int]=int");
}
return null;
}
/*If Statemnet */
/**
* Grammar production:
* f0 -> "if"
* f1 -> "("
* f2 -> Expression()
* f3 -> ")"
* f4 -> Statement()
* f5 -> "else"
* f6 -> Statement()
*/
@Override
public String visit(IfStatement is, Argu argu){
String expr=is.f2.accept(this,argu);
if(!expr.equals("boolean")){
throw new RuntimeException("Type Error: if( "+expr+" ): expr must always be of type boolean");
}
is.f4.accept(this,argu);
is.f6.accept(this,argu);
return null;
}
/*While Statement */
/**
* Grammar production:
* f0 -> "while"
* f1 -> "("
* f2 -> Expression()
* f3 -> ")"
* f4 -> Statement()
*/
@Override
public String visit(WhileStatement ws, Argu argu){
String expr=ws.f2.accept(this,argu);
if(!expr.equals("boolean")){
throw new RuntimeException("Type Error: while(" +expr+" ): expr must always be of type boolean");
}
ws.f4.accept(this,argu);
return null;
}
/*Print Statement */
/**
* Grammar production:
* f0 -> "System.out.println"
* f1 -> "("
* f2 -> Expression()
* f3 -> ")"
* f4 -> ";"
*/
@Override
public String visit(PrintStatement ps, Argu argu){
String expr=ps.f2.accept(this,argu);
if(!expr.equals("int")){
throw new RuntimeException("Type Error: System.out.println( "+expr+" ): expr must always be of type int\n ");
}
return null;
}
/*EXPRESSIONS*/
/*ARITHMETIC & COMPARISONS */
/**
* Grammar production:
* f0 -> PrimaryExpression()
* f1 -> "+","-","*", "<", "&&"
* f2 -> PrimaryExpression()
*/
/*Plus Expression */
@Override
public String visit(PlusExpression pe, Argu argu){
String expr1=pe.f0.accept(this,argu);
String expr2=pe.f2.accept(this,argu);
if(expr1.equals("int") && expr2.equals("int")){
return "int";
}
throw new RuntimeException("Type mismatch: operator expects int but got " + expr1);
}
/*Minus Expression */
@Override
public String visit(MinusExpression pe, Argu argu){
String expr1=pe.f0.accept(this,argu);
String expr2=pe.f2.accept(this,argu);
if(expr1.equals("int") && expr2.equals("int")){
return "int";
}
throw new RuntimeException("Type mismatch: operator expects int but got " + expr1);
}
/*Times Expression */
@Override
public String visit(TimesExpression te, Argu argu){
String expr1=te.f0.accept(this,argu);
String expr2=te.f2.accept(this,argu);
if(expr1.equals("int") && expr2.equals("int")){
return "int";
}
throw new RuntimeException("Type mismatch: operator expects int but got " + expr1);
}
/*Compare Expression */
@Override
public String visit(CompareExpression te, Argu argu){
String expr1=te.f0.accept(this,argu);
String expr2=te.f2.accept(this,argu);
if(expr1.equals("int") && expr2.equals("int")){
return "boolean";
}
throw new RuntimeException("Type mismatch: operator expects int but got " + expr1);
}
/*And Expression */
@Override
public String visit(AndExpression ae, Argu argu){
String expr1=ae.f0.accept(this,argu);
String expr2=ae.f2.accept(this,argu);
if(expr1.equals("boolean") && expr2.equals("boolean")){
return "boolean";
}
throw new RuntimeException("Type mismatch: operator expects boolean but got " + expr1);
}
/* ARRAYS */
/*Array Lookup */
/**
* Grammar production:
* f0 -> PrimaryExpression()
* f1 -> "["
* f2 -> PrimaryExpression()
* f3 -> "]"
*/
@Override
public String visit(ArrayLookup al, Argu argu){
String expr1=al.f0.accept(this,argu);
String expr2=al.f2.accept(this,argu);
if(expr1.equals("int[]") && expr2.equals("int")){
return "int";
}
throw new RuntimeException("Type error, arrays are always: int[int]");
}
/*Array Lenght */
/**
* Grammar production:
* f0 -> PrimaryExpression()
* f1 -> "."
* f2 -> "length"
*/
@Override
public String visit(ArrayLength al, Argu argu){
String expr=al.f0.accept(this,argu);
if(expr.equals("int[]")){
return "int";
}
throw new RuntimeException("Type error, arrays are always: int[]");
}
/*Array Allocation */
/**
* Grammar production:
* f0 -> "new"
* f1 -> "int"
* f2 -> "["
* f3 -> Expression()
* f4 -> "]"
*/
@Override
public String visit(ArrayAllocationExpression aae, Argu argu){
String expr=aae.f3.accept(this,argu);
if(expr.equals("int")){
return "int[]";
}
throw new RuntimeException("Type error, arrays are always: int[int]");
}
/*Not expression */
/**
* Grammar production:
* f0 -> "!"
* f1 -> PrimaryExpression()
*/
@Override
public String visit(NotExpression ne, Argu argu){
String expr=ne.f1.accept(this,argu);
if(expr.equals("boolean")){
return "boolean";
}
throw new RuntimeException("Type error, expected boolean, instead got: "+expr);
}
/*bracket expression */
/**
* Grammar production:
* f0 -> "("
* f1 -> Expression()
* f2 -> ")"
*/
@Override
public String visit(BracketExpression ne, Argu argu){
String expr=ne.f1.accept(this,argu);
return expr;
}
/*Allocatiion Expression */
/**
* Grammar production:
* f0 -> "new"
* f1 -> Identifier()
* f2 -> "("
* f3 -> ")"
*/
@Override
public String visit(AllocationExpression ae, Argu argu){
String id=ae.f1.f0.toString();
if(symbolTable.containsKey(id)){
return id;
}
throw new RuntimeException("Type error, no such type: "+id);
}
/*Message Send Expression*/
/**
* Grammar production:
* f0 -> PrimaryExpression()
* f1 -> "."
* f2 -> Identifier()
* f3 -> "("
* f4 -> ( ExpressionList() )?
* f5 -> ")"
*/
@Override
public String visit(MessageSend ms, Argu argu){
String objType=ms.f0.accept(this,argu);
String methodName=ms.f2.f0.toString();
if(symbolTable.containsKey(objType)){
List<MethodInfo> methods=checkMethod(objType, methodName);
if(methods!=null){
Argu argu2=new Argu(argu.className,argu.methodName);
ms.f4.accept(this,argu2);
for(MethodInfo mInfo: methods){
if(argu2.argTypes.size()==mInfo.formalParams.size()){
Iterator<String> formalParamIt=mInfo.formalParams.values().iterator(); //it will be ordered cause we use linked hashmap
Iterator<String> argIt= argu2.argTypes.iterator();
boolean flag=true;
while(formalParamIt.hasNext() && argIt.hasNext()){
if(!checkType(argIt.next(),formalParamIt.next())){
flag=false;
break;
}
}
if(flag){
return mInfo.methodType;
}
}
}
}
}
throw new RuntimeException("Type error in message send: "+objType+"."+methodName+"( ExpressionList())?)");
}
/*EXPRESSION LIST */
/**
* Grammar production:
* f0 -> Expression()
* f1 -> ExpressionTail()
*/
//To put the parameters in the argTypes List
@Override
public String visit(ExpressionList el,Argu argu){
argu.argTypes.add(el.f0.accept(this,argu));
el.f1.accept(this,argu);
return null;
}
/*EXPRESSION TERM */
/**
* Grammar production:
* f0 -> ","
* f1 -> Expression()
*/
@Override
public String visit(ExpressionTerm et,Argu argu){
argu.argTypes.add(et.f1.accept(this, argu));
return null;
}
/*LITERALS */
/*Integer literal */
@Override
public String visit(IntegerLiteral il, Argu argu){
return "int";
}
/*TrueLiteral */
@Override
public String visit(TrueLiteral tl,Argu argu){
return "boolean";
}
/*FalseLiteral */
@Override
public String visit(FalseLiteral tl,Argu argu){
return "boolean";
}
/*This expression */
@Override
public String visit(ThisExpression tl,Argu argu){
return argu.className;
}
/*TYPE */
/**
* Grammar production:
* f0 -> ArrayType()
* | BooleanType()
* | IntegerType()
* | Identifier()
*/
@Override
public String visit(Type t, Argu argu){
if(t.f0.choice instanceof Identifier){
return ((Identifier)t.f0.choice).f0.toString();
}
return t.f0.accept(this, argu);
}
/*ARRAY TYPE */
@Override
public String visit(ArrayType at, Argu argu){
return "int[]";
}
/*BOOLEAN TYPE */
@Override
public String visit(BooleanType bt, Argu argu){
return "boolean";
}
/*INTEGER TYPE */
@Override
public String visit(IntegerType it, Argu argu){
return "int";
}
/*IDENTIFIER */
/**
* Grammar production:
* f0 -> <IDENTIFIER>
*/
//Returns the type of the identifier
// When called at class level context we just return the identifier name as a string.
// visit(Identifier) is ONLY used for variable lookup when inside a method body.
//If identifier wasnt declared it throws an exception
//DO NOT USE TO GET IDENTIFIER NAME IN THIS VISITOR//
@Override
public String visit(Identifier i, Argu argu){
//class level
if(argu == null || argu.methodName == null){
return i.f0.toString();
}
//method level
MethodInfo currentMethod = getCurrentMethod(argu.className, argu.methodName);
//inside method scope
if(currentMethod.vars.containsKey(i.f0.toString())){
return currentMethod.vars.get(i.f0.toString());
}
//formal parameter
if(currentMethod.formalParams.containsKey(i.f0.toString())){
return currentMethod.formalParams.get(i.f0.toString());
}
//check class fields
String current=argu.className;
while(current!=null){
if(symbolTable.get(current).vars.containsKey(i.f0.toString())){
return symbolTable.get(current).vars.get(i.f0.toString());
}
current=symbolTable.get(current).parent;
}
throw new RuntimeException("Identifier: "+i.f0.toString()+" not defined");
}
}