Search in sources :

Example 1 with DelegateToken

use of com.googlecode.aviator.lexer.token.DelegateToken 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 2 with DelegateToken

use of com.googlecode.aviator.lexer.token.DelegateToken in project aviatorscript by killme2008.

the class OptimizeCodeGenerator method onLambdaBodyEnd.

@Override
public void onLambdaBodyEnd(final Token<?> lookhead) {
    // this.lambdaGenerator.compileCallMethod();
    LambdaFunctionBootstrap bootstrap = this.lambdaGenerator.getLmabdaBootstrap();
    if (this.lambdaBootstraps == null) {
        this.lambdaBootstraps = new HashMap<String, LambdaFunctionBootstrap>();
    }
    this.lambdaBootstraps.put(bootstrap.getName(), bootstrap);
    DelegateToken token = new DelegateToken(lookhead, DelegateTokenType.Lambda_New);
    token.setLambdaFunctionBootstrap(bootstrap);
    this.tokenList.add(token);
    this.parser.restoreScope(this.lambdaGenerator.getScopeInfo());
    this.lambdaGenerator = null;
    this.parser.setCodeGenerator(this.parentCodeGenerator);
}
Also used : DelegateToken(com.googlecode.aviator.lexer.token.DelegateToken) LambdaFunctionBootstrap(com.googlecode.aviator.runtime.LambdaFunctionBootstrap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 3 with DelegateToken

use of com.googlecode.aviator.lexer.token.DelegateToken in project aviatorscript by killme2008.

the class OptimizeCodeGenerator method callASM.

private void callASM(final Map<String, VariableMeta> variables, final Map<String, Integer> methods, final Set<Token<?>> constants) {
    this.codeGen.initConstants(constants);
    this.codeGen.initVariables(variables);
    this.codeGen.initMethods(methods);
    this.codeGen.setLambdaBootstraps(this.lambdaBootstraps);
    this.codeGen.start();
    for (int i = 0; i < this.tokenList.size(); i++) {
        Token<?> token = this.tokenList.get(i);
        switch(token.getType()) {
            case Operator:
                OperatorToken op = (OperatorToken) token;
                switch(op.getOperatorType()) {
                    case ADD:
                        this.codeGen.onAdd(token);
                        break;
                    case SUB:
                        this.codeGen.onSub(token);
                        break;
                    case MULT:
                        this.codeGen.onMult(token);
                        break;
                    case Exponent:
                        this.codeGen.onExponent(token);
                        break;
                    case DIV:
                        this.codeGen.onDiv(token);
                        break;
                    case MOD:
                        this.codeGen.onMod(token);
                        break;
                    case EQ:
                        this.codeGen.onEq(token);
                        break;
                    case NEQ:
                        this.codeGen.onNeq(token);
                        break;
                    case LT:
                        this.codeGen.onLt(token);
                        break;
                    case LE:
                        this.codeGen.onLe(token);
                        break;
                    case GT:
                        this.codeGen.onGt(token);
                        break;
                    case GE:
                        this.codeGen.onGe(token);
                        break;
                    case NOT:
                        this.codeGen.onNot(token);
                        break;
                    case NEG:
                        this.codeGen.onNeg(token);
                        break;
                    case AND:
                        this.codeGen.onAndRight(token);
                        break;
                    case OR:
                        this.codeGen.onJoinRight(token);
                        break;
                    case FUNC:
                        this.codeGen.onMethodInvoke(token);
                        break;
                    case INDEX:
                        this.codeGen.onArrayIndexEnd(token);
                        break;
                    case MATCH:
                        this.codeGen.onMatch(token);
                        break;
                    case TERNARY:
                        this.codeGen.onTernaryRight(token);
                        break;
                    case BIT_AND:
                        this.codeGen.onBitAnd(token);
                        break;
                    case BIT_OR:
                        this.codeGen.onBitOr(token);
                        break;
                    case BIT_XOR:
                        this.codeGen.onBitXor(token);
                        break;
                    case BIT_NOT:
                        this.codeGen.onBitNot(token);
                        break;
                    case SHIFT_LEFT:
                        this.codeGen.onShiftLeft(token);
                        break;
                    case SHIFT_RIGHT:
                        this.codeGen.onShiftRight(token);
                        break;
                    case DEFINE:
                        this.codeGen.onAssignment(token.withMeta(Constants.DEFINE_META, true));
                        break;
                    case ASSIGNMENT:
                        this.codeGen.onAssignment(token);
                        break;
                    case U_SHIFT_RIGHT:
                        this.codeGen.onUnsignedShiftRight(token);
                        break;
                }
                break;
            case Delegate:
                DelegateToken delegateToken = (DelegateToken) token;
                final Token<?> realToken = delegateToken.getToken();
                switch(delegateToken.getDelegateTokenType()) {
                    case And_Left:
                        this.codeGen.onAndLeft(realToken);
                        break;
                    case Join_Left:
                        this.codeGen.onJoinLeft(realToken);
                        break;
                    case Array:
                        this.codeGen.onArray(realToken);
                        break;
                    case Index_Start:
                        this.codeGen.onArrayIndexStart(realToken);
                        break;
                    case Ternary_Boolean:
                        this.codeGen.onTernaryBoolean(realToken);
                        break;
                    case Ternary_Left:
                        this.codeGen.onTernaryLeft(realToken);
                        break;
                    case Method_Name:
                        this.codeGen.onMethodName(realToken);
                        break;
                    case Method_Param:
                        this.codeGen.onMethodParameter(realToken);
                        break;
                    case Lambda_New:
                        this.codeGen.genNewLambdaCode(delegateToken.getLambdaFunctionBootstrap());
                        break;
                    case Ternay_End:
                        this.codeGen.onTernaryEnd(realToken);
                        break;
                }
                break;
            default:
                this.codeGen.onConstant(token);
                break;
        }
    }
}
Also used : DelegateToken(com.googlecode.aviator.lexer.token.DelegateToken) OperatorToken(com.googlecode.aviator.lexer.token.OperatorToken)

Aggregations

DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)3 OperatorToken (com.googlecode.aviator.lexer.token.OperatorToken)2 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)2 BaseExpression (com.googlecode.aviator.BaseExpression)1 Expression (com.googlecode.aviator.Expression)1 LiteralExpression (com.googlecode.aviator.LiteralExpression)1 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)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 LambdaFunctionBootstrap (com.googlecode.aviator.runtime.LambdaFunctionBootstrap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1