Search in sources :

Example 1 with DelegateTokenType

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

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

the class OptimizeCodeGenerator method executeOperator.

private int executeOperator(final Map<Integer, DelegateTokenType> index2DelegateType, final Token<?> operatorToken, final OperatorType operatorType, final int operatorIndex, int operandCount) {
    Token<?> token = null;
    operandCount += index2DelegateType.size();
    // check if literal expression can be executed
    boolean canExecute = true;
    // operand count
    int count = 0;
    // operand start index
    int operandStartIndex = -1;
    for (int j = operatorIndex - 1; j >= 0; j--) {
        token = this.tokenList.get(j);
        if (token == null) {
            // we must compact token list and retry executing
            return -1;
        }
        final TokenType tokenType = token.getType();
        // Check if operand is a literal operand
        if (!isLiteralOperand(token, tokenType, count + 1, index2DelegateType)) {
            canExecute = false;
            break;
        }
        count++;
        if (count == operandCount) {
            operandStartIndex = j;
            break;
        }
    }
    // if we can execute it on compile
    if (canExecute) {
        // arguments
        AviatorObject[] args = new AviatorObject[operandCount];
        int index = 0;
        for (int j = operandStartIndex; j < operatorIndex; j++) {
            token = this.tokenList.get(j);
            if (token.getType() == TokenType.Delegate) {
                this.tokenList.set(j, null);
                continue;
            }
            args[index++] = getAviatorObjectFromToken(token);
            // set argument token to null
            this.tokenList.set(j, null);
        }
        AviatorObject result = OperationRuntime.eval(getCompileEnv(), args, operatorType);
        // set result as token to tokenList for next executing
        this.tokenList.set(operatorIndex, getTokenFromOperand(operatorToken, result));
        return 1;
    }
    return 0;
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) TokenType(com.googlecode.aviator.lexer.token.Token.TokenType) DelegateTokenType(com.googlecode.aviator.lexer.token.DelegateToken.DelegateTokenType)

Aggregations

DelegateTokenType (com.googlecode.aviator.lexer.token.DelegateToken.DelegateTokenType)2 OperatorToken (com.googlecode.aviator.lexer.token.OperatorToken)1 OperatorType (com.googlecode.aviator.lexer.token.OperatorType)1 TokenType (com.googlecode.aviator.lexer.token.Token.TokenType)1 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)1