Skip to content

Commit c94aebe

Browse files
committed
(feat) begin to impl interpreter
1 parent 8b239f0 commit c94aebe

File tree

11 files changed

+543
-22
lines changed

11 files changed

+543
-22
lines changed

src/main/java/com/googlecode/aviator/AviatorEvaluator.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public final class AviatorEvaluator {
4646
*/
4747
public static final int EVAL = 1;
4848

49+
/**
50+
* Evaluate expression in interpret mode.
51+
*/
52+
public static final int INTERPRET = 2;
53+
4954
/**
5055
* Aviator version
5156
*/
@@ -117,13 +122,13 @@ public static Map<String, Object> newEnv(final Object... args) {
117122
(args != null && args.length <= 16) ? new ArrayHashMap<String, Object>()
118123
: new HashMap<String, Object>(args != null ? args.length : 16);
119124

120-
if (args != null) {
121-
for (int i = 0; i < args.length; i += 2) {
122-
String key = (String) args[i];
123-
env.put(key, args[i + 1]);
124-
}
125-
}
126-
return env;
125+
if (args != null) {
126+
for (int i = 0; i < args.length; i += 2) {
127+
String key = (String) args[i];
128+
env.put(key, args[i + 1]);
129+
}
130+
}
131+
return env;
127132
}
128133

129134
/**

src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.googlecode.aviator.annotation.ImportScope;
5252
import com.googlecode.aviator.asm.Opcodes;
5353
import com.googlecode.aviator.code.CodeGenerator;
54+
import com.googlecode.aviator.code.InterpretCodeGenerator;
5455
import com.googlecode.aviator.code.NoneCodeGenerator;
5556
import com.googlecode.aviator.code.OptimizeCodeGenerator;
5657
import com.googlecode.aviator.code.asm.ASMCodeGenerator;
@@ -1052,11 +1053,11 @@ private void loadInternalLibs() {
10521053
* Compiled Expression cache
10531054
*/
10541055
private final ConcurrentHashMap<String/* text expression */, FutureTask<Expression>/*
1055-
* Compiled
1056-
* expression
1057-
* task
1058-
*/> expressionCache =
1059-
new ConcurrentHashMap<String, FutureTask<Expression>>();
1056+
* Compiled
1057+
* expression
1058+
* task
1059+
*/> expressionCache =
1060+
new ConcurrentHashMap<String, FutureTask<Expression>>();
10601061

10611062
private LRUMap<String, FutureTask<Expression>> expressionLRUCache;
10621063

@@ -1514,11 +1515,14 @@ public CodeGenerator newCodeGenerator(final AviatorClassLoader classLoader,
15141515
switch (getOptimizeLevel()) {
15151516
case AviatorEvaluator.COMPILE:
15161517
ASMCodeGenerator asmCodeGenerator =
1517-
new ASMCodeGenerator(this, sourceFile, classLoader, this.traceOutputStream);
1518+
new ASMCodeGenerator(this, sourceFile, classLoader, this.traceOutputStream);
15181519
asmCodeGenerator.start();
15191520
return asmCodeGenerator;
15201521
case AviatorEvaluator.EVAL:
15211522
return new OptimizeCodeGenerator(this, sourceFile, classLoader, this.traceOutputStream);
1523+
case AviatorEvaluator.INTERPRET:
1524+
// TODO
1525+
return new InterpretCodeGenerator(this, null);
15221526
default:
15231527
throw new IllegalArgumentException("Unknow option " + getOptimizeLevel());
15241528
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.googlecode.aviator;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import com.googlecode.aviator.code.interpreter.Context;
6+
import com.googlecode.aviator.code.interpreter.IR;
7+
import com.googlecode.aviator.lexer.SymbolTable;
8+
import com.googlecode.aviator.parser.VariableMeta;
9+
import com.googlecode.aviator.utils.Env;
10+
11+
public class InterpreExpression extends BaseExpression {
12+
13+
private final List<IR> instruments;
14+
15+
public InterpreExpression(final AviatorEvaluatorInstance instance, final List<VariableMeta> vars,
16+
final SymbolTable symbolTable, final List<IR> instruments) {
17+
super(instance, vars, symbolTable);
18+
this.instruments = instruments;
19+
20+
}
21+
22+
@Override
23+
public Object executeDirectly(final Map<String, Object> env) {
24+
Context ctx = new Context(this.instruments, (Env) env);
25+
IR ir = null;
26+
while ((ir = ctx.getPc()) != null) {
27+
ir.eval(ctx);
28+
if (!ctx.next()) {
29+
break;
30+
}
31+
}
32+
return ctx.peek().getValue(env);
33+
}
34+
}

src/main/java/com/googlecode/aviator/Options.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ public Value intoValue(final Object val) {
217217
int level = (int) val;
218218
if (level == AviatorEvaluator.EVAL) {
219219
return EVAL_VALUE;
220+
} else if (level == AviatorEvaluator.INTERPRET) {
221+
return INTERPRET_VALUE;
220222
} else {
221223
return COMPILE_VALUE;
222224
}
@@ -250,8 +252,9 @@ public boolean isValidValue(final Object val) {
250252
case ASSIGNABLE_ALLOWED_CLASS_SET:
251253
return val instanceof Set;
252254
case OPTIMIZE_LEVEL:
253-
return val instanceof Integer && (((Integer) val).intValue() == AviatorEvaluator.EVAL
254-
|| ((Integer) val).intValue() == AviatorEvaluator.COMPILE);
255+
final int level = ((Integer) val).intValue();
256+
return val instanceof Integer && (level == AviatorEvaluator.EVAL
257+
|| level == AviatorEvaluator.COMPILE || level == AviatorEvaluator.INTERPRET);
255258
case MAX_LOOP_COUNT:
256259
return val instanceof Long || val instanceof Integer;
257260
case MATH_CONTEXT:
@@ -272,6 +275,8 @@ public boolean isValidValue(final Object val) {
272275

273276
public static final Value COMPILE_VALUE = new Value(AviatorEvaluator.COMPILE);
274277

278+
public static final Value INTERPRET_VALUE = new Value(AviatorEvaluator.INTERPRET);
279+
275280
private static final Value FULL_FEATURE_SET = new Value(Feature.getFullFeatures());
276281
private static final boolean TRACE_EVAL_DEFAULT_VAL =
277282
Boolean.parseBoolean(System.getProperty("aviator.trace_eval", "false"));

0 commit comments

Comments
 (0)