-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogramAst.java
1400 lines (1257 loc) · 48.9 KB
/
programAst.java
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
import java.util.*;
//expr ->
// assign_expr
class exprAst extends Ast {
assign_exprAst statement;
public exprAst(assign_exprAst statement) {
this.statement = statement;
}
public String generate(int level) {
String res;
res = statement.generate(level);
res = "void";
return res;
}
}
// assign_expr -> l_expr '=' operator_expr
class assign_exprAst extends Ast {
String l_expr;
operator_exprAst expr_back;
public assign_exprAst(String l_expr, operator_exprAst expr_back) {
this.l_expr = l_expr;
this.expr_back = expr_back;
}
public String generate(int level) {
Order loadOrder;
Function currentFunction = Functionarrary.getFunctionTable().getCurrentFuction();
String res0 = "void", res1 = "void", res2 = "void";
if (currentFunction.is_variable(l_expr)) {
if (currentFunction.is_const(l_expr, level))
System.exit(1);
int index = currentFunction.get_index_variables(l_expr, level);
if(index == -1)
System.exit(1);
loadOrder = new Order("loca", level);
loadOrder.addOper((long) index);
Variable current_var = currentFunction.get_variable(l_expr, level);
if(current_var == null)
System.exit(1);
res1 = currentFunction.get_variable(l_expr, level).get_type();
}
else if(currentFunction.is_parameters(l_expr)){
if (currentFunction.is_parameter_const(l_expr))
System.exit(1);
int index = currentFunction.get_index_parameter(l_expr);
if(index == -1)
System.exit(1);
loadOrder = new Order("arga", level);
loadOrder.addOper((long) index);
Variable current_var = currentFunction.get_parameter(l_expr);
if(current_var == null)
System.exit(1);
res1 = currentFunction.get_parameter(l_expr).get_type();
}
else{
if (!startcode.getStartCodeTable().is_variable(l_expr))
System.exit(1);
if (startcode.getStartCodeTable().is_const(l_expr))
System.exit(1);
int index = startcode.getStartCodeTable().get_index(l_expr);
loadOrder = new Order("globa", level);
loadOrder.addOper((long) index);
res1 = startcode.getStartCodeTable().get_variable(l_expr).get_type();
}
Functionarrary.getFunctionTable().getCurrentFuction().addorders(loadOrder);
res2 = expr_back.generate(level);
if (!res1.equals(res2))
System.exit(1);
res0 = res1;
Order Store = new Order("store.64", level);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(Store);
return "void";
}
}
// binary_operator -> '+' | '-'
// operator_expr -> multiplicative_expr (binary_operator multiplicative_expr)*
class operator_exprAst extends Ast {
multiplicative_exprAst multiplicativeExpressionL;
ArrayList<String> additiveOperator = new ArrayList<String>();
ArrayList<multiplicative_exprAst> operator_exprnR = new ArrayList<>();
public operator_exprAst(multiplicative_exprAst multiplicativeExpressionL, ArrayList<String> additiveOperator,
ArrayList<multiplicative_exprAst> operator_exprnR) {
this.multiplicativeExpressionL = multiplicativeExpressionL;
this.additiveOperator = additiveOperator;
this.operator_exprnR = operator_exprnR;
}
public operator_exprAst(multiplicative_exprAst multiplicativeExpressionL) {
this.multiplicativeExpressionL = multiplicativeExpressionL;
}
public String generate(int level) {
String res1 = "void";
String res2 = "void";
String res3 = "void";
res2 = multiplicativeExpressionL.generate(level);
int len = additiveOperator.size();
if(len == 0)
return res2;
else{
for(int i=0;i<len;i++)
{
res3 = operator_exprnR.get(i).generate(level);
res1 = res2;
Order order = new Order();
String addOperator = additiveOperator.get(i);
if (!res2.equals(res3)) {
System.exit(1);
}
if (res2.equals("int") && addOperator.equals("+")) {
order.setOpcode("add.i");
order.setlevel(level);
} else if (res2.equals("int") && addOperator.equals("-")) {
order.setOpcode("sub.i");
order.setlevel(level);
} else if (res2.equals("double") && addOperator.equals("+")) {
order.setOpcode("add.f");
order.setlevel(level);
} else if (res2.equals("double") && addOperator.equals("-")) {
order.setOpcode("sub.f");
order.setlevel(level);
}
if (level == 0)// GLOBAL
startcode.getStartCodeTable().orders.add(order);
else// LOCAL
Functionarrary.getFunctionTable().getCurrentFuction().addorders(order);
}
}
return res1;
}
}
// relational-operator -> '==' | '!=' | '<' | '>' | '<=' | '>='
// condition ->
// operator_expr[relational-operator operator_expr]
class conditionAst extends Ast {
operator_exprAst expr_front;
String binary_operator;
operator_exprAst expr_back;
public conditionAst(operator_exprAst expr_front, String binary_operator, operator_exprAst expr_back) {
this.expr_front = expr_front;
this.expr_back = expr_back;
this.binary_operator = binary_operator;
}
public conditionAst(operator_exprAst expr_front) {
this.expr_front = expr_front;
}
public String generate(int level) {
String res0 = "void", res1 = "void";
String res2 = "void";
String res3 = "void";
res2 = expr_front.generate(level);
if (expr_back == null) {
return "boolean";
}
res3 = expr_back.generate(level);
if (!((res2.equals(res3)
|| (res2.equals("int") && res3.equals("char")))
|| (res2.equals("char") && res3.equals("int"))))
System.exit(1);
res1 = res2;
ArrayList<Order> Calculation = new ArrayList<>();
if (res1.equals("int") && binary_operator.equals("==")) {
Order cmp = new Order("cmp.i", level);
Order not = new Order("not", level);
Calculation.add(cmp);
Calculation.add(not);
res0 = "int";
} else if (res1.equals("int") && binary_operator.equals("!=")) {
Order cmp = new Order("cmp.i", level);
Calculation.add(cmp);
res0 = "int";
} else if (res1.equals("int") && binary_operator.equals("<")) {
Order cmp = new Order("cmp.i", level);
Order setlt = new Order("set.lt", level);
Calculation.add(cmp);
Calculation.add(setlt);
res0 = "int";
} else if (res1.equals("int") && binary_operator.equals(">")) {
Order cmp = new Order("cmp.i", level);
Order setgt = new Order("set.gt", level);
Calculation.add(cmp);
Calculation.add(setgt);
res0 = "int";
} else if (res1.equals("int") && binary_operator.equals("<=")) {
Order cmp = new Order("cmp.i", level);
Order setgt = new Order("set.gt", level);
Order not = new Order("not", level);
Calculation.add(cmp);
Calculation.add(setgt);
Calculation.add(not);
res0 = "int";
} else if (res1.equals("int") && binary_operator.equals(">=")) {
Order cmp = new Order("cmp.i", level);
Order setlt = new Order("set.lt", level);
Order not = new Order("not", level);
Calculation.add(cmp);
Calculation.add(setlt);
Calculation.add(not);
res0 = "int";
} else if (res1.equals("double") && binary_operator.equals("==")) {
Order cmp = new Order("cmp.f", level);
Order not = new Order("not", level);
Calculation.add(cmp);
Calculation.add(not);
res0 = "int";
} else if (res1.equals("double") && binary_operator.equals("!=")) {
Order cmp = new Order("cmp.f", level);
Calculation.add(cmp);
res0 = "int";
} else if (res1.equals("double") && binary_operator.equals("<")) {
Order cmp = new Order("cmp.f", level);
Order setlt = new Order("set.lt", level);
Calculation.add(cmp);
Calculation.add(setlt);
res0 = "double";
} else if (res1.equals("double") && binary_operator.equals(">")) {
Order cmp = new Order("cmp.i", level);
Order setgt = new Order("set.gt", level);
Calculation.add(cmp);
Calculation.add(setgt);
res0 = "double";
} else if (res1.equals("double") && binary_operator.equals("<=")) {
Order cmp = new Order("cmp.f", level);
Order setgt = new Order("set.gt", level);
Order not = new Order("not", level);
Calculation.add(cmp);
Calculation.add(setgt);
Calculation.add(not);
res0 = "double";
} else if (res1.equals("double") && binary_operator.equals(">=")) {
Order cmp = new Order("cmp.f", level);
Order setlt = new Order("set.lt", level);
Order not = new Order("not", level);
Calculation.add(cmp);
Calculation.add(setlt);
Calculation.add(not);
res0 = "double";
}
int len = Calculation.size();
for (int i = 0; i < len; i++) {
Order this_order = Calculation.get(i);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(this_order);
}
return "boolean";
}
}
// multiplicative_operator -> '*' | '/'
// multiplicative_expr ->
// as_expr (multiplicative_operator as_expr)*
class multiplicative_exprAst extends Ast {
as_exprAst unaryExpressionL;
ArrayList<String> mulOperator = new ArrayList<>();
ArrayList<as_exprAst> unaryExpressionR = new ArrayList<>();
public multiplicative_exprAst(as_exprAst unaryExpressionL, ArrayList<String> mulOperator,
ArrayList<as_exprAst> unaryExpressionR) {
this.unaryExpressionL = unaryExpressionL;
this.mulOperator = mulOperator;
this.unaryExpressionR = unaryExpressionR;
}
public multiplicative_exprAst(as_exprAst unaryExpressionL) {
this.unaryExpressionL = unaryExpressionL;
}
public String generate(int level) {
String res0 = "void", res1 = "void", res2 = "void";
res1 = unaryExpressionL.generate(level);
int len = mulOperator.size();
if(len == 0)
return res1;
for(int i=0;i<len;i++){
res2 = unaryExpressionR.get(i).generate(level);
res0 = res1;
String mulop = mulOperator.get(i);
Order order = new Order();
if (!res2.equals(res1))
System.exit(1);
if (res2.equals("int") && mulop.equals("*")) {
order.setOpcode("mul.i");
order.setlevel(level);
} else if (res2.equals("int") && mulop.equals("/")) {
order.setOpcode("div.i");
order.setlevel(level);
} else if (res2.equals("double") && mulop.equals("*")) {
order.setOpcode("mul.f");
order.setlevel(level);
} else if (res2.equals("double") && mulop.equals("/")) {
order.setOpcode("div.f");
order.setlevel(level);
}
if (level == 0)// GLOBAL
startcode.getStartCodeTable().orders.add(order);
else// LOCAL
Functionarrary.getFunctionTable().getCurrentFuction().addorders(order);
}
return res0;
}
}
// as_expr -> primary_expr {'as' ty}
class as_exprAst extends Ast {
primary_exprAst l_expr;
ArrayList<String> ty;
public as_exprAst(primary_exprAst l_expr, ArrayList<String> ty) {
this.l_expr = l_expr;
this.ty = ty;
}
public as_exprAst(primary_exprAst l_expr) {
this.l_expr = l_expr;
}
public String generate(int level) {
String res;
res = l_expr.generate(level);
if (ty == null)
return res;
for(int i=0;i<ty.size();i++){
Order Change = new Order();
if (ty.get(i).equals("double"))
Change = new Order("itof", level);
else if (ty.get(i).equals("int"))
Change = new Order("ftoi", level);
else
System.exit(1);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(Change);
}
return ty.get(ty.size()-1);
}
}
// primary_expr ->
// negate_expr
// | call_expr
// | group_expr
// | ident_expr
// | literal_expr
class primary_exprAst extends Ast {
Ast primary_expr;
public primary_exprAst(Ast primary_expr) {
this.primary_expr = primary_expr;
}
public String generate(int level) {
String res = "void";
if (primary_expr instanceof negate_exprAst) {
negate_exprAst negate_expr = (negate_exprAst) primary_expr;
res = negate_expr.generate(level);
} else if (primary_expr instanceof call_exprAst) {
call_exprAst call_expr = (call_exprAst) primary_expr;
res = call_expr.generate(level);
} else if (primary_expr instanceof group_exprAst) {
group_exprAst group_expr = (group_exprAst) primary_expr;
res = group_expr.generate(level);
} else if (primary_expr instanceof ident_exprAst) {
ident_exprAst ident_expr = (ident_exprAst) primary_expr;
res = ident_expr.generate(level);
} else if (primary_expr instanceof literal_exprAst) {
literal_exprAst literal_expr = (literal_exprAst) primary_expr;
res = literal_expr.generate(level);
}
return res;
}
}
// negate_expr -> '-' primary_expr
class negate_exprAst extends Ast {
primary_exprAst expr_back;
public negate_exprAst(primary_exprAst expr_back) {
this.expr_back = expr_back;
}
public String generate(int level) {
String res = "void";
res = expr_back.generate(level);
Order order = new Order();
if (res.equals("int")) {
order = new Order("neg.i", level);
} else if (res.equals("double")) {
order = new Order("neg.f", level);
} else {
System.exit(1);
}
if (level == 0)
startcode.getStartCodeTable().orders.add(order);
else
Functionarrary.getFunctionTable().getCurrentFuction().addorders(order);
return res;
}
}
// call_param_list -> operator_expr (',' operator_expr)*
class call_param_listAst extends Ast {
public ArrayList<operator_exprAst> exprs;
String value;
public call_param_listAst(ArrayList<operator_exprAst> exprs) {
this.exprs = exprs;
}
// public call_param_listAst(String value) {
// this.value = value;
// }
public String generate(int level) {
int i = 0, len = exprs.size();
String res = "void";
// if(value != null){
// Variable new_variable = new Variable("String", this.value, true, true,
// level);
// int index = startcode.getStartCodeTable().variables.size();
// startcode.getStartCodeTable().variables.add(new_variable);
// Order push = new Order("push",level);
// push.addOper((long)index);
// Functionarrary.getFunctionTable().getCurrentFuction().addorders(push);
// return "void";
// }
while (i < len) {
res = exprs.get(i).generate(level);
i++;
}
return "void";
}
}
// call_expr -> IDENT '(' call_param_list? ')'
class call_exprAst extends Ast {
String ident;
call_param_listAst call_param_list;
public call_exprAst(String ident, call_param_listAst call_param_list) {
this.ident = ident;
this.call_param_list = call_param_list;
}
public call_exprAst(String ident) {
this.ident = ident;
}
public String generate(int level) {
if (ident.equals("getint") || ident.equals("getdouble") || ident.equals("getchar") || ident.equals("putln")) {
if(ident.equals("getchar")){
Order scanc = new Order("scan.c", level);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(scanc);
}
else{
Order stackalloc = new Order("stackalloc", level);
if(ident.equals("putln"))
stackalloc.addOper(0L);
else
stackalloc.addOper(1L);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(stackalloc);
int index = startcode.getStartCodeTable().variables.size();
Variable this_var = new Variable("string", ident, true, true, 0);
startcode.getStartCodeTable().variables.add(this_var);
Order call = new Order("callname", level);
call.addOper((long) index);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(call);
}
if (ident.equals("getint") || ident.equals("getchar"))
return "int";
if (ident.equals("getdouble"))
return "double";
else
return "void";
}
if (ident.equals("putchar") || ident.equals("putint")
|| ident.equals("putstr") || ident.equals("putdouble")) {
Order stackalloc = new Order("stackalloc", level);
stackalloc.addOper(0L);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(stackalloc);
String res = call_param_list.generate(level);
int index = startcode.getStartCodeTable().variables.size();
Variable this_var = new Variable("string", ident, true, true, 0);
startcode.getStartCodeTable().variables.add(this_var);
Order call = new Order("callname", level);
call.addOper((long) index);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(call);
return "void";
}
if (!Functionarrary.getFunctionTable().is_function(ident))
System.exit(1);
String res = "void";
res = Functionarrary.getFunctionTable().get_function(ident).get_type();
int function_return_slots = Functionarrary.getFunctionTable().get_function(ident).get_return_slots();
Order stackalloc = new Order("stackalloc", level);
stackalloc.addOper((long) function_return_slots);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(stackalloc);
int function_index = constantarray.getConstantTable().get_index(ident);
if (function_index == -1)
System.exit(1);
if (call_param_list != null)
res = call_param_list.generate(level);
Order call = new Order("call", level);
call.addOper((long) function_index);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(call);
Function this_func = Functionarrary.getFunctionTable().get_function(this.ident);
res = this_func.type;
return res;
}
}
// literal_expr -> UINT_LITERAL | DOUBLE_LITERAL | STRING_LITERAL | CHAR_LITERAL
class literal_exprAst extends Ast {
String LITERAL;
public literal_exprAst(String LITERAL) {
this.LITERAL = LITERAL;
}
public String judge() {
String res = "void";
int len = this.LITERAL.length();
if (len == 2 && this.LITERAL.charAt(0)=='\'') {
res = "char";
return res;
}
if (len == 3 && this.LITERAL.charAt(0)=='\'' && this.LITERAL.charAt(1)=='\\') {
res = "char";
return res;
}
if (len >= 1 && !Character.isDigit(this.LITERAL.charAt(0))) {
res = "string";
return res;
}
int flag = 0, sum_char = 0;
res = "int";
for (int i = 0; i < len; i++) {
if (Character.isDigit(this.LITERAL.charAt(i)) && flag <= 1)
continue;
else if (this.LITERAL.charAt(i) == '.') {
flag = 1;
res = "double";
} else if (Character.isAlphabetic(this.LITERAL.charAt(0))) {
flag = 2;
res = "string";
}
}
return res;
}
public String generate(int level) {
String res = judge();
if (res.equals("int")) {
Order push = new Order("push", level);
push.addOper(Long.parseLong(this.LITERAL));
if (level > 0)
Functionarrary.getFunctionTable().getCurrentFuction().addorders(push);
else
startcode.getStartCodeTable().orders.add(push);
} else if (res.equals("double")) {
Order push = new Order("push", level);
double value = Double.parseDouble(this.LITERAL);
long l = Double.doubleToLongBits(value);
push.addOper(l);
if (level > 0)
Functionarrary.getFunctionTable().getCurrentFuction().addorders(push);
else
startcode.getStartCodeTable().orders.add(push);
} else if (res.equals("char")) {
Order push = new Order("push", level);
char ch ;
if(this.LITERAL.length()==2){
ch = this.LITERAL.charAt(1);
}
else {
ch = this.LITERAL.charAt(2);
if(ch == 'b')
ch = '\b';
else if(ch == 'f')
ch = '\f';
else if(ch == 'r')
ch = '\r';
else if(ch == 'n')
ch = '\n';
else if(ch == 't')
ch = '\t';
else if(ch == '\"')
ch = '\"';
else if(ch == '\'')
ch = '\'';
else if(ch == '\\')
ch = '\\';
}
int value = Integer.valueOf(ch);
push.addOper((long) value);
if (level > 0)
Functionarrary.getFunctionTable().getCurrentFuction().addorders(push);
else
startcode.getStartCodeTable().orders.add(push);
} else if (res.equals("string")) {
int len = startcode.getStartCodeTable().variables.size();
String help_string = "";
int str_len = this.LITERAL.length();
for(int p = 0;p<str_len;p++){
char ch = this.LITERAL.charAt(p);
if(ch == '\\'){
char temp = this.LITERAL.charAt(p+1);
if(temp == 'b')
help_string += '\b';
else if(temp == 'f')
help_string += '\f';
else if(temp == 'r')
help_string += '\r';
else if(temp == 'n')
help_string += '\n';
else if(temp == 't')
help_string += '\t';
else if(temp == '\"')
help_string += '\"';
else if(temp == '\'')
help_string += '\'';
else if(temp == '\\')
help_string += '\\';
p++;
}
else
help_string += ch;
}
Variable new_constant = new Variable(res, help_string, true, false, 0);
startcode.getStartCodeTable().variables.add(new_constant);
Order push = new Order("push", level);
push.addOper((long) len);
if (level > 0)
Functionarrary.getFunctionTable().getCurrentFuction().addorders(push);
else
startcode.getStartCodeTable().orders.add(push);
}
if(res.equals("char"))
res="int";
return res;
}
}
// ident_expr -> IDENT
class ident_exprAst extends Ast {
String ident;
public ident_exprAst(String ident) {
this.ident = ident;
}
public String generate(int level) {
String res = "void";
Order loadOrder;
Function currentFunction = Functionarrary.getFunctionTable().getCurrentFuction();
String res1 = "void";
if (currentFunction.is_variable(this.ident)) {
loadOrder = new Order("loca", level);
int index = currentFunction.get_index_variables(this.ident, level);
if(index == -1)
System.exit(1);
loadOrder.addOper((long) index);
Variable current_var = currentFunction.get_variable(this.ident, level);
if(current_var == null)
System.exit(1);
res1 = currentFunction.get_variable(this.ident, level).get_type();
res = res1;
}
else if(currentFunction.is_parameters(this.ident)){
loadOrder = new Order("arga", level);
int index = currentFunction.get_index_parameter(this.ident);
if(index == -1)
System.exit(1);
loadOrder.addOper((long) index);
Variable current_var = currentFunction.get_parameter(this.ident);
if(current_var == null)
System.exit(1);
res1 = current_var.get_type();
res = res1;
}
else {
if (!startcode.getStartCodeTable().is_variable(this.ident))
System.exit(1);
loadOrder = new Order("globa", level);
loadOrder.addOper((long) startcode.getStartCodeTable().get_index(ident));
Variable current_var = startcode.getStartCodeTable().get_variable(this.ident);
if(current_var == null)
System.exit(1);
res1 = current_var.get_type();
res = res1;
}
Functionarrary.getFunctionTable().getCurrentFuction().addorders(loadOrder);
Order load = new Order("load.64", level);
Functionarrary.getFunctionTable().getCurrentFuction().addorders(load);
return res;
}
}
// group_expr -> '(' operator_exprAst ')'
class group_exprAst extends Ast {
operator_exprAst expr;
public group_exprAst(operator_exprAst expr) {
this.expr = expr;
}
public String generate(int level) {
String res = "void";
res = this.expr.generate(level);
return res;
}
}
// stmt ->
// expr_stmt
// | decl_stmt
// | if_stmt
// | while_stmt
// | break_stmt
// | continue_stmt
// | return_stmt
// | block_stmt
// | empty_stmt
class stmtAst extends Ast {
Ast stmtAst;
public stmtAst(Ast stmtAst) {
this.stmtAst = stmtAst;
}
public String generate(int level) {
String res = "";
if (stmtAst instanceof expr_stmtAst) {
expr_stmtAst expr_stmt = (expr_stmtAst) stmtAst;
res = expr_stmt.generate(level);
} else if (stmtAst instanceof decl_stmtAst) {
decl_stmtAst decl_stmt = (decl_stmtAst) stmtAst;
res = decl_stmt.generate(level);
} else if (stmtAst instanceof if_stmtAst) {
if_stmtAst if_stmt = (if_stmtAst) stmtAst;
res = if_stmt.generate(level);
} else if (stmtAst instanceof while_stmtAst) {
while_stmtAst while_stmt = (while_stmtAst) stmtAst;
res = while_stmt.generate(level);
} else if (stmtAst instanceof break_stmtAst) {
break_stmtAst break_stmt = (break_stmtAst) stmtAst;
res = break_stmt.generate(level);
} else if (stmtAst instanceof continue_stmtAst) {
continue_stmtAst continue_stmt = (continue_stmtAst) stmtAst;
res = continue_stmt.generate(level);
} else if (stmtAst instanceof return_stmtAst) {
return_stmtAst return_stmt = (return_stmtAst) stmtAst;
res = return_stmt.generate(level);
} else if (stmtAst instanceof block_stmtAst) {
block_stmtAst block_stmt = (block_stmtAst) stmtAst;
res = block_stmt.generate(level);
} else if (stmtAst instanceof empty_stmtAst) {
empty_stmtAst empty_stmt = (empty_stmtAst) stmtAst;
res = empty_stmt.generate(level);
} else {
System.exit(1);
}
return res;
}
}
// expr_stmt -> expr|operator_expr ';'
class expr_stmtAst extends Ast {
Ast expr;
public expr_stmtAst(Ast expr) {
this.expr = expr;
}
public String generate(int level) {
String res = "void";
if(expr instanceof exprAst){
exprAst this_expr = (exprAst)expr;
res = this_expr.generate(level);
}
else if(expr instanceof operator_exprAst){
operator_exprAst this_expr = (operator_exprAst)expr;
res = this_expr.generate(level);
}
else
System.exit(1);
return res;
}
}
// let_decl_stmt -> 'let' IDENT ':' ty ('=' operator_expr)? ';'
class let_decl_stmtAst extends Ast {
String ident;
String ty;
operator_exprAst expr;
public let_decl_stmtAst(String ident, String ty, operator_exprAst expr) {
this.expr = expr;
this.ty = ty;
this.ident = ident;
}
public let_decl_stmtAst(String ident, String ty) {
this.ident = ident;
this.ty = ty;
}
public String generate(int level) {
String res = "void", res1 = "void";
if (this.ty.equals("int"))
res = "int";
else if (this.ty.equals("double"))
res = "double";
else
System.exit(1);
Variable new_variable = new Variable();
if (expr != null) {
new_variable = new Variable(this.ty, this.ident, false, true, level);
if (level > 0) {
Functionarrary.getFunctionTable().getCurrentFuction().variables.add(new_variable);
} else{
startcode.getStartCodeTable().variables.add(new_variable);
}
if (level > 0) {
Function currentFunction = Functionarrary.getFunctionTable().getCurrentFuction();
int index = currentFunction.get_index_variables(this.ident, level);
if(index == -1)
System.exit(1);
Order loca = new Order("loca", level);
loca.addOper((long) index);
currentFunction.addorders(loca);
res1 = expr.generate(level);
if (!res.equals(res1))
System.exit(1);
Order store = new Order("store.64", level);
currentFunction.addorders(store);
} else if (level == 0) {
int index = startcode.getStartCodeTable().get_index(this.ident);
Order globa = new Order("globa", level);
globa.addOper((long) index);
startcode.getStartCodeTable().orders.add(globa);
res1 = expr.generate(level);
if (!res.equals(res1))
System.exit(1);
Order store = new Order("store.64", level);
startcode.getStartCodeTable().orders.add(store);
} else
System.exit(1);
} else {
new_variable = new Variable(this.ty, this.ident, false, false, level);
if (level > 0) {
Functionarrary.getFunctionTable().getCurrentFuction().variables.add(new_variable);
} else if (level == 0) {
startcode.getStartCodeTable().variables.add(new_variable);
} else
System.exit(1);
}
return res;
}
}
// const_decl_stmt -> 'const' IDENT ':' ty '=' operator_expr ';'
class const_decl_stmtAst extends Ast {
String ident;
String ty;
operator_exprAst expr;
public const_decl_stmtAst(String ident, String ty, operator_exprAst expr) {
this.ident = ident;
this.ty = ty;
this.expr = expr;
}
public String generate(int level) {
String res = " ", res1 = " ";
res = this.ty;
Variable new_variable = new Variable(this.ty, this.ident, true, true, level);
if (level > 0) {
Functionarrary.getFunctionTable().getCurrentFuction().variables.add(new_variable);
Function currentFunction = Functionarrary.getFunctionTable().getCurrentFuction();
int index = currentFunction.get_index_variables(this.ident, level);
if(index == -1)
System.exit(1);
Order loca = new Order("loca", level);
loca.addOper((long) index);
currentFunction.addorders(loca);
res1 = expr.generate(level);
if (!res.equals(res1))
System.exit(1);
Order store = new Order("store.64", level);
currentFunction.addorders(store);
} else if (level == 0) {
startcode.getStartCodeTable().variables.add(new_variable);
int index = startcode.getStartCodeTable().get_index(this.ident);
Order globa = new Order("globa", level);
globa.addOper((long) index);
startcode.getStartCodeTable().orders.add(globa);
res1 = expr.generate(level);
if (!res.equals(res1))
System.exit(1);
Order store = new Order("store.64", level);
startcode.getStartCodeTable().orders.add(store);
} else
System.exit(1);
return res;
}
}
// decl_stmt -> let_decl_stmt | const_decl_stmt
class decl_stmtAst extends Ast {
Ast decl_stmt;
public decl_stmtAst(Ast decl_stmt) {
this.decl_stmt = decl_stmt;
}
public String generate(int level) {
String res = "void";
if (decl_stmt instanceof let_decl_stmtAst) {
let_decl_stmtAst let_decl_stmt = (let_decl_stmtAst) decl_stmt;
res = let_decl_stmt.generate(level);
}
else if (decl_stmt instanceof const_decl_stmtAst) {
const_decl_stmtAst const_decl_stmt = (const_decl_stmtAst) decl_stmt;
res = const_decl_stmt.generate(level);
}
else
System.exit(1);
return res;
}
}
// if_stmt -> 'if' condition block_stmt ('else' 'if' condition block_stmt)*
// ('else' block_stmt)?
class if_stmtAst extends Ast {
conditionAst condition_if;
block_stmtAst block_stmt_if;
ArrayList<conditionAst> condition_else_if = new ArrayList<>();
ArrayList<block_stmtAst> block_stmt_else_if = new ArrayList<>();
block_stmtAst block_stmt_else;
ArrayList<Integer> start = new ArrayList<Integer>();
ArrayList<Integer> jump_in = new ArrayList<Integer>();
ArrayList<Integer> jump_out = new ArrayList<Integer>();
public if_stmtAst(conditionAst condition_if, block_stmtAst block_stmt_if, ArrayList<conditionAst> condition_else_if,
ArrayList<block_stmtAst> block_stmt_else_if, block_stmtAst block_stmt_else) {
this.condition_if = condition_if;
this.block_stmt_if = block_stmt_if;
this.condition_else_if = condition_else_if;
this.block_stmt_else_if = block_stmt_else_if;
this.block_stmt_else = block_stmt_else;
}
public String process(int level, conditionAst this_condition, block_stmtAst this_block) {
String res = "void";
Function currentFunction = Functionarrary.getFunctionTable().getCurrentFuction();
if (this_condition != null) {
int index1 = currentFunction.get_last_order_index();
start.add(index1);
res = this_condition.generate(level);
if (!res.equals("boolean"))
System.exit(1);
Order brtrue = new Order("br.true", level);
brtrue.addOper(1L);
currentFunction.addorders(brtrue);
int index2 = currentFunction.get_last_order_index();
Order br1 = new Order("br", level);
br1.addOper(0L);
currentFunction.addorders(br1);