-
Notifications
You must be signed in to change notification settings - Fork 0
/
borisfuncs.c
1938 lines (1887 loc) · 101 KB
/
borisfuncs.c
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
/*
* Main Functions for the boris compiler
*
* including parse-tree-builder, parse-tree-walker, type-checker.
*
* Author: Yi Zhou
* March 31, 2019
*
*/
# include <stdio.h>
# include <stdlib.h>
# include <stdarg.h>
# include <string.h>
# include "boris.h"
# include "boris.tab.h" // yylloc definition, token number
void yyerror(char* s, ...){
va_list ap;
va_start(ap, s);
fprintf(stderr, RED);
fprintf(stderr, "position %d,%d-%d,%d: ",yylloc.first_line,yylloc.first_column, yylloc.last_line, yylloc.last_column);
vfprintf(stderr, s, ap);
fprintf(stderr, RESET);
fprintf(stderr, "\n");
}
void scannerTester(){
printf("> Hello from scannerTester for boris compiler!\n");
int tok;
do {
tok = yylex();
if(tok == 0) break;
printf("token[%d].", tok);
if (tok == END_OF_LINE) {
printf("[\\n]");
} else{
printf("[%s]", yytext);
}
printf(" In position %d,%d-%d,%d.",yylloc.first_line,yylloc.first_column, yylloc.last_line, yylloc.last_column);
if (tok == ID) {
printf(GREEN);
printf("[ID] Retained infomation: yylval.sval=%s",yylval.sval);
printf(RESET);
} else if (tok == INT_LIT){
printf(GREEN);
printf("[INT_LIT] Retained infomation: yylval.ival=%d",yylval.ival);
printf(RESET);
}
printf("\n");
} while (1);
}
void parserTester(){
printf("> Hello from parserTester for boris compiler!\n");
yyparse();
return;
}
struct pNode *newpNode(int type, ...){
/* Initialize a new parse tree node*/
/* arguments should in thee following format: int num, struct Node* child_1, struct Node* child_2, struct Node* child_3, ..., struct Node* child_num*/
struct pNode * parent = malloc(sizeof(struct pNode));
if(!parent) {
yyerror("Out of space.");
exit(0);
}
va_list arguments;
va_start ( arguments, type );
int num = va_arg ( arguments, int );
if (num > PARSE_TREE_MAX_CHILD) {
yyerror("Parse tree node initialization error, too many child nodes");
exit(996);
}
parent->line = yylloc.first_line;
parent->childscount = num;
parent->pnodetype = type;
parent->true_block = NULL;
parent->false_block = NULL;
parent->next_block = NULL;
int i = 0;
for ( ; i < num; i++ ) {
parent->childs[i] = va_arg ( arguments, struct pNode * );
}
for ( ; i < PARSE_TREE_MAX_CHILD; i++) {
parent->childs[i] = NULL;
}
va_end ( arguments );
return parent;
}
struct pNode *newsNode(char* sval){
struct sNode * parent = malloc(sizeof(struct sNode));
if(!parent) {
yyerror("Out of space.");
exit(0);
}
parent->pnodetype = NODETYPE_ID;
parent->line = yylloc.first_line;
parent->childscount = 0;
parent->sval = sval;
return (struct pNode *)parent;
}
struct pNode *newiNode(int ival){
struct iNode * parent = malloc(sizeof(struct iNode));
if(!parent) {
yyerror("Out of space.");
exit(0);
}
parent->pnodetype = NODETYPE_INT;
parent->line = yylloc.first_line;
parent->childscount = 0;
parent->ival = ival;
return (struct pNode *)parent;
}
struct pNode *newplaceholderNode(int tok){
struct placeholderNode * parent = malloc(sizeof(struct placeholderNode));
if(!parent) {
yyerror("Out of space.");
exit(0);
}
parent->pnodetype = NODETYPE_PLACEHOLDER;
parent->line = yylloc.first_line;
parent->childscount = 0;
parent->tok = tok;
switch (tok)
{
case KW_ARRAY:{ strcpy(parent->tokstr, "array"); break; }
case KW_DEFUN:{ strcpy(parent->tokstr, "defun"); break; }
case KW_DO:{ strcpy(parent->tokstr, "do"); break; }
case KW_ELSE:{ strcpy(parent->tokstr, "else"); break; }
case KW_ELSIF:{ strcpy(parent->tokstr, "elsif"); break; }
case KW_END:{ strcpy(parent->tokstr, "end"); break; }
case KW_FOR:{ strcpy(parent->tokstr, "for"); break; }
case KW_FOREACH:{ strcpy(parent->tokstr, "foreach"); break; }
case KW_GLOBAL:{ strcpy(parent->tokstr, "global"); break; }
case KW_IF:{ strcpy(parent->tokstr, "if"); break; }
case KW_IN:{ strcpy(parent->tokstr, "in"); break; }
case KW_LOCAL:{ strcpy(parent->tokstr, "local"); break; }
case KW_PRINT:{ strcpy(parent->tokstr, "print"); break; }
case KW_RETURN:{ strcpy(parent->tokstr, "return"); break; }
case KW_THEN:{ strcpy(parent->tokstr, "then"); break; }
case KW_TUPLE:{ strcpy(parent->tokstr, "tuple"); break; }
case KW_WHILE:{ strcpy(parent->tokstr, "while"); break; }
case OP_ASSIGN:{ strcpy(parent->tokstr, "="); break; }
case OP_COMMA:{ strcpy(parent->tokstr, ","); break; }
case OP_DIV:{ strcpy(parent->tokstr, "/"); break; }
case OP_DOT:{ strcpy(parent->tokstr, "."); break; }
case OP_DOTDOT:{ strcpy(parent->tokstr, ".."); break; }
case OP_EQUAL:{ strcpy(parent->tokstr, "=="); break; }
case OP_EXCHANGE:{ strcpy(parent->tokstr, "<->"); break; }
case OP_GREATER:{ strcpy(parent->tokstr, ">"); break; }
case OP_GREATEREQUAL:{ strcpy(parent->tokstr, ">="); break; }
case OP_LBRAK:{ strcpy(parent->tokstr, "["); break; }
case OP_LESS:{ strcpy(parent->tokstr, "<"); break; }
case OP_LESSEQUAL:{ strcpy(parent->tokstr, "<="); break; }
case OP_LPAR:{ strcpy(parent->tokstr, "("); break; }
case OP_MINUS:{ strcpy(parent->tokstr, "-"); break; }
case OP_MULT:{ strcpy(parent->tokstr, "*"); break; }
case OP_NOTEQUA:{ strcpy(parent->tokstr, "!="); break; }
case OP_PLUS:{ strcpy(parent->tokstr, "+"); break; }
case OP_RBRAK:{ strcpy(parent->tokstr, "]"); break; }
case OP_RPAR:{ strcpy(parent->tokstr, ")"); break; }
case OP_SEMI:{ strcpy(parent->tokstr, ";"); break; }
default:{ strcpy(parent->tokstr, "unknown"); break; }
}
return (struct pNode *)parent;
}
void printManySpace(int count){
for (int i = 0; i < count; i++) printf(" ");
}
void visualize(struct pNode *p, int level){
if (level == 0) printf("Start parse tree visualization:\n");
if (p == NULL) {
printManySpace(level*4);
printf("[EMPTY_NODE]\n");
return;
}
char nodetype2nodestr[][100] = {
"ID", //1024
"INT", //1025
"PLACEHOLDER", //1026
"EXPR_COMMA_EXPR", //1027
"EXPR_MINUS_EXPR", //1028
"EXPR_PLUS_EXPR", //1029
"EXPR_DIV_EXPR", //1030
"EXPR_MULT_EXPR", //1031
"LPAR_EXPR_LPAR", //1032
"SINGLE_ID_AS_EXPR", //1033
"FUNC_CALL_AS_EXPR", //1034
"TUPLE_REF_AS_EXPR", //1035
"ARRAY_REF_AS_EXPR", //1036
"SINGLE_ID_AS_LHSITEM", //1037
"TUPLE_REF_AS_LHSITEM", //1038
"ARRAY_REF_AS_LHSITEM", //1039
"LHS", //1040
"COMMA_LHSITEN_LIST", //1041
"BOOLEXPR", //1042
"RANGE", //1043
"LHS_ASSIGN_EXPR_AS_STATEMENT", //1044
"LHS_EXCHANGE_LHS_AS_STATEMENT", //1045
"STATEMENT_LIST", //1046
"WHILE_STATEMENT", //1047
"ELSIF_SENTENCE", //1048
"ELSE_SENTENCE", //1049
"ELSIF_SENTENCE_LIST", //1050
"IF_STATEMENT", //1051
"IF_ELSE_STATEMENT", //1052
"FOREACH_RANGE_STATEMENT", //1053
"ARRAY_ID", //1054
"FOREACH_ARRAYID_STATEMENT", //1055
"PRINT_STATEMENT", //1056
"RETURN_STATEMENT", //1057
"SINGLE_INT_AS_EXPR", //1058
"LOCAL_DECL", //1059
"GLOBAL_DECL", //1060
"ARRAY_DECL", //1061
"ARRAY_DECL_WITH_ANONY_FUNC", //1062
"STATEMENT_AS_SD", //1063
"DECL_AS_SD", //1064
"SD_LIST", //1065
"BODY", //1066
"COMMA_ID_LIST", //1067
"FUNC_DEFN", //1068
"STATEMENT_AS_SDD", //1069
"DECL_AS_SDD", //1070
"DEFN_AS_SDD", //1071
"SDD_LIST", //1072
"ROOT_INPUT", //1073
"NO_EXPR_GLOBAL_DECL", //1074
"NO_EXPR_LOCAL_DECL", //1075
"ITER_ID", //1076
};
switch(p->pnodetype) {
case NODETYPE_ID: {
printManySpace(level*4);
char* nodestr = nodetype2nodestr[p->pnodetype-NODETYPE_ID];
printf("[%s]%s (@line %d)\n", nodestr, ((struct sNode*)p)->sval, p->line);
break;
}
case NODETYPE_INT: {
printManySpace(level*4);
char* nodestr = nodetype2nodestr[p->pnodetype-NODETYPE_ID];
printf("[%s]%d (@line %d)\n", nodestr, ((struct iNode*)p)->ival, p->line);
break;
}
case NODETYPE_PLACEHOLDER: {
printManySpace(level*4);
char* nodestr = nodetype2nodestr[p->pnodetype-NODETYPE_ID];
printf("[%s-%d]%s (@line %d)\n", nodestr, ((struct placeholderNode*)p)->tok, ((struct placeholderNode*)p)->tokstr, p->line);
break;
}
case NODETYPE_EXPR_COMMA_EXPR:
case NODETYPE_EXPR_MINUS_EXPR:
case NODETYPE_EXPR_PLUS_EXPR:
case NODETYPE_EXPR_MULT_EXPR:
case NODETYPE_EXPR_DIV_EXPR:
case NODETYPE_LPAR_EXPR_RPAR:
case NODETYPE_FUNC_CALL_AS_EXPR:
case NODETYPE_SINGLE_ID_AS_EXPR:
case NODETYPE_TUPLE_REF_AS_EXPR:
case NODETYPE_ARRAY_REF_AS_EXPR:
case NODETYPE_SINGLE_ID_AS_LHSITEM:
case NODETYPE_TUPLE_REF_AS_LHSITEM:
case NODETYPE_ARRAY_REF_AS_LHSITEM:
case NODETYPE_LHS:
case NODETYPE_COMMA_LHSITEN_LIST:
case NODETYPE_BOOLEXPR:
case NODETYPE_RANGE:
case NODETYPE_LHS_ASSIGN_EXPR_AS_STATEMENT:
case NODETYPE_LHS_EXCHANGE_LHS_AS_STATEMENT:
case NODETYPE_STATEMENT_LIST:
case NODETYPE_WHILE_STATEMENT:
case NODETYPE_ELSIF_SENTENCE:
case NODETYPE_ELSE_SENTENCE:
case NODETYPE_ELSIF_SENTENCE_LIST:
case NODETYPE_IF_STATEMENT:
case NODETYPE_IF_ELSE_STATEMENT:
case NODETYPE_FOREACH_RANGE_STATEMENT:
case NODETYPE_ARRAY_ID:
case NODETYPE_FOREACH_ARRAYID_STATEMENT:
case NODETYPE_PRINT_STATEMENT:
case NODETYPE_RETURN_STATEMENT:
case NODETYPE_SINGLE_INT_AS_EXPR:
case NODETYPE_LOCAL_DECL:
case NODETYPE_GLOBAL_DECL:
case NODETYPE_ARRAY_DECL:
case NODETYPE_ARRAY_DECL_WITH_ANONY_FUNC:
case NODETYPE_STATEMENT_AS_SD:
case NODETYPE_DECL_AS_SD:
case NODETYPE_SD_LIST:
case NODETYPE_BODY:
case NODETYPE_COMMA_ID_LIST:
case NODETYPE_FUNC_DEFN:
case NODETYPE_STATEMENT_AS_SDD:
case NODETYPE_DECL_AS_SDD:
case NODETYPE_DEFN_AS_SDD:
case NODETYPE_SDD_LIST:
case NODETYPE_ROOT_INPUT:
case NODETYPE_NO_EXPR_GLOBAL_DECL:
case NODETYPE_NO_EXPR_LOCAL_DECL:
case NODETYPE_ITER_ID:{
printManySpace(level*4);
char* nodestr = nodetype2nodestr[p->pnodetype-NODETYPE_ID];
printf("[%s] (@line %d)\n", nodestr, p->line);
for (int i = 0; i < p->childscount; i++){
struct pNode *child = p->childs[i];
visualize(child, level+1);
}
break;
}
default: printf("internal error: bad node type%d\n", p->pnodetype);
}
}
void treefree(struct pNode *p){
if (p == NULL) return;
switch(p->pnodetype) {
/* non-terminals */
case NODETYPE_EXPR_COMMA_EXPR:
case NODETYPE_EXPR_MINUS_EXPR:
case NODETYPE_EXPR_PLUS_EXPR:
case NODETYPE_EXPR_MULT_EXPR:
case NODETYPE_EXPR_DIV_EXPR:
case NODETYPE_LPAR_EXPR_RPAR:
case NODETYPE_FUNC_CALL_AS_EXPR:
case NODETYPE_SINGLE_ID_AS_EXPR:
case NODETYPE_TUPLE_REF_AS_EXPR:
case NODETYPE_ARRAY_REF_AS_EXPR:
case NODETYPE_SINGLE_ID_AS_LHSITEM:
case NODETYPE_TUPLE_REF_AS_LHSITEM:
case NODETYPE_ARRAY_REF_AS_LHSITEM:
case NODETYPE_LHS:
case NODETYPE_COMMA_LHSITEN_LIST:
case NODETYPE_BOOLEXPR:
case NODETYPE_RANGE:
case NODETYPE_LHS_ASSIGN_EXPR_AS_STATEMENT:
case NODETYPE_LHS_EXCHANGE_LHS_AS_STATEMENT:
case NODETYPE_STATEMENT_LIST:
case NODETYPE_WHILE_STATEMENT:
case NODETYPE_ELSIF_SENTENCE:
case NODETYPE_ELSE_SENTENCE:
case NODETYPE_ELSIF_SENTENCE_LIST:
case NODETYPE_IF_STATEMENT:
case NODETYPE_IF_ELSE_STATEMENT:
case NODETYPE_FOREACH_RANGE_STATEMENT:
case NODETYPE_ARRAY_ID:
case NODETYPE_FOREACH_ARRAYID_STATEMENT:
case NODETYPE_PRINT_STATEMENT:
case NODETYPE_RETURN_STATEMENT:
case NODETYPE_SINGLE_INT_AS_EXPR:
case NODETYPE_LOCAL_DECL:
case NODETYPE_GLOBAL_DECL:
case NODETYPE_ARRAY_DECL:
case NODETYPE_ARRAY_DECL_WITH_ANONY_FUNC:
case NODETYPE_STATEMENT_AS_SD:
case NODETYPE_DECL_AS_SD:
case NODETYPE_SD_LIST:
case NODETYPE_BODY:
case NODETYPE_COMMA_ID_LIST:
case NODETYPE_FUNC_DEFN:
case NODETYPE_STATEMENT_AS_SDD:
case NODETYPE_DECL_AS_SDD:
case NODETYPE_DEFN_AS_SDD:
case NODETYPE_SDD_LIST:
case NODETYPE_ROOT_INPUT:
case NODETYPE_NO_EXPR_GLOBAL_DECL:
case NODETYPE_NO_EXPR_LOCAL_DECL:
case NODETYPE_ITER_ID:{
for (int i = 0; i < p->childscount; i++){
treefree(p->childs[i]);
}
free(p);
break;
}
/* terminals */
case NODETYPE_INT:
case NODETYPE_PLACEHOLDER:{
free(p);
break;
}
case NODETYPE_ID:{
free(((struct sNode*)p)->sval);
free(p);
break;
}
default: printf("internal error: free bad node %d\n", p->pnodetype);
}
}
/*
* Following: tree walker and type [synthesis, inference]
*/
// look up the type of a symbol named sval, based current context
// return type in {int, tuple, array, func, unknown}, no {link}
int type_lookup(char* sval, struct symboltable* global_tb, struct symboltableStack* local_tbstk) {
if (LOCAL_ENV) {
struct symboltable* local_tb = top_symboltableStack(local_tbstk);
struct symboltableRecord* record = lookup_symbol(sval, LOCAL_SCOPE, local_tb);
if (record) {
fprintf(stderr, GREEN"[type_lookup]: %s is a local `%c` symbol. declared in line %d\n"RESET, sval, record->valuetype, record->line);
return record->valuetype;
}
} else {
struct symboltableRecord* record = lookup_symbol(sval, GLOBAL_SCOPE, global_tb);
if (record) {
fprintf(stderr, GREEN"[type_lookup]: %s is a global `%c` symbol. declared in line %d\n"RESET, sval, record->valuetype, record->line);
return record->valuetype;
}
}
fprintf(stderr, RED"[type_lookup]: %s is not found in current envrionment.\n"RESET, sval);
exit(999);
return -1;
}
// first check out local if possible, otherwise lookup global. if not found, return NULL
struct symboltable* get_matched_symboltable(char* sval, struct symboltable* global_tb, struct symboltableStack* local_tbstk){
if(LOCAL_ENV){
struct symboltable* local_tb = top_symboltableStack(local_tbstk);
struct symboltableRecord* record = lookup_symbol(sval, LOCAL_SCOPE, local_tb);
if(record)
return local_tb;
} else {
struct symboltableRecord* g_record = lookup_symbol(sval, GLOBAL_SCOPE, global_tb);
if(g_record){
return global_tb;
}
}
printf("matched symboltb not found for %s\n", sval);
return NULL;
}
int check_type_in_list(int valuetype, int truth[], int length){
int equal = 0;
for (int i = 0; i < length; i++){
if (valuetype == truth[i]) equal++;
}
if (equal == 0) {
fprintf(stderr, RED"[check_type_in_list]type conflicts `%c`(%d), should be in [", valuetype, valuetype);
for (int i = 0; i < length; i++){
if (i == length-1) fprintf(stderr, "`%c`", truth[i]);
else fprintf(stderr, "`%c`,", truth[i]);
}
fprintf(stderr, "]\n"RESET);
}
return equal;
}
int check_type_equal(int valuetype, int truth){
int trutharray[] = {truth};
return check_type_in_list(valuetype, trutharray, 1);
}
int check_current_scope(struct symboltable* global_tb, struct symboltableStack* local_tbstk, int expected_scope){
int current_scope = LOCAL_ENV ? LOCAL_SCOPE : GLOBAL_SCOPE;
if (current_scope != expected_scope) {
fprintf(stderr, RED"[check_current_scope]: scope not expected. get `%c`, should be `%c`.\n"RESET, current_scope, expected_scope);
return 0;
}
return 1;
}
// test prpose only
void mock_local_env(struct symboltable* global_tb, struct symboltableStack* local_tbstk){
struct symboltable* tb = init_symboltable(MAX_SYMBOLTABLE_SIZE, LOCAL_SCOPE);
push_symboltableStack(tb, local_tbstk);
}
// calclate NODETYPE_LHS depth
int get_node_depth(struct pNode* p){
if (p == NULL) return 0;
switch (p->pnodetype)
{
case NODETYPE_SINGLE_ID_AS_LHSITEM:
case NODETYPE_ARRAY_REF_AS_LHSITEM:
case NODETYPE_TUPLE_REF_AS_LHSITEM:
return 1;
case NODETYPE_COMMA_LHSITEN_LIST:
case NODETYPE_LHS:{
int sum = 0;
for (int i = 0; i < p->childscount; i++){
sum += get_node_depth(p->childs[i]);
}
return sum;
}
default:
return 0;
}
}
//calculate expr width
int get_expr_width(struct pNode* p, struct symboltable* global_tb, struct symboltableStack* local_tbstk){
if (p == NULL) return 0;
switch (p->pnodetype){
case NODETYPE_EXPR_COMMA_EXPR:
return get_expr_width(p->childs[0], global_tb, local_tbstk) + get_expr_width(p->childs[2], global_tb, local_tbstk);
case NODETYPE_SINGLE_INT_AS_EXPR:
case NODETYPE_TUPLE_REF_AS_EXPR:
case NODETYPE_ARRAY_REF_AS_EXPR:
case NODETYPE_EXPR_MINUS_EXPR:
case NODETYPE_EXPR_PLUS_EXPR:
case NODETYPE_EXPR_MULT_EXPR:
case NODETYPE_EXPR_DIV_EXPR:
return 1;
case NODETYPE_SINGLE_ID_AS_EXPR:{
struct sNode * snode = (struct sNode *)(p->childs[0]);
struct symboltable* matched_tb = get_matched_symboltable(snode->sval, global_tb, local_tbstk);
struct symboltableRecord* record = lookup_symbol(snode->sval, matched_tb->scope, matched_tb);
if (record->valuetype == VALUETYPE_INT) {
return 1;
} else if (record->valuetype == VALUETYPE_TUPLE) {
return record->value->ivallistlength;
}
}
default:
printf("get_expr_width with bad nodetype %d, tuple creation may get bad size\n",p->pnodetype);
return 1;
}
}
// determine type of expression: if A = expr(B), then A's type can be synthesised from right hand side
int type_synthesis(struct pNode* exprnode, struct symboltable* global_tb, struct symboltableStack* local_tbstk){
if (exprnode == NULL) return -1;
switch(exprnode->pnodetype){
case NODETYPE_EXPR_COMMA_EXPR:{
/* For expr including ',', make sure left and right are both int or tuple
*
* expr: expr OP_COMMA expr { $$ = newpNode(NODETYPE_EXPR_COMMA_EXPR, 3, $1, newplaceholderNode(OP_COMMA), $3);}
*/
int left_valuetype = type_synthesis(exprnode->childs[0], global_tb, local_tbstk);
int right_valuetype = type_synthesis(exprnode->childs[2], global_tb, local_tbstk);
int truthlist[] = {VALUETYPE_TUPLE, VALUETYPE_INT};
check_type_in_list(left_valuetype, truthlist, 2);
check_type_in_list(right_valuetype, truthlist, 2);
return VALUETYPE_TUPLE;
break;
}
case NODETYPE_EXPR_MINUS_EXPR:
case NODETYPE_EXPR_PLUS_EXPR:
case NODETYPE_EXPR_DIV_EXPR:
case NODETYPE_EXPR_MULT_EXPR:{
/* For expr including `+`,`-`,`*`,`/` make sure left and right are both int
*
* expr: expr OP_MINUS expr { $$ = newpNode(NODETYPE_EXPR_MINUS_EXPR, 3, $1, newplaceholderNode(OP_MINUS), $3);}
* | expr OP_PLUS expr { $$ = newpNode(NODETYPE_EXPR_PLUS_EXPR, 3, $1, newplaceholderNode(OP_PLUS), $3);}
* | expr OP_DIV expr { $$ = newpNode(NODETYPE_EXPR_DIV_EXPR, 3, $1, newplaceholderNode(OP_DIV), $3);}
* | expr OP_MULT expr { $$ = newpNode(NODETYPE_EXPR_MULT_EXPR, 3, $1, newplaceholderNode(OP_MULT), $3);}
*/
int left_valuetype = type_synthesis(exprnode->childs[0], global_tb, local_tbstk);
int right_valuetype = type_synthesis(exprnode->childs[2], global_tb, local_tbstk);
check_type_equal(left_valuetype, VALUETYPE_INT);
check_type_equal(right_valuetype, VALUETYPE_INT);
return VALUETYPE_INT;
break;
}
case NODETYPE_LPAR_EXPR_RPAR:{
/* Return no matter whatever is a type of the inside expression. No type check here.
*
* expr: OP_LPAR expr OP_RPAR { $$ = newpNode(NODETYPE_LPAR_EXPR_RPAR, 3, newplaceholderNode(OP_LPAR), $2, newplaceholderNode(OP_RPAR));} %prec EXPR_LPAR_RPAR_INCLUSICE
*/
int valuetype = type_synthesis(exprnode->childs[1], global_tb, local_tbstk);
return valuetype;
break;
}
case NODETYPE_SINGLE_ID_AS_EXPR:{
/* Return no matter whatever is a type of the in the . No type check here.
*
* expr: ID { $$ = newpNode(NODETYPE_SINGLE_ID_AS_EXPR, 1, newsNode($1));} %prec EXPR_NORMAL_ID
*/
struct sNode * snode = (struct sNode *)(exprnode->childs[0]);
int valuetype = type_lookup(snode->sval, global_tb, local_tbstk);
return valuetype;
break;
}
case NODETYPE_FUNC_CALL_AS_EXPR:{
/* TODO
*
* expr: ID expr { $$ = newpNode(NODETYPE_FUNC_CALL_AS_EXPR, 2, newsNode($1), $2); } %prec EXPR_FUNCTION_ID
*/
struct sNode * snode = (struct sNode *)(exprnode->childs[0]);
struct symboltableRecord* record = lookup_symbol(snode->sval, GLOBAL_SCOPE, global_tb);
if (record == NULL || record->valuetype != VALUETYPE_FUNC){
fprintf(stderr, RED"type_synthesis meet undefined function `%s`"RESET, snode->sval); //TODO
exit(999);
}
struct symboltableRecordFunction* function = (struct symboltableRecordFunction*)record->value;
int input_valuetype = type_synthesis(exprnode->childs[1], global_tb, local_tbstk);
check_type_equal(input_valuetype, function->formal_parameter_valuetype);
return function->return_valuetype;
break;
}
case NODETYPE_TUPLE_REF_AS_EXPR:{
/* For expr including `.` make sure left is a id for tuple.
*
* expr: ID OP_DOT INT_LIT { $$ = newpNode(NODETYPE_TUPLE_REF_AS_EXPR, 3, newsNode($1), newplaceholderNode(OP_DOT), newiNode($3)); } %prec EXPR_TUPLE_ID
*/
struct sNode * snode = (struct sNode *)(exprnode->childs[0]);
int valuetype = type_lookup(snode->sval, global_tb, local_tbstk);
check_type_equal(valuetype, VALUETYPE_TUPLE);
return VALUETYPE_INT;
}
case NODETYPE_ARRAY_REF_AS_EXPR:{
/* For expr including `[ ]` make sure left is a id for array. right is a int expr
*
* expr: ID OP_LBRAK expr OP_RBRAK { $$ = newpNode(NODETYPE_ARRAY_REF_AS_EXPR, 4, newsNode($1), newplaceholderNode(OP_LBRAK), $3, newplaceholderNode(OP_RBRAK)); } %prec EXPR_ARRAY_ID
*/
struct sNode * snode = (struct sNode *)(exprnode->childs[0]);
int left_valuetype = type_lookup(snode->sval, global_tb, local_tbstk);
int right_valuetype = type_synthesis(exprnode->childs[2], global_tb, local_tbstk);
check_type_equal(left_valuetype, VALUETYPE_ARRAY);
check_type_equal(right_valuetype, VALUETYPE_INT);
}
case NODETYPE_SINGLE_INT_AS_EXPR:{
/* Base case.
*
* expr: INT_LIT { $$ = newpNode(NODETYPE_SINGLE_INT_AS_EXPR, 1, newiNode($1));} %prec EXPR_INT
*/
return VALUETYPE_INT;
break;
}
default: {
printf("visit unsupported node type for type_synthesis: %d\n", exprnode->pnodetype);
if (exprnode->pnodetype == NODETYPE_PLACEHOLDER) {
printf("tok:%s\n", ((struct placeholderNode*)exprnode)->tokstr);
}
}
}
return -1;
}
// determine the lhs's type based on the way it's used.
int type_inference(struct pNode* p, struct symboltable* global_tb, struct symboltableStack* local_tbstk){
if (p == NULL) return -1;
switch (p->pnodetype)
{
case NODETYPE_SINGLE_ID_AS_LHSITEM:{
// only int, tuple, unknown are allowed as a single id in lhs.
struct sNode * snode = (struct sNode *)(p->childs[0]);
int valuetype = type_lookup(snode->sval, global_tb, local_tbstk);
int truthlist[] = {VALUETYPE_TUPLE, VALUETYPE_INT, VALUETYPE_UNKNOWN, VALUETYPE_ARRAY};
check_type_in_list(valuetype, truthlist, 4);
return valuetype;
break;
}
case NODETYPE_TUPLE_REF_AS_LHSITEM:{
// only tuple is allowed in tuple ref
struct sNode * snode = (struct sNode *)(p->childs[0]);
int valuetype = type_lookup(snode->sval, global_tb, local_tbstk);
check_type_equal(valuetype, VALUETYPE_TUPLE);
return VALUETYPE_INT;
break;
}
case NODETYPE_ARRAY_REF_AS_LHSITEM:{
// only array is allowed in array ref
struct sNode * snode = (struct sNode *)(p->childs[0]);
int valuetype = type_lookup(snode->sval, global_tb, local_tbstk);
check_type_equal(valuetype, VALUETYPE_ARRAY);
return VALUETYPE_INT;
break;
}
case NODETYPE_COMMA_LHSITEN_LIST:{
// return the type of first lhsitem as return type
struct pNode * lhsnode = p->childs[1];
struct pNode * listnode = p->childs[2];
int truthlist[] = {VALUETYPE_TUPLE, VALUETYPE_INT};
int valuetype = type_inference(lhsnode, global_tb, local_tbstk);
check_type_in_list(valuetype, truthlist, 2);
if (listnode){
type_inference(listnode, global_tb, local_tbstk);//recusively check the rest
}
return valuetype;
break;
}
case NODETYPE_LHS:{
// only check the type of first lhsitem
struct pNode * lhsitem = p->childs[0];
struct pNode * listnode = p->childs[1];
if (listnode){
type_inference(listnode, global_tb, local_tbstk);//recusively check the rest
return VALUETYPE_TUPLE;
} else {
return type_inference(lhsitem, global_tb, local_tbstk);
}
break;
}
default: {
printf("visit unsupported node type for type_inference: %d\n", p->pnodetype);
}
}
return -1;
}
// helper function for `determine_formal_parameter_valuetype`
// return if the sval has been referenced as a tuple in p
int _any_tuple_reference(struct pNode* p, char* sval){
if(p == NULL){
return 0;
}
switch (p->pnodetype)
{
case NODETYPE_TUPLE_REF_AS_EXPR:
case NODETYPE_TUPLE_REF_AS_LHSITEM:{
struct sNode * snode = (struct sNode *)(p->childs[0]);
struct iNode * inode = (struct iNode *)(p->childs[2]);
if(strcmp(snode->sval, sval)==0) return inode->ival+1;
break;
}
default:{
int tempmax = 0;
for (int i = 0; i < p->childscount; i++){
int r = _any_tuple_reference(p->childs[i], sval);
tempmax = tempmax > r ? tempmax: r;
}
return tempmax;
break;
}
}
return 0;
}
// no side effects, default is int
// if there are any tuple reference of formal_para, then formal_para is a tuple
int determine_formal_parameter_valuetype(struct pNode* p){
if (p == NULL || p->pnodetype != NODETYPE_FUNC_DEFN){
fprintf(stderr, RED"[determine_formal_parameter_valuetype] bad input node. in line %d"RESET, p->line);
exit(999);
}
struct sNode * formal_parameter_name = (struct sNode *)(p->childs[3]);
if ( _any_tuple_reference(p, formal_parameter_name->sval)){
return VALUETYPE_TUPLE;
}
return VALUETYPE_INT;
}
int determine_formal_parameter_length(struct pNode* p){
struct sNode * formal_parameter_name = (struct sNode *)(p->childs[3]);
int length = _any_tuple_reference(p, formal_parameter_name->sval);
return length;
}
// helper function for `determine_return_valuetype`.
// if no return statement, return 0.
// otherwise return the the type expr of first return statement in p.
// should be a tree structure included in non-terminal `body`.
// will only do minimun type checking
int _get_first_return_statement_type(struct pNode* p, struct symboltable* global_tb, struct symboltableStack* local_tbstk){
if (p == NULL) return 0;
if (check_current_scope(global_tb, local_tbstk, LOCAL_SCOPE) == 0){
fprintf(stderr, RED"[defn node error] in line %d"RESET, p->line);
exit(999);
}
// now the following is in a local env
// do declarations in local_tb only
// global_tb is treated as read only
switch (p->pnodetype)
{
//return nodes
case NODETYPE_RETURN_STATEMENT:{
struct pNode* exprnode = p->childs[1];
return type_synthesis(exprnode, global_tb, local_tbstk);
break;
}
//decl nodes
case NODETYPE_NO_EXPR_LOCAL_DECL:{
struct symboltable* local_tb = top_symboltableStack(local_tbstk);
struct sNode * snode = (struct sNode *)(p->childs[1]);
declare_symbol(snode->sval, VALUETYPE_UNKNOWN, LOCAL_SCOPE, snode->line, local_tb);
break;
}
case NODETYPE_LOCAL_DECL:{
// only declare local id in local env
struct symboltable* local_tb = top_symboltableStack(local_tbstk);
struct sNode * snode = (struct sNode *)(p->childs[1]);
struct pNode * exprnode = p->childs[3];
int valuetype = type_synthesis(exprnode, global_tb, local_tbstk);
int truthlist[] = {VALUETYPE_INT, VALUETYPE_TUPLE};
check_type_in_list(valuetype, truthlist, 2);
declare_symbol(snode->sval, valuetype, LOCAL_SCOPE, snode->line,local_tb);
break;
}
case NODETYPE_NO_EXPR_GLOBAL_DECL:{
/*struct sNode * snode = (struct sNode *)(p->childs[1]);
struct symboltableRecord* record = lookup_symbol(snode->sval, GLOBAL_SCOPE, global_tb);
struct symboltable* local_tb = top_symboltableStack(local_tbstk);
if (record != NULL) {
int truthlist[] = {VALUETYPE_ARRAY, VALUETYPE_INT, VALUETYPE_TUPLE};
check_type_in_list(record->valuetype, truthlist, 3);
struct symboltableRecord* local_record = declare_symbol(snode->sval, record->valuetype, LOCAL_SCOPE, snode->line, local_tb);
local_record->isGlobalLink = 0;
} else {
fprintf(stderr, RED"[defn node error] can't access unknown global id in local env. in line %d"RESET, p->line);
exit(999);
}*/
fprintf(stderr, RED"[defn node error] don't have support for global access. in line %d"RESET, p->line);
exit(999);
break;
}
case NODETYPE_GLOBAL_DECL:{
fprintf(stderr, RED"[defn node error] global decl with expr in defn is not allowed. in line %d"RESET, p->line);
exit(999);
break;
}
case NODETYPE_ARRAY_DECL:
case NODETYPE_ARRAY_DECL_WITH_ANONY_FUNC:{
//KW_ARRAY ID OP_LBRAK expr OP_DOTDOT expr OP_RBRAK OP_SEMI
struct sNode * snode = (struct sNode *)(p->childs[1]);
struct symboltable* local_tb = top_symboltableStack(local_tbstk);
struct symboltableRecord* record = lookup_symbol(snode->sval, LOCAL_SCOPE, local_tb);
if (record != NULL) {
check_type_equal(record->valuetype, VALUETYPE_UNKNOWN);
set_symbol_type(snode->sval, VALUETYPE_ARRAY, LOCAL_SCOPE, snode->line, local_tb);
} else {
fprintf(stderr, RED"[defn node error] array set error, id not found in local env%d"RESET, p->line);
exit(999);
}
break;
}
//lhs assign node
case NODETYPE_LHS_ASSIGN_EXPR_AS_STATEMENT:{
struct pNode* lhsnode = p->childs[0];
struct pNode* exprnode = p->childs[2];
int lhstype = type_inference(lhsnode, global_tb, local_tbstk);
int lhsdepth = get_node_depth(lhsnode);
int exprtype = type_synthesis(exprnode, global_tb, local_tbstk);
int truthlist[] = {VALUETYPE_INT, VALUETYPE_TUPLE};
check_type_in_list(exprtype, truthlist, 2); // only {int,tuple} are allowed assign, {array, func, unknown} forbidden
if (lhsdepth > 1) {
fprintf(stderr,RED"todo, multiple assign statement, not implemented yet\n"RESET); //TODO
exit(999);
}
// lhs has depth 1, if lhs is unknown type, update type. otherwise check equal.
if (lhsnode->childs[0]->pnodetype == NODETYPE_SINGLE_ID_AS_LHSITEM) {
struct pNode* singleid_as_lhsitem = lhsnode->childs[0];
struct sNode* snode = (struct sNode*) singleid_as_lhsitem->childs[0];
if (lhstype == VALUETYPE_UNKNOWN) {
struct symboltable* matched_tb = get_matched_symboltable(snode->sval, global_tb, local_tbstk);
if (matched_tb) {
set_symbol_type(snode->sval, exprtype, matched_tb->scope, snode->line, matched_tb);
} else{
fprintf(stderr, RED"[treewalker] assign unknown value failed. in line %d"RESET, p->line);
exit(999);
}
} else {
check_type_equal(lhstype, exprtype);
}
} else if (lhsnode->childs[0]->pnodetype == NODETYPE_ARRAY_REF_AS_LHSITEM){
;
} else if (lhsnode->childs[0]->pnodetype == NODETYPE_TUPLE_REF_AS_LHSITEM){
;
} else {
printf("lhs type, no implementation\n");
exit(666);
}
break;
}
default:
{
for (int i = 0; i < p->childscount; i++){
int first_return = _get_first_return_statement_type(p->childs[i], global_tb, local_tbstk);
if (first_return) return first_return;
}
break;
}
}
return 0;
}
// no side effects, default is void
// make a simulated run to checkout the type.
// will use the expr of first return statement to determine type
int determine_return_valuetype(struct pNode* p, int formal_paramter_valuetype, struct symboltable* global_tb){
if (p == NULL || p->pnodetype != NODETYPE_FUNC_DEFN){
fprintf(stderr, RED"[determine_return_valuetype] bad input node. in line %d"RESET, p->line);
exit(999);
}
// set up a simulated local symboltable
struct symboltableStack* simualated_local_tbstk = init_symboltableStack(MAX_SYMBOLTABLE_STACK_SIZE);
struct symboltable* tb = init_symboltable(MAX_SYMBOLTABLE_SIZE, LOCAL_SCOPE);
push_symboltableStack(tb, simualated_local_tbstk);
// declare formal_parameter in simulated local symboltable
struct sNode * formal_parameter_name = (struct sNode *)(p->childs[3]);
declare_symbol(formal_parameter_name->sval, formal_paramter_valuetype, LOCAL_SCOPE, formal_parameter_name->line, top_symboltableStack(simualated_local_tbstk));
// determine the return type here
int return_type = _get_first_return_statement_type(p, global_tb, simualated_local_tbstk);
// clean up
pop_symboltableStack(simualated_local_tbstk);
remove_symboltableStack(simualated_local_tbstk);
if (return_type == 0){
fprintf(stderr, RED"[determine_return_valuetype] a function must have at least one return statement. in line %d"RESET, p->line);
exit(999);
}
return return_type;
}
// from a exprnode (EXPR_COMMA_EXPR), read all the values into results.
void _dfs_read_values(struct pNode* exprnode, LLVMValueRef* results, int* index, struct symboltable* global_tb, struct symboltableStack* local_tbstk, LLVMBuilderRef builder, LLVMModuleRef module){
if (exprnode->pnodetype == NODETYPE_EXPR_COMMA_EXPR) {
_dfs_read_values(exprnode->childs[0], results, index, global_tb, local_tbstk, builder, module);
_dfs_read_values(exprnode->childs[2], results, index, global_tb, local_tbstk, builder, module);
} else if (exprnode->pnodetype == NODETYPE_SINGLE_INT_AS_EXPR) {
struct iNode* inode = (struct iNode*)exprnode->childs[0];
results[(*index)++] = LLVMConstInt(LLVMInt32Type(), inode->ival, 0);
} else if (exprnode->pnodetype == NODETYPE_SINGLE_ID_AS_EXPR) {
struct sNode* snode = (struct sNode*)exprnode->childs[0];
struct symboltable* matched_tb = get_matched_symboltable(snode->sval, global_tb, local_tbstk);
struct symboltableRecord* record = lookup_symbol(snode->sval, matched_tb->scope, matched_tb);
if (record->valuetype == VALUETYPE_INT) {
results[(*index)++] = boris_codegen_expr(exprnode, builder, module, global_tb, local_tbstk);
} else if (record->valuetype == VALUETYPE_TUPLE) {
int width = record->value->ivallistlength;
LLVMValueRef tuple_address = record->value->address;
for (int i = 0; i < width; i++) {
LLVMValueRef offset = LLVMConstInt(LLVMInt32Type(), i, 0);
LLVMValueRef indices[] = { offset };
LLVMValueRef element_address = LLVMBuildGEP(builder, tuple_address, indices, 1, "");
results[(*index)++] = LLVMBuildLoad(builder, element_address, "");
}
}
}
}
void read_all_values_in_expr(struct pNode* exprnode, LLVMValueRef* results, struct symboltable* global_tb, struct symboltableStack* local_tbstk, LLVMBuilderRef builder, LLVMModuleRef module){
int index = 0;
_dfs_read_values(exprnode, results, &index, global_tb, local_tbstk, builder, module);
}
//populate the results into a array address
void populate_into_address(LLVMValueRef results[], int width, LLVMValueRef address, LLVMBuilderRef builder){
for (int i = 0; i < width; i++){
LLVMValueRef offset = LLVMConstInt(LLVMInt32Type(), i, 0); // array access should consider its start point
LLVMValueRef indices[] = { offset };
LLVMValueRef element_address = LLVMBuildGEP(builder, address, indices, 1, "");
LLVMValueRef expr_code = results[i];
LLVMBuildStore(builder, expr_code, element_address);
}
}
//global_temp_string_count
//used in treewalker, to generate a temp string
int global_temp_string_count = 0;
void get_temp_string(char* temp_string){
sprintf(temp_string, "_temp_id_%d", global_temp_string_count++);
}
char current_funcname_buffer[MAX_ID_LENGTH+1];
char* get_current_funcname(){
return current_funcname_buffer;
}
void set_current_funcname(char* funcname){
strcpy(current_funcname_buffer, funcname);
}
void treewalker(struct pNode* p, struct symboltable* global_tb, struct symboltableStack* local_tbstk, \
LLVMBuilderRef builder, LLVMModuleRef module){
if (p == NULL) return;
switch(p->pnodetype){
case NODETYPE_ROOT_INPUT:{
printf("> Start walking on parse tree:\n");
set_current_funcname("main");
treewalker(p->childs[0], global_tb, local_tbstk, builder, module);
break;
}
//nodes in `input``
case NODETYPE_STATEMENT_LIST: //1046
case NODETYPE_STATEMENT_AS_SDD: //1069
case NODETYPE_DECL_AS_SDD: //1070
case NODETYPE_DEFN_AS_SDD: //1071
case NODETYPE_SDD_LIST: //1072
//leaf - keyword place holder
case NODETYPE_PLACEHOLDER: //1026
//#function calls
case NODETYPE_STATEMENT_AS_SD: //1063
case NODETYPE_DECL_AS_SD: //1064
case NODETYPE_SD_LIST: //1065
case NODETYPE_BODY: //1066
{
for (int i = 0; i < p->childscount; i++)
treewalker(p->childs[i], global_tb, local_tbstk, builder, module);
break;
}
case NODETYPE_WHILE_STATEMENT: { //1047
struct pNode* boolnode = p->childs[1];
struct pNode* stmtsnode = p->childs[3];