Search in sources :

Example 11 with NumberToken

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

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

Example 13 with NumberToken

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

the class ASMCodeGeneratorUnitTest method testOnBitNot1.

@Test
public void testOnBitNot1() throws Exception {
    this.codeGenerator.onConstant(new NumberToken(-3L, "-3"));
    this.codeGenerator.onBitNot(null);
    Object result = eval(new HashMap<String, Object>());
    assertEquals(2, result);
}
Also used : NumberToken(com.googlecode.aviator.lexer.token.NumberToken) Test(org.junit.Test)

Example 14 with NumberToken

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

the class ASMCodeGeneratorUnitTest method testOnConstant_Double.

@Test
public void testOnConstant_Double() throws Exception {
    this.codeGenerator.onConstant(new NumberToken(3.3D, "3.3"));
    Expression exp = this.codeGenerator.getResult(true);
    Object result = exp.execute();
    assertEquals(3.3D, result);
}
Also used : Expression(com.googlecode.aviator.Expression) NumberToken(com.googlecode.aviator.lexer.token.NumberToken) Test(org.junit.Test)

Example 15 with NumberToken

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

the class ASMCodeGenerator method onConstant0.

private void onConstant0(final Token<?> lookhead, final boolean inConstructor) {
    if (lookhead == null) {
        return;
    }
    visitLineNumber(lookhead);
    // load token to stack
    switch(lookhead.getType()) {
        case Number:
            if (loadConstant(lookhead, inConstructor)) {
                return;
            }
            // load numbers
            NumberToken numberToken = (NumberToken) lookhead;
            Number number = numberToken.getNumber();
            if (TypeUtils.isBigInt(number)) {
                this.mv.visitLdcInsn(numberToken.getLexeme());
                this.mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/aviator/runtime/type/AviatorBigInt", "valueOf", "(Ljava/lang/String;)Lcom/googlecode/aviator/runtime/type/AviatorBigInt;");
            } else if (TypeUtils.isDecimal(number)) {
                loadEnv();
                // this.pushOperand();
                this.mv.visitLdcInsn(numberToken.getLexeme());
                String methodDesc = "(Ljava/util/Map;Ljava/lang/String;)Lcom/googlecode/aviator/runtime/type/AviatorDecimal;";
                if (inConstructor) {
                    methodDesc = "(Lcom/googlecode/aviator/AviatorEvaluatorInstance;Ljava/lang/String;)Lcom/googlecode/aviator/runtime/type/AviatorDecimal;";
                }
                this.mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/aviator/runtime/type/AviatorDecimal", "valueOf", methodDesc);
                this.popOperand();
            } else if (TypeUtils.isDouble(number)) {
                this.mv.visitLdcInsn(number);
                this.mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/aviator/runtime/type/AviatorDouble", "valueOf", "(D)Lcom/googlecode/aviator/runtime/type/AviatorDouble;");
            } else {
                this.mv.visitLdcInsn(number);
                this.mv.visitMethodInsn(INVOKESTATIC, "com/googlecode/aviator/runtime/type/AviatorLong", "valueOf", "(J)Lcom/googlecode/aviator/runtime/type/AviatorLong;");
            }
            this.pushOperand();
            // this.popOperand();
            break;
        case String:
            if (loadConstant(lookhead, inConstructor)) {
                return;
            }
            // load string
            this.mv.visitTypeInsn(NEW, "com/googlecode/aviator/runtime/type/AviatorString");
            this.mv.visitInsn(DUP);
            this.mv.visitLdcInsn(lookhead.getValue(null));
            this.mv.visitLdcInsn(true);
            this.mv.visitLdcInsn(lookhead.getMeta(Constants.INTER_META, true));
            this.mv.visitLdcInsn(lookhead.getLineNo());
            this.mv.visitMethodInsn(INVOKESPECIAL, "com/googlecode/aviator/runtime/type/AviatorString", CONSTRUCTOR_METHOD_NAME, "(Ljava/lang/String;ZZI)V");
            this.pushOperand(6);
            this.popOperand(5);
            break;
        case Pattern:
            if (loadConstant(lookhead, inConstructor)) {
                return;
            }
            // load pattern
            this.mv.visitTypeInsn(NEW, "com/googlecode/aviator/runtime/type/AviatorPattern");
            this.mv.visitInsn(DUP);
            this.mv.visitLdcInsn(lookhead.getValue(null));
            this.mv.visitMethodInsn(INVOKESPECIAL, "com/googlecode/aviator/runtime/type/AviatorPattern", CONSTRUCTOR_METHOD_NAME, "(Ljava/lang/String;)V");
            this.pushOperand(3);
            this.popOperand(2);
            break;
        case Variable:
            // load variable
            Variable variable = (Variable) lookhead;
            if (variable.equals(Variable.TRUE)) {
                this.mv.visitFieldInsn(GETSTATIC, "com/googlecode/aviator/runtime/type/AviatorBoolean", "TRUE", "Lcom/googlecode/aviator/runtime/type/AviatorBoolean;");
                this.pushOperand();
            } else if (variable.equals(Variable.FALSE)) {
                this.mv.visitFieldInsn(GETSTATIC, "com/googlecode/aviator/runtime/type/AviatorBoolean", "FALSE", "Lcom/googlecode/aviator/runtime/type/AviatorBoolean;");
                this.pushOperand();
            } else if (variable.equals(Variable.NIL)) {
                this.mv.visitFieldInsn(GETSTATIC, "com/googlecode/aviator/runtime/type/AviatorNil", "NIL", "Lcom/googlecode/aviator/runtime/type/AviatorNil;");
                this.pushOperand();
            } else {
                String outterVarName = variable.getLexeme();
                String innerVarName = this.innerVars.get(outterVarName);
                if (innerVarName != null) {
                    // Is it stored in local?
                    Map<String, Integer> name2Index = this.labelNameIndexMap.get(this.currentLabel);
                    if (name2Index != null && name2Index.get(innerVarName) != null) {
                        int localIndex = name2Index.get(innerVarName);
                        this.mv.visitVarInsn(ALOAD, localIndex);
                        this.pushOperand();
                    } else {
                        // Get field at first time
                        this.mv.visitVarInsn(ALOAD, 0);
                        this.mv.visitFieldInsn(GETFIELD, this.className, innerVarName, "Lcom/googlecode/aviator/runtime/type/AviatorJavaType;");
                        // Variable is used more than once,store it to local
                        if (this.variables.get(outterVarName).getRefs() > 1) {
                            this.mv.visitInsn(DUP);
                            int localIndex = getLocalIndex();
                            this.mv.visitVarInsn(ASTORE, localIndex);
                            if (name2Index == null) {
                                name2Index = new HashMap<>();
                                this.labelNameIndexMap.put(this.currentLabel, name2Index);
                            }
                            name2Index.put(innerVarName, localIndex);
                            this.pushOperand(3);
                            this.popOperand(2);
                        } else {
                            this.pushOperand(2);
                            this.popOperand();
                        }
                    }
                } else {
                    this.mv.visitTypeInsn(NEW, JAVA_TYPE_OWNER);
                    this.mv.visitInsn(DUP);
                    this.mv.visitLdcInsn(outterVarName);
                    this.mv.visitMethodInsn(INVOKESPECIAL, JAVA_TYPE_OWNER, CONSTRUCTOR_METHOD_NAME, "(Ljava/lang/String;)V");
                    this.pushOperand(3);
                    this.popOperand(2);
                }
            }
            break;
    }
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable) NumberToken(com.googlecode.aviator.lexer.token.NumberToken)

Aggregations

NumberToken (com.googlecode.aviator.lexer.token.NumberToken)15 Test (org.junit.Test)7 Variable (com.googlecode.aviator.lexer.token.Variable)6 StringToken (com.googlecode.aviator.lexer.token.StringToken)3 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)3 HashMap (java.util.HashMap)3 Expression (com.googlecode.aviator.Expression)2 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)2 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)2 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 CharToken (com.googlecode.aviator.lexer.token.CharToken)1 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)1 AviatorBoolean (com.googlecode.aviator.runtime.type.AviatorBoolean)1 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)1 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1