Search in sources :

Example 1 with DefineExpressionCode

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

the class JetTemplateCodeVisitor method visitDefine_expression.

@Override
public Code visitDefine_expression(Define_expressionContext ctx) {
    SegmentCode code = (SegmentCode) ctx.type().accept(this);
    String name = assert_java_identifier(ctx.IDENTIFIER(), true);
    return new DefineExpressionCode(code.getTypedKlass(), name, ctx);
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode)

Example 2 with DefineExpressionCode

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

the class JetTemplateCodeVisitor method visitDefine_directive.

@Override
public Code visitDefine_directive(Define_directiveContext ctx) {
    SegmentListCode define_expression_list = (SegmentListCode) ctx.define_expression_list().accept(this);
    BlockCode code = scopeCode.createBlockCode(define_expression_list.size());
    for (SegmentCode node : define_expression_list.getChildren()) {
        DefineExpressionCode c = (DefineExpressionCode) node;
        String name = c.getName();
        if (!scopeCode.define(name, c.getTypedKlass())) {
            throw reportError("Duplicate local variable " + name, c.getNode());
        }
        String typeName = c.getTypedKlass().asBoxedTypedKlass().toString();
        code.addLine(typeName + " " + name + " = (" + typeName + ") " + Code.CONTEXT_NAME + ".get(\"" + name + "\"); // line: " + c.getNode().getStart().getLine());
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode)

Example 3 with DefineExpressionCode

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

the class JetTemplateCodeVisitor method visitMacro_directive.

@Override
public Code visitMacro_directive(Macro_directiveContext ctx) {
    String text = ctx.getChild(0).getText();
    String name = text.substring(7, text.length() - 1).trim();
    MacroCode macroCode = scopeCode.createMacroCode();
    macroCode.setName(name);
    scopeCode = macroCode.getMethodCode();
    scopeCode.define(Code.CONTEXT_NAME, TypedKlass.JetContext);
    // 处理参数
    Define_expression_listContext define_expression_list = ctx.define_expression_list();
    if (define_expression_list != null) {
        SegmentListCode define_list_code = (SegmentListCode) define_expression_list.accept(this);
        macroCode.setDefineListCode(define_list_code);
        // 设置参数 Context
        for (SegmentCode node : define_list_code.getChildren()) {
            DefineExpressionCode c = (DefineExpressionCode) node;
            scopeCode.define(c.getName(), c.getTypedKlass());
        }
    }
    // 需要先定义 macro,这样可以支持 macro 的递归调用 (issue 102)
    if (macroMap == null) {
        macroMap = new HashMap<String, MacroCode>(8);
    }
    MacroCode old = macroMap.put(name, macroCode);
    if (old != null) {
        throw reportError("Duplicated macro defination " + name, ctx);
    }
    tcc.addMacro(macroCode);
    // 访问 macro body
    // add body content
    scopeCode.setBodyCode(ctx.block().accept(this));
    scopeCode = scopeCode.pop();
    return Code.EMPTY;
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) MacroCode(jetbrick.template.parser.code.MacroCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) Define_expression_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Define_expression_listContext)

Aggregations

DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)3 SegmentCode (jetbrick.template.parser.code.SegmentCode)3 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)2 BlockCode (jetbrick.template.parser.code.BlockCode)1 MacroCode (jetbrick.template.parser.code.MacroCode)1 Define_expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Define_expression_listContext)1