-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathJassInterpreter.java
More file actions
526 lines (434 loc) · 18.3 KB
/
Copy pathJassInterpreter.java
File metadata and controls
526 lines (434 loc) · 18.3 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
package de.peeeq.wurstio.jassinterpreter;
import com.google.common.collect.Maps;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.intermediatelang.*;
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;
import de.peeeq.wurstscript.intermediatelang.interpreter.TimerMockHandler;
import de.peeeq.wurstscript.jassAst.*;
import de.peeeq.wurstscript.jassIm.Element;
import de.peeeq.wurstscript.jassIm.ImProg;
import de.peeeq.wurstscript.jassinterpreter.ExitwhenException;
import de.peeeq.wurstscript.jassinterpreter.ReturnException;
import de.peeeq.wurstscript.utils.Utils;
import org.eclipse.jdt.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JassInterpreter implements AbstractInterpreter {
private JassProg prog;
private static final ReturnException staticReturnException = new ReturnException(null);
private Map<String, ILconst> globalVarMap;
private boolean trace = false;
private final Map<String, ExecutableJassFunction> functionCache = new HashMap<>();
private final TimerMockHandler timerMockHandler = new TimerMockHandler();
public void loadProgram(JassProg prog) {
this.prog = prog;
globalVarMap = Maps.newLinkedHashMap();
List<JassVar> globals = prog.getGlobals();
// globals initialisieren
for (JassVar v : globals) {
ILconst value = null;
if (v instanceof JassArrayVar) {
value = new JassArray(v.getType());
} else {
// --- not initialized
}
globalVarMap.put(v.getName(), value);
}
}
public static ILconst getDefaultValue(String type) {
switch (type) {
case "integer":
return new ILconstInt(0);
case "boolean":
return ILconstBool.FALSE;
case "real":
return new ILconstReal(0.0f);
default:
return ILconstNull.instance();
}
}
public ILconst executeFunction(String name, ILconst... arguments) {
if (trace) {
WLogger.trace(() -> name + "( " + Utils.join(arguments, ", ") + ")");
}
ExecutableJassFunction func = searchFunction(name);
return func.execute(this, arguments);
}
@Nullable
ILconst executeJassFunction(JassFunction func, ILconst... arguments) {
List<JassVar> locals = func.getLocals();
List<JassStatement> body = func.getBody();
Map<String, ILconst> localVarMap = Maps.newLinkedHashMap();
// locals initialisieren
for (JassVar v : locals) {
ILconst value = null;
if (v instanceof JassArrayVar) {
value = new JassArray(v.getType());
}
localVarMap.put(v.getName(), value);
}
if (func.getParams().size() != arguments.length) {
throw new InterpreterException("Wrong number of parameters: " + func.getParams().size() + " != " + arguments.length);
}
int i = 0;
for (JassSimpleVar v : func.getParams()) {
localVarMap.put(v.getName(), arguments[i]);
i++;
}
try {
this.executeStatements(localVarMap, body);
} catch (ReturnException e) {
if (trace) {
WLogger.trace(() -> "end function " + func.getName() + " returns " + e.getVal());
}
return e.getVal();
}
if (trace) {
WLogger.trace(() -> "end function " + func.getName());
}
return null;
}
private void executeStatements(Map<String, ILconst> localVarMap, List<JassStatement> body) {
for (JassStatement s : body) {
executeStatement(localVarMap, s);
}
}
private void executeStatement(final Map<String, ILconst> localVarMap, JassStatement s) {
s.match(new JassStatement.MatcherVoid() {
@Override
public void case_JassStmtSetArray(JassStmtSetArray jassStmtSetArray) {
ILconst right = executeExpr(localVarMap, jassStmtSetArray.getRight());
ILconstInt index = (ILconstInt) executeExpr(localVarMap, jassStmtSetArray.getIndex());
JassArray v = (JassArray) getVarValue(localVarMap, jassStmtSetArray.getLeft());
v.set(index.getVal(), right);
}
@Override
public void case_JassStmtSet(JassStmtSet jassStmtSet) {
ILconst right = executeExpr(localVarMap, jassStmtSet.getRight());
setVarValue(localVarMap, jassStmtSet.getLeft(), right);
}
@Override
public void case_JassStmtReturnVoid(JassStmtReturnVoid jassStmtReturnVoid) {
throw staticReturnException.setVal(null);
}
@Override
public void case_JassStmtReturn(JassStmtReturn jassStmtReturn) {
ILconst c = executeExpr(localVarMap, jassStmtReturn.getReturnValue());
throw staticReturnException.setVal(c);
}
@Override
public void case_JassStmtLoop(JassStmtLoop jassStmtLoop) {
try {
while (true) {
executeStatements(localVarMap, jassStmtLoop.getBody());
}
} catch (ExitwhenException e) {
// end loop
}
}
@Override
public void case_JassStmtIf(JassStmtIf jassStmtIf) {
ILconstBool cond = (ILconstBool) executeExpr(localVarMap, jassStmtIf.getCond());
if (cond.getVal()) {
executeStatements(localVarMap, jassStmtIf.getThenBlock());
} else {
executeStatements(localVarMap, jassStmtIf.getElseBlock());
}
}
@Override
public void case_JassStmtExitwhen(JassStmtExitwhen jassStmtExitwhen) {
ILconstBool cond = (ILconstBool) executeExpr(localVarMap, jassStmtExitwhen.getCond());
if (cond.getVal()) {
throw ExitwhenException.instance();
}
}
@Override
public void case_JassStmtCall(JassStmtCall jassStmtCall) {
ILconst[] args = new ILconst[jassStmtCall.getArguments().size()];
int i = 0;
for (JassExpr arg : jassStmtCall.getArguments()) {
args[i] = executeExpr(localVarMap, arg);
i++;
}
executeFunction(jassStmtCall.getFuncName(), args);
}
});
// if (s instanceof ILif) {
// translateStatementIf(localVarMap, (ILif)s);
// } else if (s instanceof ILsetVar) {
// translateIlcopy( localVarMap, (ILsetVar)s);
// } else if (s instanceof IlsetConst) {
// translateIlsetConst( localVarMap, (IlsetConst)s);
// } else if (s instanceof ILsetBinary) {
// translateIlbinary( localVarMap, (ILsetBinary)s);
// } else if (s instanceof IlsetUnary) {
// translateIlunary(localVarMap, (IlsetUnary) s);
// } else if (s instanceof ILreturn) {
// translateReturn(localVarMap, (ILreturn) s);
// } else if (s instanceof ILloop) {
// translateLoop(localVarMap, (ILloop) s);
// } else if (s instanceof ILexitwhen) {
// translateExitwhen(localVarMap, (ILexitwhen) s);
// } else if (s instanceof ILfunctionCall) {
// translateFunctionCall(localVarMap, (ILfunctionCall) s);
// } else if (s instanceof IlbuildinFunctionCall) {
// translateBuildinFunction(localVarMap, (IlbuildinFunctionCall) s);
// } else if (s instanceof ILsetBinaryCR) {
// translateILsetBinaryCR(localVarMap, (ILsetBinaryCR) s);
// } else if (s instanceof ILsetBinaryCL) {
// translateILsetBinaryCL(localVarMap, (ILsetBinaryCL) s);
// } else if (s instanceof ILarraySetVar) {
// translateILarraySetVar(localVarMap, (ILarraySetVar) s);
// } else if (s instanceof ILsetVarArray) {
// translateILsetVarArray(localVarMap, (ILsetVarArray) s);
// } else if (s instanceof ILerror) {
// throw new InterpreterException("IL execution error: " + lookupVarValue(localVarMap, ((ILerror) s).msg));
// } else {
//
// throw new InterpreterException("not implemented " + s);
// }
}
private ILconst executeExpr(final Map<String, ILconst> localVarMap, JassExpr expr) {
return expr.match(new JassExpr.Matcher<ILconst>() {
@Override
public ILconst case_JassExprVarArrayAccess(JassExprVarArrayAccess e) {
JassArray ar = (JassArray) getVarValue(localVarMap, e.getVarName());
ILconstInt index = (ILconstInt) executeExpr(localVarMap, e.getIndex());
return ar.get(index.getVal());
}
@Override
public ILconst case_JassExprRealVal(JassExprRealVal e) {
return new ILconstReal(e.getValR());
}
@Override
public ILconst case_JassExprUnary(JassExprUnary e) {
final ILconst right = executeExpr(localVarMap, e.getRight());
return e.getOpU().match(new JassOpUnary.Matcher<ILconst>() {
@Override
public ILconst case_JassOpNot(JassOpNot jassOpNot) {
ILconstBool b = (ILconstBool) right;
return b.negate();
}
@Override
public ILconst case_JassOpMinus(JassOpMinus jassOpMinus) {
return ((ILconstNum) right).negate();
}
});
}
@Override
public ILconst case_JassExprFuncRef(JassExprFuncRef e) {
return new ILconstFuncRef(e.getFuncName());
}
@Override
public ILconst case_JassExprBoolVal(JassExprBoolVal e) {
return ILconstBool.instance(e.getValB());
}
@Override
public ILconst case_JassExprBinary(final JassExprBinary e) {
return e.getOp().match(new JassOpBinary.Matcher<ILconst>() {
ILconst getLeft() {
return executeExpr(localVarMap, e.getLeftExpr());
}
ILconstNum getLeftNum() {
return (ILconstNum) getLeft();
}
ILconstAddable getLeftAddable() {
return (ILconstAddable) getLeft();
}
ILconstBool getLeftBool() {
return (ILconstBool) getLeft();
}
ILconst getRight() {
return executeExpr(localVarMap, e.getRight());
}
ILconstNum getRightNum() {
return (ILconstNum) getRight();
}
ILconstAddable getRightAddable() {
return (ILconstAddable) getRight();
}
ILconstBool getRightBool() {
return (ILconstBool) getRight();
}
@Override
public ILconst case_JassOpDiv(JassOpDiv jassOpDiv) {
return getLeftNum().div(getRightNum());
}
@Override
public ILconst case_JassOpLess(JassOpLess jassOpLess) {
return getLeftNum().less(getRightNum());
}
@Override
public ILconst case_JassOpAnd(JassOpAnd jassOpAnd) {
if (getLeftBool().getVal()) {
return getRightBool();
} else {
return ILconstBool.FALSE;
}
}
@Override
public ILconst case_JassOpUnequals(JassOpUnequals jassOpUnequals) {
return ILconstBool.instance(!getLeft().isEqualTo(getRight()));
}
@Override
public ILconst case_JassOpGreaterEq(JassOpGreaterEq jassOpGreaterEq) {
return getLeftNum().greaterEq(getRightNum());
}
@Override
public ILconst case_JassOpMinus(JassOpMinus jassOpMinus) {
return getLeftNum().sub(getRightNum());
}
@Override
public ILconst case_JassOpMult(JassOpMult jassOpMult) {
return getLeftNum().mul(getRightNum());
}
@Override
public ILconst case_JassOpGreater(JassOpGreater jassOpGreater) {
return getLeftNum().greater(getRightNum());
}
@Override
public ILconst case_JassOpPlus(JassOpPlus jassOpPlus) {
return getLeftAddable().add(getRightAddable());
}
@Override
public ILconst case_JassOpLessEq(JassOpLessEq jassOpLessEq) {
return getLeftNum().lessEq(getRightNum());
}
@Override
public ILconst case_JassOpOr(JassOpOr jassOpOr) {
if (getLeftBool().getVal()) {
return ILconstBool.TRUE;
} else {
return getRightBool();
}
}
@Override
public ILconst case_JassOpEquals(JassOpEquals jassOpEquals) {
return ILconstBool.instance(getLeft().isEqualTo(getRight()));
}
});
}
@Override
public ILconst case_JassExprStringVal(JassExprStringVal e) {
return new ILconstString(e.getValS());
}
@Override
public ILconst case_JassExprIntVal(JassExprIntVal e) {
return ILconstInt.create(Integer.parseInt(e.getValI()));
}
@Override
public ILconst case_JassExprFunctionCall(JassExprFunctionCall e) {
ILconst[] args = new ILconst[e.getArguments().size()];
int i = 0;
for (JassExpr arg : e.getArguments()) {
args[i] = executeExpr(localVarMap, arg);
i++;
}
return executeFunction(e.getFuncName(), args);
}
@Override
public ILconst case_JassExprVarAccess(JassExprVarAccess e) {
return getVarValue(localVarMap, e.getVarName());
}
@Override
public ILconst case_JassExprNull(JassExprNull e) {
return ILconstNull.instance();
}
});
}
private void setVarValue(Map<String, ILconst> localVarMap, String varName, ILconst s) {
if (isLocal(localVarMap, varName)) {
localVarMap.put(varName, s);
} else if (isGlobal(varName)) {
globalVarMap.put(varName, s);
} else {
throw new InterpreterException("var " + varName + " is neither local nor global?");
}
}
private boolean isLocal(Map<String, ILconst> localVarMap, String varName) {
return localVarMap.containsKey(varName);
}
private boolean isGlobal(String varName) {
return globalVarMap.containsKey(varName);
}
private ILconst getVarValue(Map<String, ILconst> localVarMap, String name) {
ILconst value = localVarMap.get(name);
if (value == null) {
value = globalVarMap.get(name);
if (value == null) {
throw new InterpreterException("Variable " + name + " not found.");
}
}
return value;
}
private ExecutableJassFunction searchFunction(String fname) {
return functionCache.computeIfAbsent(fname, name -> {
for (JassFunction f : prog.getFunctions()) {
if (f.getName().equals(name)) {
if (!f.getIsCompiletimeNative()) {
return new UserDefinedJassFunction(f);
}
}
}
return searchNativeJassFunction(name);
});
}
private ExecutableJassFunction searchNativeJassFunction(String name) {
if (name.equals("ExecuteFunc")) {
return executeFuncNative();
}
ReflectionNativeProvider nf = new ReflectionNativeProvider(this);
ExecutableJassFunction functionPair = nf.getFunctionPair(name);
return functionPair != null ? functionPair : new UnknownJassFunction(name);
}
private ExecutableJassFunction executeFuncNative() {
return (jassInterpreter, arguments) -> {
ILconstString funcName = (ILconstString) arguments[0];
jassInterpreter.executeFunction(funcName.getVal());
return ILconstBool.TRUE;
};
}
public void trace(boolean b) {
trace = b;
}
@Override
public void runFuncRef(ILconstFuncRef f, @Nullable Element trace) {
if (f == null) {
throw new RuntimeException("Function was null in " + trace);
}
ExecutableJassFunction func = searchFunction(f.getFuncName());
func.execute(this);
}
@Override
public TimerMockHandler getTimerMockHandler() {
return timerMockHandler;
}
public void runProgram() {
for (JassVar var : prog.getGlobals()) {
if (var instanceof JassInitializedVar) {
JassInitializedVar iVar = (JassInitializedVar) var;
globalVarMap.put(iVar.getName(), executeExpr(Collections.emptyMap(), iVar.getVal()));
}
}
executeFunction("main");
timerMockHandler.completeTimers();
}
@Override
public void completeTimers() {
timerMockHandler.completeTimers();
}
@Override
public ImProg getImProg() {
throw new UnsupportedOperationException("Not supported in Jass interpreter.");
}
@Override
public int getInstanceCount(int val) {
throw new UnsupportedOperationException("Not supported in Jass interpreter.");
}
@Override
public int getMaxInstanceCount(int val) {
throw new UnsupportedOperationException("Not supported in Jass interpreter.");
}
}