Search in sources :

Example 1 with VarAssignStatement

use of org.beetl.core.statement.VarAssignStatement 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)

Example 2 with VarAssignStatement

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

the class AntlrProgramBuilder method parseAssign.

/**
 * 定义变量
 * @param amc
 * @return
 */
protected VarAssignStatement parseAssign(AssignMentContext amc) {
    VarAssignStatement vas = null;
    if (amc instanceof AssignGeneralInStContext) {
        AssignGeneralInStContext agc = (AssignGeneralInStContext) amc;
        ExpressionContext expCtx = agc.generalAssignExp().expression();
        Expression exp = parseExpress(expCtx);
        VarRefContext varRefCtx = agc.generalAssignExp().varRef();
        if (varRefCtx.children.size() == 1) {
            // var a=1;
            Token token = varRefCtx.Identifier().getSymbol();
            vas = new VarAssignStatement(exp, getBTToken(token));
        } else {
            // var a.b=1 since 2.7.0
            VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
            vas = new VarRefAssignStatement(exp, ref);
        }
        return vas;
    } else if (amc instanceof AssignIdContext) {
        AssignIdContext idCtx = (AssignIdContext) amc;
        vas = new VarAssignStatement(Literal.NULLLiteral, getBTToken(idCtx.Identifier().getSymbol()));
        return vas;
    } else if (amc instanceof AssignTemplateVarContext) {
        AssignTemplateVarContext templateVarCtx = (AssignTemplateVarContext) amc;
        BlockContext blockCtx = templateVarCtx.block();
        BlockStatement block = this.parseBlock(blockCtx.statement(), blockCtx);
        ContentBodyExpression bodyExp = new ContentBodyExpression(block, getBTToken(templateVarCtx.Identifier().getSymbol()));
        vas = new VarAssignStatement(bodyExp, getBTToken(templateVarCtx.Identifier().getSymbol()));
    } else {
        throw new RuntimeException("不支持 在 " + amc.start.getLine());
    }
    return vas;
}
Also used : VarRef(org.beetl.core.statement.VarRef) AssignGeneralInStContext(org.beetl.core.parser.BeetlParser.AssignGeneralInStContext) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) BlockStatement(org.beetl.core.statement.BlockStatement) AssignIdContext(org.beetl.core.parser.BeetlParser.AssignIdContext) 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) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) VarRefContext(org.beetl.core.parser.BeetlParser.VarRefContext) AssignTemplateVarContext(org.beetl.core.parser.BeetlParser.AssignTemplateVarContext)

Aggregations

BlockStatement (org.beetl.core.statement.BlockStatement)2 VarAssignStatement (org.beetl.core.statement.VarAssignStatement)2 VarRefAssignStatement (org.beetl.core.statement.VarRefAssignStatement)2 ArrayList (java.util.ArrayList)1 Token (org.antlr.v4.runtime.Token)1 AssignGeneralInStContext (org.beetl.core.parser.BeetlParser.AssignGeneralInStContext)1 AssignIdContext (org.beetl.core.parser.BeetlParser.AssignIdContext)1 AssignMentContext (org.beetl.core.parser.BeetlParser.AssignMentContext)1 AssignTemplateVarContext (org.beetl.core.parser.BeetlParser.AssignTemplateVarContext)1 BlockContext (org.beetl.core.parser.BeetlParser.BlockContext)1 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)1 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)1 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)1 VarRefContext (org.beetl.core.parser.BeetlParser.VarRefContext)1 ASTNode (org.beetl.core.statement.ASTNode)1 AjaxStatement (org.beetl.core.statement.AjaxStatement)1 AndExpression (org.beetl.core.statement.AndExpression)1 ArthExpression (org.beetl.core.statement.ArthExpression)1 BreakStatement (org.beetl.core.statement.BreakStatement)1 CompareExpression (org.beetl.core.statement.CompareExpression)1