Search in sources :

Example 1 with BaseExpression

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

the class AviatorString method getLexeme.

public String getLexeme(final Map<String, Object> env, final boolean warnOnCompile) {
    AviatorEvaluatorInstance engine = RuntimeUtils.getInstance(env);
    if (!this.isLiteral || !this.hasInterpolation || !engine.isFeatureEnabled(Feature.StringInterpolation) || this.lexeme == null || this.lexeme.length() < 3) {
        return this.lexeme;
    }
    StringSegments segs = null;
    BaseExpression exp = (BaseExpression) (env == null ? null : env.get(Constants.EXP_VAR));
    if (exp != null) {
        segs = exp.getStringSegements(this.lexeme, this.lineNo);
    } else {
        segs = engine.compileStringSegments(this.lexeme);
        if (warnOnCompile) {
            warnOnCompileWithoutCaching();
        }
    }
    assert (segs != null);
    return segs.toString(env, this.lexeme);
}
Also used : StringSegments(com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments) AviatorEvaluatorInstance(com.googlecode.aviator.AviatorEvaluatorInstance) BaseExpression(com.googlecode.aviator.BaseExpression)

Example 2 with BaseExpression

use of com.googlecode.aviator.BaseExpression 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 3 with BaseExpression

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

the class ASMCodeGenerator method getResult.

/*
   * (non-Javadoc)
   *
   * @see com.googlecode.aviator.code.CodeGenerator#getResult()
   */
@Override
public Expression getResult(final boolean unboxObject) {
    end(unboxObject);
    byte[] bytes = this.classWriter.toByteArray();
    try {
        Class<?> defineClass = ClassDefiner.defineClass(this.className, Expression.class, bytes, this.classLoader);
        Constructor<?> constructor = defineClass.getConstructor(AviatorEvaluatorInstance.class, List.class, SymbolTable.class);
        BaseExpression exp = (BaseExpression) constructor.newInstance(this.instance, new ArrayList<VariableMeta>(this.variables.values()), this.symbolTable);
        exp.setLambdaBootstraps(this.lambdaBootstraps);
        exp.setFuncsArgs(this.funcsArgs);
        exp.setSourceFile(this.sourceFile);
        return exp;
    } catch (ExpressionRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        if (e.getCause() instanceof ExpressionRuntimeException) {
            throw (ExpressionRuntimeException) e.getCause();
        }
        throw new CompileExpressionErrorException("define class error", e);
    }
}
Also used : BaseExpression(com.googlecode.aviator.BaseExpression) ExpressionRuntimeException(com.googlecode.aviator.exception.ExpressionRuntimeException) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) ArrayList(java.util.ArrayList)

Aggregations

BaseExpression (com.googlecode.aviator.BaseExpression)3 ArrayList (java.util.ArrayList)2 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 StringSegments (com.googlecode.aviator.AviatorEvaluatorInstance.StringSegments)1 Expression (com.googlecode.aviator.Expression)1 LiteralExpression (com.googlecode.aviator.LiteralExpression)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)1 OperatorToken (com.googlecode.aviator.lexer.token.OperatorToken)1 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)1 StringToken (com.googlecode.aviator.lexer.token.StringToken)1 Token (com.googlecode.aviator.lexer.token.Token)1 CompileTypes (com.googlecode.aviator.parser.CompileTypes)1 VariableMeta (com.googlecode.aviator.parser.VariableMeta)1 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1