Skip to content

Commit 549367d

Browse files
committed
(feat) Impl printInstruments for InterpretExpression
1 parent 0abd372 commit 549367d

File tree

5 files changed

+58
-25
lines changed

5 files changed

+58
-25
lines changed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.googlecode.aviator;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
35
import java.util.IdentityHashMap;
46
import java.util.List;
57
import java.util.Map;
@@ -11,6 +13,7 @@
1113
import com.googlecode.aviator.lexer.SymbolTable;
1214
import com.googlecode.aviator.lexer.token.Token;
1315
import com.googlecode.aviator.parser.VariableMeta;
16+
import com.googlecode.aviator.runtime.LambdaFunctionBootstrap;
1417
import com.googlecode.aviator.runtime.RuntimeUtils;
1518
import com.googlecode.aviator.runtime.type.AviatorJavaType;
1619
import com.googlecode.aviator.runtime.type.AviatorObject;
@@ -61,20 +64,43 @@ public AviatorObject loadConstant(final Token<?> token) {
6164
return this.constantPool.get(token);
6265
}
6366

67+
public void printInstruments() {
68+
traceInstruments(Collections.<String, Object>emptyMap(), null, true);
69+
}
70+
71+
private void traceInstruments(final Map<String, Object> env, final String name,
72+
final boolean traceLambda) {
73+
int pc = 0;
74+
RuntimeUtils.printlnTrace(env, (name == null ? this.sourceFile : name) + " instruments: ");
75+
for (IR ir : this.instruments) {
76+
RuntimeUtils.printlnTrace(env, " " + (pc++) + " " + ir.toString());
77+
}
78+
RuntimeUtils.printlnTrace(env, " " + pc + " return");
79+
80+
if (this.lambdaBootstraps != null) {
81+
final List<LambdaFunctionBootstrap> bootstraps =
82+
new ArrayList<>(this.lambdaBootstraps.values());
83+
Collections.sort(bootstraps);
84+
for (LambdaFunctionBootstrap bootstrap : bootstraps) {
85+
final Expression exp = bootstrap.getExpression();
86+
if (exp instanceof InterpretExpression) {
87+
InterpretExpression iexp = (InterpretExpression) exp;
88+
iexp.traceInstruments(env, bootstrap.getName(), traceLambda);
89+
} else {
90+
RuntimeUtils.printlnTrace(env, bootstrap.getName() + " instruments: " + exp);
91+
}
92+
}
93+
}
94+
}
95+
6496
@Override
6597
public Object executeDirectly(final Map<String, Object> env) {
6698
final boolean trace = RuntimeUtils.isTracedEval(env);
6799
if (trace) {
68-
int pc = 0;
69-
RuntimeUtils.printlnTrace(env, "Expression instruments: ");
70-
for (IR ir : this.instruments) {
71-
RuntimeUtils.printlnTrace(env, " " + (pc++) + " " + ir.toString());
72-
}
73-
RuntimeUtils.printlnTrace(env, " " + pc + " return");
100+
traceInstruments(env, null, false);
74101
RuntimeUtils.printlnTrace(env, "Execute instruments: ");
75102
}
76103

77-
78104
InterpretContext ctx = new InterpretContext(this, this.instruments, (Env) env);
79105
IR ir = null;
80106
while ((ir = ctx.getPc()) != null) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public Object executeDirectly(final Map<String, Object> env) {
4646
}
4747
return this.result;
4848
}
49+
50+
@Override
51+
public String toString() {
52+
return "LiteralExpression [result=" + this.result + "]";
53+
}
4954
}

src/main/java/com/googlecode/aviator/code/interpreter/ir/OperatorIR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ public void eval(final InterpretContext context) {
9595

9696
@Override
9797
public String toString() {
98-
return this.op.name();
98+
return this.op.name().toLowerCase();
9999
}
100100
}

src/main/java/com/googlecode/aviator/runtime/LambdaFunctionBootstrap.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author dennis
1717
*
1818
*/
19-
public class LambdaFunctionBootstrap {
19+
public class LambdaFunctionBootstrap implements Comparable<LambdaFunctionBootstrap> {
2020
// the generated lambda class name
2121
private final String name;
2222
// The compiled lambda body expression
@@ -30,6 +30,11 @@ public class LambdaFunctionBootstrap {
3030
private final ThreadLocal<LambdaFunction> fnLocal = new ThreadLocal<>();
3131

3232

33+
@Override
34+
public int compareTo(final LambdaFunctionBootstrap o) {
35+
return this.name.compareTo(o.name);
36+
}
37+
3338
public String getName() {
3439
return this.name;
3540
}
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
package com.googlecode.aviator.example;
22

3+
import java.util.Arrays;
4+
import java.util.List;
35
import com.googlecode.aviator.AviatorEvaluator;
4-
import com.googlecode.aviator.EvalMode;
6+
import com.googlecode.aviator.Expression;
57

68

79
public class SimpleExample {
810
public static void main(final String[] args) throws Exception {
9-
// AviatorEvaluator.setOption(Options.EVAL_MODE, EvalMode.INTERPRETER);
10-
// AviatorEvaluator.setOption(Options.TRACE_EVAL, true);
11-
12-
AviatorEvaluator.newInstance(EvalMode.INTERPRETER);
13-
Object result = AviatorEvaluator.execute("a=1; '#{a+100}'");
11+
Long result = (Long) AviatorEvaluator.execute("1+2+3");
1412
System.out.println(result);
15-
System.out.println(1 == 1 && 2 != 2 ? 1 : 2 == 3 ? 4 : 5);
1613

17-
// String hello = (String) AviatorEvaluator.execute("'hello,' + name",
18-
// AviatorEvaluator.newEnv("name", "aviator"));
19-
// System.out.println(hello);
20-
//
21-
// Expression exp = AviatorEvaluator.compile("map(list, lambda(v) -> v + u end)");
22-
// System.out.println("Uninitialized global variables: " + exp.getVariableFullNames());
23-
// List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
24-
// int u = 99;
25-
// System.out.println("executed: " + exp.execute(exp.newEnv("list", list, "u", u)));
14+
String hello = (String) AviatorEvaluator.execute("'hello,' + name",
15+
AviatorEvaluator.newEnv("name", "aviator"));
16+
System.out.println(hello);
17+
18+
Expression exp = AviatorEvaluator.compile("map(list, lambda(v) -> v + u end)");
19+
System.out.println("Uninitialized global variables: " + exp.getVariableFullNames());
20+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
21+
int u = 99;
22+
System.out.println("executed: " + exp.execute(exp.newEnv("list", list, "u", u)));
2623
}
2724
}

0 commit comments

Comments
 (0)