Search in sources :

Example 1 with Statement

use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseBlock.

protected BlockStatement parseBlock(List list, ParserRuleContext ctx) {
    pbCtx.enterBlock();
    ASTNode[] statements = new ASTNode[list.size()];
    List<Statement> nodes = new ArrayList<Statement>();
    boolean hasGoto = false;
    for (int i = 0; i < statements.length; i++) {
        nodes.add(parseStatment((ParserRuleContext) list.get(i)));
    }
    BlockStatement block = new BlockStatement(nodes.toArray(new Statement[0]), this.getBTToken(ctx.getStart()));
    this.checkGoto(block);
    pbCtx.exitBlock();
    return block;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) StaticTextByteASTNode(org.beetl.core.statement.StaticTextByteASTNode) ASTNode(org.beetl.core.statement.ASTNode) StaticTextASTNode(org.beetl.core.statement.StaticTextASTNode) ArrayList(java.util.ArrayList) BlockStatement(org.beetl.core.statement.BlockStatement)

Example 2 with Statement

use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseForSt.

protected Statement parseForSt(ForStContext ctx) {
    pbCtx.enterBlock();
    // break,continue语句到此为止
    pbCtx.current.canStopContinueBreakFlag = true;
    StatementContext forContext = ctx.statement(0);
    StatementContext elseContext = null;
    ForControlContext forTypeCtx = ctx.forControl();
    if (forTypeCtx.forInControl() != null) {
        // for(a in list)...
        ForInControlContext forCtx = forTypeCtx.forInControl();
        VarDefineNode forVar = new VarDefineNode(this.getBTToken(forCtx.Identifier().getSymbol()));
        if (pbCtx.hasDefined(forVar.token.text) != null) {
            GrammarToken token = pbCtx.hasDefined(forVar.token.text);
            BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "已经在第" + token.line + "行定义");
            ex.pushToken(forVar.token);
            throw ex;
        }
        VarDefineNode loopStatusVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier().getSymbol().getText() + "LP", forCtx.Identifier().getSymbol().getLine(), 0));
        if (pbCtx.hasDefined(loopStatusVar.token.text) != null) {
            GrammarToken token = pbCtx.hasDefined(loopStatusVar.token.text);
            BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "For循环隐含变量,已经在第" + token.line + "行定义");
            ex.pushToken(loopStatusVar.token);
            throw ex;
        }
        pbCtx.addVarAndPostion(forVar);
        pbCtx.addVarAndPostion(loopStatusVar);
        // //beetl.2 兼容
        // VarDefineNode indexVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier()
        // .getSymbol().getText()
        // + "_index", forCtx.Identifier().getSymbol().getLine(), 0));
        // 
        // VarDefineNode sizeVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier()
        // .getSymbol().getText()
        // + "_size", forCtx.Identifier().getSymbol().getLine(), 0));
        // 
        // pbCtx.addVarAndPostion(indexVar);
        // 
        // pbCtx.addVarAndPostion(sizeVar);
        Expression exp = this.parseExpress(forCtx.expression());
        Statement forPart = this.parseStatment(forContext);
        // elsefor
        Statement elseForPart = null;
        if (ctx.Elsefor() != null) {
            elseContext = ctx.statement(1);
            elseForPart = this.parseStatment(elseContext);
        }
        boolean hasSafe = false;
        if (exp instanceof VarRef) {
            VarRef varRef = (VarRef) exp;
            hasSafe = varRef.hasSafe;
        }
        if (pbCtx.isSafeOutput) {
            hasSafe = true;
        }
        ForStatement forStatement = new ForStatement(forVar, exp, hasSafe, forPart, elseForPart, forVar.token);
        this.checkGoto(forStatement);
        pbCtx.exitBlock();
        return forStatement;
    } else {
        GeneralForControlContext forCtx = forTypeCtx.generalForControl();
        Expression[] initExp = null;
        VarAssignStatementSeq varInitSeq = null;
        Expression condtion = null;
        Expression[] updateExp = null;
        if (forCtx.forInit() != null) {
            ForInitContext forInitCtx = forCtx.forInit();
            if (forInitCtx.Var() == null) {
                // for( a=1,b=3;
                List<ExpressionContext> list = forInitCtx.expressionList().expression();
                initExp = this.parseExpressionCtxList(list);
            } else {
                // for( var a=1,b=3;
                VarDeclareListContext varDeclare = forInitCtx.varDeclareList();
                varInitSeq = this.parseVarDeclareList(varDeclare);
            }
        }
        if (forCtx.expression() != null) {
            condtion = this.parseExpress(forCtx.expression());
        }
        if (forCtx.forUpdate() != null) {
            ForUpdateContext updateCtx = forCtx.forUpdate();
            List<ExpressionContext> list = updateCtx.expressionList().expression();
            updateExp = this.parseExpressionCtxList(list);
        }
        Statement forPart = this.parseStatment(forContext);
        // elsefor
        Statement elseForPart = null;
        if (ctx.Elsefor() != null) {
            elseContext = ctx.statement(1);
            elseForPart = this.parseStatment(elseContext);
        }
        String str = forTypeCtx.getText();
        GeneralForStatement forStat = new GeneralForStatement(varInitSeq, initExp, condtion, updateExp, forPart, elseForPart, this.getBTToken(str, forTypeCtx.start.getLine()));
        pbCtx.exitBlock();
        return forStat;
    }
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarAssignStatementSeq(org.beetl.core.statement.VarAssignStatementSeq) BeetlException(org.beetl.core.exception.BeetlException) GrammarToken(org.beetl.core.statement.GrammarToken) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) ForUpdateContext(org.beetl.core.parser.BeetlParser.ForUpdateContext) ForInitContext(org.beetl.core.parser.BeetlParser.ForInitContext) VarDeclareListContext(org.beetl.core.parser.BeetlParser.VarDeclareListContext) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext) GeneralForControlContext(org.beetl.core.parser.BeetlParser.GeneralForControlContext) GeneralForControlContext(org.beetl.core.parser.BeetlParser.GeneralForControlContext) ForControlContext(org.beetl.core.parser.BeetlParser.ForControlContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) VarDefineNode(org.beetl.core.statement.VarDefineNode) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) ForStatement(org.beetl.core.statement.ForStatement) ForInControlContext(org.beetl.core.parser.BeetlParser.ForInControlContext) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 3 with Statement

use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseWhile.

protected WhileStatement parseWhile(WhileStContext wc) {
    pbCtx.enterBlock();
    // break,continue语句到此为止
    pbCtx.current.canStopContinueBreakFlag = true;
    ExpressionContext condtionCtx = wc.parExpression().expression();
    StatementContext bodyCtx = wc.statement();
    Expression condtion = this.parseExpress(condtionCtx);
    Statement body = this.parseStatment(bodyCtx);
    WhileStatement whileStat = new WhileStatement(condtion, body, this.getBTToken(wc.getStart()));
    pbCtx.exitBlock();
    return whileStat;
}
Also used : StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) WhileStatement(org.beetl.core.statement.WhileStatement) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext)

Example 4 with Statement

use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.

the class TypeBindingProbe method infer.

/**
 * 确定表达式的类型
 */
protected void infer() {
    InferContext ctx = new InferContext();
    ctx.types = types;
    ctx.gt = this.program.gt;
    for (Statement st : this.program.metaData.statements) {
        st.infer(ctx);
    }
}
Also used : InferContext(org.beetl.core.InferContext) Statement(org.beetl.core.statement.Statement)

Example 5 with Statement

use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseVarDeclareList.

private VarAssignStatementSeq parseVarDeclareList(VarDeclareListContext ctx) {
    List<AssignMentContext> list = ctx.assignMent();
    List<ASTNode> listNode = new ArrayList<ASTNode>();
    for (AssignMentContext amc : list) {
        VarAssignStatement vas = this.parseAssign(amc);
        listNode.add(vas);
        if (!(vas instanceof VarRefAssignStatement)) {
            // 如果是临时变量定义
            this.registerNewVar(vas);
        }
    }
    VarAssignStatementSeq seq = new VarAssignStatementSeq(listNode.toArray(new Statement[0]), null);
    return seq;
}
Also used : VarAssignStatementSeq(org.beetl.core.statement.VarAssignStatementSeq) AssignMentContext(org.beetl.core.parser.BeetlParser.AssignMentContext) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) StaticTextByteASTNode(org.beetl.core.statement.StaticTextByteASTNode) ASTNode(org.beetl.core.statement.ASTNode) StaticTextASTNode(org.beetl.core.statement.StaticTextASTNode) ArrayList(java.util.ArrayList) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement)

Aggregations

Statement (org.beetl.core.statement.Statement)8 AjaxStatement (org.beetl.core.statement.AjaxStatement)7 BlockStatement (org.beetl.core.statement.BlockStatement)7 BreakStatement (org.beetl.core.statement.BreakStatement)7 ContinueStatement (org.beetl.core.statement.ContinueStatement)7 DirectiveStatement (org.beetl.core.statement.DirectiveStatement)7 EndStatement (org.beetl.core.statement.EndStatement)7 ForStatement (org.beetl.core.statement.ForStatement)7 GeneralForStatement (org.beetl.core.statement.GeneralForStatement)7 IfStatement (org.beetl.core.statement.IfStatement)7 ReturnStatement (org.beetl.core.statement.ReturnStatement)7 SelectStatement (org.beetl.core.statement.SelectStatement)7 SwitchStatement (org.beetl.core.statement.SwitchStatement)7 TagStatement (org.beetl.core.statement.TagStatement)7 TagVarBindingStatement (org.beetl.core.statement.TagVarBindingStatement)7 TryCatchStatement (org.beetl.core.statement.TryCatchStatement)7 VarAssignStatement (org.beetl.core.statement.VarAssignStatement)7 VarRefAssignStatement (org.beetl.core.statement.VarRefAssignStatement)7 WhileStatement (org.beetl.core.statement.WhileStatement)7 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)4