Search in sources :

Example 6 with Expression

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);
}
Also used : Expression(com.googlecode.aviator.Expression)

Example 7 with Expression

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)));
}
Also used : AviatorEvaluatorInstance(com.googlecode.aviator.AviatorEvaluatorInstance) Expression(com.googlecode.aviator.Expression)

Example 8 with Expression

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();
}
Also used : Expression(com.googlecode.aviator.Expression)

Example 9 with Expression

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)));
}
Also used : Expression(com.googlecode.aviator.Expression)

Example 10 with Expression

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;
}
Also used : Expression(com.googlecode.aviator.Expression)

Aggregations

Expression (com.googlecode.aviator.Expression)31 Test (org.junit.Test)16 HashMap (java.util.HashMap)8 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)3 Map (java.util.Map)3 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)2 StringToken (com.googlecode.aviator.lexer.token.StringToken)2 CollectRep (com.usthe.common.entity.message.CollectRep)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 LiteralExpression (com.googlecode.aviator.LiteralExpression)1 CompareNotSupportedException (com.googlecode.aviator.exception.CompareNotSupportedException)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)1