-
Notifications
You must be signed in to change notification settings - Fork 1
/
symtable.cpp
1026 lines (996 loc) · 34.2 KB
/
symtable.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
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
// Chenxing Ji
// cji13
// Nelson Yeap
// nyeap
#include <bitset>
#include <stdexcept>
#include <unordered_map>
#include <vector>
#include "astree.h"
#include "auxlib.h"
#include "symtable.h"
#include "attr_bitset.h"
#include "yyparse.h"
using namespace std;
symbol_table global_identifiers_table;
symbol_table type_name_table;//symbol table for all the types
int next_block = 0;
bool inside_func = false;
int local_counter = 0;
int param_counter = 0;
int field_counter = 0;
int in_struct = 0;
int in_func = 0;
int curr_block_count = 0;
string curr_func_type;
string literal_string = "string";
string literal_int = "int";
string literal_char = "char";
string literal_array = "array";
string literal_test = "test";
string literal_nullx = "nullx";
string literal_struct = "struct";
string literal_void = "void";
string literal_not_implemented = "not_implement_type";
const string* return_symbol_type(symbol* node)
{
if(node->attribute[static_cast<unsigned>(attr::STRUCT)]){
return node->struct_name;
}
if(node->attribute[static_cast<unsigned>(attr::STRING)]){
return &literal_string;
}
if(node->attribute[static_cast<unsigned>(attr::INT)]){
return &literal_int;
}
if(node->attribute[static_cast<unsigned>(attr::CHAR)]){
return &literal_char;
}
return &literal_not_implemented;
}
void attr_on_arrow(symbol* symbol, astree* node)
{
if (*return_symbol_type(symbol) == "int"){
node->attribute.set(static_cast<unsigned>(attr::INT));
}else if(*return_symbol_type(symbol) == "string"){
node->attribute.set(static_cast<unsigned>(attr::STRING));
}else if(*return_symbol_type(symbol) == "char"){
node->attribute.set(static_cast<unsigned>(attr::CHAR));
}
}
void set_type_attr_on_operator(astree *node)
{
if (*return_type(node->children[0]) == "int"){
node->attribute.set(static_cast<unsigned>(attr::INT));
}else if(*return_type(node->children[0]) == "string"){
node->attribute.set(static_cast<unsigned>(attr::STRING));
}else if(*return_type(node->children[0]) == "char"){
node->attribute.set(static_cast<unsigned>(attr::CHAR));
}else if(*return_type(node->children[0]) == "void"){
node->attribute.set(static_cast<unsigned>(attr::VOID));
}
}
bool int_checker(astree* node){
if (node->children.size() == 1)
{
if(node->children[0]->attribute[static_cast<unsigned>(attr::INT)])
{
return true;
}
else
{
return false;
}
}
if(node->children[0]->attribute[static_cast<unsigned>(attr::INT)]and
node->children[1]->attribute[static_cast<unsigned>(attr::INT)]){
return true;
}
else{
return false;
}
}
const string* return_type(astree* node){
if(node->attribute[static_cast<unsigned>(attr::STRUCT)]){
return node->struct_name;
}
if(node->attribute[static_cast<unsigned>(attr::STRING)]){
return &literal_string;
}
if(node->attribute[static_cast<unsigned>(attr::INT)]){
return &literal_int;
}
if(node->attribute[static_cast<unsigned>(attr::CHAR)]){
return &literal_char;
}
if(node->attribute[static_cast<unsigned>(attr::NULLX)]){
return &literal_nullx;
}
if(node->attribute[static_cast<unsigned>(attr::VOID)]){
return &literal_void;
}
fprintf(stderr,"The type is no implemented: %s\n", node->lexinfo->c_str());
return &literal_not_implemented;
}
bool type_check(astree* node1, astree* node2)
{
const string* type1 = return_type(node1);
const string* type2 = return_type(node2);
if (*type1 == *type2)
{
return true;
}
return false;
}
void sym_dump(FILE* outfile, astree* node){
if((node->attribute[static_cast<unsigned>(attr::STRUCT)] or
node->attribute[static_cast<unsigned>(attr::FUNCTION)])and
!node->attribute[static_cast<unsigned>(attr::FIELD)] and
!node->attribute[static_cast<unsigned>(attr::LOCAL)] and
!node->attribute[static_cast<unsigned>(attr::PARAM)]){
fprintf(outfile, "\n");
}
else {
for(int i = 0; i < next_block; ++i){
fprintf(outfile, " ");//indent acccordingly:
}
}
fprintf(outfile, "%s: (%zu.%zu.%zu) ", node->lexinfo->c_str(),
node->lloc.filenr, node->lloc.linenr, node->lloc.offset
);
if(!node->attribute[static_cast<unsigned>(attr::FIELD)]){
fprintf(outfile,"{%zd} ", node->block_num);
}
if(node->attribute[static_cast<unsigned>(attr::STRUCT)]){
fprintf(outfile,"struct %s ", node->struct_name->c_str());
}
fprintf(outfile,"%s",to_string_array(node->attribute).c_str());
if (node->attribute[static_cast<unsigned>(attr::LOCAL)]){
fprintf(outfile,"%d", local_counter);
} else if(node->attribute[static_cast<unsigned>(attr::PARAM)]){
fprintf(outfile,"%d", param_counter);
} else if(node->attribute[static_cast<unsigned>(attr::FIELD)]){
fprintf(outfile,"%d", field_counter);
}
fprintf(outfile,"\n");
}
symbol* astree_to_symbol(astree*node)
{
if (node == nullptr)
{
return nullptr;
}
symbol* symbol_temp = new symbol();
symbol_temp->attribute = node->attribute;
symbol_temp->lloc = node->lloc;
symbol_temp->block_num = node->block_num;
symbol_temp->struct_name = node->struct_name;
symbol_temp->params = nullptr;
symbol_temp->fields = nullptr;
return symbol_temp;
}
void assign_sym(FILE* out, astree* node, symbol_table* local_table){
if (node == nullptr)
{
return;
}
node->block_count = curr_block_count;
node->block_num = next_block;
switch (node->symbol)
{
case TOK_STRUCT:
{
++curr_block_count;
in_struct = 1;
node->children[0]->block_count = curr_block_count;
node->children[0]->block_num = next_block;
node->children[0]->attribute.set(
static_cast<int>(attr::STRUCT));
node->children[0]->struct_name = node->children[0]->lexinfo;
symbol* symbol = astree_to_symbol(node->children[0]);
type_name_table.insert(symbol_entry(
(string*)node->children[0]->lexinfo, symbol));
symbol_table* field_table = new symbol_table();
symbol->fields = field_table;
sym_dump(out,node->children[0]);
for (auto child:node->children)
{
child->block_count = curr_block_count;
child->block_num = next_block;
if (child == node->children[0])
{
continue;
}
if(child->symbol == TOK_ARRAY){
child->children[1]->block_count = curr_block_count;
child->children[1]->block_num = next_block;
child->children[1]->attribute.set(
static_cast<int>(attr::FIELD));
DFS(out, child, field_table);
auto grandchild = astree_to_symbol(child->children[1]);
field_table->insert(symbol_entry((string*)
child->children[1]->lexinfo,grandchild));
++field_counter;
}else{
child->children[0]->attribute.set(
static_cast<int>(attr::FIELD));
DFS(out,child, field_table);
auto grandchild = astree_to_symbol(child->children[0]);
field_table->insert(symbol_entry(
(string*)child->children[0]->lexinfo, grandchild));
sym_dump(out,child->children[0]);
++field_counter;
}
}
in_struct = 0;
}
break;
case TOK_TYPEID:
{
auto finder = type_name_table.find((string*)node->lexinfo);
if (finder == type_name_table.end())
{
symbol* symbol = astree_to_symbol(node);
type_name_table.insert(symbol_entry(
(string*)node->lexinfo,symbol));
}
else
{
node->decl_lloc = finder->second->lloc;
}
if (node->children[0] != nullptr)
{
node->children[0]->struct_name = finder->first;
node->children[0]->attribute.set(
static_cast<unsigned>(attr::STRUCT));
}
if (!(in_struct || in_func))
{
node->children[0]->attribute.set(
static_cast<unsigned>(attr::VARIABLE));
node->children[0]->attribute.set(
static_cast<unsigned>(attr::LVAL));
node->children[0]->attribute.set(
static_cast<unsigned>(attr::LOCAL));
node->children[0]->block_num = next_block;
sym_dump(out,node->children[0]);
}
}
break;
case TOK_IDENT:
{
if (local_table == nullptr)
{
break;
}
auto finder = local_table->find((string*)node->lexinfo);
if (finder == local_table->end())
{
finder = global_identifiers_table.find(
(string*)node->lexinfo);
}
if (finder == global_identifiers_table.end())
{
fprintf(stderr,"ident \"%s\" not found in global table\n",
node->lexinfo->c_str());
}
else
{
node->struct_name = finder->second->struct_name;
node->attribute = finder->second->attribute;
node->decl_lloc = finder->second->lloc;
}
}
break;
case TOK_FUNCTION:
{
++curr_block_count;
next_block = 0;
param_counter = 0;
local_counter = 0;
in_func = 1;
node->block_num = 0;
node->children[0]->children[0]->block_count = curr_block_count;
node->children[0]->children[0]->block_num = next_block;
++next_block;
curr_func_type = *node->children[0]->lexinfo;
symbol_table* new_local_table = new symbol_table();
auto finder = global_identifiers_table.find(
(string*)node->children[0]->children[0]->lexinfo);
node->children[1]->block_count = curr_block_count;
node->children[1]->block_num = next_block;
if (finder != global_identifiers_table.end())
{
break;
// error out cause already a fucntion with that name
}
else
{
assign_sym(out, node->children[0], new_local_table);
node->children[0]->children[0]->attribute.set
(static_cast<unsigned>(attr::FUNCTION));
sym_dump(out, node->children[0]->children[0]);
symbol* func_name = astree_to_symbol(
node->children[0]->children[0]);
global_identifiers_table.insert(symbol_entry(
(string*)node->children[0]->children[0]->lexinfo,
func_name));
for (auto child:node->children[1]->children){
child->children[0]->block_num = next_block;
assign_sym(out,child, new_local_table);
child->block_count = curr_block_count;
child->block_num = next_block;
if (child->symbol == TOK_ARRAY)
{
child->children[1]->block_count = curr_block_count;
child->children[1]->block_num = next_block;
child->children[1]->attribute.set
(static_cast<unsigned>(attr::PARAM));
child->children[1]->attribute.set
(static_cast<unsigned>(attr::VARIABLE));
child->children[1]->attribute.set
(static_cast<unsigned>(attr::LVAL));
sym_dump(out,child->children[1]);
symbol* symbol = astree_to_symbol(child->children[1]);
new_local_table->insert(symbol_entry(
(string*)child->children[1]->lexinfo,symbol));
}
else
{
child->children[0]->block_count = curr_block_count;
child->children[0]->block_num = next_block;
child->children[0]->attribute.set
(static_cast<unsigned>(attr::PARAM));
child->children[0]->attribute.set
(static_cast<unsigned>(attr::VARIABLE));
child->children[0]->attribute.set
(static_cast<unsigned>(attr::LVAL));
sym_dump(out,child->children[0]);
symbol* symbol = astree_to_symbol(child->children[0]);
new_local_table->insert(symbol_entry(
(string*)child->children[0]->lexinfo,symbol));
}
++param_counter;
}
in_func = 0;
if (node->children.size() < 3)
{
break;
}
for (auto child:node->children[2]->children)
{
assign_sym(out, child, new_local_table);
}
}
next_block = 0;
}
break;
case TOK_BLOCK:
{
++next_block;
for (auto child:node->children)
{
assign_sym(out, child, local_table);
}
--next_block;
}
break;
case TOK_VARDECL:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(node->children[0]->symbol == TOK_ARRAY)
{
node->children[0]->children[1]->attribute.set(
static_cast<unsigned>(attr::VARIABLE));
node->children[0]->children[1]->attribute.set(
static_cast<unsigned>(attr::LVAL));
node->children[0]->children[1]->block_num = next_block;
node->children[0]->children[1]->block_count =
curr_block_count;
symbol* symbol = astree_to_symbol(
node->children[0]->children[1]);
local_table->insert(symbol_entry(
(string*)node->children[0]->children[1]->lexinfo,
symbol));
sym_dump(out,node->children[0]->children[1]);
}else{
node->children[0]->children[0]->attribute.set(
static_cast<unsigned>(attr::VARIABLE));
node->children[0]->children[0]->attribute.set(
static_cast<unsigned>(attr::LVAL));
node->children[0]->children[0]->block_count =
curr_block_count;
node->children[0]->children[0]->block_num = next_block;
symbol* symbol = astree_to_symbol(
node->children[0]->children[0]);
local_table->insert(symbol_entry((string*)
node->children[0]->children[0]->lexinfo,symbol));
sym_dump(out,node->children[0]->children[0]);
}
}
break;
case TOK_VOID:
{
if(node->children.size() == 0){
break;
}
node->children[0]->attribute.set(
static_cast<int>(attr::VOID));
}
break;
case TOK_BOOL:
{
auto finder = type_name_table.find((string*)node->lexinfo);
if (finder == type_name_table.end())
{
symbol* symbol = astree_to_symbol(node);
node->decl_lloc = node->lloc;
type_name_table.insert(symbol_entry
((string*)node->lexinfo,symbol));
}
}
break;
case TOK_CHAR:
{
auto finder = type_name_table.find((string*)node->lexinfo);
if (finder == type_name_table.end())
{
symbol* symbol = astree_to_symbol(node);
type_name_table.insert(symbol_entry
((string*)node->lexinfo,symbol));
}
break;
}
case TOK_INT:
{
if (in_struct||in_func|| next_block == 0)
{
auto finder = type_name_table.find((string*)node->lexinfo);
if (finder == type_name_table.end())
{
symbol* symbol = astree_to_symbol(node);
type_name_table.insert(symbol_entry
((string*)node->lexinfo,symbol));
}
if(node->children.size() == 0){
break;
}
node->children[0]->attribute.set(
static_cast<int>(attr::INT));
}
else
{
if (node->children.size() == 0)
{
break;
}
node->children[0]->block_num = next_block;
auto finder = local_table->find(
(string*)node->children[0]->lexinfo);
node->children[0]->attribute.set(
static_cast<int>(attr::INT));
node->children[0]->attribute.set(
static_cast<int>(attr::LOCAL));
node->children[0]->attribute.set(
static_cast<int>(attr::VARIABLE));
node->children[0]->attribute.set(
static_cast<int>(attr::LVAL));
if (finder == local_table->end())
{
symbol* symbol = astree_to_symbol(node->children[0]);
local_table->insert(symbol_entry(
(string*)node->children[0]->lexinfo,symbol));
}
sym_dump(out, node->children[0]);
++local_counter;
}
}
break;
case TOK_STRING:
{
if (in_struct||in_func || next_block == 0)
{
auto finder = type_name_table.find((string*)node->lexinfo);
if (finder == type_name_table.end())
{
symbol* symbol = astree_to_symbol(node);
type_name_table.insert(symbol_entry(
(string*)node->lexinfo,symbol));
}
if(node->children.size() == 0){
break;
}
node->children[0]->attribute.set(
static_cast<int>(attr::STRING));
}
else
{
if(node->children.size() == 0){
break;
}
node->children[0]->block_num = next_block;
auto finder = local_table->find((string*)node->lexinfo);
if (finder == local_table->end())
{
symbol* symbol = astree_to_symbol(node);
local_table->insert(symbol_entry(
(string*)node->lexinfo,symbol));
}
node->children[0]->attribute.set(
static_cast<int>(attr::LOCAL));
node->children[0]->attribute.set(
static_cast<int>(attr::VARIABLE));
next_block = 0;
node->children[0]->attribute.set(
static_cast<int>(attr::LVAL));
sym_dump(out, node->children[0]);
++local_counter;
}
node->children[0]->attribute.set(
static_cast<int>(attr::STRING));
break;
}
case TOK_IF:
{
for (auto child:node->children)
{
assign_sym(out, child, local_table);
}
}
break;
case TOK_NULL:
{
node->attribute.set(static_cast<int>(attr::NULLX));
node->attribute.set(static_cast<int>(attr::CONST));
}
break;
case TOK_NEW:
{
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_EQ:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
set_type_attr_on_operator(node);
}
break;
case TOK_NE:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_LT:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_LE:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_GT:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_GE:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case '=':
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(node->children[0]->symbol == TOK_ARRAY or
node->children[0]->symbol == TOK_INDEX){
}
else if(node->children[1]->symbol == TOK_NEWARRAY or
node->children[1]->symbol == TOK_ARRAY){
}else{
if (!type_check(node->children[0],node->children[1]))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
}
}
break;
case '+':
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(!int_checker(node))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
set_type_attr_on_operator(node);
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case '-':
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(!int_checker(node))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
set_type_attr_on_operator(node);
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case '*':
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(!int_checker(node))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
set_type_attr_on_operator(node);
node->attribute.set(static_cast<unsigned>(attr::VREG));
fprintf(out, "\n");
}
break;
case '/':
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(!int_checker(node))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
set_type_attr_on_operator(node);
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case '%':
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!int_checker(node))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),
node->children[1]->lexinfo->c_str());
}
set_type_attr_on_operator(node);
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_POS:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if(!int_checker(node))
{
fprintf(stderr,"ERROR RIGHT OPERAND IS NOT AN INTEGER");
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_NEG:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (!int_checker(node))
{
fprintf(stderr,"ERROR RIGHT OPERAND IS NOT AN INTEGER");
}
node->attribute.set(static_cast<unsigned>(attr::INT));
node->attribute.set(static_cast<unsigned>(attr::VREG));
}
break;
case TOK_ARROW:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
auto finder = type_name_table.find(
(string*)node->children[0]->struct_name);
if (finder != type_name_table.end())
{
auto field_finder = finder->second->fields->find(
(string*)node->children[1]->lexinfo);
if (field_finder == finder->second->fields->end())
{
fprintf(stderr,
"ERROR RIGHT OPERAND NOT A FIELD IN STRUCT\n");
}
else
{
attr_on_arrow(field_finder->second, node);
node->attribute.set(
static_cast<unsigned>(attr::VADDR));
node->attribute.set(
static_cast<unsigned>(attr::LVAL));
}
}
else
{
fprintf(stderr,"ERROR LEFT OPERAND IS NOT A STRUCT\n");
}
}
break;
case TOK_CALL:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
set_type_attr_on_operator(node);
}
break;
case TOK_INTCON:
{
node->attribute.set(static_cast<int>(attr::INT));
node->attribute.set(static_cast<int>(attr::CONST));
}
break;
case TOK_CHARCON:
{
node->attribute.set(static_cast<int>(attr::CHAR));
node->attribute.set(static_cast<int>(attr::CONST));
}
break;
case TOK_STRINGCON:
{
node->attribute.set(static_cast<int>(attr::STRING));
node->attribute.set(static_cast<int>(attr::CONST));
}
break;
case TOK_RETURN:
for (auto child:node->children)
{
assign_sym(out, child,local_table);
}
if(curr_func_type == "void" and node->children.size() == 0){
break;
}
if(node->children.size() == 0){
fprintf(stderr, "ERROR non-void function ");
fprintf(stderr, "type need to have a object to return\n");
break;
}
if (!(*return_type(node->children[0])==curr_func_type ))
{
fprintf(stderr,"ERROR TYPES DONT CHECK ON %s = %s\n",
node->children[0]->lexinfo->c_str(),curr_func_type.c_str());
}
break;
case TOK_FIELD:
{
node->attribute.set(static_cast<int>(attr::VADDR));
node->attribute.set(static_cast<int>(attr::LVAL));
}
break;
case TOK_WHILE:
{
for (auto child: node->children)
{
assign_sym(out,child,local_table);
}
}
break;
case TOK_PROTO:
{
next_block = 0;
node->children[0]->children[0]->attribute.set(
static_cast<int>(attr::FUNCTION));
node->children[0]->children[0]->block_num = next_block;
next_block++;
if (node->children[0]->symbol == TOK_INT){
node->children[0]->children[0]->attribute.set(
static_cast<unsigned>(attr::INT));
}else if(node->children[0]->symbol == TOK_STRING){
node->children[0]->children[0]->attribute.set(
static_cast<unsigned>(attr::STRING));
}else if(node->children[0]->symbol == TOK_CHAR){
node->children[0]->children[0]->attribute.set(
static_cast<unsigned>(attr::CHAR));
}else if(node->children[0]->symbol == TOK_VOID){
node->children[0]->children[0]->attribute.set(
static_cast<unsigned>(attr::VOID));
}
global_identifiers_table.insert(symbol_entry(
(string*)node->children[0]->children[0]->lexinfo,
astree_to_symbol(node->children[0]->children[0])));
next_block--;
next_block = 0;
sym_dump(out,node->children[0]->children[0]);
}
break;
case TOK_INDEX:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (*return_type(node->children[0]) == "int"){
node->attribute.set(static_cast<unsigned>(attr::INT));
}else if(*return_type(node->children[0]) == "string"){
node->attribute.set(static_cast<unsigned>(attr::CHAR));
}else if(*return_type(node->children[0]) == "CHAR"){
node->attribute.set(static_cast<unsigned>(attr::CHAR));
}
}
break;
case TOK_ARRAY:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
if (node->children[0]->symbol == TOK_INT){
node->children[1]->attribute.set(
static_cast<unsigned>(attr::INT));
}else if(node->children[0]->symbol == TOK_STRING){
node->children[1]->attribute.set(
static_cast<unsigned>(attr::STRING));
}else if(node->children[0]->symbol == TOK_CHAR){
node->children[1]->attribute.set(
static_cast<unsigned>(attr::CHAR));
}
node->children[1]->attribute.set(
static_cast<int>(attr::ARRAY));
}
break;
case TOK_NEWARRAY:
{
for (auto child:node->children)
{
assign_sym(out,child,local_table);
}
}
break;
}
}
void traverse_root(FILE* out, astree* root)
{
int temp = 0;
for (auto child: root->children)
{
next_block = 0;
if(child->symbol == TOK_VARDECL){
fprintf(out, "\n");
for (auto grandchild:child->children)
{
assign_sym(out,grandchild,nullptr);
}
if(child->children[0]->symbol == TOK_ARRAY)
{
child->children[0]->children[1]->attribute.set(
static_cast<unsigned>(attr::VARIABLE));
child->children[0]->children[1]->block_num = next_block;
symbol* symbol = astree_to_symbol(
child->children[0]->children[1]);
global_identifiers_table.insert(symbol_entry(
(string*)child->children[0]->children[1]->lexinfo,
symbol));
sym_dump(out,child->children[0]->children[1]);
}else{
child->children[0]->children[1]->attribute.set(
static_cast<unsigned>(attr::VARIABLE));
child->children[0]->children[1]->block_num = next_block;
symbol* symbol = astree_to_symbol(