Search in sources :

Example 1 with CodeGenerator

use of com.googlecode.aviator.code.CodeGenerator in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method validate.

/**
 * Validate a script text whether is a legal aviatorscript text, throw exception if not.
 *
 * @since 5.0.2
 * @param script the script text
 */
public void validate(final String script) {
    if (script == null || script.trim().length() == 0) {
        throw new CompileExpressionErrorException("Blank script");
    }
    ExpressionLexer lexer = new ExpressionLexer(this, script);
    CodeGenerator codeGenerator = new NoneCodeGenerator();
    ExpressionParser parser = new ExpressionParser(this, lexer, codeGenerator);
    parser.parse();
}
Also used : ExpressionLexer(com.googlecode.aviator.lexer.ExpressionLexer) CompileExpressionErrorException(com.googlecode.aviator.exception.CompileExpressionErrorException) NoneCodeGenerator(com.googlecode.aviator.code.NoneCodeGenerator) ExpressionParser(com.googlecode.aviator.parser.ExpressionParser) NoneCodeGenerator(com.googlecode.aviator.code.NoneCodeGenerator) EvalCodeGenerator(com.googlecode.aviator.code.EvalCodeGenerator) ASMCodeGenerator(com.googlecode.aviator.code.asm.ASMCodeGenerator) OptimizeCodeGenerator(com.googlecode.aviator.code.OptimizeCodeGenerator) CodeGenerator(com.googlecode.aviator.code.CodeGenerator) InterpretCodeGenerator(com.googlecode.aviator.code.interpreter.InterpretCodeGenerator)

Example 2 with CodeGenerator

use of com.googlecode.aviator.code.CodeGenerator in project aviatorscript by killme2008.

the class ASMCodeGeneratorUnitTest method testOnLambdaDefine.

@Test
public void testOnLambdaDefine() throws Exception {
    this.codeGenerator.setParser(new Parser() {

        @Override
        public void setCodeGenerator(final CodeGenerator codeGenerator) {
        }

        @Override
        public void restoreScope(final ScopeInfo info) {
        }

        @Override
        public SymbolTable getSymbolTable() {
            return null;
        }

        @Override
        public CodeGenerator getCodeGenerator() {
            return ASMCodeGeneratorUnitTest.this.codeGenerator;
        }

        @Override
        public ScopeInfo enterScope(final boolean inForLoop) {
            return null;
        }
    });
    assertNull(this.codeGenerator.getLambdaGenerator());
    this.codeGenerator.onLambdaDefineStart(new Variable("lambda", 0, 0));
    LambdaGenerator lambdaGenerator = this.codeGenerator.getLambdaGenerator();
    assertNotNull(lambdaGenerator);
    this.codeGenerator.onLambdaArgument(new Variable("x", 0, 1), new FunctionParam(0, "x", false));
    this.codeGenerator.onLambdaArgument(new Variable("y", 0, 2), new FunctionParam(1, "y", false));
    this.codeGenerator.onLambdaBodyStart(new Variable(">", 0, 3));
    lambdaGenerator.onConstant(new Variable("x", 0, 4));
    lambdaGenerator.onConstant(new Variable("y", 0, 5));
    lambdaGenerator.onAdd(null);
    this.codeGenerator.onLambdaBodyEnd(new Variable("end", 0, 7));
    HashMap<String, Object> env = new HashMap<String, Object>();
    env.put("x", 2);
    env.put("y", 3);
    Object result = eval(env);
    assertTrue(result instanceof LambdaFunction);
    assertEquals(5, ((LambdaFunction) result).call(env, new AviatorJavaType("x"), new AviatorJavaType("y")).getValue(env));
    assertNull(this.codeGenerator.getLambdaGenerator());
}
Also used : LambdaGenerator(com.googlecode.aviator.code.LambdaGenerator) Variable(com.googlecode.aviator.lexer.token.Variable) HashMap(java.util.HashMap) SymbolTable(com.googlecode.aviator.lexer.SymbolTable) ScopeInfo(com.googlecode.aviator.parser.ScopeInfo) LambdaFunction(com.googlecode.aviator.runtime.function.LambdaFunction) CodeGenerator(com.googlecode.aviator.code.CodeGenerator) Parser(com.googlecode.aviator.parser.Parser) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) FunctionParam(com.googlecode.aviator.runtime.FunctionParam) Test(org.junit.Test)

Example 3 with CodeGenerator

use of com.googlecode.aviator.code.CodeGenerator in project aviatorscript by killme2008.

the class ExpressionParser method ternary.

public boolean ternary() {
    int gcTimes = this.getCGTimes;
    if (this.lookhead == Variable.NEW) {
        newStatement();
        return true;
    }
    join();
    if (this.lookhead == null || expectChar(':') || expectChar(',')) {
        return gcTimes < this.getCGTimes;
    }
    Token<?> opToken = this.lookhead;
    if (expectChar('?')) {
        move(true);
        CodeGenerator cg = getCodeGeneratorWithTimes();
        cg.onTernaryBoolean(opToken);
        if (!ternary()) {
            reportSyntaxError("invalid token for ternary operator");
        }
        if (expectChar(':')) {
            move(true);
            cg.onTernaryLeft(this.lookhead);
            if (!ternary()) {
                reportSyntaxError("invalid token for ternary operator");
            }
            cg.onTernaryRight(this.lookhead);
        } else {
            reportSyntaxError("expect ':'");
        }
    }
    return gcTimes < this.getCGTimes;
}
Also used : CodeGenerator(com.googlecode.aviator.code.CodeGenerator)

Example 4 with CodeGenerator

use of com.googlecode.aviator.code.CodeGenerator in project aviatorscript by killme2008.

the class ExpressionParser method returnStatement.

/**
 * Call __reducer_return(result)
 */
public void returnStatement() {
    move(true);
    CodeGenerator cg = getCodeGeneratorWithTimes();
    cg.onTernaryEnd(this.lookhead);
    if (expectChar(';')) {
        // 'return;' => 'return nil;'
        if (this.scope.newLexicalScope) {
            cg.onMethodName(Constants.ReducerReturnFn);
            cg.onConstant(Variable.NIL);
            cg.onMethodParameter(this.lookhead);
            cg.onMethodInvoke(this.lookhead);
        } else {
            cg.onConstant(Variable.NIL);
        }
        move(true);
        return;
    } else {
        if (this.scope.newLexicalScope) {
            cg.onMethodName(Constants.ReducerReturnFn);
            if (!ternary()) {
                reportSyntaxError("invalid value for return, missing ';'?");
            }
            cg.onMethodParameter(this.lookhead);
            cg.onMethodInvoke(this.lookhead);
        } else {
            if (!ternary()) {
                reportSyntaxError("invalid value for return, missing ';'?");
            }
        }
    }
    if (!expectChar(';')) {
        reportSyntaxError("missing ';' for return statement");
    }
    move(true);
}
Also used : CodeGenerator(com.googlecode.aviator.code.CodeGenerator)

Example 5 with CodeGenerator

use of com.googlecode.aviator.code.CodeGenerator in project aviatorscript by killme2008.

the class AviatorEvaluatorInstance method innerCompile.

private Expression innerCompile(final String expression, final String sourceFile, final boolean cached) {
    ExpressionLexer lexer = new ExpressionLexer(this, expression);
    CodeGenerator codeGenerator = newCodeGenerator(sourceFile, cached);
    ExpressionParser parser = new ExpressionParser(this, lexer, codeGenerator);
    Expression exp = parser.parse();
    if (getOptionValue(Options.TRACE_EVAL).bool) {
        ((BaseExpression) exp).setExpression(expression);
    }
    return exp;
}
Also used : ExpressionLexer(com.googlecode.aviator.lexer.ExpressionLexer) ExpressionParser(com.googlecode.aviator.parser.ExpressionParser) NoneCodeGenerator(com.googlecode.aviator.code.NoneCodeGenerator) EvalCodeGenerator(com.googlecode.aviator.code.EvalCodeGenerator) ASMCodeGenerator(com.googlecode.aviator.code.asm.ASMCodeGenerator) OptimizeCodeGenerator(com.googlecode.aviator.code.OptimizeCodeGenerator) CodeGenerator(com.googlecode.aviator.code.CodeGenerator) InterpretCodeGenerator(com.googlecode.aviator.code.interpreter.InterpretCodeGenerator)

Aggregations

CodeGenerator (com.googlecode.aviator.code.CodeGenerator)9 EvalCodeGenerator (com.googlecode.aviator.code.EvalCodeGenerator)2 NoneCodeGenerator (com.googlecode.aviator.code.NoneCodeGenerator)2 OptimizeCodeGenerator (com.googlecode.aviator.code.OptimizeCodeGenerator)2 ASMCodeGenerator (com.googlecode.aviator.code.asm.ASMCodeGenerator)2 InterpretCodeGenerator (com.googlecode.aviator.code.interpreter.InterpretCodeGenerator)2 ExpressionLexer (com.googlecode.aviator.lexer.ExpressionLexer)2 ExpressionParser (com.googlecode.aviator.parser.ExpressionParser)2 LambdaGenerator (com.googlecode.aviator.code.LambdaGenerator)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 SymbolTable (com.googlecode.aviator.lexer.SymbolTable)1 Variable (com.googlecode.aviator.lexer.token.Variable)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 HashMap (java.util.HashMap)1 Test (org.junit.Test)1