-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.java
669 lines (551 loc) · 14.1 KB
/
temp.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
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
package cool;
public class Semantic{
private boolean errorFlag = false;
public void reportError(String filename, int lineNo, String error){
errorFlag = true;
System.err.println(filename+":"+lineNo+": "+error);
}
public boolean getErrorFlag(){
return errorFlag;
}
/*
Don't change code above this line
*/
public Semantic(AST.program program){
//Write Semantic analyzer code here
ProgramNode pr = new ProgramNode(program);
Visitor v = new Visitor();
pr.accept(v);
}
}
class ClassNode{
public AST.class_ self;
public ClassNode parent;
public int isVisited;
public ClassNode(AST.class_ c){
this.self = c;
this.parent = null;
this.isVisited=0;
}
}
class InheritanceGraph{
ArrayList<ClassNode> listOfClasses;
public void setGraph(ArrayList<ClassNode> listOfClasses){
this.listOfClasses = listOfClasses;
}
// Function for checking if the inheritance graph is correct or not if not then return false else true
public boolean checkInheritanceGraph(){
for(int i=0;i<listOfClasses.size();i++){
ClassNode temp = listOfClasses.get(i);
if (temp.self.parent.equals("Object")) {
temp.parent=null;
continue;
}
boolean cont = false;
for (int j=0;j<listOfClasses.size();j++){
if (temp.self.parent.equals(listOfClasses.get(j).self.name)){
System.out.println(temp.self.name + " -> " + listOfClasses.get(j).self.name );
temp.parent = listOfClasses.get(j);
cont =true;
}
}
if (!cont){
System.out.println("No matching parentclass found for class " + temp.self.name);
return false;
}
}
for (ClassNode cn : listOfClasses){
boolean isCyclic = checkCycles(cn);
if (isCyclic) {
System.out.println("The inheritance graph contains cycles");
return false;
}
}
return true;
}
// Method to check if the inheritance graph contains cycles returns true if contains else false
public boolean checkCycles(ClassNode classNode){
if (classNode.self.parent.equals("Object")){
return false;
}
else if (classNode.isVisited == 1){
return true;
}
else {
classNode.isVisited=1;
boolean check = checkCycles(classNode.parent);
classNode.isVisited=0;
return check;
}
}
}
interface ASTvisitor {
public boolean visit(ProgramNode P);
public boolean visit(CNode C);
public boolean visit(FeatureNode F);
public boolean visit(ExpressionNode E);
}
class Visitor implements ASTvisitor {
public ScopeTable<AST.ASTNode> Table = new ScopeTable<AST.ASTNode>();
public InheritanceGraph IG = new InheritanceGraph();
@Override
public boolean visit(ProgramNode P){
P.visitPcount++;
ArrayList<ClassNode> listOfClasses = new ArrayList<ClassNode>();
for (AST.class_ cl : P.prog.classes){ // For creating the inheritance graph
listOfClasses.add(new ClassNode(cl));
}
IG.setGraph(listOfClasses);
if (!IG.checkInheritanceGraph()) return false;
return true;
}
@Override
public boolean visit(CNode C){
C.visitCcount++;
Table.insertClass(C.c);
return true;
}
@Override
public boolean visit(FeatureNode F){
if (F.feat instanceof AST.method){
F.visitMcount++;
if (F.visitMcount<2){
if (F.method.typeid.equals("NULL")){
System.out.println("Return Type is NULL");
return false;
}
// Change the below condition to take into account the overriden methods
else if (Table.lookUpClassSpace(F.method.name)!=null){
System.out.println("Feature with this name id already exists");
return false;
}
for (AST.formal fo1 : F.method.formals){
for (AST.formal fo2 : F.method.formals){
if (fo1 !=fo2 && fo1.name.equals(fo2.name)){
System.out.println("The formal parameters of the Function "+F.method.name+" have same ID's");
return false;
}
}
}
Table.insert(F.method.name,F.method);
}
else{
}
}
else if (F.feat instanceof AST.attr){
F.visitAcount++;
if (F.visitAcount <2){
if (F.attribute.typeid.equals("NULL")){
System.out.println("Typeid is NULL");
return false;
}
// Change this to also take into account the inherited attr
else if (Table.lookUpClassSpace(F.attribute.name)!=null){
System.out.println("Attribute with this nameId :" + F.attribute.name + " already exitst");
return false;
}
Table.insert(F.attribute.name,F.attribute);
}
else {
}
}
return true;
}
@Override
public boolean visit(ExpressionNode E){
E.visitEcount++;
if (E.expr instanceof AST.block){
if (E.visitEcount<2) {
Table.enterScope();
return true;
}
else {
// Set the type of the expression node as the type of the last expression in
// the expList.
}
}
else if (E.expr instanceof AST.assign){
if (E.visitEcount < 2) {
//Table.lookupClassSpace(E.ex);
}
}
return true;
}
public String getType(ExpressionNode E){
if (E.expr instanceof AST.int_const){
return "int";
}
else if (E.expr instanceof AST.string_const){
return "string";
}
else if (E.expr instanceof AST.bool_const){
return "bool";
}
else if (E.expr instanceof AST.object){
}
return "Object";
}
}
interface ASTnode {
public void accept(ASTvisitor visitor);
public String typeInfo = "no_type";
}
class ExpressionNode implements ASTnode{
AST.expression expr;
AST.block blockexpr;
ArrayList<ExpressionNode> expList;
String typeInfo;
int visitEcount;
public ExpressionNode(AST.expression a){
expr = a;
visitEcount=0;
if (a instanceof AST.block){
blockexpr=(AST.block) a;
expList = new ArrayList<ExpressionNode>();
for (AST.expression e : blockexpr.l1){
expList.add(new ExpressionNode(e));
}
}
}
@Override
public void accept(ASTvisitor visitor){
visitor.visit(this);
for (ExpressionNode e : expList){
e.accept(visitor);
}
}
}
class FeatureNode implements ASTnode{
public AST.attr attribute;
public AST.feature feat;
public AST.method method;
public int visitMcount,visitAcount;
//int acceptMcount=0,acceptAcount=0;
ExpressionNode expMethod,expAttr;
public FeatureNode (AST.feature fe){
feat=fe;
if (fe instanceof AST.method){
method = (AST.method) fe;
expMethod = new ExpressionNode(method.body);
visitMcount=0;
}else{
attribute = (AST.attr)fe;
expAttr = new ExpressionNode(attribute.value);
visitAcount=0;
}
}
@Override
public void accept(ASTvisitor visitor){
visitor.visit(this);
if (feat instanceof AST.method){
expMethod.accept(visitor);
}
else if (feat instanceof AST.attr){
expAttr.accept(visitor);
}
}
}
class CNode implements ASTnode{
AST.class_ c;
ArrayList<FeatureNode> featNodeList;
public int visitCcount;
public CNode(AST.class_ cl){
c = cl;
visitCcount=0;
featNodeList = new ArrayList<FeatureNode>();
for (AST.feature fe : c.features){
featNodeList.add(new FeatureNode(fe));
}
}
@Override
public void accept(ASTvisitor visitor){
visitor.visit(this);
for (FeatureNode fe : featNodeList){
fe.accept(visitor);
}
}
}
class ProgramNode implements ASTnode {
AST.program prog;
ArrayList<CNode> cNodeList;
public int visitPcount;
int numMainClass=0;
boolean flag=false;
public ProgramNode(AST.program program){
prog = program;
cNodeList = new ArrayList<CNode>();
visitPcount=0;
for (AST.class_ cl : prog.classes){
if (cl.name.equals("Main"))
if (++numMainClass > 1) flag=true;
cNodeList.add(new CNode(cl));
}
}
@Override
public void accept(ASTvisitor visitor){
if (flag) {
System.out.println("There are more than 2 Main class");
return;
}
boolean cond = visitor.visit(this); // Checking the inheritance graph
if (!cond) return;
for (CNode cl : cNodeList){
cl.accept(visitor);
}
}
};
public String getType(AST.expression E){
if (!E.type.equals("_no_type")){
return E.type;
}
else if (E instanceof AST.int_const){
return "int";
}
else if (E instanceof AST.string_const){
return "string";
}
else if (E instanceof AST.bool_const){
return "bool";
}
else if (E instanceof AST.object){
AST.object o = (AST.object) E;
AST.ASTNode node= Table.lookUpGlobal(o.name);
if (node != null) return node.typeid;
}
else if (E instanceof AST.plus){
AST.plus pl = (AST.plus) E;
AST.expression e1 = pl.e1;
AST.expression e2 = pl.e2;
if (getType(e1).equals("int") && getType(e2).equals("int")){
E.type = "int";
return "int";
}else{
}
}
else if (E instanceof AST.sub){
AST.sub pl = (AST.plus) E;
AST.expression e1 = pl.e1;
AST.expression e2 = pl.e2;
if (getType(e1).equals("int") && getType(e2).equals("int")){
E.type = "int";
return "int";
}else{
}
}
else if (E instanceof AST.mul){
AST.mul pl = (AST.plus) E;
AST.expression e1 = pl.e1;
AST.expression e2 = pl.e2;
if (getType(e1).equals("int") && getType(e2).equals("int")){
E.type = "int";
return "int";
}else{
}
}
else if (E instanceof AST.divide){
AST.divide pl = (AST.plus) E;
AST.expression e1 = pl.e1;
AST.expression e2 = pl.e2;
if (getType(e1).equals("int") && getType(e2).equals("int")){
E.type = "int";
return "int";
}else{
}
}
else if (E instanceof AST.comp){
AST.com compl = (AST.comp) E;
AST.expression e1 = compl.e1 ;
if(getType(e1).equals("int")){
E.type = "int" ;
return "int";
}
else{
}
/* if (E.type.equals("_no_type") && getType())
*/ }
else if (E instanceof AST.lt){
AST.lt elt = (AST.lt) E;
AST.expression e1 = elt.e1;
AST.expression e2 = elt.e2;
if (getType(e1).equals("int") && getType(e2).equals("int") ){
E.type = "bool";
return "bool";
}else{
/* attach error to stderr
take it as bool and continue checking semantics
*/
}
}
else if (E instanceof AST.leq){
AST.leq elt = (AST.leq) E;
AST.expression e1 = elt.e1;
AST.expression e2 = elt.e2;
if (getType(e1).equals("int") && getType(e2).equals("int") ){
E.type = "bool";
return "bool" ;
}else{
/* attach error to stderr
take it as bool and continue checking semantics
*/
}
}
else if (E instanceof AST.eq){
AST.eq elt = (AST.eq) E;
AST.expression e1 = elt.e1;
AST.expression e2 = elt.e2;
if(getType(e1).equals("int") || getType(e2).equals("int") || getType(e1).equals("string") || getType(e2).equals("string") || getType(e1).equals("bool") || getType(e2).equals("bool")){
if(getType(e1).equals(getType(e2))){
// E.type = "bool" ;
// return "bool";
}else{
//error
}
}
E.type = "bool" ;
return "bool";
}
else if (E instanceof AST.neg){ //debug //check this
AST.neg not = (AST.neg) E;
AST.expression e1 = not.e1;
if(getType(e1).equals("bool")){
E.type = "bool";
return "bool";
}else{
}
}
else if (E instanceof AST.isvoid){
return "bool"
}
else if (E instanceof AST.new_){
AST.new_ new__ = (AST.new_)E ;
E.type = new__.typeid ;
return new__ .typeid ;
}
else if(E instanceof AST.typcase){ //should write join function
AST.typecase typcase_ = (AST.typecase)E ;
List <branch> branches_ = typcase_.branches ;
AST.expression e1 = typcase_.predicate ;
String type1 = getType(e1) ;
List <String> type_ ;
bool flag = true ;
for(branch b1 : branches_){
AST.expression e = b1.value ;
String s = getType(e) ;
if(subtype(s,t)){ // s <= t
flag = false ;
}
type_.add(getType(e));
}
if(flag){
//warning : case may not match to any thing
}
type_ = join(type_)
E.type = type_ ;
return type_;
}
else if(E instanceof AST.let){
AST.let let_ = (AST.let)E ;
AST.expression = e2 = let_.value ; //is it possible that there is no value
AST.expression e1 = let_.body ;
String valuetype = getType(e2);
String type_ = getType(e1) ;
if(type_ != let_.typeid){
//error
}
E.type = type_ ;
return type_ ;
}
else if(E.instanceof AST.block){ //dont know whether to check all expr
AST.block block_ = (AST.block)E ;
AST.expression e1 = block_.l1.get(block_.l1.size() - 1);
String type_ = getType(e1) ;
E.type = type_ ;
return type_ ;
}
else if(E.instanceof AST.loop){ // dont know whether to check loop body
AST.loop loop_ = (AST.loop)E ;
AST.expression pred = loop_.predicate ;
if(getType(pred) != "bool"){
//error : invalid loop variable
}
E.type = "object" ;
return "object" ;
}
else if(E.instanceof AST.cond){
AST.block if_ = (AST.block)E ;
AST.expression e0 = if_.predicate ;
AST.expression e2 = if_.elsebody ;
AST.expression e1 = if_.ifbody ;
if(getType(e0) != "bool" ){
//error : no bool in branch
}
List <String> type_ ;
type_.add(getType(e1));
type_.add(getType(e2));
type_ = join(type_)
E.type = type_ ;
return type_;
}
else if(E.instanceof AST.){ //<expr>.<id>(<expr>,...,<expr>) , <id>(<expr>,...,<expr>) =>DISPATCH CLASS
}
else if (E.instanceof AST.){ //STATIC DISPATCH
}
else if (E.instanceof AST.assign){
AST.assign assign = (AST.assign) E;
AST.expression e1 = compl.e1 ;
String type_ = getType(e1) ;
E.type = type_ ;
return type_;
}
return "Object";
}
}
boolean subtype(a,b){
List <String> pathOfa = getPathOf(a) ;
for(int i = 0 ; i < pathOfa.size() ; i++){
if(pathOfa.get(i).equals(b)){
return true ;
}
}
return false ;
}
String join(List<string> a){
if(a.size().equals(0)){
return "object" ; //should give error
}
else if(a.size().equals(1)){
return a.get(0);
}
String res = a.get(0) ;
for(int i = 1 ; i < a.size() ; i++){
res = join2(res,a.get(i));
}
return res ;
}
String join2(String a , String b){
List <String> pathOfa = getPathOf(a) ;
List <String> pathOfb = getPathOf(b);
for(int i = 0 ; i < pathOfa.size() ; i++ ){
for(int j = 0 ; j < pathOfb.size() ; j++){
if(pathOfa.get(i).equals(pathOfb.get(j))){
return pathOfa.get(i) ;
}
}
}
}
List<String> getPathOf(String a){ //returns path it crossed till object
List <String> path ;
for (int i = 0; i < IG.listOfClasses.size(); i++) {
if(IG.listOfClasses.get(i).name.equals(a)){
ClassNode t = IG.listOfClasses.get(i).parent;
path.add(a);
while(t != null){
path.add(t.name);
t = t.parent;
}
break;
};
}
path.add("object") ;
return path ;
}