-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quadrupler.java
196 lines (142 loc) · 4.66 KB
/
Quadrupler.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
import java.util.ArrayList;
public class Quadrupler {
ArrayList<Quadruple> quads;
int numInQuad;
// changed everything to ints
// take in root
public Quadrupler(Node treeRoot) {
quads = new ArrayList<Quadruple>();
// xtraTreePrep(treeRoot);
postOrder(treeRoot);
for (Quadruple q : quads) {
System.out.println("[op: " + q.oper + " vars:" + q.var1 + " "
+ q.var2 + " result:" + q.result + "]");
}
}
public void postOrder(Node nod) {
// or select case
int childnum = nod.children.size();
if (childnum == 0) {
// already connected temp to data.
// jump quads :( :)
if (nod.method == 900 || nod.method == 901 || nod.method == 902
|| nod.method == 905) {
Quadruple q = new Quadruple(nod.method, null, null, null);
quads.add(q);
nod.pointArray = q;
numInQuad = quads.indexOf(q);
}
//cout
else if(nod.method==110){
Object word = nod.children.get(0).temp;
makeQuad(nod.method, word, null, null, nod);
}
}
else {
// first do children
for (Node child : nod.children) {
postOrder(child);
}
if (childnum == 1) {
nod.temp = nod.children.get(0).temp;
}
else if (childnum == 2) { // 2 and 3 separate for clarity
nod.print();
// System.out.println(" has 2 children");
Node child = nod.children.get(0);
int iop = child.method;
// String sop = nod.children.get(0).strMethod;
Object temp = genTemp();
// unary ops
// -
// <exp2> --> “!” <exp2>
// <exp5> --> “~ “<exp5>
if (iop == 270 || iop == 271 || iop == 160 || iop == 190
|| iop == 492 || iop == 496) {
makeQuad(iop, nod.children.get(1).temp, null, temp, nod);
}
// assignments <assign> --> <listofvariables> "=" <exp>
// 0 child stores variable name, 1 stores the value that will be assigned
else if (nod.method == 90) {
makeQuad(nod.method, nod.children.get(1).temp, null,
nod.children.get(0).temp, nod);
// System.err.println
// ("assign quad: "+nod.children.get(1).temp);
}
}
else { // >=3
Object temp = genTemp();
int iop = nod.children.get(1).method;
Object lastTemp = nod.children.get(0).temp;
// math and logic ops
if (iop > 400 && iop < 500) {
for (int i = 1; i < childnum - 1; i += 2) {
temp = genTemp();
iop = nod.children.get(i).method;
if (iop > 400 && nod.method < 500) { // remove check?
Quadruple q = new Quadruple(iop, lastTemp,
nod.children.get(i + 1).temp, temp);
nod.temp = temp;
lastTemp = temp;
quads.add(q);
}
}
}
// if stat -> exp ifspot statement [else statement endelse]
// 100 -> 0)exp 1)900 2)statement [3)102 4)statement 5)901]
else if (nod.method == 100) { // method of ifstat
Node exp = nod.children.get(0);
Node jumpFalse = nod.children.get(1);
// System.out.println (jumpFalse.method); // check
Quadruple jf = jumpFalse.pointArray;
jf.var2 = exp.temp;
if (childnum <= 3) {
jf.var1 = numInQuad + 1; // this should be last spot.
} else { // there's an else
Node jumpTrue = nod.children.get(3);
Quadruple jTrue = jumpTrue.pointArray;
jf.var1 = quads.indexOf(jTrue);
// System.err.println ("index of "+jf.var1);
jTrue.var1 = numInQuad + 1;
jTrue.var2 = exp.temp;
}
}
// <while> --> beforeexp <exp> jumpfalse <statement> jump
else if (nod.method == 80) {
Node whileSpot = nod.children.get(0);
Node exp = nod.children.get(1);
Node jumpFalse = nod.children.get(2);
Quadruple jf = jumpFalse.pointArray;
jf.var2 = exp.temp;
jf.var1 = numInQuad + 1; // this should be after jump
Node jump = nod.children.get(4);
//System.err.println("the jump Node:" + jump.method); // test
Quadruple jumpQ = jump.pointArray;
Quadruple beforeWhile = whileSpot.pointArray;
jumpQ.var1 = quads.indexOf(beforeWhile);
}
}
}
}
// gen temp values for later lookup in hashmap - all as string..
Integer tempor = 10;
private Object genTemp() {
tempor++;
// return tempor;
String t = "temp*" + tempor.toString();
return t;
}
public void makeQuad(Integer op, Object v1, Object v2, Object result,
Node nod) {
Quadruple q = new Quadruple(op, v1, v2, result);
quads.add(q);
// check set
nod.temp = result;
numInQuad = quads.indexOf(q);
// System.out.println ("NIQ:"+numInQuad);
// nod.pointArray = numInQuad;
}
public ArrayList<Quadruple> getQuadArray() {
return quads;
}
}