-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_unary_exec.java
213 lines (208 loc) · 7.8 KB
/
binary_unary_exec.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
import java.util.Objects;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.UnaryExpr;
public class binary_unary_exec {
static iClass iDouble, iFloat, iLong, iInteger, iString, iBool;
static {
Scope scope = ScopeImpl.newRootScope(null);
iDouble = iClassWrapped.from(scope, Double.class);
iFloat = iClassWrapped.from(scope, Float.class);
iLong = iClassWrapped.from(scope, Long.class);
iInteger = iClassWrapped.from(scope, Integer.class);
iString = iClassWrapped.from(scope, String.class);
iBool = iClassWrapped.from(scope, Boolean.class);
}
@SuppressWarnings("all")
public static Object binary_exec(Scope scope, BinaryExpr.Operator op, iObject l, iObject r) throws Throwable {
scope.log("binary_exec(op=%s,l=(%s),r=(%s))", op, l, r);
if (l instanceof iObjectVariable)
return binary_exec(scope, op, l.asVariable().get(), r);
if (r instanceof iObjectVariable)
return binary_exec(scope, op, l, r.asVariable().get());
iClass lc = l.getClazz();
iClass rc = r.getClazz();
/*
* OR("||"), AND("&&"), BINARY_OR("|"), BINARY_AND("&"), XOR("^"), EQUALS("=="),
* NOT_EQUALS("!="), LESS("<"), GREATER(">"), LESS_EQUALS("<="),
* GREATER_EQUALS(">="), LEFT_SHIFT("<<"), SIGNED_RIGHT_SHIFT(">>"),
* UNSIGNED_RIGHT_SHIFT(">>>"), PLUS("+"), MINUS("-"), MULTIPLY("*"),
* DIVIDE("/"), REMAINDER("%");
*/
if (op == BinaryExpr.Operator.PLUS && (lc.equals(iString) || rc.equals(iString)))
return Objects.toString(l.asWrapped().x) + Objects.toString(r.asWrapped().x);
if (lc.equals(iDouble) || rc.equals(iDouble)) {
double ll = ((Number) l.asWrapped().x).doubleValue();
double rr = ((Number) r.asWrapped().x).doubleValue();
switch (op) {
case EQUALS:
return ll == rr;
case NOT_EQUALS:
return ll != rr;
case LESS:
return ll < rr;
case GREATER:
return ll > rr;
case LESS_EQUALS:
return ll <= rr;
case GREATER_EQUALS:
return ll >= rr;
case PLUS:
return ll + rr;
case MINUS:
return ll - rr;
case MULTIPLY:
return ll * rr;
case DIVIDE:
return ll / rr;
case REMAINDER:
return ll % rr;
}
} else if (lc.equals(iFloat) || rc.equals(iFloat)) {
float ll = ((Number) l.asWrapped().x).floatValue();
float rr = ((Number) r.asWrapped().x).floatValue();
switch (op) {
case EQUALS:
return ll == rr;
case NOT_EQUALS:
return ll != rr;
case LESS:
return ll < rr;
case GREATER:
return ll > rr;
case LESS_EQUALS:
return ll <= rr;
case GREATER_EQUALS:
return ll >= rr;
case PLUS:
return ll + rr;
case MINUS:
return ll - rr;
case MULTIPLY:
return ll * rr;
case DIVIDE:
return ll / rr;
case REMAINDER:
return ll % rr;
}
} else if (lc.equals(iLong) || rc.equals(iLong)) {
long ll = ((Number) l.asWrapped().x).longValue();
long rr = ((Number) r.asWrapped().x).longValue();
switch (op) {
case BINARY_OR:
return ll | rr;
case BINARY_AND:
return ll & rr;
case XOR:
return ll ^ rr;
case EQUALS:
return ll == rr;
case NOT_EQUALS:
return ll != rr;
case LESS:
return ll < rr;
case GREATER:
return ll > rr;
case LESS_EQUALS:
return ll <= rr;
case GREATER_EQUALS:
return ll >= rr;
case LEFT_SHIFT:
return ll << rr;
case SIGNED_RIGHT_SHIFT:
return ll >> rr;
case UNSIGNED_RIGHT_SHIFT:
return ll >>> rr;
case PLUS:
return ll + rr;
case MINUS:
return ll - rr;
case MULTIPLY:
return ll * rr;
case DIVIDE:
return ll / rr;
case REMAINDER:
return ll % rr;
}
} else if (lc.equals(iInteger) || rc.equals(iInteger)) {
int ll = ((Number) l.asWrapped().x).intValue();
int rr = ((Number) r.asWrapped().x).intValue();
// System.out.printf("[bin] ll=%s rr=%s\n", ll, rr);
switch (op) {
case BINARY_OR:
return ll | rr;
case BINARY_AND:
return ll & rr;
case XOR:
return ll ^ rr;
case EQUALS:
return ll == rr;
case NOT_EQUALS:
return ll != rr;
case LESS:
return ll < rr;
case GREATER:
return ll > rr;
case LESS_EQUALS:
return ll <= rr;
case GREATER_EQUALS:
return ll >= rr;
case LEFT_SHIFT:
return ll << rr;
case SIGNED_RIGHT_SHIFT:
return ll >> rr;
case UNSIGNED_RIGHT_SHIFT:
return ll >>> rr;
case PLUS:
return ll + rr;
case MINUS:
return ll - rr;
case MULTIPLY:
return ll * rr;
case DIVIDE:
return ll / rr;
case REMAINDER:
return ll % rr;
}
}
if (op == BinaryExpr.Operator.EQUALS)
return Objects.equals(l, r);
if (lc.equals(iBool) || rc.equals(iBool)) {
boolean ll = (boolean) l.asWrapped().x;
boolean rr = (boolean) r.asWrapped().x;
switch (op) {
case AND:
return ll & rr;
}
}
throw new UnsupportedOperationException();
}
public static Object unary_exec(Scope scope, UnaryExpr.Operator op, iObject x) throws Throwable {
scope.log("binary_exec(op=%s,x=(%s))", op, x);
iObjectVariable xv = x.asVariable();
iClass c = xv.getClazz();
iObjectWrapped p = x.asVariable().get().asWrapped();
/*
*
* PLUS("+", false), MINUS("-", false), PREFIX_INCREMENT("++", false),
* PREFIX_DECREMENT("--", false), LOGICAL_COMPLEMENT("!", false),
* BITWISE_COMPLEMENT("~", false), POSTFIX_INCREMENT("++", true),
* POSTFIX_DECREMENT("--", true);
*
*/
if (c.equals(iInteger)) {
int v = (int) p.x;
if (op == UnaryExpr.Operator.PREFIX_INCREMENT) {
return xv.set(new iObjectWrapped(scope, v + 1));
} else if (op == UnaryExpr.Operator.POSTFIX_INCREMENT) {
xv.set(new iObjectWrapped(scope, v + 1));
return p;
} else if (op == UnaryExpr.Operator.PREFIX_DECREMENT) {
return xv.set(new iObjectWrapped(scope, v - 1));
} else if (op == UnaryExpr.Operator.POSTFIX_DECREMENT) {
xv.set(new iObjectWrapped(scope, v - 1));
return p;
}
}
throw new UnsupportedOperationException();
}
}