use of jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext 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;
}
use of jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext in project jetbrick-template-1x by subchen.
the class JetTemplateCodeVisitor method visitIf_directive.
@Override
public Code visitIf_directive(If_directiveContext ctx) {
BlockCode code = scopeCode.createBlockCode(16);
SegmentCode expr_code = (SegmentCode) ctx.expression().accept(this);
code.addLine("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("}");
// elseif ...
List<Elseif_directiveContext> elseif_directive_list = ctx.elseif_directive();
for (Elseif_directiveContext elseif_directive : elseif_directive_list) {
code.addChild(elseif_directive.accept(this));
}
// else ...
Else_directiveContext else_directive = ctx.else_directive();
if (else_directive != null) {
code.addChild(else_directive.accept(this));
}
return code;
}
Aggregations