Search in sources :

Example 21 with Expression

use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.

the class ASMCodeGeneratorUnitTest method testOnConstant_Double.

@Test
public void testOnConstant_Double() throws Exception {
    this.codeGenerator.onConstant(new NumberToken(3.3D, "3.3"));
    Expression exp = this.codeGenerator.getResult(true);
    Object result = exp.execute();
    assertEquals(3.3D, result);
}
Also used : Expression(com.googlecode.aviator.Expression) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) Test(org.junit.Test)

Example 22 with Expression

use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.

the class ASMCodeGeneratorUnitTest method testOnConstant_Nil.

@Test
public void testOnConstant_Nil() throws Exception {
    this.codeGenerator.onConstant(Variable.NIL);
    Expression exp = this.codeGenerator.getResult(true);
    Object result = exp.execute();
    assertNull(result);
}
Also used : Expression(com.googlecode.aviator.Expression) Test(org.junit.Test)

Example 23 with Expression

use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.

the class Benchmark method benchmarkScript.

private static void benchmarkScript() throws Exception {
    Expression exp = AviatorEvaluator.getInstance().compileScript("scripts/benchmark.av");
    System.out.println(exp.execute());
}
Also used : Expression(com.googlecode.aviator.Expression)

Example 24 with Expression

use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.

the class OptimizeCodeGenerator method getResult.

@Override
public Expression getResult(final boolean unboxObject) {
    // execute literal expression
    while (execute() > 0) {
        ;
    }
    Map<String, VariableMeta> /* metadata */
    variables = new LinkedHashMap<String, VariableMeta>();
    Map<String, Integer> /* counter */
    methods = new HashMap<String, Integer>();
    Set<Token<?>> constants = new HashSet<>();
    for (Token<?> token : this.tokenList) {
        if (ExpressionParser.isConstant(token, this.instance)) {
            constants.add(token);
        }
        switch(token.getType()) {
            case Variable:
                if (SymbolTable.isReservedKeyword((Variable) token)) {
                    continue;
                }
                String varName = token.getLexeme();
                VariableMeta meta = variables.get(varName);
                if (meta == null) {
                    meta = new VariableMeta((CompileTypes) token.getMeta(Constants.TYPE_META), varName, token.getMeta(Constants.INIT_META, false), token.getStartIndex());
                    variables.put(varName, meta);
                } else {
                    meta.add(token);
                }
                break;
            case Delegate:
                DelegateToken delegateToken = (DelegateToken) token;
                if (delegateToken.getDelegateTokenType() == DelegateTokenType.Method_Name) {
                    Token<?> realToken = delegateToken.getToken();
                    if (realToken == null) {
                        continue;
                    }
                    if (realToken.getType() == TokenType.Variable) {
                        String methodName = token.getLexeme();
                        if (!methods.containsKey(methodName)) {
                            methods.put(methodName, 1);
                        } else {
                            methods.put(methodName, methods.get(methodName) + 1);
                        }
                    }
                } else if (delegateToken.getDelegateTokenType() == DelegateTokenType.Array) {
                    Token<?> realToken = delegateToken.getToken();
                    if (realToken.getType() == TokenType.Variable) {
                        varName = token.getLexeme();
                        VariableMeta varMeta = variables.get(varName);
                        if (varMeta == null) {
                            varMeta = new VariableMeta((CompileTypes) realToken.getMeta(Constants.TYPE_META), varName, realToken.getMeta(Constants.INIT_META, false), realToken.getStartIndex());
                            variables.put(varName, varMeta);
                        } else {
                            varMeta.add(realToken);
                        }
                    }
                }
                break;
        }
    }
    Expression exp = null;
    // Last token is a literal token,then return a LiteralExpression
    if (this.tokenList.size() <= 1) {
        if (this.tokenList.isEmpty()) {
            exp = new LiteralExpression(this.instance, null, new ArrayList<>(variables.values()));
        } else {
            final Token<?> lastToken = this.tokenList.get(0);
            if (ExpressionParser.isLiteralToken(lastToken, this.instance)) {
                exp = new LiteralExpression(this.instance, getAviatorObjectFromToken(lastToken).getValue(getCompileEnv()), new ArrayList<>(variables.values()));
            }
        }
    }
    if (exp == null) {
        // call asm to generate byte codes
        callASM(variables, methods, constants);
        // get result from asm
        exp = this.codeGen.getResult(unboxObject);
    }
    if (exp instanceof BaseExpression) {
        ((BaseExpression) exp).setCompileEnv(getCompileEnv());
        ((BaseExpression) exp).setSourceFile(this.sourceFile);
    }
    return exp;
}
Also used : BaseExpression(com.googlecode.aviator.BaseExpression) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DelegateToken(com.googlecode.aviator.lexer.token.DelegateToken) LiteralExpression(com.googlecode.aviator.LiteralExpression) ArrayList(java.util.ArrayList) DelegateToken(com.googlecode.aviator.lexer.token.DelegateToken) PatternToken(com.googlecode.aviator.lexer.token.PatternToken) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) StringToken(com.googlecode.aviator.lexer.token.StringToken) Token(com.googlecode.aviator.lexer.token.Token) OperatorToken(com.googlecode.aviator.lexer.token.OperatorToken) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) CompileTypes(com.googlecode.aviator.parser.CompileTypes) VariableMeta(com.googlecode.aviator.parser.VariableMeta) LinkedHashMap(java.util.LinkedHashMap) Expression(com.googlecode.aviator.Expression) LiteralExpression(com.googlecode.aviator.LiteralExpression) BaseExpression(com.googlecode.aviator.BaseExpression) HashSet(java.util.HashSet)

Example 25 with Expression

use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.

the class DemoResultSetSeq method main.

public static void main(final String[] args) throws Exception {
    // Mock a result set.
    ResultSet resultSet = Mockito.mock(ResultSet.class);
    Mockito.when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(resultSet.getString("username")).thenReturn("dennis").thenReturn("catty");
    Mockito.when(resultSet.getInt("age")).thenReturn(30).thenReturn(20);
    // Use it in aviator
    Expression exp = AviatorEvaluator.getInstance().compileScript("examples/result_set_seq.av");
    exp.execute(exp.newEnv("results", new ResultSetSequence(resultSet)));
}
Also used : Expression(com.googlecode.aviator.Expression) ResultSet(java.sql.ResultSet)

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