Search in sources :

Example 6 with Variable

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

the class ASMCodeGeneratorUnitTest method testOnConstant_Variable.

@Test
public void testOnConstant_Variable() throws Exception {
    this.codeGenerator.onConstant(new Variable("a", 0, 0));
    Expression exp = this.codeGenerator.getResult(true);
    HashMap<String, Object> env = new HashMap<String, Object>();
    long now = System.currentTimeMillis();
    env.put("a", now);
    Object result = exp.execute(env);
    assertEquals(now, result);
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) Expression(com.googlecode.aviator.Expression) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with Variable

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

the class ASMCodeGeneratorUnitTest method doBitOpTests.

public void doBitOpTests(final OperatorType operatorType) throws Exception {
    NumberToken a = new NumberToken(99L, "3");
    NumberToken b = new NumberToken(2, "2");
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("c", 9L);
    this.codeGenerator.onConstant(new Variable("c", 0, 7));
    this.codeGenerator.onConstant(a);
    this.codeGenerator.onConstant(b);
    switch(operatorType) {
        case BIT_OR:
            this.codeGenerator.onBitOr(null);
            this.codeGenerator.onBitOr(null);
            Object result = eval(env);
            assertEquals(7 | 3 | 2, result);
            break;
        case BIT_AND:
            this.codeGenerator.onBitAnd(null);
            this.codeGenerator.onBitAnd(null);
            result = eval(env);
            assertEquals(7 & 3 & 2, result);
            break;
        case BIT_XOR:
            this.codeGenerator.onBitXor(null);
            this.codeGenerator.onBitXor(null);
            result = eval(env);
            assertEquals(7 ^ 3 ^ 2, result);
            break;
        case SHIFT_LEFT:
            this.codeGenerator.onShiftLeft(null);
            this.codeGenerator.onShiftLeft(null);
            result = eval(env);
            assertEquals(7 << 3 << 2, result);
            break;
        case SHIFT_RIGHT:
            this.codeGenerator.onShiftRight(null);
            this.codeGenerator.onShiftRight(null);
            result = eval(env);
            assertEquals(7 >> 3 >> 2, result);
            break;
        case U_SHIFT_RIGHT:
            this.codeGenerator.onUnsignedShiftRight(null);
            this.codeGenerator.onUnsignedShiftRight(null);
            result = eval(env);
            assertEquals(7 >>> 3 >>> 2, result);
            break;
    }
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) HashMap(java.util.HashMap) NumberToken(com.googlecode.aviator.lexer.token.NumberToken)

Example 8 with Variable

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

the class ASMCodeGeneratorUnitTest method doLogicOpTest.

public void doLogicOpTest(final OperatorType operatorType) throws Exception {
    NumberToken a = new NumberToken(3L, "3");
    NumberToken b = new NumberToken(3L, "3");
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("c", 9L);
    switch(operatorType) {
        case EQ:
            this.codeGenerator.onConstant(a);
            this.codeGenerator.onConstant(b);
            this.codeGenerator.onEq(null);
            Object result = eval(env);
            assertEquals(Boolean.TRUE, result);
            break;
        case NEQ:
            this.codeGenerator.onConstant(a);
            this.codeGenerator.onConstant(b);
            this.codeGenerator.onNeq(null);
            result = eval(env);
            assertEquals(Boolean.FALSE, result);
            break;
        case GT:
            this.codeGenerator.onConstant(a);
            this.codeGenerator.onConstant(b);
            this.codeGenerator.onGt(null);
            result = eval(env);
            assertEquals(Boolean.FALSE, result);
            break;
        case GE:
            this.codeGenerator.onConstant(a);
            this.codeGenerator.onConstant(b);
            this.codeGenerator.onGe(null);
            result = eval(env);
            assertEquals(Boolean.TRUE, result);
            break;
        case LT:
            this.codeGenerator.onConstant(a);
            this.codeGenerator.onConstant(new Variable("c", 0, 0));
            this.codeGenerator.onLt(null);
            result = eval(env);
            assertEquals(Boolean.TRUE, result);
            break;
        case LE:
            this.codeGenerator.onConstant(a);
            this.codeGenerator.onConstant(b);
            this.codeGenerator.onLe(null);
            result = eval(env);
            assertEquals(Boolean.TRUE, result);
            break;
    }
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) HashMap(java.util.HashMap) NumberToken(com.googlecode.aviator.lexer.token.NumberToken)

Example 9 with Variable

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

the class ExpressionParser method factor0.

private boolean factor0() {
    if (this.lookhead == null) {
        reportSyntaxError("illegal token");
    }
    if (this.lookhead == Variable.END) {
        return false;
    }
    if (expectChar('(')) {
        move(true);
        this.scope.enterParen();
        ternary();
        if (expectChar(')')) {
            move(true);
            this.scope.leaveParen();
        }
    } else if (this.lookhead.getType() == TokenType.Number || this.lookhead.getType() == TokenType.String || this.lookhead.getType() == TokenType.Variable || this.lookhead == Variable.TRUE || this.lookhead == Variable.FALSE || isOPVariable(this.lookhead)) {
        if (this.lookhead.getType() == TokenType.Variable) {
            checkVariableName(this.lookhead);
        }
        // binary operation as variable for seq functions
        if (this.lookhead.getType() == TokenType.Char) {
            CharToken charToken = (CharToken) this.lookhead;
            if (!ExpressionLexer.isBinaryOP(charToken.getCh())) {
                reportSyntaxError("unexpect char '" + charToken.getCh() + "'");
            }
            // make it as variable
            this.lookhead = this.lexer.getSymbolTable().reserve(new Variable(charToken.getLexeme(), charToken.getLineNo(), charToken.getStartIndex()));
        }
        move(true);
        // function
        Token<?> prev = getPrevToken();
        if (prev.getType() == TokenType.Variable && expectChar('(')) {
            if (prev == Variable.LAMBDA) {
                lambda(false);
            } else if (prev == Variable.FN) {
                lambda(true);
            } else {
                method(prev);
            }
        } else if (prev.getType() == TokenType.Variable) {
            if (!arrayAccess()) {
                getCodeGeneratorWithTimes().onConstant(prev);
            }
        } else {
            getCodeGeneratorWithTimes().onConstant(prev);
        }
    } else if (expectChar('/')) {
        pattern();
    } else if (expectChar('}')) {
        return false;
    } else {
        reportSyntaxError("invalid token");
    }
    return true;
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) CharToken(com.googlecode.aviator.lexer.token.CharToken) 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) CharToken(com.googlecode.aviator.lexer.token.CharToken) Token(com.googlecode.aviator.lexer.token.Token)

Example 10 with Variable

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

the class ASMCodeGeneratorUnitTest method doArithOpTest.

public void doArithOpTest(final OperatorType operatorType) throws Exception {
    NumberToken a = new NumberToken(3L, "3");
    NumberToken b = new NumberToken(3.5d, "3.5");
    Map<String, Object> env = new HashMap<String, Object>();
    env.put("c", 9L);
    this.codeGenerator.onConstant(new Variable("c", 0, 0));
    this.codeGenerator.onConstant(a);
    this.codeGenerator.onConstant(b);
    switch(operatorType) {
        case ADD:
            this.codeGenerator.onAdd(null);
            this.codeGenerator.onAdd(null);
            Object result = eval(env);
            assertEquals(15.5, (Double) result, 0.001);
            break;
        case SUB:
            this.codeGenerator.onSub(null);
            this.codeGenerator.onSub(null);
            result = eval(env);
            assertEquals(9.5, (Double) result, 0.001);
            break;
        case MULT:
            this.codeGenerator.onMult(null);
            this.codeGenerator.onMult(null);
            result = eval(env);
            assertEquals(94.5, (Double) result, 0.001);
            break;
        case DIV:
            this.codeGenerator.onDiv(null);
            this.codeGenerator.onDiv(null);
            result = eval(env);
            assertEquals(10.50, (Double) result, 0.001);
            break;
        case MOD:
            this.codeGenerator.onMod(null);
            this.codeGenerator.onMod(null);
            result = eval(env);
            assertEquals(0.0, (Double) result, 0.001);
            break;
    }
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) HashMap(java.util.HashMap) NumberToken(com.googlecode.aviator.lexer.token.NumberToken)

Aggregations

Variable (com.googlecode.aviator.lexer.token.Variable)12 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)7 HashMap (java.util.HashMap)5 Test (org.junit.Test)4 StringToken (com.googlecode.aviator.lexer.token.StringToken)3 CharToken (com.googlecode.aviator.lexer.token.CharToken)2 Expression (com.googlecode.aviator.Expression)1 CodeGenerator (com.googlecode.aviator.code.CodeGenerator)1 LambdaGenerator (com.googlecode.aviator.code.LambdaGenerator)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 SymbolTable (com.googlecode.aviator.lexer.SymbolTable)1 DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)1 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)1 Token (com.googlecode.aviator.lexer.token.Token)1 Parser (com.googlecode.aviator.parser.Parser)1 ScopeInfo (com.googlecode.aviator.parser.ScopeInfo)1 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)1 LambdaFunction (com.googlecode.aviator.runtime.function.LambdaFunction)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1