-
Notifications
You must be signed in to change notification settings - Fork 0
/
JVMTool.java
386 lines (378 loc) · 24.1 KB
/
JVMTool.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
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import java.util.ArrayList;
import java.util.HashMap;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
public class JVMTool {
private static final String TYPE_NUMBER = "Ljava/math/BigDecimal;";
private static final String TYPE_TEXT = "Ljava/lang/String;";
private static final String TYPE_BOOLEAN = "Ljava/lang/Boolean;";
private static final String TYPE_NULL_OR_UNKNOWN = "Ljava/lang/Object;";
private final HashMap<String, Integer> variables = new HashMap<>();
private int variablesCounter = 0;
private final ArrayList<String> fields = new ArrayList<>();
private boolean addAddFunction = false;
private boolean addSubFunction = false;
private boolean addMulFunction = false;
private boolean addDivFunction = false;
private boolean addModFunction = false;
private boolean addPowFunction = false;
private String putVales(MethodVisitor methodWriter, ValueBase val, ClassWriter classWriter, String className) {
if (val instanceof SyntaxTree.Number) {
methodWriter.visitTypeInsn(NEW, "java/math/BigDecimal");
methodWriter.visitInsn(DUP);
methodWriter.visitLdcInsn(val.toString());
methodWriter.visitMethodInsn(INVOKESPECIAL, "java/math/BigDecimal", "<init>", "(Ljava/lang/String;)V", false);
return TYPE_NUMBER;
} else if (val instanceof SyntaxTree.Text) {
methodWriter.visitLdcInsn(val.getData());
return TYPE_TEXT;
} else if (val instanceof SyntaxTree.Boolean) {
if ((boolean)val.getData()) {
methodWriter.visitInsn(ICONST_1);
} else {
methodWriter.visitInsn(ICONST_0);
}
methodWriter.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
return TYPE_BOOLEAN;
} else if (val instanceof SyntaxTree.Null) {
methodWriter.visitInsn(ACONST_NULL);
} else if (val instanceof SyntaxTree.Variable) {
if (((SyntaxTree.Variable) val).getVariableName().contains(":")) {
Integer address;
address = variables.get(((SyntaxTree.Variable) val).getVariableName());
if (address == null) {
Errors.error(ErrorCodes.ERROR_VARIABLE_DOES_NOT_EXISTS, ((SyntaxTree.Variable) val).getVariableName());
}
methodWriter.visitVarInsn(ALOAD, address);
} else {
methodWriter.visitFieldInsn(GETSTATIC, className, ((SyntaxTree.Variable) val).getVariableName(),
"Ljava/lang/Object;");
}
} else if (val instanceof SyntaxTree.Add) {
addAddFunction = true;
putVales(methodWriter, ((SyntaxTree.Add) val).getV1(), classWriter, className);
putVales(methodWriter, ((SyntaxTree.Add) val).getV2(), classWriter, className);
methodWriter.visitMethodInsn(INVOKESTATIC, className, "#add", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
} else if (val instanceof SyntaxTree.Sub) {
addSubFunction = true;
putVales(methodWriter, ((SyntaxTree.Sub) val).getV1(), classWriter, className);
putVales(methodWriter, ((SyntaxTree.Sub) val).getV2(), classWriter, className);
methodWriter.visitMethodInsn(INVOKESTATIC, className, "#sub", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
} else if (val instanceof SyntaxTree.Mul) {
addMulFunction = true;
putVales(methodWriter, ((SyntaxTree.Mul) val).getV1(), classWriter, className);
putVales(methodWriter, ((SyntaxTree.Mul) val).getV2(), classWriter, className);
methodWriter.visitMethodInsn(INVOKESTATIC, className, "#mul", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
} else if (val instanceof SyntaxTree.Div) {
addDivFunction = true;
putVales(methodWriter, ((SyntaxTree.Div) val).getV1(), classWriter, className);
putVales(methodWriter, ((SyntaxTree.Div) val).getV2(), classWriter, className);
methodWriter.visitMethodInsn(INVOKESTATIC, className, "#div", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
} else if (val instanceof SyntaxTree.Mod) {
addModFunction = true;
putVales(methodWriter, ((SyntaxTree.Mod) val).getV1(), classWriter, className);
putVales(methodWriter, ((SyntaxTree.Mod) val).getV2(), classWriter, className);
methodWriter.visitMethodInsn(INVOKESTATIC, className, "#div", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
} else if (val instanceof SyntaxTree.Pow) {
addPowFunction = true;
putVales(methodWriter, ((SyntaxTree.Pow) val).getV1(), classWriter, className);
putVales(methodWriter, ((SyntaxTree.Pow) val).getV2(), classWriter, className);
methodWriter.visitMethodInsn(INVOKESTATIC, className, "#pow", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
} else if (val instanceof SyntaxTree.PrintFunction) {
syntaxTreeToJVMClass2(((SyntaxTree.PrintFunction) val).getProgram(), methodWriter, classWriter, className);
} else if (val instanceof SyntaxTree.ExitFunction) {
syntaxTreeToJVMClass2(((SyntaxTree.ExitFunction) val).getProgram(), methodWriter, classWriter, className);
}
return TYPE_NULL_OR_UNKNOWN;
}
public byte[] syntaxTreeToJVMClass(ProgramBase program, String className) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
classWriter.visit(V1_8, ACC_PUBLIC, className, null, "java/lang/Object", null);
syntaxTreeToJVMClass1(program, classWriter, className);
classWriter.visitEnd();
return classWriter.toByteArray();
}
public void syntaxTreeToJVMClass1(ProgramBase program, ClassWriter classWriter, String className) {
MethodVisitor mainMethodWriter = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mainMethodWriter.visitCode();
Label methodStart = new Label();
Label methodEnd = new Label();
mainMethodWriter.visitLabel(methodStart);
syntaxTreeToJVMClass2(program, mainMethodWriter, classWriter, className);
mainMethodWriter.visitInsn(RETURN);
mainMethodWriter.visitLabel(methodEnd);
mainMethodWriter.visitMaxs(1, 1);
mainMethodWriter.visitEnd();
if (addAddFunction) {
MethodVisitor addMethodWriter = classWriter.visitMethod(ACC_PRIVATE | ACC_STATIC, "#add", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
addMethodWriter.visitCode();
addMethodWriter.visitVarInsn(ALOAD, 0);
addMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
Label label = new Label();
addMethodWriter.visitJumpInsn(IFEQ, label);
addMethodWriter.visitVarInsn(ALOAD, 1);
addMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
addMethodWriter.visitJumpInsn(IFEQ, label);
addMethodWriter.visitVarInsn(ALOAD, 0);
addMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
addMethodWriter.visitVarInsn(ALOAD, 1);
addMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
addMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "add", "(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;", false);
addMethodWriter.visitInsn(ARETURN);
addMethodWriter.visitLabel(label);
addMethodWriter.visitTypeInsn(NEW, "java/lang/StringBuilder");
addMethodWriter.visitInsn(DUP);
addMethodWriter.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
addMethodWriter.visitVarInsn(ALOAD, 0);
addMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
addMethodWriter.visitVarInsn(ALOAD, 1);
addMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
addMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
addMethodWriter.visitInsn(ARETURN);
addMethodWriter.visitMaxs(1, 1);
addMethodWriter.visitEnd();
}
if (addSubFunction) {
MethodVisitor subMethodWriter = classWriter.visitMethod(ACC_PRIVATE | ACC_STATIC, "#sub", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
subMethodWriter.visitCode();
subMethodWriter.visitVarInsn(ALOAD, 0);
subMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
Label label = new Label();
subMethodWriter.visitJumpInsn(IFEQ, label);
subMethodWriter.visitVarInsn(ALOAD, 1);
subMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
subMethodWriter.visitJumpInsn(IFEQ, label);
subMethodWriter.visitVarInsn(ALOAD, 0);
subMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
subMethodWriter.visitVarInsn(ALOAD, 1);
subMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
subMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "subtract", "(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;", false);
subMethodWriter.visitInsn(ARETURN);
subMethodWriter.visitLabel(label);
subMethodWriter.visitVarInsn(ALOAD, 0);
subMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false);
subMethodWriter.visitVarInsn(ALOAD, 1);
subMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false);
subMethodWriter.visitLdcInsn("");
subMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "replace", "(Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Ljava/lang/String;", false);
subMethodWriter.visitInsn(ARETURN);
subMethodWriter.visitMaxs(1, 1);
subMethodWriter.visitEnd();
}
if (addMulFunction) {
MethodVisitor mulMethodWriter = classWriter.visitMethod(ACC_PRIVATE | ACC_STATIC, "#mul", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
mulMethodWriter.visitCode();
mulMethodWriter.visitVarInsn(ALOAD, 0);
mulMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
Label label = new Label();
Label label1 = new Label();
Label label2 = new Label();
Label labelLoop = new Label();
Label labelLoop1 = new Label();
Label labelLoopStart = new Label();
Label labelLoopStart1 = new Label();
mulMethodWriter.visitJumpInsn(IFEQ, label);
mulMethodWriter.visitVarInsn(ALOAD, 1);
mulMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
mulMethodWriter.visitJumpInsn(IFEQ, label);
mulMethodWriter.visitVarInsn(ALOAD, 0);
mulMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
mulMethodWriter.visitVarInsn(ALOAD, 1);
mulMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "multiply", "(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;", false);
mulMethodWriter.visitInsn(ARETURN);
mulMethodWriter.visitLabel(label);
mulMethodWriter.visitVarInsn(ALOAD, 0);
mulMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
mulMethodWriter.visitJumpInsn(IFEQ, label1);
mulMethodWriter.visitTypeInsn(NEW, "java/lang/StringBuilder");
mulMethodWriter.visitInsn(DUP);
mulMethodWriter.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
mulMethodWriter.visitVarInsn(ASTORE, 2);
mulMethodWriter.visitVarInsn(ALOAD, 0);
mulMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "intValue", "()I", false);
mulMethodWriter.visitVarInsn(ISTORE, 3);
mulMethodWriter.visitLabel(labelLoopStart);
mulMethodWriter.visitVarInsn(ILOAD, 3);
mulMethodWriter.visitJumpInsn(IFLE, labelLoop);
mulMethodWriter.visitVarInsn(ALOAD, 2);
mulMethodWriter.visitVarInsn(ALOAD, 1);
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
mulMethodWriter.visitInsn(POP);
mulMethodWriter.visitIincInsn(3, -1);
mulMethodWriter.visitJumpInsn(GOTO, labelLoopStart);
mulMethodWriter.visitLabel(labelLoop);
mulMethodWriter.visitVarInsn(ALOAD, 2);
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mulMethodWriter.visitInsn(ARETURN);
mulMethodWriter.visitLabel(label1);
mulMethodWriter.visitVarInsn(ALOAD, 1);
mulMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
mulMethodWriter.visitJumpInsn(IFEQ, label2);
mulMethodWriter.visitTypeInsn(NEW, "java/lang/StringBuilder");
mulMethodWriter.visitInsn(DUP);
mulMethodWriter.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
mulMethodWriter.visitVarInsn(ASTORE, 2);
mulMethodWriter.visitVarInsn(ALOAD, 1);
mulMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "intValue", "()I", false);
mulMethodWriter.visitVarInsn(ISTORE, 3);
mulMethodWriter.visitLabel(labelLoopStart1);
mulMethodWriter.visitVarInsn(ILOAD, 3);
mulMethodWriter.visitJumpInsn(IFLE, labelLoop1);
mulMethodWriter.visitVarInsn(ALOAD, 2);
mulMethodWriter.visitVarInsn(ALOAD, 0);
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
mulMethodWriter.visitInsn(POP);
mulMethodWriter.visitIincInsn(3, -1);
mulMethodWriter.visitJumpInsn(GOTO, labelLoopStart1);
mulMethodWriter.visitLabel(labelLoop1);
mulMethodWriter.visitVarInsn(ALOAD, 2);
mulMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mulMethodWriter.visitInsn(ARETURN);
mulMethodWriter.visitLabel(label2);
mulMethodWriter.visitInsn(ACONST_NULL);
mulMethodWriter.visitInsn(ARETURN);
mulMethodWriter.visitMaxs(1, 1);
mulMethodWriter.visitEnd();
}
if (addDivFunction) {
MethodVisitor divMethodWriter = classWriter.visitMethod(ACC_PRIVATE | ACC_STATIC, "#div", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
divMethodWriter.visitCode();
divMethodWriter.visitVarInsn(ALOAD, 0);
divMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
Label label = new Label();
divMethodWriter.visitJumpInsn(IFEQ, label);
divMethodWriter.visitVarInsn(ALOAD, 1);
divMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
divMethodWriter.visitJumpInsn(IFEQ, label);
divMethodWriter.visitVarInsn(ALOAD, 0);
divMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
divMethodWriter.visitVarInsn(ALOAD, 1);
divMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
divMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "divide", "(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;", false);
divMethodWriter.visitInsn(ARETURN);
divMethodWriter.visitLabel(label);
divMethodWriter.visitInsn(ACONST_NULL);
divMethodWriter.visitInsn(ARETURN);
divMethodWriter.visitMaxs(1, 1);
divMethodWriter.visitEnd();
}
if (addModFunction) {
MethodVisitor divMethodWriter = classWriter.visitMethod(ACC_PRIVATE | ACC_STATIC, "#div", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
divMethodWriter.visitCode();
divMethodWriter.visitVarInsn(ALOAD, 0);
divMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
Label label = new Label();
divMethodWriter.visitJumpInsn(IFEQ, label);
divMethodWriter.visitVarInsn(ALOAD, 1);
divMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
divMethodWriter.visitJumpInsn(IFEQ, label);
divMethodWriter.visitVarInsn(ALOAD, 0);
divMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
divMethodWriter.visitVarInsn(ALOAD, 1);
divMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
divMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "remainder", "(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;", false);
divMethodWriter.visitInsn(ARETURN);
divMethodWriter.visitLabel(label);
divMethodWriter.visitInsn(ACONST_NULL);
divMethodWriter.visitInsn(ARETURN);
divMethodWriter.visitMaxs(1, 1);
divMethodWriter.visitEnd();
}
if (addPowFunction) {
MethodVisitor powMethodWriter = classWriter.visitMethod(ACC_PRIVATE | ACC_STATIC, "#pow", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
powMethodWriter.visitCode();
powMethodWriter.visitVarInsn(ALOAD, 0);
powMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
Label label = new Label();
powMethodWriter.visitJumpInsn(IFEQ, label);
powMethodWriter.visitVarInsn(ALOAD, 1);
powMethodWriter.visitTypeInsn(INSTANCEOF, "java/math/BigDecimal");
powMethodWriter.visitJumpInsn(IFEQ, label);
powMethodWriter.visitVarInsn(ALOAD, 0);
powMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
powMethodWriter.visitVarInsn(ALOAD, 1);
powMethodWriter.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
powMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "intValue", "()I", false);
powMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "pow", "(I)Ljava/math/BigDecimal;", false);
powMethodWriter.visitInsn(ARETURN);
powMethodWriter.visitLabel(label);
powMethodWriter.visitInsn(ACONST_NULL);
powMethodWriter.visitInsn(ARETURN);
powMethodWriter.visitMaxs(1, 1);
powMethodWriter.visitEnd();
}
}
public void syntaxTreeToJVMClass2(ProgramBase program, MethodVisitor methodVisitor, ClassWriter classWriter, String className) {
if (program instanceof SyntaxTree.Programs) {
for (ProgramBase program1 : ((SyntaxTree.Programs) program).getPrograms()) {
syntaxTreeToJVMClass2(program1, methodVisitor, classWriter, className);
}
} else if (program instanceof SyntaxTree.Print) {
ValueBase[] args = ((SyntaxTree.Print) program).getArgs();
for (int i = 0; i < args.length; i++) {
methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
("(" + putVales(methodVisitor, args[i], classWriter, className) + ")V").replace("java/lang/Boolean", "java/lang/Object")
.replace("java/math/BigDecimal", "java/lang/Object"), false);
if (i < args.length - 1) {
methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print",
("(" + putVales(methodVisitor, (((SyntaxTree.Print) program).getSeparator()), classWriter, className) + ")V")
.replace("java/lang/Boolean", "java/lang/Object")
.replace("java/math/BigDecimal", "java/lang/Object"), false);
}
}
} else if (program instanceof SyntaxTree.SetVariable) {
if (((SyntaxTree.SetVariable) program).getVariableName().contains(":")) {
putVales(methodVisitor, ((SyntaxTree.SetVariable) program).getVariableValue(), classWriter, className);
int address;
if (variables.containsKey(((SyntaxTree.SetVariable) program).getVariableName())) {
address = variables.get(((SyntaxTree.SetVariable) program).getVariableName());
} else {
variablesCounter++;
address = variablesCounter++;
variables.put(((SyntaxTree.SetVariable) program).getVariableName(), address);
}
methodVisitor.visitVarInsn(ASTORE, address);
} else {
putVales(methodVisitor, ((SyntaxTree.SetVariable) program).getVariableValue(), classWriter, className);
if (!fields.contains(((SyntaxTree.SetVariable) program).getVariableName())) {
classWriter.visitField(ACC_PRIVATE | ACC_STATIC, ((SyntaxTree.SetVariable) program).getVariableName(),
"Ljava/lang/Object;", null, null);
fields.add(((SyntaxTree.SetVariable) program).getVariableName());
}
methodVisitor.visitFieldInsn(PUTSTATIC, className, ((SyntaxTree.SetVariable) program).getVariableName(),
"Ljava/lang/Object;");
}
} else if (program instanceof SyntaxTree.Exit) {
putVales(methodVisitor, ((SyntaxTree.Exit) program).getStatus(), classWriter, className);
methodVisitor.visitTypeInsn(CHECKCAST, "java/math/BigDecimal");
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/math/BigDecimal", "intValue", "()I", false);
methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/System", "exit", "(I)V", false);
} else if (program instanceof SyntaxTree.ExecuteValue) {
putVales(methodVisitor, ((SyntaxTree.ExecuteValue) program).getValue(), classWriter, className);
} else if (program instanceof SyntaxTree.If) {
Label label = new Label();
Label label1 = null;
if (((SyntaxTree.If) program).getElseProgram() != null) {
label1 = new Label();
}
putVales(methodVisitor, ((SyntaxTree.If) program).getCondition(), classWriter, className);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
methodVisitor.visitJumpInsn(IFEQ, label);
syntaxTreeToJVMClass2(((SyntaxTree.If) program).getProgram(), methodVisitor, classWriter, className);
if (((SyntaxTree.If) program).getElseProgram() != null)
methodVisitor.visitJumpInsn(GOTO, label1);
methodVisitor.visitLabel(label);
if (((SyntaxTree.If) program).getElseProgram() != null) {
syntaxTreeToJVMClass2(((SyntaxTree.If) program).getElseProgram(), methodVisitor, classWriter, className);
methodVisitor.visitLabel(label1);
}
}
}
}