-
Notifications
You must be signed in to change notification settings - Fork 29
/
semant.cc
1482 lines (1248 loc) · 45.8 KB
/
semant.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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "semant.h"
#include "utilities.h"
#include <symtab.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
extern int semant_debug;
extern char *curr_filename;
void raise_error();
Symbol type_check(method_class* method);
Symbol type_check(attr_class* attr);
Symbol type_check(Feature f);
std::map<Symbol, attr_class*> get_class_attributes(Class_ class_definition);
SymbolTable<Symbol,Symbol> *objects_table;
ClassTable *class_table;
//////////////////////////////////////////////////////////////////////
// TYPING ENVIRONMENT
//////////////////////////////////////////////////////////////////////
Symbol current_class_name;
Class_ current_class_definition;
std::map<Symbol, method_class*> current_class_methods;
std::map<Symbol, attr_class*> current_class_attrs;
std::map<Symbol, std::map<Symbol, method_class*>> class_methods;
std::map<Symbol, std::map<Symbol, attr_class*>> class_attrs;
//////////////////////////////////////////////////////////////////////
//
// Symbols
//
// For convenience, a large number of symbols are predefined here.
// These symbols include the primitive type and method names, as well
// as fixed names used by the runtime system.
//
//////////////////////////////////////////////////////////////////////
static Symbol
arg,
arg2,
Bool,
concat,
cool_abort,
copy,
Int,
in_int,
in_string,
IO,
length,
Main,
main_meth,
No_class,
No_type,
Object,
out_int,
out_string,
prim_slot,
self,
SELF_TYPE,
Str,
str_field,
substr,
type_name,
val;
//
// Initializing the predefined symbols.
//
static void initialize_constants(void)
{
arg = idtable.add_string("arg");
arg2 = idtable.add_string("arg2");
Bool = idtable.add_string("Bool");
concat = idtable.add_string("concat");
cool_abort = idtable.add_string("abort");
copy = idtable.add_string("copy");
Int = idtable.add_string("Int");
in_int = idtable.add_string("in_int");
in_string = idtable.add_string("in_string");
IO = idtable.add_string("IO");
length = idtable.add_string("length");
Main = idtable.add_string("Main");
main_meth = idtable.add_string("main");
// _no_class is a symbol that can't be the name of any
// user-defined class.
No_class = idtable.add_string("_no_class");
No_type = idtable.add_string("_no_type");
Object = idtable.add_string("Object");
out_int = idtable.add_string("out_int");
out_string = idtable.add_string("out_string");
prim_slot = idtable.add_string("_prim_slot");
self = idtable.add_string("self");
SELF_TYPE = idtable.add_string("SELF_TYPE");
Str = idtable.add_string("String");
str_field = idtable.add_string("_str_field");
substr = idtable.add_string("substr");
type_name = idtable.add_string("type_name");
val = idtable.add_string("_val");
}
////////////////////////////////////////////////////////////////////
// CLASS TABLE
////////////////////////////////////////////////////////////////////
ClassTable::ClassTable(Classes classes) : semant_errors(0) , error_stream(cerr) {
this->install_basic_classes();
}
void ClassTable::install_basic_classes() {
// The tree package uses these globals to annotate the classes built below.
// curr_lineno = 0;
Symbol filename = stringtable.add_string("<basic class>");
// The following demonstrates how to create dummy parse trees to
// refer to basic Cool classes. There's no need for method
// bodies -- these are already built into the runtime system.
// IMPORTANT: The results of the following expressions are
// stored in local variables. You will want to do something
// with those variables at the end of this method to make this
// code meaningful.
//
// The Object class has no parent class. Its methods are
// abort() : Object aborts the program
// type_name() : Str returns a string representation of class name
// copy() : SELF_TYPE returns a copy of the object
//
// There is no need for method bodies in the basic classes---these
// are already built in to the runtime system.
Class_ Object_class =
class_(Object,
No_class,
append_Features(
append_Features(
single_Features(method(cool_abort, nil_Formals(), Object, no_expr())),
single_Features(method(type_name, nil_Formals(), Str, no_expr()))),
single_Features(method(copy, nil_Formals(), SELF_TYPE, no_expr()))),
filename);
//
// The IO class inherits from Object. Its methods are
// out_string(Str) : SELF_TYPE writes a string to the output
// out_int(Int) : SELF_TYPE " an int " " "
// in_string() : Str reads a string from the input
// in_int() : Int " an int " " "
//
Class_ IO_class =
class_(IO,
Object,
append_Features(
append_Features(
append_Features(
single_Features(method(out_string, single_Formals(formal(arg, Str)),
SELF_TYPE, no_expr())),
single_Features(method(out_int, single_Formals(formal(arg, Int)),
SELF_TYPE, no_expr()))),
single_Features(method(in_string, nil_Formals(), Str, no_expr()))),
single_Features(method(in_int, nil_Formals(), Int, no_expr()))),
filename);
//
// The Int class has no methods and only a single attribute, the
// "val" for the integer.
//
Class_ Int_class =
class_(Int,
Object,
single_Features(attr(val, prim_slot, no_expr())),
filename);
//
// Bool also has only the "val" slot.
//
Class_ Bool_class =
class_(Bool, Object, single_Features(attr(val, prim_slot, no_expr())),filename);
//
// The class Str has a number of slots and operations:
// val the length of the string
// str_field the string itself
// length() : Int returns length of the string
// concat(arg: Str) : Str performs string concatenation
// substr(arg: Int, arg2: Int): Str substring selection
//
Class_ Str_class =
class_(Str,
Object,
append_Features(
append_Features(
append_Features(
append_Features(
single_Features(attr(val, Int, no_expr())),
single_Features(attr(str_field, prim_slot, no_expr()))),
single_Features(method(length, nil_Formals(), Int, no_expr()))),
single_Features(method(concat,
single_Formals(formal(arg, Str)),
Str,
no_expr()))),
single_Features(method(substr,
append_Formals(single_Formals(formal(arg, Int)),
single_Formals(formal(arg2, Int))),
Str,
no_expr()))),
filename);
this->class_lookup[Object] = Object_class;
this->class_lookup[IO] = IO_class;
this->class_lookup[Int] = Int_class;
this->class_lookup[Bool] = Bool_class;
this->class_lookup[Str] = Str_class;
}
bool ClassTable::install_custom_classes(Classes classes) {
for (int i = classes->first(); classes->more(i); i = classes->next(i))
{
Class_ current_class = classes->nth(i);
Symbol class_name = current_class->get_name();
if (
class_name == Int ||
class_name == Bool ||
class_name == Str ||
class_name == Object ||
class_name == SELF_TYPE
)
{
semant_error(current_class) << "Redefinition of " << class_name << " is not allowed. \n";
return false;
}
else if (this->class_lookup.find(class_name) != this->class_lookup.end())
{
semant_error(current_class) << "Class " << class_name << " was previously defined.\n";
return false;
}
else
this->class_lookup[class_name] = current_class;
}
return true;
}
bool ClassTable::build_inheritance_graph() {
for (auto const& x : this->class_lookup)
{
Symbol class_name = x.first;
if (class_name == Object)
continue;
Class_ class_definition = x.second;
Symbol class_parent_name = class_definition->get_parent_name();
parent_type_of[class_name] = class_parent_name;
if(
class_parent_name == Str ||
class_parent_name == Int ||
class_parent_name == Bool ||
class_parent_name == SELF_TYPE
)
{
this->semant_error(class_definition)
<< "Class "
<< class_definition->get_name()
<< " cannot inherit class "
<< class_parent_name
<< ".\n";
return false;
}
if (this->class_lookup.find(class_parent_name) == this->class_lookup.end())
{
semant_error(x.second) << "Class "
<< class_name
<< " inherits from an undefined class "
<< class_parent_name
<< ".\n";
return false;
}
if (this->inheritance_graph.find(class_parent_name) == this->inheritance_graph.end())
this->inheritance_graph[class_parent_name] = std::vector<Symbol>();
this->inheritance_graph[class_parent_name].push_back(class_name);
}
return true;
}
enum SymbolColor { gray, black, white };
std::map<Symbol, SymbolColor> color_of;
bool ClassTable::inheritance_dfs(Symbol symbol) {
color_of[symbol] = gray;
for (auto const& x : inheritance_graph[symbol])
{
if(color_of[x] == gray)
{
semant_error() << "There exists an (in) direct circular dependency between: ";
symbol->print(semant_error());
semant_error() << " and ";
x->print(semant_error());
return false;
}
else
{
if (!inheritance_dfs(x))
return false;
}
}
color_of[symbol] = black;
return true;
}
bool ClassTable::is_inheritance_graph_acyclic() {
color_of.clear();
for (auto const& x : this->class_lookup)
color_of[x.first] = white;
for (auto const& x : this->class_lookup)
if (color_of[x.first] == white)
if (!this->inheritance_dfs(x.first))
return false;
return true;
}
bool ClassTable::is_class_table_valid() {
if (!this->is_inheritance_graph_acyclic())
return false;
if (!this->is_type_defined(Main)) {
semant_error() << "Class Main is not defined.\n";
return false;
}
return true;
}
bool ClassTable::is_subtype_of(Symbol candidate, Symbol desired_type) {
if (candidate == No_type)
return true;
if (candidate == SELF_TYPE) {
if (desired_type == SELF_TYPE)
return true;
else
candidate = current_class_name;
}
Symbol current = candidate;
while (current != Object && current != desired_type)
current = parent_type_of[current];
return current == desired_type;
}
Symbol ClassTable::least_common_ancestor_type(Symbol lhs, Symbol rhs) {
if (lhs == SELF_TYPE)
lhs = current_class_name;
if (rhs == SELF_TYPE)
rhs = current_class_name;
Symbol current_lhs = lhs;
Symbol current_rhs = rhs;
std::set<Symbol> rhs_ancestors;
while (current_rhs != Object)
{
rhs_ancestors.insert(current_rhs);
current_rhs = parent_type_of[current_rhs];
}
while (current_lhs != Object)
{
if (rhs_ancestors.find(current_lhs)!=rhs_ancestors.end())
return current_lhs;
current_lhs = parent_type_of[current_lhs];
}
return Object;
}
Symbol ClassTable::get_parent_type_of(Symbol symbol) {
if (this->parent_type_of.find(symbol) == this->parent_type_of.end())
return No_type;
return parent_type_of[symbol];
}
bool ClassTable::is_type_defined(Symbol symbol) {
return class_lookup.find(symbol) != class_lookup.end();
}
bool ClassTable::is_primitive(Symbol symbol) {
return (
symbol == Object ||
symbol == IO ||
symbol == Int ||
symbol == Bool ||
symbol == Str
);
}
////////////////////////////////////////////////////////////////////
// CLASS UTILITIES
////////////////////////////////////////////////////////////////////
std::map<Symbol, method_class*> get_class_methods(Class_ class_definition) {
std::map<Symbol, method_class*> class_methods;
Symbol class_name = class_definition->get_name();
Features class_features = class_definition->get_features();
for (int i = class_features->first(); class_features->more(i); i = class_features->next(i))
{
Feature feature = class_features->nth(i);
if (!feature->is_method())
continue;
method_class* method = static_cast<method_class*>(feature);
Symbol method_name = method->get_name();
if (class_methods.find(method_name) != class_methods.end())
{
ostream& error_stream = class_table->semant_error(class_definition);
error_stream << "The method :";
method_name->print(error_stream);
error_stream << " has already been defined!\n";
}
else
{
class_methods[method_name] = method;
}
}
return class_methods;
}
method_class* get_class_method(Symbol class_name, Symbol method_name) {
std::map<Symbol, method_class*> methods = class_methods[class_name];
if (methods.find(method_name) == methods.end())
return nullptr;
return methods[method_name];
}
attr_class* get_class_attr(Symbol class_name, Symbol attr_name) {
std::map<Symbol, attr_class*> attrs = class_attrs[class_name];
if (attrs.find(attr_name) == attrs.end())
return nullptr;
return attrs[attr_name];
}
void ensure_class_attributes_are_unique(Class_ class_definition) {
std::set<Symbol> class_attrs;
Symbol class_name = class_definition->get_name();
Features class_features = class_definition->get_features();
for (int i = class_features->first(); class_features->more(i); i = class_features->next(i))
{
Feature feature = class_features->nth(i);
if (!feature->is_attr())
continue;
attr_class* attr = static_cast<attr_class*>(feature);
Symbol attr_name = attr->get_name();
if (class_attrs.find(attr_name) != class_attrs.end())
{
class_table->semant_error(class_definition)
<< "The attribute :"
<< attr_name
<< " has already been defined!\n";
}
class_attrs.insert(attr_name);
}
}
std::map<Symbol, attr_class*> get_class_attributes(Class_ class_definition) {
std::map<Symbol, attr_class*> class_attrs;
Symbol class_name = class_definition->get_name();
Features class_features = class_definition->get_features();
for (int i = class_features->first(); class_features->more(i); i = class_features->next(i))
{
Feature feature = class_features->nth(i);
if (!feature->is_attr())
continue;
attr_class* attr = static_cast<attr_class*>(feature);
Symbol attr_name = attr->get_name();
class_attrs[attr_name] = attr;
}
return class_attrs;
}
////////////////////////////////////////////////////////////////////
// TYPECHECKING
////////////////////////////////////////////////////////////////////
void build_attribute_scopes(Class_ current_class) {
objects_table->enterscope();
std::map<Symbol, attr_class*> attrs = get_class_attributes(current_class);
for(const auto &x : attrs) {
attr_class* attr_definition = x.second;
objects_table->addid(
attr_definition->get_name(),
new Symbol(attr_definition->get_type())
);
}
if(current_class->get_name() == Object)
return;
Symbol parent_type_name = class_table->get_parent_type_of(current_class->get_name());
Class_ parent_definition = class_table->class_lookup[parent_type_name];
build_attribute_scopes(parent_definition);
}
void process_attr(Class_ current_class, attr_class* attr) {
if (get_class_attr(current_class->get_name(), attr->get_name()) != nullptr)
{
class_table->semant_error(current_class_definition)
<< " Attribute "
<< attr->get_name()
<< " is an attribute of an inherited class.\n";
raise_error();
}
Symbol parent_type_name = class_table->get_parent_type_of(current_class->get_name());
if (parent_type_name == No_type)
return;
Class_ parent_definition = class_table->class_lookup[parent_type_name];
process_attr(parent_definition, attr);
}
void process_method(Class_ current_class, method_class* original_method, method_class* parent_method) {
if (parent_method == nullptr)
return;
Formals original_method_args = original_method->get_formals();
Formals parent_method_args = parent_method->get_formals();
int original_formal_ix = 0;
int parent_formal_ix = 0;
if(original_method->get_return_type() != parent_method->get_return_type()) {
class_table->semant_error(current_class)
<< "In redefined method "
<< original_method->get_name()
<< ", the return type "
<< original_method->get_return_type()
<< " differs from the ancestor method return type "
<< parent_method->get_return_type()
<< ".\n";
}
int original_methods_formals = 0;
int parent_method_formals = 0;
while (original_method_args->more(original_methods_formals))
original_methods_formals = original_method_args->next(original_methods_formals);
while (parent_method_args->more(parent_method_formals))
parent_method_formals = parent_method_args->next(parent_method_formals);
if (original_methods_formals != parent_method_formals) {
class_table->semant_error(current_class)
<< "In redefined method "
<< original_method->get_name()
<< ", the number of arguments "
<< "(" << original_methods_formals << ")"
<< " differs from the ancestor method's "
<< "number of arguments "
<< "(" << parent_method_formals << ")"
<< ".\n";
}
while (
original_method_args->more(original_formal_ix) &&
parent_method_args->more(parent_formal_ix)
)
{
Formal original_formal = original_method_args->nth(original_formal_ix);
Formal parent_formal = parent_method_args->nth(parent_formal_ix);
if (original_formal->get_type() != parent_formal->get_type()) {
class_table->semant_error(current_class)
<< "In redefined method "
<< original_method->get_name()
<< ", the return type of argument "
<< original_formal->get_type()
<< " differs from the corresponding ancestor method argument return type "
<< parent_formal->get_type()
<< ".\n";
}
original_formal_ix = original_method_args->next(original_formal_ix);
parent_formal_ix = parent_method_args->next(parent_formal_ix);
}
Symbol parent_type_name = class_table->get_parent_type_of(current_class->get_name());
if (parent_type_name == No_type)
return;
Class_ parent_class_definition = class_table->class_lookup[parent_type_name];
process_method(
parent_class_definition,
original_method,
get_class_method(
parent_type_name,
original_method->get_name()
)
);
}
void register_class_and_its_methods(Class_ class_definition) {
class_methods[class_definition->get_name()] = get_class_methods(class_definition);
class_attrs[class_definition->get_name()] = get_class_attributes(class_definition);
}
void type_check(Class_ next_class) {
current_class_name = next_class->get_name();
current_class_definition = next_class;
current_class_methods = get_class_methods(next_class);
ensure_class_attributes_are_unique(next_class);
current_class_attrs = get_class_attributes(next_class);
objects_table = new SymbolTable<Symbol, Symbol>();
objects_table->enterscope();
objects_table->addid(self, new Symbol(current_class_definition->get_name()));
build_attribute_scopes(next_class);
for (const auto &x : current_class_methods) {
process_method(next_class, x.second, x.second);
}
for (const auto &x : current_class_attrs) {
Symbol parent_type_name = class_table->get_parent_type_of(current_class_name);
Class_ parent_definition = class_table->class_lookup[parent_type_name];
process_attr(parent_definition, x.second);
}
for (const auto &x : current_class_attrs) {
x.second->type_check();
}
for (const auto &x : current_class_methods) {
x.second->type_check();
}
objects_table->exitscope();
}
Symbol object_class::type_check() {
if (name == self) {
this->set_type(SELF_TYPE);
return SELF_TYPE;
}
Symbol* object_type = objects_table->lookup(name);
if (object_type != nullptr){
this->set_type(*object_type);
return *object_type;
}
this->set_type(Object);
class_table->semant_error(this)
<< "The referenced object "
<< name
<< " is undefined in relevant scopes.\n";
return Object;
}
Symbol no_expr_class::type_check() {
this->set_type(No_type);
return No_type;
}
Symbol isvoid_class::type_check() {
e1->type_check();
this->set_type(Bool);
return Bool;
}
Symbol new__class::type_check() {
if(type_name != SELF_TYPE && !class_table->is_type_defined(type_name))
{
this->set_type(Object);
class_table->semant_error(this)
<< "Tried to instantiate an object of undefined type: "
<< type_name
<< " .\n";
return Object;
}
this->set_type(type_name);
return type_name;
}
Symbol comp_class::type_check() {
Symbol expr_type = e1->type_check();
if (expr_type == Bool) {
this->set_type(expr_type);
return expr_type;
}
this->set_type(Object);
class_table->semant_error(this)
<< "Argument of 'not' has type "
<< expr_type
<< " instead of Bool.\n";
return Object;
}
Symbol leq_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
if(left_type == Int && right_type == Int) {
this->set_type(Bool);
return Bool;
}
else
{
this->set_type(Object);
class_table->semant_error(this)
<< "Expected both arguments of operator <= to be of type Int"
<< " but got arguments of types "
<< left_type
<< " and "
<< right_type
<< ".\n";
}
return this->get_type();
}
Symbol eq_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
bool is_left_type_primitive = left_type == Int || left_type == Bool || left_type == Str;
bool is_right_type_primitive = right_type == Int || right_type == Bool || right_type == Str;
if ((is_left_type_primitive && is_right_type_primitive) && left_type != right_type)
{
class_table->semant_error(this) << "Illegal comparison with a basic type.\n";
}
this->set_type(Bool);
return Bool;
}
Symbol lt_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
if(left_type == Int && right_type == Int) {
this->set_type(Bool);
return Bool;
}
else
{
this->set_type(Object);
class_table->semant_error(this)
<< "Expected both arguments of operator < to be of type Int"
<< " but got arguments of types "
<< left_type
<< " and "
<< right_type
<< ".\n";
}
return this->get_type();
}
Symbol neg_class::type_check() {
Symbol inner_expr_type = e1->type_check();
if (inner_expr_type != Int)
{
this->set_type(Object);
class_table -> semant_error(this)
<< "Argument of the operator '~' has type "
<< inner_expr_type
<< " instead of Int.\n";
return Object;
}
this->set_type(Int);
return Int;
}
Symbol divide_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
if(left_type == Int && right_type == Int)
this->set_type(Int);
else
{
class_table->semant_error(this)
<< "Expected both arguments of operator / to be of type Int"
<< " but got arguments of types "
<< left_type
<< " and "
<< right_type
<< ".\n";
this->set_type(Object);
}
return this->get_type();
}
Symbol mul_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
if(left_type == Int && right_type == Int)
this->set_type(Int);
else
{
class_table->semant_error(this)
<< "Expected both arguments of operator * to be of type Int"
<< " but got arguments of types "
<< left_type
<< " and "
<< right_type
<< ".\n";
this->set_type(Object);
}
return this->get_type();
}
Symbol sub_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
if(left_type == Int && right_type == Int)
this->set_type(Int);
else
{
class_table->semant_error(this)
<< "Expected both arguments of operator - to be of type Int"
<< " but got arguments of types "
<< left_type
<< " and "
<< right_type
<< ".\n";
this->set_type(Object);
}
return this->get_type();
}
Symbol plus_class::type_check() {
Symbol left_type = e1->type_check();
Symbol right_type = e2->type_check();
if(left_type == Int && right_type == Int)
this->set_type(Int);
else
{
class_table->semant_error(this)
<< "Expected both arguments of operator +to be of type Int"
<< " but got arguments of types "
<< left_type
<< " and "
<< right_type
<< ".\n";
this->set_type(Object);
}
return this->get_type();
}
Symbol let_class::type_check() {
objects_table->enterscope();
if (identifier == self)
class_table->semant_error(this) << "'self' cannot be bound in a 'let' expression.\n";
Symbol init_type = init->type_check();
if (type_decl != SELF_TYPE && !class_table->is_type_defined(type_decl))
class_table->semant_error(this)
<< "Type "
<< type_decl
<< " of let-bound identifier "
<< identifier
<< " is undefined.\n";
else if (init_type != No_type && !class_table->is_subtype_of(init_type, type_decl))
class_table->semant_error(this)
<< "Inferred type "
<< init_type
<< " in initialization of "
<< identifier
<< " is not compatible with identifier's declared type "
<< type_decl << ".\n";
objects_table->addid(identifier, new Symbol(type_decl));
this->set_type(body->type_check());
objects_table->exitscope();
return type;
}
Symbol block_class::type_check() {
Symbol last_body_expr_type = Object;
for (int i = body->first(); body->more(i); i = body->next(i))
last_body_expr_type = body->nth(i)->type_check();
this->set_type(last_body_expr_type);
return last_body_expr_type;
}
Symbol branch_class::type_check() {
Symbol declaration_type = type_decl;
Symbol declaration_id = name;
if (declaration_id == self) {
class_table->semant_error(this) << "'self' cannot be bound in a 'branch' expression.";
}
objects_table->enterscope();
objects_table->addid(declaration_id, new Symbol(declaration_type));
Symbol branch_expr_type = expr->type_check();
this->set_type(branch_expr_type);
objects_table->exitscope();
return branch_expr_type;
}
Symbol typcase_class::type_check() {
Symbol expr_type = expr->type_check();
std::set<Symbol> branch_declaration_types;
Symbol branch_result_type = Object;
for (int i = cases->first(); cases->more(i); i = cases->next(i)) {
branch_class* branch = static_cast<branch_class*>(cases->nth(i));
Symbol branch_declaration_type = branch->get_declaration_type();
if (branch_declaration_types.find(branch_declaration_type) != branch_declaration_types.end())
class_table->semant_error(branch)
<< "Duplicate branch type"
<< branch_declaration_type
<< " in case statement.\n";
else
branch_declaration_types.insert(branch_declaration_type);
if (i == cases->first())
branch_result_type = branch->type_check();
else
branch_result_type = class_table->least_common_ancestor_type(
branch_result_type,
branch->type_check()
);
}
this->set_type(branch_result_type);
return branch_result_type;
}
Symbol loop_class::type_check() {
Symbol pred_type = pred->type_check();
Symbol body_type = body->type_check();
if (pred_type != Bool)
{
class_table->semant_error(this)
<< "Expected the predicate of while to be of type Bool"
<< " but got the predicate of type "
<< pred_type
<< " instead .\n";
}
this->set_type(Object);
return Object;
}
Symbol cond_class::type_check() {
Symbol pred_type = pred->type_check();
Symbol then_type = then_exp->type_check();
Symbol else_type = else_exp->type_check();
if (pred_type != Bool)
{
class_table->semant_error(this)
<< "Expected the predicate of if to be of type Bool"
<< " but got the predicate of type "
<< pred_type
<< " instead .\n";
}
Symbol cond_type = class_table->least_common_ancestor_type(then_type, else_type);