use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class Benchmark method benchCompile.
private static void benchCompile() {
System.setProperty("aviator.preferClassloaderDefiner", "false");
// System.setProperty("aviator.preferClassloaderDefiner", "true");
long start = System.currentTimeMillis();
long sum = 0;
for (int i = 0; i < 100000; i++) {
Expression exp = AviatorEvaluator.compile("a+b*c" + i);
// prevent JIT
sum += exp.hashCode();
}
System.out.println(System.currentTimeMillis() - start);
// prevent JIT
System.out.println(sum);
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class InterpreterExample method main.
public static void main(final String[] args) {
AviatorEvaluatorInstance engine = AviatorEvaluator.newInstance(EvalMode.INTERPRETER);
// Enable tracing eval procedure(don't open it in production)
engine.setOption(Options.TRACE_EVAL, true);
Expression exp = engine.compile("score > 80 ? 'good' : 'bad'");
System.out.println(exp.execute(exp.newEnv("score", 100)));
System.out.println(exp.execute(exp.newEnv("score", 50)));
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class RunScriptExample method main.
public static void main(final String[] args) throws Exception {
// Enable java method invocation by reflection.
AviatorEvaluator.getInstance().setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
// You can trry to test every script in examples folder by changing the file name.
Expression exp = AviatorEvaluator.getInstance().compileScript("examples/hello.av");
exp.execute();
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class SimpleExample method main.
public static void main(final String[] args) throws Exception {
Long result = (Long) AviatorEvaluator.execute("1+2+3");
System.out.println(result);
String hello = (String) AviatorEvaluator.execute("'hello,' + name", AviatorEvaluator.newEnv("name", "aviator"));
System.out.println(hello);
Expression exp = AviatorEvaluator.compile("map(list, lambda(v) -> v + u end)");
System.out.println("Uninitialized global variables: " + exp.getVariableFullNames());
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int u = 99;
System.out.println("executed: " + exp.execute(exp.newEnv("list", list, "u", u)));
}
use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.
the class TestScripts method testScript.
public Object testScript(final String name, final Object... args) {
try {
System.out.println("Testing script " + name + " with args: " + Arrays.toString(args));
final String file = TestScripts.class.getResource("/scripts/" + name).getFile();
this.instance.validate(IoModule.slurp(file));
Expression exp = this.instance.compileScript(file, true);
return exp.execute(AviatorEvaluator.newEnv(args));
} catch (Throwable t) {
Reflector.sneakyThrow(t);
}
return null;
}
Aggregations