Search in sources :

Example 1 with BlockCode

use of jetbrick.template.parser.code.BlockCode in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitSet_expression.

@Override
public Code visitSet_expression(Set_expressionContext ctx) {
    String name = assert_java_identifier(ctx.IDENTIFIER(), true);
    SegmentCode code = (SegmentCode) ctx.expression().accept(this);
    // 是否同时定义一个变量
    boolean defining = false;
    // 变量类型
    TypedKlass lhs = null;
    TypeContext type = ctx.type();
    if (type != null) {
        defining = true;
        // 定义一个变量
        SegmentCode c = (SegmentCode) type.accept(this);
        lhs = c.getTypedKlass();
        if (!scopeCode.define(name, lhs)) {
            throw reportError("Duplicate local variable " + name, ctx.IDENTIFIER());
        }
    } else {
        // 直接赋值,如果变量没有定义,则先定义
        lhs = scopeCode.resolve(name, false);
        defining = (lhs == null);
        if (defining) {
            lhs = code.getTypedKlass();
            scopeCode.define(name, lhs);
        }
    }
    // 进行赋值语句类型检查
    if (!ClassUtils.isAssignable(lhs.getKlass(), code.getKlass())) {
        // 是否支持正常赋值
        if (!ClassUtils.isAssignable(code.getKlass(), lhs.getKlass())) {
            // 是否支持强制类型转换
            throw reportError("Type mismatch: cannot convert from " + code.getTypedKlass().toString() + " to " + lhs.toString(), ctx);
        }
    }
    BlockCode c = scopeCode.createBlockCode(2);
    String source = name + " = (" + lhs.getSource() + ") " + code.toString() + "; // line: " + ctx.getStart().getLine();
    if (defining) {
        source = lhs.getSource() + " " + source;
    }
    c.addLine(source);
    c.addLine(Code.CONTEXT_NAME + ".put(\"" + name + "\", " + name + ");");
    return c;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode) TypeContext(jetbrick.template.parser.grammer.JetTemplateParser.TypeContext)

Example 2 with BlockCode

use of jetbrick.template.parser.code.BlockCode in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitFor_directive.

@Override
public Code visitFor_directive(For_directiveContext ctx) {
    BlockCode code = scopeCode.createBlockCode(16);
    String id_for = getUid("for");
    scopeCode = scopeCode.push();
    // 注意:for循环变量的作用域要放在 for 内部, 防止出现变量重定义错误
    ForExpressionCode for_expr_code = (ForExpressionCode) ctx.for_expression().accept(this);
    // for block
    forStack.push(id_for);
    Code for_block_code = ctx.block().accept(this);
    forStack.pop();
    scopeCode = scopeCode.pop();
    // for-else
    Else_directiveContext else_directive = ctx.else_directive();
    Code for_else_block = (else_directive == null) ? null : else_directive.accept(this);
    // 生成代码
    String id_foritem = getUid("foritem");
    String typeName = for_expr_code.getKlassName();
    String itemName = for_expr_code.getName();
    code.addLine("Object " + id_foritem + " = context.get(\"" + itemName + "\"); // save it");
    code.addLine("JetForIterator " + id_for + " = new JetForIterator(" + for_expr_code.toString() + ");");
    code.addLine("while (" + id_for + ".hasNext()) { // line: " + ctx.getStart().getLine());
    // class item = (class) it.next() ...
    code.addLine("  " + typeName + " " + itemName + " = (" + typeName + ") " + id_for + ".next();");
    code.addLine("  context.put(\"" + itemName + "\", " + itemName + ");");
    code.addChild(for_block_code);
    code.addLine("}");
    code.addLine("context.put(\"" + itemName + "\", " + id_foritem + "); // reset it");
    // for else ...
    if (for_else_block != null) {
        code.addLine("if (" + id_for + ".empty()) { // line: " + ctx.getStart().getLine());
        code.addChild(for_else_block);
        code.addLine("}");
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) Else_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext) ScopeCode(jetbrick.template.parser.code.ScopeCode) BlockCode(jetbrick.template.parser.code.BlockCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) TemplateClassCode(jetbrick.template.parser.code.TemplateClassCode) TextCode(jetbrick.template.parser.code.TextCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) MacroCode(jetbrick.template.parser.code.MacroCode) Code(jetbrick.template.parser.code.Code) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) TagCode(jetbrick.template.parser.code.TagCode)

Example 3 with BlockCode

use of jetbrick.template.parser.code.BlockCode in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitSet_directive.

@Override
public Code visitSet_directive(Set_directiveContext ctx) {
    List<Set_expressionContext> set_expression_list = ctx.set_expression();
    BlockCode code = scopeCode.createBlockCode(set_expression_list.size());
    for (Set_expressionContext node : set_expression_list) {
        Code c = node.accept(this);
        if (c != null) {
            code.addChild(c);
        }
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) Set_expressionContext(jetbrick.template.parser.grammer.JetTemplateParser.Set_expressionContext) ScopeCode(jetbrick.template.parser.code.ScopeCode) BlockCode(jetbrick.template.parser.code.BlockCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) TemplateClassCode(jetbrick.template.parser.code.TemplateClassCode) TextCode(jetbrick.template.parser.code.TextCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) MacroCode(jetbrick.template.parser.code.MacroCode) Code(jetbrick.template.parser.code.Code) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) TagCode(jetbrick.template.parser.code.TagCode)

Example 4 with BlockCode

use of jetbrick.template.parser.code.BlockCode in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitElseif_directive.

@Override
public Code visitElseif_directive(Elseif_directiveContext ctx) {
    BlockCode code = scopeCode.createBlockCode(16);
    SegmentCode expr_code = (SegmentCode) ctx.expression().accept(this);
    code.addLine("else if (" + get_if_expression_source(expr_code) + ") { // line: " + ctx.getStart().getLine());
    scopeCode = scopeCode.push();
    code.addChild(ctx.block().accept(this));
    scopeCode = scopeCode.pop();
    code.addLine("}");
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 5 with BlockCode

use of jetbrick.template.parser.code.BlockCode in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitElse_directive.

@Override
public Code visitElse_directive(Else_directiveContext ctx) {
    BlockCode code = scopeCode.createBlockCode(16);
    if (ctx.getParent() instanceof If_directiveContext) {
        code.addLine("else { // line: " + ctx.getStart().getLine());
    }
    scopeCode = scopeCode.push();
    code.addChild(ctx.block().accept(this));
    scopeCode = scopeCode.pop();
    if (ctx.getParent() instanceof If_directiveContext) {
        code.addLine("}");
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) If_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext)

Aggregations

BlockCode (jetbrick.template.parser.code.BlockCode)9 SegmentCode (jetbrick.template.parser.code.SegmentCode)8 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)4 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)4 Code (jetbrick.template.parser.code.Code)3 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)3 MacroCode (jetbrick.template.parser.code.MacroCode)3 ScopeCode (jetbrick.template.parser.code.ScopeCode)3 TagCode (jetbrick.template.parser.code.TagCode)3 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)3 TextCode (jetbrick.template.parser.code.TextCode)3 Else_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext)2 If_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext)2 DirectiveContext (jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext)1 Elseif_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Elseif_directiveContext)1 ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)1 For_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext)1 Macro_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext)1 Set_expressionContext (jetbrick.template.parser.grammer.JetTemplateParser.Set_expressionContext)1 Tag_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Tag_directiveContext)1