-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass2reference.cc
executable file
·648 lines (523 loc) · 17.5 KB
/
pass2reference.cc
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
bool is_compatible(S_type * lhs, S_type * rhs){
S_class * lclass = dynamic_cast<S_class *>(lhs);
S_interface * linterface = dynamic_cast<S_interface *>(lhs);
S_primtype * lprim = dynamic_cast<S_primtype *>(lhs);
S_class * rclass = dynamic_cast<S_class *>(rhs);
S_interface * rinterface = dynamic_cast<S_interface *>(rhs);
S_primtype * rprim = dynamic_cast<S_primtype *>(rhs);
if (lprim || rprim){
if (!lprim){
semantic_error("Non-matching types for assignment expression", 0);
}
else if (!rprim){
semantic_error("Non-matching types for assignment expression", 0);
}
else if(lprim->name != rprim->name){
semantic_error("Non-matching types for assignment expression", 0);
}
}
if (lclass && rclass){
if(lclass != rclass){
bool is_super = false;
for (S_class * parent : rclass->inheritedClasses){
if (lclass == parent){
is_super = true;
}
}
if (!is_super){
semantic_error("Incompatible types", 0);
}
}
}
if (lclass && rinterface){
semantic_error("Attempting to pass an interface where a class is expected", 0);
}
if (linterface && rclass){
bool implemented = false;
for (S_type * interf : rclass->interfaces){
S_interface * interface = dynamic_cast<S_interface *>(interf);
if (interface == linterface){
implemented = true;
}
}
if (!implemented){
for (S_class * parent : rclass->inheritedClasses){
for (S_type * interf : parent->interfaces){
S_interface * interface = dynamic_cast<S_interface *>(interf);
if (interface == linterface){
implemented = true;
}
}
}
}
if(!implemented){
semantic_error("Attempting to pass an incompatible class when an interface is expected", 0);
}
}
if (linterface && rinterface){
if (linterface != rinterface){
semantic_error("Incompatible interfaces", 0);
}
}
return true;
}
//probably needs to be fixed?
void pass2_const_or_var(ParseTree * current){
string text = current->token->text;
if(text == "this"){
if (!currclass){
semantic_error("cannot use 'this' outside of a class", 0);
}
}
else{
S_primtype * constant = dynamic_cast<S_primtype *>(current->sem);
if (constant){
current->type = constant;
}
else{
semantics * found = scope->lookup(text);
if (!found){
semantic_error("Undefined variable", 0);
}
S_variable * var = dynamic_cast<S_variable *>(found);
if (var){
current->type = var->type;
}
/*else{
for(semantics *sem : all_decls){
S_class * isclass = dynamic_cast<S_class *>(sem);
S_interface * isinterface = dynamic_cast<S_interface *>(sem);
if (isclass){
if (isclass->name == current->token->text){
current->type = isclass;
}
}
if (isinterface){
if (isinterface->name == current->token->text){
current->type = isinterface;
}
}
}
}*/
}
}
}
void pass2_functions(ParseTree * functree){
//checks for correct return value
S_function * function = dynamic_cast<S_function *>(functree->sem);
currfunc = function;
bool returned = false;
//calls pass 2 on formals
pass2(functree->children[2]);
pass2(functree->children[3]);
//check that the return matches (goes through stmtblock, 4th child)
ParseTree * stmts = functree->children[3]->children[1];
for(ParseTree * child : stmts->children){
//pass2(child);
if (child->description == "return"){
returned = true;
if(function->returnType == child->type){
functree->type = child->type;
}
else{
semantic_error("Return of incorrect types", function->line);
}
}
}
if (function->returnType && !returned){
semantic_error("No return in non-void function", function->line);
}
currfunc = NULL;
}
void pass2_return(ParseTree * retree){
if(retree->children.size() > 1){
ParseTree * ret_stmt = retree->children[1];
pass2(ret_stmt);
retree->type = ret_stmt->type;
}
else{
retree->type = NULL;
}
}
void pass2_binop(ParseTree * bintree){
string op = bintree->children[1]->token->text;
ParseTree * left = bintree->children[0];
ParseTree * right = bintree->children[2];
pass2(left);
pass2(right);
if(op == "+" || op == "-" || op == "/" || op == "*" || op == "%"){
//arithmetic type match
if(left->type->name != right->type->name){
semantic_error("Non-matching types for arithmetic expression", 0);
}
string type = left->type->name;
if(type != "int" && type != "double"){
semantic_error("Non-numeric types for arithmetic expression", 0);
}
bintree->type = left->type;
}
else if(op == "<" || op == "<=" || op == ">" || op == ">="){
//relational types match int or double
if(left->type->name != right->type->name){
semantic_error("Non-matching types for relational expression", 0);
}
string type = left->type->name;
if(type != "int" && type != "double"){
semantic_error("Non-numeric types for relational expression", 0);
}
bintree->type = new S_primtype("bool");
}
else if(op == "==" || op == "!="){
//equality types match
if (! is_compatible(left->type, right->type)){
semantic_error("Non-matching types for relational expression", 0);
}
bintree->type = new S_primtype("bool");
}
else if(op == "="){
//assignment type compatibility
//Interface assignment type compatibility
S_primtype * isprim = dynamic_cast<S_primtype *>(left->type);
if (isprim){
if(left->type->name != right->type->name){
semantic_error("Non-matching types for assignment expression", 0);
}
bintree->type = left->type;
}
else if(left->type->name != right->type->name){
if(is_compatible(left->type, right->type)){
bintree->type = left->type;
}
else{
semantic_error("Non-matching types for assignment expression", 0);
}
}
}
else if(op == "&&" || op == "||"){
//logical types match
if(left->type->name != "bool" || right->type->name != "bool"){
semantic_error("Non boolean values used in logical expression", 0);
}
bintree->type = new S_primtype("bool");
}
}
void pass2_uop(ParseTree * optree){
string op = optree->children[0]->token->text;
pass2(optree->children[1]);
if(op == "-"){
if(optree->children[1]->type->name != "int" && optree->children[1]->type->name != "double"){
semantic_error("Non-numeric type used with negation operator", 0);
}
optree->type = optree->children[1]->type;
}
if(op == "!"){
if(optree->children[1]->type->name != "bool"){
semantic_error("Non-boolean type used with not operator", 0);
}
optree->type = new S_primtype("bool");
}
}
void pass2_variables(ParseTree * vartree){
S_variable * variable = dynamic_cast<S_variable *>(vartree->sem);
S_primtype * isprim = dynamic_cast<S_primtype *>(variable->type);
if (isprim){
vartree->type = isprim;
}
S_class * isclass = dynamic_cast<S_class *>(variable->type);
if (isclass){
vartree->type = isclass;
}
S_interface * isinterface = dynamic_cast<S_interface *>(variable->type);
if (isinterface){
vartree->type = isinterface;
}
S_arraytype * isarray = dynamic_cast<S_arraytype *>(variable->type);
if (isarray){
vartree->type = isarray;
}
}
void pass2_class(ParseTree * classtree){
S_class * thisclass = dynamic_cast<S_class *>(classtree->sem);
currclass = thisclass;
is_in_a_class = true;
pass2(classtree->children[3]);
currclass = NULL;
}
void pass2_fields(ParseTree * fieldtree){
for (ParseTree * child : fieldtree->children){
pass2(child);
}
}
void pass2_stmtblock(ParseTree * blocktree){
pass2(blocktree->children[0]);
pass2(blocktree->children[1]);
}
void pass2_vardecls(ParseTree * vartree){
for (ParseTree * child : vartree->children){
pass2(child);
}
}
void pass2_stmts(ParseTree * stmttree){
for (ParseTree * child : stmttree->children){
pass2(child);
}
}
void pass2_interface(ParseTree * itree){
for (ParseTree * child : itree->children){
if(child != itree->children[0]){
pass2(child);
}
}
}
void pass2_formals(ParseTree * ftree){
for (ParseTree * child : ftree->children){
pass2(child);
}
}
void pass2_ifstmts(ParseTree * iftree){
for (ParseTree * child : iftree->children){
pass2(child);
}
if (iftree->children[0]->type->name != "bool"){
semantic_error("If statement requires a boolean conditional", 0);
}
}
void pass2_whilestmts(ParseTree * whiletree){
loopcount += 1;
for (ParseTree * child : whiletree->children){
pass2(child);
}
if (whiletree->children[0]->type->name != "bool"){
semantic_error("While statement requires a boolean conditional", 0);
}
loopcount -= 1;
}
void pass2_forloops(ParseTree * fortree){
loopcount += 1;
for (ParseTree * child : fortree->children){
if(child){
pass2(child);
}
}
if (fortree->children[1]->type->name != "bool"){
semantic_error("Test portion of for loop must have a boolean value", 0);
}
loopcount -= 1;
}
void pass2_break(){
//check if in loop
if (loopcount <= 0){
semantic_error("break in a non loop!", 0);
}
}
void pass2_print(ParseTree * printtree){
pass2(printtree->children[1]);
}
void pass2_readint(ParseTree * inttree){
inttree->type = new S_primtype("int");
}
//COME BACK TO THESE WOOLSON
void pass2_new(ParseTree * newtree){
//valid parameters
ParseTree * child = newtree->children[0];
bool found = false;
// need to fix this
for(semantics *sem : all_decls){
S_class * isclass = dynamic_cast<S_class *>(sem);
if (isclass){
if (isclass->name == child->token->text){
newtree->type = isclass;
found = true;
}
}
}
if (!found){
semantic_error("New operator requires a class", 0);
}
}
void pass2_newarray(ParseTree * newtree){
//valid params
//works for primitive types only
ParseTree * num = newtree->children[0];
ParseTree * arrtype = newtree->children[1];
pass2(num);
pass2(arrtype);
}
void pass2_field_access(ParseTree * dottree){
//valid field access
for (ParseTree * child : dottree->children){
pass2(child);
}
}
void pass2_aref(ParseTree * areftree){
//type check
for (ParseTree * child : areftree->children){
pass2(child);
}
S_arraytype * arr = dynamic_cast<S_arraytype *>(areftree->children[0]->type);
if (!arr){
semantic_error("Attempt to reference an object that is not an array", 0);
}
S_primtype * index = dynamic_cast<S_primtype *>(areftree->children[1]->type);
if (!index || !(index->name == "int")){
semantic_error("Non-integer index", 0);
}
areftree->type = arr->element_type;
}
void pass2_call(ParseTree * calltree){
for (ParseTree * child : calltree->children){
pass2(child);
}
}
void pass2_actuals(ParseTree * actualstree){
for (ParseTree * child : actualstree->children){
pass2(child);
}
}
void pass2_this(ParseTree * thistree){
for (ParseTree * child : thistree->children){
pass2(child);
}
}
//this code is probably garbage
void pass2_type(ParseTree * current){
cout << "MADE IT TO PASS2_TYPE " << endl;
if (current->description == "primtype"){
ParseTree * child = current->children[0];
pass2(child);
cout << "Pass2 type " << child->type->name << endl;
current->type = child->type;
}
if (current->description == "usertype"){
//S_class * isclass = dynamic_cast<S_class *>(current->sem);
cout << current->sem << endl;
if (dynamic_cast<S_type *>(current->sem)){
//cout << "USERTYPE! " << endl;
current->type = dynamic_cast<S_type *>(current->sem);
}
}
}
void pass2(ParseTree * current){
//compatible types only for user-declared
if (current->description.size() == 0){
pass2_const_or_var(current);
}
else if (current->description == "functiondecl"){
//compatible return value
pass2_functions(current);
}
else if (current->description == "return"){
pass2_return(current);
}
else if (current->description == "binop"){
pass2_binop(current);
}
else if (current->description == "class"){
//class object valid in class being declared: class A { A a; }
//Instance variable lookup is used when scope lookup fails. Inherited instance variables are found
Symtab * oldscope = scope;
scope = current->symtab;
pass2_class(current);
scope = oldscope;
}
else if (current->description == "interface"){
Symtab * oldscope = scope;
scope = current->symtab;
pass2_interface(current);
scope = oldscope;
}
//else if (current->description == "extends"){}
//else if (current->description == "implements"){}
else if (current->description == "fields"){
pass2_fields(current);
}
else if (current->description == "variable"){
pass2_variables(current);
}
else if (current->description == "formals"){
pass2_formals(current);
}
else if (current->description == "stmtblock"){
Symtab * oldscope = scope;
scope = current->symtab;
pass2_stmtblock(current);
scope = oldscope;
}
else if (current->description == "vardecls"){
pass2_vardecls(current);
}
else if (current->description == "stmts"){
pass2_stmts(current);
}
else if (current->description == "if"){
//expr is a bool
pass2_ifstmts(current);
}
else if (current->description == "while"){
//expr is a bool
pass2_whilestmts(current);
}
else if (current->description == "for"){
//check the three exprs (looks just like a c++ for loop)
pass2_forloops(current);
}
else if (current->description == "break"){
pass2_break();
}
else if (current->description == "print"){
pass2_print(current);
}
//else if (current->description == "nullstmt"){} ** might need to make type null or something
else if (current->description == "uop"){
//type match
pass2_uop(current);
}
else if (current->description == "readinteger"){
pass2_readint(current);
}
else if (current->description == "new"){
//valid parameters
pass2_new(current);
}
else if (current->description == "newarray"){
//valid parameters
pass2_newarray(current);
}
else if (current->description == "field_access"){
//valid field access
pass2_field_access(current);
}
else if (current->description == "aref"){
//type check (integer?)
pass2_aref(current);
}
else if (current->description == "call"){
//actuals in a function call compatible with declared formal types
//class object accepted in place of an interface object when passing actuals
//valid/invalid interface object function call
pass2_call(current);
}
else if (current->description == "actuals"){
pass2_actuals(current);
}
else if (current->description == "usertype"){
pass2_type(current);
}
else if (current->description == "primtype"){
pass2_type(current);
}
else if (current->description == "arraytype"){
pass2_type(current);
}
// else if(current->token){
// if (current->token->text == "this"){
//
// }
// }
//may need to be an else
else{
for(ParseTree * child : current->children){
pass2(child);
}
}
}