-
Notifications
You must be signed in to change notification settings - Fork 2
/
typecheck.cpp
751 lines (676 loc) · 26.4 KB
/
typecheck.cpp
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#include "typecheck.hpp"
// Defines the function used to throw type errors. The possible
// type errors are defined as an enumeration in the header file.
void typeError(TypeErrorCode code) {
switch (code) {
case undefined_variable:
std::cerr << "Undefined variable." << std::endl;
break;
case undefined_method:
std::cerr << "Method does not exist." << std::endl;
break;
case undefined_class:
std::cerr << "Class does not exist." << std::endl;
break;
case undefined_member:
std::cerr << "Class member does not exist." << std::endl;
break;
case not_object:
std::cerr << "Variable is not an object." << std::endl;
break;
case expression_type_mismatch:
std::cerr << "Expression types do not match." << std::endl;
break;
case argument_number_mismatch:
std::cerr << "Method called with incorrect number of arguments." << std::endl;
break;
case argument_type_mismatch:
std::cerr << "Method called with argument of incorrect type." << std::endl;
break;
case while_predicate_type_mismatch:
std::cerr << "Predicate of while loop is not boolean." << std::endl;
break;
case repeat_predicate_type_mismatch:
std::cerr << "Predicate of repeat loop is not boolean." << std::endl;
break;
case if_predicate_type_mismatch:
std::cerr << "Predicate of if statement is not boolean." << std::endl;
break;
case assignment_type_mismatch:
std::cerr << "Left and right hand sides of assignment types mismatch." << std::endl;
break;
case return_type_mismatch:
std::cerr << "Return statement type does not match declared return type." << std::endl;
break;
case constructor_returns_type:
std::cerr << "Class constructor returns a value." << std::endl;
break;
case no_main_class:
std::cerr << "The \"Main\" class was not found." << std::endl;
break;
case main_class_members_present:
std::cerr << "The \"Main\" class has members." << std::endl;
break;
case no_main_method:
std::cerr << "The \"Main\" class does not have a \"main\" method." << std::endl;
break;
case main_method_incorrect_signature:
std::cerr << "The \"main\" method of the \"Main\" class has an incorrect signature." << std::endl;
break;
}
exit(1);
}
// TypeCheck Visitor Functions: These are the functions you will
// complete to build the symbol table and type check the program.
// Not all functions must have code, many may be left empty.
void TypeCheck::visitProgramNode(ProgramNode* node) {
// WRITEME: Replace with code if necessary
classTable = new ClassTable();
node->visit_children(this);
// Check if there is a main class
if(classTable->count("Main") == 0)
typeError(no_main_class);
}
void TypeCheck::visitClassNode(ClassNode* node) {
// WRITEME: Replace with code if necessary
ClassInfo c;
currentClassName = node->identifier_1->name;
if(node->identifier_2 != NULL) {
// Check if that super class exists
if(classTable->count(node->identifier_2->name) == 0)
typeError(undefined_class);
c.superClassName = node->identifier_2->name;
}
else
c.superClassName = "";
currentMethodTable = new MethodTable();
currentVariableTable = new VariableTable();
currentMemberOffset = 0;
currentLocalOffset = 0;
std::string superClass = c.superClassName;
while(superClass != "") {
VariableTable *superVars = classTable->find(c.superClassName)->second.members;
superClass = classTable->find(superClass)->second.superClassName;
for(std::map<std::string, VariableInfo>::iterator i = superVars->begin(); i != superVars->end(); i++) {
VariableInfo vi = i->second;
vi.offset = currentMemberOffset;
(*currentVariableTable)[i->first] = vi;
currentMemberOffset += 4;
}
}
for(std::list<DeclarationNode*>::iterator i = node->declaration_list->begin(); i != node->declaration_list->end(); i++)
visitDeclarationNode(*i);
c.members = currentVariableTable;
c.membersSize = currentVariableTable->size()*4;
// Check to make sure the main class doesn't have members
if(currentClassName == "Main" && currentVariableTable->size() > 0)
typeError(main_class_members_present);
classTable->insert(std::pair<std::string, ClassInfo>(currentClassName, c));
for(std::list<MethodNode*>::iterator i = node->method_list->begin(); i != node->method_list->end(); i++)
visitMethodNode(*i);
c.methods = currentMethodTable;
// Check if the main class has a main method
if(currentClassName == "Main" && currentMethodTable->count("main") == 0)
typeError(no_main_method);
std::map<std::string, ClassInfo>::iterator i;
i = classTable->find(currentClassName);
i->second.methods = currentMethodTable;
}
void TypeCheck::visitMethodNode(MethodNode* node) {
// WRITEME: Replace with code if necessary
MethodInfo m;
m.parameters = new std::list<CompoundType>();
CompoundType c;
currentVariableTable = new VariableTable();
currentLocalOffset = -4;
currentParameterOffset = 12;
node->type->accept(this);
c.baseType = node->type->basetype;
c.objectClassName = node->type->objectClassName;
m.returnType = c;
for(std::list<ParameterNode*>::iterator i = node->parameter_list->begin(); i != node->parameter_list->end(); i++) {
(*i)->type->accept(this);
CompoundType ct;
ct.baseType = (*i)->type->basetype;
ct.objectClassName = (*i)->type->objectClassName;
m.parameters->push_back(ct);
VariableInfo v;
v.type = ct;
v.offset = currentParameterOffset;
v.size = 4;
currentParameterOffset += 4;
currentVariableTable->insert(std::pair<std::string, VariableInfo>((*i)->identifier->name, v));
}
visitMethodBodyNode(node->methodbody);
m.variables = currentVariableTable;
m.localsSize = currentLocalOffset * (-1) - 4;
if(node->type->basetype == bt_none && node->methodbody->returnstatement != NULL)
typeError(return_type_mismatch);
if(node->type->basetype != bt_none && node->methodbody->basetype != node->type->basetype)
typeError(return_type_mismatch);
if(node->type->basetype == bt_object && node->methodbody->basetype == bt_object) {
if(node->methodbody->objectClassName != node->type->objectClassName) {
// check if the super class has it
std::string superClass = classTable->find(node->methodbody->objectClassName)->second.superClassName;
while(superClass != "") {
if(superClass != node->type->objectClassName)
superClass = classTable->find(superClass)->second.superClassName;
else
break;
}
if(superClass == "")
typeError(return_type_mismatch);
else {
}
}
}
if(node->type->basetype != bt_none && node->methodbody->returnstatement == NULL)
typeError(return_type_mismatch);
if(currentClassName == node->identifier->name && node->type->basetype != bt_none)
typeError(constructor_returns_type);
if(currentClassName == "Main" && node->identifier->name == "main") {
if(m.parameters->size() > 0)
typeError(main_method_incorrect_signature);
}
currentMethodTable -> insert(std::pair<std::string, MethodInfo>(node->identifier->name, m));
std::map<std::string, ClassInfo>::iterator i;
i = classTable->find(currentClassName);
i->second.methods = currentMethodTable;
}
void TypeCheck::visitMethodBodyNode(MethodBodyNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if(node->returnstatement != NULL) {
node->basetype = node->returnstatement->basetype;
node->objectClassName = node->returnstatement->objectClassName;
}
}
void TypeCheck::visitParameterNode(ParameterNode* node) {
// WRITEME: Replace with code if necessary
}
void TypeCheck::visitDeclarationNode(DeclarationNode* node) {
// WRITEME: Replace with code if necessary
CompoundType c;
node->type->accept(this);
c.baseType = node->type->basetype;
if(node->type->objectClassName != "") {
if(classTable->count(node->type->objectClassName) == 0)
typeError(undefined_class);
c.objectClassName = node->type->objectClassName;
}
for(std::list<IdentifierNode*>::iterator i = node->identifier_list->begin(); i != node->identifier_list->end(); i++) {
VariableInfo v;
v.type = c;
v.size = 4;
(*i)->basetype = node->type->basetype;
if(currentLocalOffset == 0) {
v.offset = currentMemberOffset;
currentMemberOffset += 4;
}
else {
v.offset = currentLocalOffset;
currentLocalOffset -= 4;
}
(*currentVariableTable)[(*i)->name] = v;
}
}
void TypeCheck::visitReturnStatementNode(ReturnStatementNode* node) {
// WRITEME: Replace with code if necessary
node->expression->accept(this);
node->basetype = node->expression->basetype;
node->objectClassName = node->expression->objectClassName;
}
void TypeCheck::visitAssignmentNode(AssignmentNode* node) {
// WRITEME: Replace with code if necessary
node->expression->accept(this);
std::string className;
CompoundType c;
c.objectClassName = " ";
if(currentVariableTable->count(node->identifier_1->name) > 0) {
c = currentVariableTable->find(node->identifier_1->name)->second.type;
}
else if(classTable->find(currentClassName)->second.members->count(node->identifier_1->name) > 0) {
c = classTable->find(currentClassName)->second.members->find(node->identifier_1->name)->second.type;
}
else {
ClassInfo currentCI;
currentCI = classTable->find(currentClassName)->second;
std::string superClass = currentCI.superClassName;
while(superClass != "") {
currentCI = classTable->find(superClass)->second;
if(currentCI.members->count(node->identifier_1->name) > 0) {
c = currentCI.members->find(node->identifier_1->name)->second.type;
break;
}
superClass = currentCI.superClassName;
}
}
if(node->identifier_2 == NULL) {
if(c.objectClassName != " ") {
node->basetype = c.baseType;
node->objectClassName = c.objectClassName;
}
else
typeError(undefined_variable);
}
else {
if(c.objectClassName == " ")
typeError(undefined_variable);
if(c.baseType != bt_object)
typeError(not_object);
if(classTable->find(c.objectClassName)->second.members->count(node->identifier_2->name) > 0) {
node->basetype = classTable->find(c.objectClassName)->second.members->find(node->identifier_2->name)->second.type.baseType;
node->objectClassName = classTable->find(c.objectClassName)->second.members->find(node->identifier_2->name)->second.type.objectClassName;
}
else
typeError(undefined_member);
}
if(node->basetype != node->expression->basetype || node->objectClassName != node->expression->objectClassName)
typeError(assignment_type_mismatch);
}
void TypeCheck::visitCallNode(CallNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
node->basetype = node->methodcall->basetype;
node->objectClassName = node->methodcall->objectClassName;
}
void TypeCheck::visitIfElseNode(IfElseNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression->basetype != bt_boolean)
typeError(if_predicate_type_mismatch);
}
void TypeCheck::visitWhileNode(WhileNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression->basetype != bt_boolean)
typeError(while_predicate_type_mismatch);
}
void TypeCheck::visitRepeatNode(RepeatNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression->basetype != bt_boolean)
typeError(repeat_predicate_type_mismatch);
}
void TypeCheck::visitPrintNode(PrintNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
}
void TypeCheck::visitPlusNode(PlusNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_integer || node->expression_2->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_integer;
}
void TypeCheck::visitMinusNode(MinusNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_integer || node->expression_2->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_integer;
}
void TypeCheck::visitTimesNode(TimesNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_integer || node->expression_2->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_integer;
}
void TypeCheck::visitDivideNode(DivideNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_integer || node->expression_2->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_integer;
}
void TypeCheck::visitLessNode(LessNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_integer || node->expression_2->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_boolean;
}
void TypeCheck::visitLessEqualNode(LessEqualNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_integer || node->expression_2->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_boolean;
}
void TypeCheck::visitEqualNode(EqualNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != node->expression_2->basetype)
typeError(expression_type_mismatch);
if (node->expression_1->basetype == bt_none || node->expression_1->basetype == bt_object)
typeError(expression_type_mismatch);
node->basetype = bt_boolean;
}
void TypeCheck::visitAndNode(AndNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_boolean || node->expression_2->basetype != bt_boolean)
typeError(expression_type_mismatch);
node->basetype = bt_boolean;
}
void TypeCheck::visitOrNode(OrNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (node->expression_1->basetype != bt_boolean || node->expression_2->basetype != bt_boolean)
typeError(expression_type_mismatch);
node->basetype = bt_boolean;
}
void TypeCheck::visitNotNode(NotNode* node) {
// WRITEME: Replace with code if necessary
node->expression->accept(this);
if (node->expression->basetype != bt_boolean)
typeError(expression_type_mismatch);
node->basetype = bt_boolean;
}
void TypeCheck::visitNegationNode(NegationNode* node) {
// WRITEME: Replace with code if necessary
node->expression->accept(this);
if (node->expression->basetype != bt_integer)
typeError(expression_type_mismatch);
node->basetype = bt_integer;
}
void TypeCheck::visitMethodCallNode(MethodCallNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
MethodInfo methodinfo;
methodinfo.localsSize = -1;
if (node->identifier_2 == NULL) {
if (currentMethodTable -> count(node->identifier_1->name) > 0)
methodinfo = currentMethodTable->find(node->identifier_1->name)->second;
ClassInfo currentClassInfo;
currentClassInfo = classTable->find(currentClassName)->second;
std::string superClass = currentClassInfo.superClassName;
while (superClass != "") {
currentClassInfo = classTable->find(superClass)->second;
if (currentClassInfo.methods->count(node->identifier_1->name) > 0) {
methodinfo = currentClassInfo.methods->find(node->identifier_1->name)->second;
break;
}
superClass = currentClassInfo.superClassName;
}
}
else {
CompoundType ct;
ct.objectClassName = " ";
if (currentVariableTable->count(node->identifier_1->name) > 0) {
ct = currentVariableTable->find(node->identifier_1->name)->second.type;
}
else if (classTable->find(currentClassName)->second.members->count(node->identifier_1->name) > 0) {
ct = classTable->find(currentClassName)->second.members->find(node->identifier_1->name)->second.type;
}
else {
ClassInfo currentClassInfo;
currentClassInfo = classTable->find(currentClassName)->second;
std::string superClass = currentClassInfo.superClassName;
while (superClass != "") {
currentClassInfo = classTable->find(superClass)->second;
if (currentClassInfo.members->count(node->identifier_1->name) > 0) {
ct = currentClassInfo.members->find(node->identifier_1->name)->second.type;
break;
}
superClass = currentClassInfo.superClassName;
}
}
if (ct.objectClassName == " " || ct.baseType != bt_object) {
typeError(not_object);
return;
}
if (classTable->find(ct.objectClassName)->second.methods->count(node->identifier_2->name) > 0) {
methodinfo = classTable->find(ct.objectClassName)->second.methods->find(node->identifier_2->name)->second;
}
else {
ClassInfo currentClassInfo;
currentClassInfo = classTable->find(ct.objectClassName)->second;
std::string superClass = currentClassInfo.superClassName;
std::string obj = node->identifier_2->name;
while (superClass != "") {
currentClassInfo = classTable->find(superClass)->second;
if (currentClassInfo.methods->count(obj) > 0) {
methodinfo = currentClassInfo.methods->find(obj)->second;
break;
}
superClass = currentClassInfo.superClassName;
}
}
}
if (methodinfo.localsSize == -1) {
typeError(undefined_method);
return;
}
if (methodinfo.parameters->size() != node->expression_list->size()) {
typeError(argument_number_mismatch);
return;
}
std::list<CompoundType>::iterator methodParams = methodinfo.parameters->begin();
for(std::list<ExpressionNode*>::iterator nodeParams = node->expression_list->begin(); nodeParams != node->expression_list->end(); nodeParams++){
if ((*nodeParams)->basetype != (*methodParams).baseType || (*nodeParams)->objectClassName != (*methodParams).objectClassName)
typeError(argument_type_mismatch);
methodParams++;
}
node->basetype = methodinfo.returnType.baseType;
node->objectClassName = methodinfo.returnType.objectClassName;
}
void TypeCheck::visitMemberAccessNode(MemberAccessNode* node) {
// WRITEME: Replace with code if necessary
CompoundType ct;
ct.objectClassName = " ";
if (currentVariableTable->count(node->identifier_1->name) > 0)
ct = currentVariableTable->find(node->identifier_1->name)->second.type;
if (classTable->find(currentClassName)->second.members->count(node->identifier_1->name) > 0) {
ct = classTable->find(currentClassName)->second.members->find(node->identifier_1->name)->second.type;
}
else {
ClassInfo currentClassInfo;
currentClassInfo = classTable->find(currentClassName)->second;
std::string superClass = currentClassInfo.superClassName;
while (superClass != "") {
currentClassInfo = classTable->find(superClass)->second;
if (currentClassInfo.members->count(node->identifier_1->name) > 0) {
ct = currentClassInfo.members->find(node->identifier_1->name)->second.type;
break;
}
superClass = currentClassInfo.superClassName;
}
}
if (ct.baseType != bt_object || ct.objectClassName == " ") {
typeError(not_object);
return;
}
if (classTable->find(ct.objectClassName)->second.members->count(node->identifier_2->name) > 0) {
ct = classTable->find(ct.objectClassName)->second.members->find(node->identifier_2->name)->second.type;
}
else {
ClassInfo currentClassInfo;
currentClassInfo.membersSize = -1;
currentClassInfo = classTable->find(ct.objectClassName)->second;
std::string superClass = currentClassInfo.superClassName;
std::string obj = node->identifier_2->name;
ct.objectClassName = " ";
while (superClass != "") {
currentClassInfo = classTable->find(superClass)->second;
if (currentClassInfo.members->count(obj) > 0) {
ct = currentClassInfo.members->find(obj)->second.type;
break;
}
superClass = currentClassInfo.superClassName;
}
if (ct.objectClassName == " ") {
typeError(undefined_member);
return;
}
}
node->basetype = ct.baseType;
node->objectClassName = ct.objectClassName;
}
void TypeCheck::visitVariableNode(VariableNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
CompoundType varType;
varType.objectClassName = " ";
if (currentVariableTable->count(node->identifier->name) > 0)
varType = currentVariableTable->find(node->identifier->name)->second.type;
if (classTable->find(currentClassName)->second.members->count(node->identifier->name)>0) {
varType = classTable->find(currentClassName)->second.members->find(node->identifier->name)->second.type;
}
else {
ClassInfo currentClassInfo;
currentClassInfo = classTable->find(currentClassName)->second;
std::string superClass = currentClassInfo.superClassName;
while (superClass != "") {
currentClassInfo = classTable->find(superClass)->second;
if (currentClassInfo.members->count(node->identifier->name) > 0) {
varType = currentClassInfo.members->find(node->identifier->name)->second.type;
break;
}
superClass = currentClassInfo.superClassName;
}
}
if (varType.objectClassName != " ") {
node->basetype = varType.baseType;
node->objectClassName = varType.objectClassName;
}
else{
typeError(undefined_variable);
}
}
void TypeCheck::visitIntegerLiteralNode(IntegerLiteralNode* node) {
// WRITEME: Replace with code if necessary
node->basetype = bt_integer;
node->objectClassName = "";
}
void TypeCheck::visitBooleanLiteralNode(BooleanLiteralNode* node) {
// WRITEME: Replace with code if necessary
node->basetype = bt_boolean;
node->objectClassName = "";
}
void TypeCheck::visitNewNode(NewNode* node) {
// WRITEME: Replace with code if necessary
node->visit_children(this);
if (classTable->count(node->identifier->name) == 0) {
typeError(undefined_class);
return;
}
if(node->expression_list != NULL) {
MethodInfo methodinfo = classTable->find(node->identifier->name)->second.methods->find(node->identifier->name)->second;
if (methodinfo.parameters->size() != node->expression_list->size())
typeError(argument_number_mismatch);
std::list<CompoundType>::iterator methodParams = methodinfo.parameters->begin();
for(std::list<ExpressionNode*>::iterator nodeParams = node->expression_list->begin(); nodeParams != node->expression_list->end(); nodeParams++) {
if ((*nodeParams)->basetype != (*methodParams).baseType || (*nodeParams)->objectClassName != (*methodParams).objectClassName)
typeError(argument_type_mismatch);
methodParams++;
}
}
node->basetype = bt_object;
node->objectClassName = node->identifier->name;
}
void TypeCheck::visitIntegerTypeNode(IntegerTypeNode* node) {
// WRITEME: Replace with code if necessary
node->basetype = bt_integer;
node->objectClassName = "";
}
void TypeCheck::visitBooleanTypeNode(BooleanTypeNode* node) {
// WRITEME: Replace with code if necessary
node->basetype = bt_boolean;
node->objectClassName = "";
}
void TypeCheck::visitObjectTypeNode(ObjectTypeNode* node) {
// WRITEME: Replace with code if necessary
node->basetype = bt_object;
node->objectClassName = node->identifier->name;
}
void TypeCheck::visitNoneNode(NoneNode* node) {
// WRITEME: Replace with code if necessary
node->basetype = bt_none;
node->objectClassName = "";
}
void TypeCheck::visitIdentifierNode(IdentifierNode* node) {
// WRITEME: Replace with code if necessary
}
void TypeCheck::visitIntegerNode(IntegerNode* node) {
// WRITEME: Replace with code if necessary
}
// The following functions are used to print the Symbol Table.
// They do not need to be modified at all.
std::string genIndent(int indent) {
std::string string = std::string("");
for (int i = 0; i < indent; i++)
string += std::string(" ");
return string;
}
std::string string(CompoundType type) {
switch (type.baseType) {
case bt_integer:
return std::string("Integer");
case bt_boolean:
return std::string("Boolean");
case bt_none:
return std::string("None");
case bt_object:
return std::string("Object(") + type.objectClassName + std::string(")");
default:
return std::string("");
}
}
void print(VariableTable variableTable, int indent) {
std::cout << genIndent(indent) << "VariableTable {";
if (variableTable.size() == 0) {
std::cout << "}";
return;
}
std::cout << std::endl;
for (VariableTable::iterator it = variableTable.begin(); it != variableTable.end(); it++) {
std::cout << genIndent(indent + 2) << it->first << " -> {" << string(it->second.type);
std::cout << ", " << it->second.offset << ", " << it->second.size << "}";
if (it != --variableTable.end())
std::cout << ",";
std::cout << std::endl;
}
std::cout << genIndent(indent) << "}";
}
void print(MethodTable methodTable, int indent) {
std::cout << genIndent(indent) << "MethodTable {";
if (methodTable.size() == 0) {
std::cout << "}";
return;
}
std::cout << std::endl;
for (MethodTable::iterator it = methodTable.begin(); it != methodTable.end(); it++) {
std::cout << genIndent(indent + 2) << it->first << " -> {" << std::endl;
std::cout << genIndent(indent + 4) << string(it->second.returnType) << "," << std::endl;
std::cout << genIndent(indent + 4) << it->second.localsSize << "," << std::endl;
print(*it->second.variables, indent + 4);
std::cout <<std::endl;
std::cout << genIndent(indent + 2) << "}";
if (it != --methodTable.end())
std::cout << ",";
std::cout << std::endl;
}
std::cout << genIndent(indent) << "}";
}
void print(ClassTable classTable, int indent) {
std::cout << genIndent(indent) << "ClassTable {" << std::endl;
for (ClassTable::iterator it = classTable.begin(); it != classTable.end(); it++) {
std::cout << genIndent(indent + 2) << it->first << " -> {" << std::endl;
if (it->second.superClassName != "")
std::cout << genIndent(indent + 4) << it->second.superClassName << "," << std::endl;
print(*it->second.members, indent + 4);
std::cout << "," << std::endl;
print(*it->second.methods, indent + 4);
std::cout <<std::endl;
std::cout << genIndent(indent + 2) << "}";
if (it != --classTable.end())
std::cout << ",";
std::cout << std::endl;
}
std::cout << genIndent(indent) << "}" << std::endl;
}
void print(ClassTable classTable) {
print(classTable, 0);
}