-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreenode.cpp
More file actions
1442 lines (1049 loc) · 49 KB
/
treenode.cpp
File metadata and controls
1442 lines (1049 loc) · 49 KB
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
// Created by David Ehley, Jesse Karakash, Aaron Diaz, Abel Rodarte on 2/1/18.
// Current Compiler for Stage 1 and Stage 2
// Assignment 5
// 5-4-2018
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stack>
#include "treenode.h"
#include "FunctionTable.h"
#include "Variable.h"
#include "ParameterList.h"
using namespace std;
string countSpace = ""; // space holder
int numSpace =0; // space counter
int address = 6;
int ifCountCheck = 0, elseCountCheck = 0, secondCheck = 0, whileCount = 0;
bool ifElseFlag = false;
string jumpCode = "";
vector<int> globalVec;
vector<string> variableNames;
vector<int> lineNums;
FunctionTable func;
Variable var;
ParameterList param;
/*treenode::treenode(int value_arg) {
value = value_arg; // Initializing value
}*/
treenode::treenode(int numRule, string ruleText) {
ruleNum = numRule; // rule number that was passed in
type = ruleText; // rule descriptor
}
void treenode::add_child(treenode *node){
// if(child.size() < 10){ // as long as the vector is less than 10, add the child to it
child.push_back(node);
// }
}
void treenode::buildMapVec(string scope) {
// looking for a function rule
if(ruleNum == 60){
searchGlobal();
scope = type; // saving off the function name
func.build(new SymTab(type, text, "1",scope, to_string(address))); // adding the data to the map
checkAddress();
}
// if parameter array is found, create a vector entry
if(ruleNum == 91){
searchGlobal();
//adding [] since the parameter is an array
param.add(new SymTab(child[1]->type,child[0]->text+"[]", "1", scope, to_string(address)));
checkAddress();
}
// if parameter is found, create a vector entry
if(ruleNum == 90){
searchGlobal();
param.add(new SymTab(child[1]->type,child[0]->text, "1", scope, to_string(address)));
checkAddress();
}
// if a variable array is found, create a variable map entry
if(ruleNum == 41){
//if the address is found in the vector of global address's, move to the next one
searchGlobal();
if(scope == "global"){ // if global, push the address so it isnt used again
globalVec.push_back(address);
}
//adding [] since the variable is an array
var.build(new SymTab(child[1]->type,child[0]->text+"[]", child[2]->type, scope, to_string(address))) ;
checkAddress();
}
// if a variable is found, create a variable map entry
if(ruleNum == 40){
searchGlobal();
if(scope == "global"){ // if global, push the address so it isnt used again
globalVec.push_back(address);
}
variableNames.push_back(child[1]->type);
var.build(new SymTab(child[1]->type,child[0]->text,"1", scope, to_string(address))) ;
checkAddress();
}
// resetting address once it uses 8 registers
// going thru the tree in a preorder method again, just passing in the scope this time
for(int count = 0; count < child.size(); count++){
child[count]->buildMapVec(scope); // call to print the next root
}
}
void treenode::makeFile(string file){
ofstream outfile(file);
int lineCount = 0;
codeGeneration(outfile, lineCount);
outfile.close();
}
void treenode::codeGeneration(ofstream &outfile, int &lineCount) {
stack<int> stacker; // initiate stack, not sure on what type it has to be yet, so i just put int for now
// have to assign this to x and y
if(ruleNum == 180 && child[1]->type == "input"){
outfile << lineCount << ": IN 4,0,0\n";
lineCount++;
unordered_map<int, SymTab*>::iterator it = var.map.begin();
while (it != var.map.end()){
if(it->second->name == child[0]->type){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ST 4," << it->second->address << "(0)\n";
lineCount++;
}
it++;
}
}
// finds mathops to do stuff
if(ruleNum == 180 && (//child[0]->type == "+" || child[0]->type == "-" || child[0]->type == "*" || child[0]->type == "/" ||
child[1]->type == "+" || child[1]->type == "-" || child[1]->type == "*" || child[1]->type == "/")){
assign(outfile, lineCount);
}
// handles output -> arglist -> x and output -> arglist -> x, y
// does not handle mathops
if(ruleNum == 262 && type == "output" && child[0]->child[0]->child.size() == 0){
for(int i = 0; i < child[0]->child.size(); i++){
unordered_map<int, SymTab*>::iterator it = var.map.begin();
while (it != var.map.end()) {
if (it->second->name == child[0]->child[i]->type) {
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 4," << it->second->address << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": OUT 4,0,0\n";
lineNums.push_back(lineCount);
lineCount++;
}
it++;
}
}
}
// same as above statement, exept checking for mathops
if(ruleNum == 262 && type == "output" && child[0]->child[0]->child.size() == 2){
child[0]->assignOut(outfile,lineCount);
for(int i = 0; i < child[0]->child.size(); i++){
unordered_map<int, SymTab*>::iterator it = var.map.begin();
while (it != var.map.end()) {
if (it->second->name == child[0]->child[i]->type) {
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 4," << it->second->address << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": OUT 4,0,0\n";
lineNums.push_back(lineCount);
lineCount++;
}
it++;
}
}
}
if(ruleNum == 160){
ifCountCheck = 1; // sets it to one since it should jump at least 1
whileCount = lineCount;
outfile << lineCount << ": LDC 1,"<< whileCount + 2 <<"(0)\n";
lineCount++;
outfile << lineCount << ": ST 1,"<< whileCount + 2 <<"(0)\n";
lineCount++;
outfile<<"* beginning of while loop\n";
for(int index = 0; index <51;index++){
try {
child[1]->child[1]->child.at(index);
string path = child[1]->child[1]->child[index]->child[1]->type;
if(path == "input"){
ifCountCheck +=2;
}
if(path == "+" ||path == "-" ||path == "*" || path =="/"){
bool check1 = false;
bool check2 = false;
string var1 = child[1]->child[1]->child[index]->child[1]->child[0]->type;
string var2 = child[1]->child[1]->child[index]->child[1]->child[1]->type;
if(find(variableNames.begin(), variableNames.end(), var1) != variableNames.end()){
// ifCountCheck += 1; // if its a variable that will only add one line of code
check1 = true;
}
if(find(variableNames.begin(), variableNames.end(), var2) != variableNames.end()){
// ifCountCheck += 1; // if its a variable that will only add one line of code
check2 = true;
}
if(check1 == true && check2 == true){
ifCountCheck+=2;
} else{
ifCountCheck+=5;
}
ifCountCheck+=2; //this for the actual add command and store
}
path = "";
}catch (out_of_range& oor){
}
}
int modLine = ifCountCheck+lineCount+4-1;
outfile << modLine << ": LD 7,"<< whileCount + 2 <<"(0)\n";
lineNums.push_back(modLine);
}
//rule for basic if's
if(ruleNum == 150){
outfile<<"* beginning of if statement\n";
//if all thats in the if is a output statement, we just have to skip 2 lines
if(child[1]->type == "output"){
ifCountCheck = 2;
//if its a simple math expr, it will have to jump ahead 6 lines
}else{
ifCountCheck = 6;
}
}
//rule for if..else's
//still needs work
if(ruleNum == 151){
outfile<<"* beginning of if else statement\n";
ifElseFlag = true;
if(child[1]->type == "output") {
ifCountCheck = 2;
elseCountCheck = 3;
secondCheck = 6;
} //if here that means we have an assignment
else{
ifCountCheck = 6;
elseCountCheck = 5;
secondCheck = 8;
}
if(child[2]->type == "output") {
}
}
if (ruleNum == 200){
bool check1 = false, check2 = false, check3 = false, check4 = false;
//checking what the opcode should be
if(child[0]->type == ">") {
jumpCode = "JLT";
check1 = true;
} else if(child[0]->type == "<"){
jumpCode = "JGT";
check2 = true;
} else if(child[0]->type == ">="){
jumpCode = "JLE";
check3 = true;
} else if (child[0]->type == "<="){
jumpCode = "JGE";
check4 = true;
} else{
jumpCode = "JEQ";
}
lineCount++;
ifStatement(outfile, lineCount);
//checking to see if theres an else or not
if(ifElseFlag == true){
if(check1 == true){
jumpCode = "JGT";
} else if (check2 == true){
jumpCode = "JLT";
}
else if (check3 == true){
jumpCode = "JGE";
}else if (check4 == true){
jumpCode = "JLE";
}
lineCount+=elseCountCheck;
ifStatement(outfile, lineCount);
lineCount-=secondCheck;
}
}
//recursively going through the tree
for(int count = 0; count < child.size(); count++){
child[count]->codeGeneration(outfile, lineCount);
}
}
// gens code for if statements
// child[0] == relop (>, =>, <, <=, ==), child[1] and child[2] vars/constants
void treenode::ifStatement(ofstream &outfile, int &lineCount){
unordered_map<int, SymTab*>::iterator it = var.map.begin();
// keeps count of how many variables exist in the statement
int tempCounter = 0;
// stores the address of the first variable, if it exists
string firstAddress = "";
// stores the type of the first variable, used if this is a constant
string firstType = "";
// stores the address of the second variable, if it exists
string secondAddress = "";
// stores the type of the second variable, used if this is a constant
string secondType = "";
while (it != var.map.end()) {
if (it->second->name == child[1]->type) {
firstAddress = it->second->address;
tempCounter++;
}
else if( it->second->name == child[2]->type){
secondAddress = it->second->address;
tempCounter++;
}
it++;
}
// if address of first is not found, then it is constant to be checked
if(firstAddress == ""){
firstType = child[1]->type;
}
// if address of second is not found, then it is constant to be checked
if(secondAddress == ""){
secondType = child[2]->type;
}
if(tempCounter == 2){
// here because very good reasons
lineCount--;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 4,"<< firstAddress <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 2,"<< secondAddress <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": SUB 4,4,2\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": " << jumpCode <<" 4," << ifCountCheck <<"(7)\n";
lineNums.push_back(lineCount);
lineCount++;
}
// handles ifs with 1 variable in table and 1 constant
if(tempCounter ==1){
// here because very good reasons
lineCount--;
// the two if statements below mirror each other, to handle x > 2 and 2 > x
// first child has no type set, meaning it was found in our table. Second is guaranteed to be our constant here
if(firstType == ""){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 4,"<< firstAddress <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 2,"<< secondType <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": SUB 4,4,2\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": " << jumpCode <<" 4," << ifCountCheck <<"(7)\n";
lineNums.push_back(lineCount);
lineCount++;
}
// second child has no type set, meaning it was found in our table. First is guaranteed to be our constant here
else if(secondType == ""){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 4,"<< firstType <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 2,"<< secondAddress <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": SUB 4,4,2\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": " << jumpCode <<" 4," << ifCountCheck <<"(7)\n";
lineNums.push_back(lineCount);
lineCount++;
}
}
// occurs if error happens or if both are constants
if(tempCounter == 0){
// here because very good reasons
lineCount--;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 4,"<< firstType <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 2,"<< secondType <<"(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": SUB 4,4,2\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": " << jumpCode <<" 4," << ifCountCheck <<"(7)\n";
lineNums.push_back(lineCount);
lineCount++;
}
}
void treenode::assignOut(ofstream &outfile, int &lineCount){
string tempAddress = "";
unordered_map<int, SymTab*>::iterator it = var.map.begin();
while (it != var.map.end()) {
if (it->second->name == child[0]->child[0]->type) {
tempAddress = it->second->address;
}
it++;
}
if(child[0]->child[0]->ruleNum == 220 || 240){
mathOps(outfile, lineCount);
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ST 4," << tempAddress << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": OUT 4,0,0\n";
lineNums.push_back(lineCount);
lineCount++;
}
}
void treenode::assign(ofstream &outfile, int &lineCount){
string tempAddress = "";
unordered_map<int, SymTab*>::iterator it = var.map.begin();
while (it != var.map.end()) {
if (it->second->name == child[0]->type) {
tempAddress = it->second->address;
}
it++;
}
if(child[1]->ruleNum == 220 || 240){
mathOps(outfile, lineCount);
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ST 4," << tempAddress << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
}
}
//generate code for mathops
void treenode::mathOps(ofstream &outfile, int &lineCount) {
//stack operations for later, push and pop
// outfile << lineCount << ": LDA 5,1(5)\n";
// lineCount++;
// outfile << lineCount << ": LDA 5,-1(5)\n";
// lineCount++;
// stores the assignment address, this is passed in as an argument so its not lost recursively
for(int i = 0; i < child.size(); i++){
// Generates code for math operations dealing with addition
if(child[i]->type == "+"){
string address = "";
// address of right side
string address2 = "";
unordered_map<int, SymTab*>::iterator it = var.map.begin();
bool isConstant = false;
int doubleConstant = 0;
bool first = false, second = false;
string doubleAddress = "";
int doubleVar = 2;
while (it != var.map.end()) {
if(child[i]->child[0]->type == child[i]->child[1]->type && (child[i]->ruleNum == 220 || child[i]->ruleNum == 240)){
lineCount = checkLineNums(lineCount);
if(doubleVar < 4){
doubleAddress = it->second->address;
}
outfile << lineCount << ": LD " << doubleVar << "," << doubleAddress << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
doubleVar*=2;
first = true;
second = true;
}
else if (it->second->name == child[i]->child[0]->type) {
lineCount = checkLineNums(lineCount);
outfile<<"*case1\n";
outfile << lineCount << ": LD 4," << it->second->address << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
string address = it->second->address;
first = true;
}
else if (it->second->name == child[i]->child[1]->type) {
lineCount = checkLineNums(lineCount);
outfile<<"*case2\n";
outfile << lineCount << ": LD 2," << it->second->address << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
string address2 = it->second->address;
second = true;
}
else{
string address2 = it->second->address;
isConstant = true;
}
if(it->second->name == child[0]->type) {
doubleAddress = it->second->address;
}
it++;
}
// variables so we dont have to write out child 1million and a half times
string one = child[i]->child[0]->type;
string two = child[i]->child[1]->type;
//if they are both constants, and they are not an operation
if ((first == false && second == false) && one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "-"){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[0]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 2," << child[i]->child[1]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
//if only the first child is a constant
} else if(first == false && second == true && (one != "*" || "/" || "+" || "-")){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[0]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
//if only the second child is a constant
} else if (first == true && second == false && (two != "*" || "/" || "+" || "-")){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[1]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
}
//the only time reg 1 is used is when there are constants
if ((first == false && second == false) && one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "-"){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[0]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 2," << child[i]->child[1]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
//if only the first child is a constant
} else if(first == false && second == true && (one != "*" || "/" || "+" || "-")){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[0]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
//if only the second child is a constant
} else if (first == true && second == false && (two != "*" || "/" || "+" || "-")){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[1]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
}
//the only time reg 1 is used is when there are constants
if(isConstant == true && one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "-") {
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ADD 4,4,1\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ST 4,0(5)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDA 5, 1(5)\n";
lineNums.push_back(lineCount);
lineCount++;
}else if (one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "-"){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ADD 4,4,2\n";
lineNums.push_back(lineCount);
lineCount++;
}
else if(isConstant == true && (one == "*" || one == "-" || one == "+" || one == "-" || two == "*" || two == "-" || two == "+" || two != "-")){
offset = 9;
// these statements pop our stack
tmpLine = lineCount;
outfile << lineCount + offset + 1<< ": LDA 5,-1(5)\n";
lineNums.push_back(lineCount + offset + 1);
outfile << lineCount + offset + 2<< ": LD 1,0(5)\n";
lineNums.push_back(lineCount + offset + 2);
outfile << lineCount + offset + 3<< ": LDA 5,-1(5)\n";
lineNums.push_back(lineCount + offset + 3);
outfile << lineCount + offset + 4<< ": LD 2,0(5)\n";
lineNums.push_back(lineCount + offset + 4);
outfile << lineCount + offset + 5<< ": ADD 4,2,1\n";
lineNums.push_back(lineCount + offset + 5);
tempOffset = lineCount + 1;
offset++;
}
else{
offset = 5;
outfile << lineCount + offset << ": ADD 4,4,2\n";
lineNums.push_back(lineCount + offset );
tempOffset = lineCount + 1;
offset++;
}
}
// Generates code for math operations dealing with subtraction
if(child[i]->type == "-"){
// address of left side, can do checks if this is empty string, then you know its a constant or another mathop
string address = "";
// address of right side
string address2 = "";
unordered_map<int, SymTab*>::iterator it = var.map.begin();
bool isConstant = false;
int doubleConstant = 0;
bool first = false, second = false;
string doubleAddress = "";
int doubleVar = 2;
while (it != var.map.end()) {
if(child[i]->child[0]->type == child[i]->child[1]->type){
lineCount = checkLineNums(lineCount);
if(doubleVar < 4){
doubleAddress = it->second->address;
}
outfile << lineCount << ": LD " << doubleVar << "," << doubleAddress << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
doubleVar*=2;
first = true;
second = true;
}
else if (it->second->name == child[i]->child[0]->type) {
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 2," << it->second->address << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
string address = it->second->address;
first = true;
}
else if (it->second->name == child[i]->child[1]->type) {
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LD 2," << it->second->address << "(0)\n";
lineNums.push_back(lineCount);
lineCount++;
string address2 = it->second->address;
second = true;
} else{ // this is working
string address2 = it->second->address;
isConstant = true;
}
if(it->second->name == child[0]->type)
doubleAddress = it->second->address;
it++;
}
// variables so we dont have to write out child 1million and a half times
string one = child[i]->child[0]->type;
string two = child[i]->child[1]->type;
//if they are both constants, and they are not an operation
if ((first == false && second == false) && one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "/"){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[0]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 2," << child[i]->child[1]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
//if only the first child is a constant
} else if(first == false && second == true && (one != "*" || "/" || "+" || "-")){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[0]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
//if only the second child is a constant
} else if (first == true && second == false && (two != "*" || "/" || "+" || "-")){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDC 1," << child[i]->child[1]->type << "(5)\n";
lineNums.push_back(lineCount);
lineCount++;
}
//the only time reg 1 is used is when there are constants
if(isConstant == true && one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "/") {
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": SUB 4,2,1\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": ST 4,0(5)\n";
lineNums.push_back(lineCount);
lineCount++;
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": LDA 5, 1(5)\n";
lineNums.push_back(lineCount);
lineCount++;
}else if (one != "*" && one != "-" && one != "+" && one != "-" && two != "*" && two != "-" && two != "+" && two != "/"){
lineCount = checkLineNums(lineCount);
outfile << lineCount << ": SUB 4,4,2\n";
lineNums.push_back(lineCount);
lineCount++;
}
else if(isConstant == true && (one == "*" || one == "-" || one == "+" || one == "-" || two == "*" || two == "-" || two == "+" || two == "/")){
offset = 14;
// these statements pop our stack
tmpLine = lineCount;
outfile << lineCount + offset + 1<< ": LDA 5,-1(5)\n";
lineNums.push_back(lineCount + offset + 1);
outfile << lineCount + offset + 2<< ": LD 1,0(5)\n";
lineNums.push_back(lineCount + offset + 2);
outfile << lineCount + offset + 3<< ": LDA 5,-1(5)\n";
lineNums.push_back(lineCount + offset + 3);
outfile << lineCount + offset + 4<< ": LD 2,0(5)\n";
lineNums.push_back(lineCount + offset + 4);
if(first == true) {
outfile << lineCount + offset + 5 << ": SUB 4,1,2\n";
}else{
outfile << lineCount + offset + 5 << ": SUB 4,2,1\n";
}
lineNums.push_back(lineCount + offset + 5);
tempOffset = lineCount + 1;
offset++;
}
else{
offset = 10;
outfile << lineCount + offset + 1<< ": LDA 5,-1(5)\n";
lineNums.push_back(lineCount + offset + 1);
outfile << lineCount + offset + 2<< ": LD 2,0(5)\n";
lineNums.push_back(lineCount + offset + 2);
outfile << lineCount + offset + 3<< ": LDA 5,-1(5)\n";
lineNums.push_back(lineCount + offset + 3);
outfile << lineCount + offset + 4<< ": LD 4,0(5)\n";
lineNums.push_back(lineCount + offset + 4);
outfile << lineCount + offset + 5 << ": SUB 4,4,2\n";
lineNums.push_back(lineCount + offset + 5);
tempOffset = lineCount + 1;
if(ruleNum == 180){
outfile << lineCount+offset+6 << ": ST 4," << doubleAddress << "(0)\n";
lineNums.push_back(lineCount+offset+6);
tempOffset = lineCount + 1;
}
offset++;
}
}
// Generates code for math operations dealing with multiplication
if(child[i]->type == "*"){
// address of left side, can do checks if this is empty string, then you know its a constant or another mathop
string address = "";
// address of right side
string address2 = "";
unordered_map<int, SymTab*>::iterator it = var.map.begin();
// finds the address locations of the two children, if they exist, and sets them both to constants we can
// use in our outfile.... hopefully
// running on simpleExpressionc. , this prints out the address of x (0), as it is the only node we go to other
// than "+", which is not found within the table. statements handling the side with no address (constants,
// variables, etc.) should be handled after this while loop with recursion. Recursion was taken out due to
// error listed below
bool isConstant = false;
int doubleConstant = 0;
int doubleVar = 2;
bool first = false, second = false;