Search in sources :

Example 11 with Variable

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

Example 12 with Variable

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

the class SymbolTable method reserve.

public Variable reserve(final String lexeme) {
    if (isReserved(lexeme)) {
        return getVariable(lexeme);
    } else {
        final Variable var = new Variable(lexeme, 0, -1);
        this.table.put(lexeme, var);
        return var;
    }
}
Also used : Variable(com.googlecode.aviator.lexer.token.Variable)

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