forked from Uyouii/cCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Praser.cpp
1649 lines (1342 loc) · 50.6 KB
/
Praser.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
#include"Praser.h"
#include"block.h"
#include "tools.h"
#include<map>
using namespace std;
Praser::Praser(gramTree* root) {
this->root = root;
praserInit();
}
Praser::~Praser() {
//print_map();
print_code();
}
void Praser::praserInit() {
Block wholeBlock;
blockStack.push_back(wholeBlock);
//事先内置函数print和read
funcNode writeNode;
writeNode.name = "print";
writeNode.rtype = "void";
varNode pnode;
pnode.type = "int";
writeNode.paralist.push_back(pnode);
funcPool.insert({ "print", writeNode });
funcNode readNode;
readNode.name = "read";
readNode.rtype = "int";
funcPool.insert({"read",readNode});
praserGramTree(root); //开始分析语法树
}
void Praser::praserGramTree(struct gramTree* node) {
//cout << "at " << node->name << endl;
if (node == NULL || node->line == -1)
return;
if (node->name == "declaration") {
node = praser_declaration(node);
}
else if (node->name == "function_definition") {
node = praser_function_definition(node);
}
else if (node->name == "statement") {
node = praser_statement(node);
}
//继续向下分析
if (node != NULL) {
praserGramTree(node->left);
praserGramTree(node->right);
}
}
struct gramTree* Praser::praser_statement(struct gramTree* node) {
struct gramTree* next = node->left;
if (node->left->name == "labeled_statement") {
}
if (node->left->name == "compound_statement") {
praser_compound_statement(node->left);
}
if (node->left->name == "expression_statement") {
praser_expression_statement(node->left);
}
if (node->left->name == "selection_statement") {
praser_selection_statement(node->left);
}
if (node->left->name == "iteration_statement") {
praser_iteration_statement(node->left);
}
if (node->left->name == "jump_statement") {
praser_jump_statement(node->left);
}
return node->right;
}
void Praser::praser_jump_statement(struct gramTree* node) {
if (node->left->name == "GOTO") {
}
else if (node->left->name == "CONTINUE") {
}
else if (node->left->name == "BREAK") {
int num = getBreakBlockNumber();
if (num < 0) {
error(node->left->line, "This scope doesn't support break.");
}
innerCode.addCode("GOTO " + blockStack[num].breakLabelname);
}
else if (node->left->name == "RETURN") {
string funcType = getFuncRType();
if (node->left->right->name == "expression") {//return expression
varNode rnode = praser_expression(node->left->right);
innerCode.addCode(innerCode.createCodeforReturn(rnode));
if (rnode.type != funcType) {
error(node->left->right->line, "return type doesn't equal to function return type.");
}
}
else if (node->left->right->name == ";"){//return ;
innerCode.addCode("RETURN");
if (funcType != "void") {
error(node->left->right->line, "You should return " + blockStack.back().func.rtype);
}
}
}
}
void Praser::praser_expression_statement(struct gramTree *node) {
if (node->left->name == "expression") {
praser_expression(node->left);
}
}
varNode Praser::praser_expression(struct gramTree* node) {
if (node->left->name == "expression") {
return praser_expression(node->left);
}
else if (node->left->name == "assignment_expression") {
return praser_assignment_expression(node->left);
}
if (node->right->name == ",") {
return praser_assignment_expression(node->right->right);
}
}
void Praser::praser_compound_statement(struct gramTree* node) {
//继续分析处理compound_statement
praserGramTree(node);
}
//if else
void Praser::praser_selection_statement(struct gramTree* node) {
if (node->left->name == "IF") {
if (node->left->right->right->right->right->right == NULL) {
//添加一个新的block
Block newblock;
blockStack.push_back(newblock);
gramTree* expression = node->left->right->right;
varNode exp_rnode = praser_expression(expression);
gramTree* statement = node->left->right->right->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
if (exp_rnode.type == "bool") {
innerCode.addCode("IF " + exp_rnode.boolString + " GOTO " + label1);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(exp_rnode) + " != " + tempzeroname + " GOTO " + label1);
}
innerCode.addCode("GOTO " + label2);
innerCode.addCode("LABEL " + label1 + " :");
praser_statement(statement);
innerCode.addCode("LABEL " + label2 + " :");
//弹出添加的block
blockStack.pop_back();
}
else if (node->left->right->right->right->right->right->name == "ELSE") {
//添加一个新的block
Block newblock1;
blockStack.push_back(newblock1);
gramTree* expression = node->left->right->right;
varNode exp_rnode = praser_expression(expression);
gramTree* statement1 = node->left->right->right->right->right;
gramTree* statement2 = node->left->right->right->right->right->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
string label3 = innerCode.getLabelName();
if (exp_rnode.type == "bool") {
innerCode.addCode("IF " + exp_rnode.boolString + " GOTO " + label1);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(exp_rnode) + " != " + tempzeroname + " GOTO " + label1);
}
innerCode.addCode("GOTO " + label2);
innerCode.addCode("LABEL " + label1 + " :");
praser_statement(statement1);
innerCode.addCode("GOTO " + label3);
//弹出添加的block
blockStack.pop_back();
//else
innerCode.addCode("LABEL " + label2 + " :");
Block newblock2;
blockStack.push_back(newblock2);
praser_statement(statement2);
innerCode.addCode("LABEL " + label3 + " :");
//弹出添加的block
blockStack.pop_back();
}
}
else if (node->left->name == "SWITCH") {
}
}
//循环 while for do while
void Praser::praser_iteration_statement(struct gramTree* node) {
if (node->left->name == "WHILE") {
//添加一个新的block
Block newblock;
newblock.canBreak = true;
blockStack.push_back(newblock);
struct gramTree* expression = node->left->right->right;
struct gramTree* statement = node->left->right->right->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
string label3 = innerCode.getLabelName();
blockStack.back().breakLabelname = label3;
innerCode.addCode("LABEL " + label1 + " :");
varNode var = praser_expression(expression);
if (var.type == "bool") {
innerCode.addCode("IF " + var.boolString + " GOTO " + label2);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(var) + " != " + tempzeroname + " GOTO " + label2);
}
innerCode.addCode("GOTO " + label3);
innerCode.addCode("LABEL " + label2 + " :");
praser_statement(statement);
innerCode.addCode("GOTO " + label1);
innerCode.addCode("LABEL " + label3 + " :");
//弹出添加的block
blockStack.pop_back();
}
else if (node->left->name == "DO") {
//添加一个新的block
Block newblock;
newblock.canBreak = true;
blockStack.push_back(newblock);
struct gramTree* statement = node->left->right;
struct gramTree* expression = node->left->right->right->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
blockStack.back().breakLabelname = label2;
innerCode.addCode("LABEL " + label1 + " :");
praser_statement(statement);
varNode var = praser_expression(expression);
if (var.type == "bool") {
innerCode.addCode("IF " + var.boolString + " GOTO " + label1);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(var) + " != " + tempzeroname + " GOTO " + label1);
}
/*innerCode.addCode("GOTO " + label1);*/
innerCode.addCode("LABEL " + label2 + " :");
//弹出添加的block
blockStack.pop_back();
}
else if (node->left->name == "FOR") {
if (node->left->right->right->name == "expression_statement") {
//FOR '(' expression_statement expression_statement ')'statement
if (node->left->right->right->right->right->name == ")") {
//添加一个新的block
Block newblock;
newblock.canBreak = true;
blockStack.push_back(newblock);
gramTree* exp_state1 = node->left->right->right;
gramTree* exp_state2 = exp_state1->right;
gramTree* statement = exp_state2->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
string label3 = innerCode.getLabelName();
blockStack.back().breakLabelname = label3;
if (exp_state1->left->name == "expression") {
praser_expression(exp_state1->left);
}
innerCode.addCode("LABEL " + label1 + " :");
varNode var;
if (exp_state2->left->name == "expression") {
var = praser_expression(exp_state2->left);
if (var.type == "bool") {
innerCode.addCode("IF " + var.boolString + " GOTO " + label2);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(var) + " != " + tempzeroname + " GOTO " + label2);
}
}
else {
innerCode.addCode("GOTO " + label2);
}
innerCode.addCode("GOTO " + label3);
innerCode.addCode("LABEL " + label2 + " :");
praser_statement(statement);
innerCode.addCode("GOTO " + label1);
innerCode.addCode("LABEL " + label3 + " :");
////如果需要break
//if (blockStack.back().breakLabelNum > 0) {
// innerCode.addCode("LABEL label" + inttostr(blockStack.back().breakLabelNum) + " :");
//}
//弹出添加的block
blockStack.pop_back();
}
//FOR ( expression_statement expression_statement expression ) statement
else if (node->left->right->right->right->right->name == "expression") {
//添加一个新的block
Block newblock;
newblock.canBreak = true;
blockStack.push_back(newblock);
gramTree* exp_state1 = node->left->right->right;
gramTree* exp_state2 = exp_state1->right;
gramTree* exp = exp_state2->right;
gramTree* statement = exp->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
string label3 = innerCode.getLabelName();
blockStack.back().breakLabelname = label3;
if (exp_state1->left->name == "expression") {
praser_expression(exp_state1->left);
}
innerCode.addCode("LABEL " + label1 + " :");
varNode var;
if (exp_state2->left->name == "expression") {
var = praser_expression(exp_state2->left);
if (var.type == "bool") {
innerCode.addCode("IF " + var.boolString + " GOTO " + label2);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(var) + " != " + tempzeroname + " GOTO " + label2);
}
}
else {
innerCode.addCode("GOTO " + label2);
}
innerCode.addCode("GOTO " + label3);
innerCode.addCode("LABEL " + label2 + " :");
praser_statement(statement);
praser_expression(exp);
innerCode.addCode("GOTO " + label1);
innerCode.addCode("LABEL " + label3 + " :");
////如果需要break
//if (blockStack.back().breakLabelNum > 0) {
// innerCode.addCode("LABEL label" + inttostr(blockStack.back().breakLabelNum) + " :");
//}
//弹出添加的block
blockStack.pop_back();
}
}
if (node->left->right->right->name == "declaration") {
//FOR '(' declaration expression_statement ')' statement
if (node->left->right->right->right->right->name == ")") {
//添加一个新的block
Block newblock;
newblock.canBreak = true;
blockStack.push_back(newblock);
gramTree *declaration = node->left->right->right;
gramTree *expression_statement = declaration->right;
gramTree *statement = expression_statement->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
string label3 = innerCode.getLabelName();
blockStack.back().breakLabelname = label3;
praser_declaration(declaration);
innerCode.addCode("LABEL " + label1 + " :");
varNode var;
if (expression_statement->left->name == "expression") {
var = praser_expression(expression_statement->left);
if (var.type == "bool") {
innerCode.addCode("IF " + var.boolString + " GOTO " + label2);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(var) + " != " + tempzeroname + " GOTO " + label2);
}
}
else {
innerCode.addCode("GOTO " + label2);
}
innerCode.addCode("GOTO " + label3);
innerCode.addCode("LABEL " + label2 + " :");
praser_statement(statement);
//cout << "here" << endl;
innerCode.addCode("GOTO " + label1);
innerCode.addCode("LABEL " + label3 + " :");
////如果需要break
//if (blockStack.back().breakLabelNum > 0) {
// innerCode.addCode("LABEL label" + inttostr(blockStack.back().breakLabelNum) + " :");
//}
//弹出添加的block
blockStack.pop_back();
}
//FOR ( declaration expression_statement expression ) statement
else if (node->left->right->right->right->right->name == "expression") {
//添加一个新的block
Block newblock;
newblock.canBreak = true;
blockStack.push_back(newblock);
gramTree *declaration = node->left->right->right;
gramTree *expression_statement = declaration->right;
gramTree *expression = expression_statement->right;
gramTree *statement = expression->right->right;
string label1 = innerCode.getLabelName();
string label2 = innerCode.getLabelName();
string label3 = innerCode.getLabelName();
blockStack.back().breakLabelname = label3;
praser_declaration(declaration);
innerCode.addCode("LABEL " + label1 + " :");
varNode var;
if (expression_statement->left->name == "expression") {
var = praser_expression(expression_statement->left);
if (var.type == "bool") {
innerCode.addCode("IF " + var.boolString + " GOTO " + label2);
}
else {
string tempzeroname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newznode = createTempVar(tempzeroname, "int");
innerCode.addCode(tempzeroname + " := #0");
innerCode.addCode("IF " + innerCode.getNodeName(var) + " != " + tempzeroname + " GOTO " + label2);
}
}
else {
innerCode.addCode("GOTO " + label2);
}
innerCode.addCode("GOTO " + label3);
innerCode.addCode("LABEL " + label2 + " :");
praser_statement(statement);
praser_expression(expression);
//cout << "here" << endl;
innerCode.addCode("GOTO " + label1);
innerCode.addCode("LABEL " + label3 + " :");
////如果需要break
//if (blockStack.back().breakLabelNum > 0) {
// innerCode.addCode("LABEL label" + inttostr(blockStack.back().breakLabelNum) + " :");
//}
//弹出添加的block
blockStack.pop_back();
}
}
}
}
//函数定义
struct gramTree* Praser::praser_function_definition(struct gramTree* node) {
gramTree* type_specifier = node->left;
gramTree* declarator = node->left->right;
gramTree* compound_statement = declarator->right;
string funcType = type_specifier->left->content;
string funcName = declarator->left->left->content;
/*if (build_in_function.find(funcName) != build_in_function.end()) {
error(declarator->left->left->line, "Function name can't be bulid in function.");
}*/
bool isdeclared = false;
funcNode declarFunc;
if (funcPool.find(funcName) != funcPool.end()) {
//函数重复定义
if (funcPool[funcName].isdefinied) {
error(declarator->left->left->line, "Function " + funcName + " is duplicated definition.");
}
//函数事先声明过但是没有定义
else {
isdeclared = true;
//先删除掉函数池中的函数的声明
declarFunc = funcPool[funcName];
funcPool.erase(funcPool.find(funcName));
}
}
//进入新的block
Block funBlock;
funBlock.isfunc = true;
funBlock.func.name = funcName;
funBlock.func.rtype = funcType;
funBlock.func.isdefinied = true;
//将函数记录在块内并添加到函数池
blockStack.push_back(funBlock);
funcPool.insert({funcName,funBlock.func});
innerCode.addCode("FUNCTION " + funcName + " :");
//获取函数形参列表
if(declarator->left->right->right->name == "parameter_list")
praser_parameter_list(declarator->left->right->right, funcName,true);
//此时函数池中的func已经添加了参数列表
funcNode func = funcPool[funcName];
//如果函数事先声明过,则比较函数的参数列表和返回类型
if (isdeclared) {
if (func.rtype != declarFunc.rtype) {
error(type_specifier->left->line, "Function return type doesn't equal to the function declared before.");
}
cout << funBlock.func.paralist.size() << endl;
if (func.paralist.size() != declarFunc.paralist.size()) {
error(declarator->left->right->right->line, "The number of function parameters doesn't equal to the function declared before.");
}
for (int i = 0; i < funBlock.func.paralist.size(); i++) {
if (func.paralist[i].type != declarFunc.paralist[i].type)
error(declarator->left->right->right->line, "The parameter " + funBlock.func.paralist[i].name + "'s type doesn't equal to the function declared before." );
}
}
//更新Block中func的参数列表
funBlock.func = func;
//分析函数的正文
praser_compound_statement(compound_statement);
//函数结束后,弹出相应的block
blockStack.pop_back();
return node->right;
}
//获取函数形参列表,函数定义需要获取形参,声明则不需要
void Praser::praser_parameter_list(struct gramTree* node,string funcName,bool definite) {
if (node->left->name == "parameter_list") {
praser_parameter_list(node->left, funcName,definite);
}
else if (node->left->name == "parameter_declaration") {
praser_parameter_declaration(node->left,funcName,definite);
}
if (node->right->name == ",") {
praser_parameter_declaration(node->right->right, funcName,definite);
}
}
//获取单个形参内容,函数定义需要获取形参,声明则不需要
void Praser::praser_parameter_declaration(struct gramTree* node, string funcName,bool definite) {
//cout << "praser_parameter_declaration" << endl;
gramTree* type_specifier = node->left;
gramTree* declarator = node->left->right;
string typeName = type_specifier->left->content;
if (typeName == "void") {
error(type_specifier->line, "Void can't definite parameter.");
}
//================================================
//暂时只考虑变量,不考虑数组作为形参
string varName = declarator->left->content;
varNode newnode;
newnode.name = varName;
newnode.type = typeName;
if (definite) {
newnode.num = innerCode.varNum++;
blockStack.back().func.paralist.push_back(newnode);
}
funcPool[funcName].paralist.push_back(newnode);
//将函数的形参添加到当前块的变量池中
blockStack.back().varMap.insert({varName,newnode});
if(definite)
innerCode.addCode(innerCode.createCodeforParameter(newnode));
}
struct gramTree* Praser::praser_declaration(struct gramTree *node) {
//cout << "at " << node->name << endl;
//node = declaration
struct gramTree* begin = node->left; //begin:type_specifier
if (begin->right->name == ";")
return node->right;
string vartype = begin->left->content;
if (vartype == "void") {
error(begin->left->line,"void type can't assign to variable"); //报错
}
struct gramTree* decl = begin->right; //init_declarator_list
/*while (decl->right) {
praser_init_declarator(vartype, decl->right->right);
decl = decl->left;
}
praser_init_declarator(vartype, decl);*/
praser_init_declarator_list(vartype, decl);
return node->right;
}
void Praser::praser_init_declarator_list(string vartype, struct gramTree* node) {
if (node->left->name == "init_declarator_list") {
praser_init_declarator_list(vartype, node->left);
}
else if (node->left->name == "init_declarator") {
praser_init_declarator(vartype, node->left);
}
if (node->right->name == ",") {
praser_init_declarator(vartype, node->right->right);
}
}
//分析变量初始化
void Praser::praser_init_declarator(string vartype, struct gramTree* node) {
//cout << "at " << node->name << endl;
struct gramTree* declarator = node->left;
if (!declarator->right) {
//获取变量的名字
if (declarator->left->name == "IDENTIFIER") {
struct gramTree* id = declarator->left;
string var = id->content;
if (!lookupCurruntVar(var)) {
varNode newvar;
newvar.name = var;
newvar.type = vartype;
newvar.num = innerCode.varNum++;
blockStack.back().varMap.insert({ var,newvar });
}
else error(declarator->left->line, "Variable multiple declaration.");
}
else {
//函数声明
if (declarator->left->right->name == "(") {
string funcName = declarator->left->left->content;
string funcType = vartype;
if (blockStack.size() > 1) {
error(declarator->left->right->line, "Functinon declaration must at global environment.");
}
gramTree* parameter_list = declarator->left->right->right;
funcNode newFunc;
newFunc.isdefinied = false;
newFunc.name = funcName;
newFunc.rtype = funcType;
funcPool.insert({ funcName,newFunc });
//分析函数形参列表
praser_parameter_list(parameter_list,funcName,false);
}
//数组声明
else if (declarator->left->right->name == "[") {
string arrayName = declarator->left->left->content;
string arrayType = vartype;
gramTree* assign_exp = declarator->left->right->right;
varNode rnode = praser_assignment_expression(assign_exp);
if (rnode.type != "int") {
error(declarator->left->right->line,"Array size must be int.");
}
varNode tnode;
if (arrayType == "int") {
//创建一个新的临时变量来储存数组的大小
string tempname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
tnode = createTempVar(tempname, "int");
blockStack.back().varMap.insert({ tempname,tnode });
varNode tempVar3;
string tempName3 = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
tempVar3.name = tempName3;
tempVar3.type = "int";
blockStack.back().varMap.insert({ tempName3,tempVar3 });
innerCode.addCode(tempName3 + " := #4");
innerCode.addCode(tnode.name + " := " + tempName3 +" * " + rnode.name);
}
else if (arrayType == "double") {
//创建一个新的临时变量来储存数组的大小
string tempname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
tnode = createTempVar(tempname, "int");
blockStack.back().varMap.insert({ tempname,tnode });
varNode tempVar3;
string tempName3 = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
tempVar3.name = tempName3;
tempVar3.type = "int";
blockStack.back().varMap.insert({ tempName3,tempVar3 });
innerCode.addCode(tempName3 + " := #8");
innerCode.addCode(tnode.name + " := " + tempName3 + " * " + rnode.name);
}
else if (arrayType == "bool") {
tnode = rnode;
}
arrayNode anode;
anode.name = arrayName;
anode.type = arrayType;
anode.num = innerCode.arrayNum++;
innerCode.addCode("DEC " + innerCode.getarrayNodeName(anode) + " " + tnode.name);
blockStack.back().arrayMap.insert({ arrayName,anode });
}
}
}
//有初始化
else if (declarator->right->name == "=") {
//获取变量的名字
varNode newvar;
if (declarator->left->name == "IDENTIFIER") {
struct gramTree* id = declarator->left;
string var = id->content;
if (!lookupCurruntVar(var)) {
newvar.name = var;
newvar.type = vartype;
newvar.num = innerCode.varNum++;
blockStack.back().varMap.insert({ var,newvar });
}
else error(declarator->left->line, "Variable multiple declaration.");
}
else error(declarator->left->line, "It's not a variable!");
gramTree* initializer = declarator->right->right;
if (initializer == NULL) {
error(declarator->line, "Lack the initializer for variable.");
}
else {
if (initializer->left->name == "assignment_expression") {
varNode rnode = praser_assignment_expression(initializer->left);
innerCode.addCode(innerCode.createCodeforAssign(newvar,rnode));
string rtype = rnode.type;
if (rtype != vartype)
error(initializer->left->line, "Wrong type to variable " + declarator->left->content + ": " +
rtype + " to " + vartype);
}
}
}
else error(declarator->right->line, "Wrong value to variable");
}
varNode Praser::praser_assignment_expression(struct gramTree* assign_exp) { //返回变量节点
//cout << "praser_assignment_expression" << endl;
if (assign_exp->left->name == "logical_or_expression") {
struct gramTree* logical_or_exp = assign_exp->left;
return praser_logical_or_expression(logical_or_exp);
}
//赋值运算
else if(assign_exp->left->name == "unary_expression"){
struct gramTree* unary_exp = assign_exp->left;
string op = assign_exp->left->right->left->name;
struct gramTree* next_assign_exp = assign_exp->left->right->right;
varNode node1 = praser_unary_expression(unary_exp);
varNode node2 = praser_assignment_expression(next_assign_exp);
varNode node3;
if (op == "=") {
node3 = node2;
}
else {
string tempname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
node3 = createTempVar(tempname, node1.type);
blockStack.back().varMap.insert({ tempname,node3 });
if (op == "MUL_ASSIGN") { //*=
if (node1.type != node2.type) {
error(assign_exp->left->line, "Different type for two variables.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "*", node1, node2));
}
else if (op == "DIV_ASSIGN") { //*=
if (node1.type != node2.type) {
error(assign_exp->left->line, "Different type for two variables.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "/", node1, node2));
}
else if (op == "MOD_ASSIGN") { //*=
if (node1.type != "int" || node2.type != "int") {
error(assign_exp->left->line, "The two variables must be int.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "%", node1, node2));
}
else if (op == "ADD_ASSIGN") { //*=
if (node1.type != node2.type) {
error(assign_exp->left->line, "Different type for two variables.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "+", node1, node2));
}
else if (op == "SUB_ASSIGN") { //*=
if (node1.type != node2.type) {
error(assign_exp->left->line, "Different type for two variables.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "-", node1, node2));
}
else if (op == "LEFT_ASSIGN") { //*=
if (node1.type != "int" || node2.type != "int") {
error(assign_exp->left->line, "The two variables must be int.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "<<", node1, node2));
}
else if (op == "RIGHT_ASSIGN") { //*=
if (node1.type != "int" || node2.type != "int") {
error(assign_exp->left->line, "The two variables must be int.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, ">>", node1, node2));
}
else if (op == "AND_ASSIGN") { //*=
if (node1.type != "int" || node2.type != "int") {
error(assign_exp->left->line, "The two variables must be int.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "&", node1, node2));
}
else if (op == "XOR_ASSIGN") { //*=
if (node1.type != "int" || node2.type != "int") {
error(assign_exp->left->line, "The two variables must be int.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "^", node1, node2));
}
else if (op == "OR_ASSIGN") { //*=
if (node1.type != "int" || node2.type != "int") {
error(assign_exp->left->line, "The two variables must be int.");
}
innerCode.addCode(innerCode.createCodeforVar(tempname, "|", node1, node2));
}
}
innerCode.addCode(innerCode.createCodeforAssign(node1, node3));
return node1;
}
}
varNode Praser::praser_logical_or_expression(struct gramTree* logical_or_exp) {
if(logical_or_exp->left->name == "logical_and_expression"){
struct gramTree* logical_and_exp = logical_or_exp->left;
return praser_logical_and_expression(logical_and_exp);
}
else if (logical_or_exp->left->name == "logical_or_expression") {
//logical_or_expression -> logical_or_expression OR_OP logical_and_expression
varNode node1 = praser_logical_or_expression(logical_or_exp->left);
varNode node2 = praser_logical_and_expression(logical_or_exp->left->right->right);
if (node1.type != "bool" || node2.type != "bool") {
error(logical_or_exp->left->right->line, "Logical Or operation should only used to bool. ");
}
string tempname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newnode = createTempVar(tempname, node1.type);
blockStack.back().varMap.insert({ tempname,newnode });
innerCode.addCode(innerCode.createCodeforVar(tempname, "||", node1, node2));
newnode.boolString = innerCode.getNodeName(node1) + " || " + innerCode.getNodeName(node2);
return newnode;
}
}
varNode Praser::praser_logical_and_expression(struct gramTree* logical_and_exp) {
if (logical_and_exp->left->name == "inclusive_or_expression") {
gramTree* inclusive_or_exp = logical_and_exp->left;
return praser_inclusive_or_expression(inclusive_or_exp);
}
else if (logical_and_exp->left->name == "logical_and_expression") {
varNode node1 = praser_logical_and_expression(logical_and_exp->left);
varNode node2 = praser_inclusive_or_expression(logical_and_exp->left->right->right);
if (node1.type != "bool" || node2.type != "bool") {
error(logical_and_exp->left->right->line, "Logical And operation should only used to bool. ");
}
string tempname = "temp" + inttostr(innerCode.tempNum);
++innerCode.tempNum;
varNode newnode = createTempVar(tempname, node1.type);
blockStack.back().varMap.insert({ tempname,newnode });
innerCode.addCode(innerCode.createCodeforVar(tempname, "&&", node1, node2));