Search in sources :

Example 1 with OperatorToken

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

the class OptimizeCodeGenerator method execute.

private int execute() {
    int exeCount = 0;
    final int size = this.tokenList.size();
    printTokenList();
    for (int i = 0; i < size; i++) {
        Token<?> token = this.tokenList.get(i);
        if (token.getType() == TokenType.Operator) {
            final OperatorToken op = (OperatorToken) token;
            final OperatorType operatorType = op.getOperatorType();
            final int operandCount = operatorType.getArity();
            switch(operatorType) {
                case FUNC:
                case INDEX:
                    // Could not optimize function and index call
                    break;
                default:
                    Map<Integer, DelegateTokenType> index2DelegateType = getIndex2DelegateTypeMap(operatorType);
                    final int result = executeOperator(index2DelegateType, token, operatorType, i, operandCount);
                    if (result < 0) {
                        compactTokenList();
                        return exeCount;
                    }
                    exeCount += result;
                    break;
            }
        }
    }
    compactTokenList();
    return exeCount;
}
Also used : OperatorToken(com.googlecode.aviator.lexer.token.OperatorToken) OperatorType(com.googlecode.aviator.lexer.token.OperatorType) DelegateTokenType(com.googlecode.aviator.lexer.token.DelegateToken.DelegateTokenType)

Example 2 with OperatorToken

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

the class OptimizeCodeGenerator method onMethodInvoke.

@Override
public void onMethodInvoke(final Token<?> lookhead) {
    OperatorToken token = new OperatorToken(lookhead, OperatorType.FUNC);
    token.setMetaMap(lookhead != null ? lookhead.getMetaMap() : null);
    this.tokenList.add(token);
}
Also used : OperatorToken(com.googlecode.aviator.lexer.token.OperatorToken)

Example 3 with OperatorToken

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

OperatorToken (com.googlecode.aviator.lexer.token.OperatorToken)3 DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)1 DelegateTokenType (com.googlecode.aviator.lexer.token.DelegateToken.DelegateTokenType)1 OperatorType (com.googlecode.aviator.lexer.token.OperatorType)1