Search in sources :

Example 6 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpr_new_array.

@Override
public Code visitExpr_new_array(Expr_new_arrayContext ctx) {
    SegmentCode code = (SegmentCode) ctx.type().accept(this);
    if (code.getKlass().isArray()) {
        throw reportError("Cannot specify an array dimension after an empty dimension", ctx.type());
    }
    StringBuilder typeSource = new StringBuilder(code.toString());
    // ็”Ÿๆˆไปฃ็ 
    StringBuilder source = new StringBuilder(32);
    source.append("(new ").append(code.toString());
    for (ExpressionContext expression : ctx.expression()) {
        SegmentCode c = (SegmentCode) expression.accept(this);
        if (!ClassUtils.isAssignable(Integer.TYPE, c.getKlass())) {
            throw reportError("Type mismatch: cannot convert from " + c.getKlassName() + " to int.", expression);
        }
        source.append('[').append(c.toString()).append(']');
        typeSource.append("[]");
    }
    source.append(')');
    TypedKlass resultKlass = resolver.resolveTypedKlass(typeSource.toString());
    return new SegmentCode(resultKlass, source.toString(), ctx);
}
Also used : TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode) ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)

Example 7 with SegmentCode

use of jetbrick.template.parser.code.SegmentCode 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 8 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpr_class_cast.

@Override
public Code visitExpr_class_cast(Expr_class_castContext ctx) {
    SegmentCode code = (SegmentCode) ctx.type().accept(this);
    Code expr_code = ctx.expression().accept(this);
    String source = "((" + code.toString() + ")" + expr_code.toString() + ")";
    return new SegmentCode(code.getTypedKlass(), source, ctx);
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) 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 9 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpr_hash_map.

@Override
public Code visitExpr_hash_map(Expr_hash_mapContext ctx) {
    String source = "Collections.EMPTY_MAP";
    Hash_map_entry_listContext hash_map_entry_list = ctx.hash_map_entry_list();
    if (hash_map_entry_list != null) {
        Code code = hash_map_entry_list.accept(this);
        source = "JetUtils.asMap(" + code.toString() + ")";
    }
    return new SegmentCode(Map.class, source, ctx);
}
Also used : Hash_map_entry_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Hash_map_entry_listContext) SegmentCode(jetbrick.template.parser.code.SegmentCode) 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 10 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpr_instanceof.

@Override
public Code visitExpr_instanceof(Expr_instanceofContext ctx) {
    SegmentCode lhs = (SegmentCode) ctx.expression().accept(this);
    SegmentCode rhs = (SegmentCode) ctx.type().accept(this);
    if (!ClassUtils.isAssignable(lhs.getKlass(), rhs.getKlass()) && !ClassUtils.isAssignable(lhs.getKlass(), rhs.getKlass())) {
        throw reportError("Incompatible conditional operand types " + lhs.getKlassName() + " and " + rhs.getKlassName(), ctx.getChild(1));
    }
    String source = "(" + lhs.toString() + " instanceof " + rhs.toString() + ")";
    return new SegmentCode(Boolean.TYPE, source, ctx);
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode)

Aggregations

SegmentCode (jetbrick.template.parser.code.SegmentCode)42 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)14 TypedKlass (jetbrick.template.parser.support.TypedKlass)12 BlockCode (jetbrick.template.parser.code.BlockCode)11 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)9 ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)9 MacroCode (jetbrick.template.parser.code.MacroCode)8 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)7 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)7 Code (jetbrick.template.parser.code.Code)6 ScopeCode (jetbrick.template.parser.code.ScopeCode)6 TagCode (jetbrick.template.parser.code.TagCode)6 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)6 TextCode (jetbrick.template.parser.code.TextCode)6 Expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext)6 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Field (java.lang.reflect.Field)2 ConstantContext (jetbrick.template.parser.grammer.JetTemplateParser.ConstantContext)2