-
Notifications
You must be signed in to change notification settings - Fork 1
/
Interpret.java
202 lines (164 loc) · 3.38 KB
/
Interpret.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
import java.util.ArrayList;
import java.util.HashMap;
public class Interpret {
ArrayList<Quadruple> quads;
HashMap<Object, Object> map;
public Interpret(ArrayList<Quadruple> quds){
quads=quds;
map = new HashMap<Object, Object>();
int i = 0;
while(i<quads.size())
{
//System.out.println ("interpreting*");
Quadruple q = quads.get(i);
Object ans = null;
int a=-1;
int b=-1;
boolean ay = false;
boolean be = false;
int op = q.oper;
Object obj1 = map.get(q.var1);
Object obj2 = map.get(q.var2);
if(q.var1 instanceof Boolean){
ay = (Boolean) q.var1;
}
else if(obj1 instanceof Boolean){
ay = (Boolean) obj1;
}
else if (q.var1 instanceof Integer){
a = (Integer) q.var1;
}
else if(q.var1 instanceof String){
String str1=(String) q.var1;
a=getInt(q.var1, str1);
}
//next value
if(q.var2 instanceof Boolean){
be = (Boolean) q.var2;
}
else if(obj2 instanceof Boolean){
be = (Boolean) obj2;
}
else if (q.var2 instanceof Integer){
b = (Integer) q.var2;
}
else if(q.var2 instanceof String){
String str2= (String) q.var2;
b=getInt(q.var2, str2);
}
switch(op){
//assign
case 90:
ans = a;
break;
case 110:
System.out.println ("Print: " + (String)q.var1);
break;
//unary
case 270:
ans = -a;
break;
case 271:
ans = +a; //0
break;
//jump false
case 2000:
be = (Boolean) map.get(q.var2);
//if true do nothing, else jump
i=(be)? i:a-1; //-1 since will be incrememnted
break;
case 430:
//System.out.println ("adding" +a);
ans = a+b;
break;
case 431:
ans = a-b;
break;
case 450:
ans = a*b;
break;
case 451:
ans = a/b;
break;
case 452:
ans = a%b;
break;
//booleans **
//“<=”|”<”|”>”|”>=”
case 410:
ans = a<=b;
break;
case 411:
ans = a<b;
break;
case 412:
ans = a>b;
break;
case 413:
ans = a>=b;
break;
//compare booleans
case 490:
ans = ay || be;
break;
case 491:
ans = ay && be;
break;
case 492:
ans = !ay;
break;
//| ^ & ~ ?
case 493:
ans = ay|be;
break;
case 494:
ans = ay^be;
break;
case 495:
ans = ay&be;
break;
case 496: //~ ?
//ans = ~ay;
break;
//jumpfalse
case 900:
if(!be){i=a-1;}
break;
//jump true
case 901:
if(be){i=a-1;}
break;
case 902:
break;
case 905:
i=a-1;
break;
default:
System.out.println ("op for quad not found");
}
if(ans!=null){
map.put(q.result, ans);
System.out.println ("result:"+ans + " (var1:"+q.var1+")");
}
i++;
}
}
private int getInt(Object var, String str1)
{
int num=-1;
Character ch = str1.charAt(0);
if(map.containsKey(var)){
Object obj = (Integer) map.get(var);
//can't be variable name*
num = (Integer) obj;
//System.out.println ("map lookup:"+str1+" "+num );
}
else if (Character.isDigit(ch)){
num = Integer.parseInt( str1 );
}
else{
System.out.println ("not a number");
}
return num;
}
}