Search in sources :

Example 26 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpr_conditional_ternary.

@Override
public Code visitExpr_conditional_ternary(Expr_conditional_ternaryContext ctx) {
    SegmentCode condition = (SegmentCode) ctx.expression(0).accept(this);
    SegmentCode lhs = (SegmentCode) ctx.expression(1).accept(this);
    SegmentCode rhs = (SegmentCode) ctx.expression(2).accept(this);
    String source = "(" + get_if_expression_source(condition) + "?" + lhs.toString() + ":" + rhs.toString() + ")";
    TypedKlass klass = PromotionUtils.getResultClassForConditionalOperator(lhs.getTypedKlass(), rhs.getTypedKlass());
    return new SegmentCode(klass, source, ctx);
}
Also used : TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 27 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpression_list.

@Override
public Code visitExpression_list(Expression_listContext ctx) {
    List<ExpressionContext> expression_list = ctx.expression();
    SegmentListCode code = new SegmentListCode(expression_list.size());
    for (ExpressionContext expression : expression_list) {
        SegmentCode c = (SegmentCode) expression.accept(this);
        assert_not_void_expression(c);
        code.addChild(c);
    }
    return code;
}
Also used : ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) SegmentCode(jetbrick.template.parser.code.SegmentCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode)

Example 28 with SegmentCode

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

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

the class JetTemplateCodeVisitor method visitExpr_math_unary_prefix.

@Override
public Code visitExpr_math_unary_prefix(Expr_math_unary_prefixContext ctx) {
    ExpressionContext expression = ctx.expression();
    SegmentCode code = (SegmentCode) expression.accept(this);
    String op = ctx.getChild(0).getText();
    assert_not_null_constantContext(expression);
    // 类型检查
    Class<?> resultKlass;
    if (op.length() == 1) {
        // +, -, ~
        resultKlass = PromotionUtils.get_unary_basic(code.getKlass(), op);
    } else {
        // ++, --
        if (expression.getChildCount() == 1 && expression.getChild(0) instanceof ConstantContext) {
            throw reportError("Invalid argument to operation " + op + ", required: variable, found Value", expression);
        }
        resultKlass = PromotionUtils.get_unary_inc_dec(code.getKlass(), op);
    }
    if (resultKlass == null) {
        throw reportError("The UnaryOperator \"" + op + "\" is not applicable for the operand " + code.getKlassName(), ctx.getChild(0));
    }
    String source = "(" + op + code.toString() + ")";
    return new SegmentCode(code.getTypedKlass(), source, ctx);
}
Also used : ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) SegmentCode(jetbrick.template.parser.code.SegmentCode) ConstantContext(jetbrick.template.parser.grammer.JetTemplateParser.ConstantContext)

Example 30 with SegmentCode

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

the class JetTemplateCodeVisitor method visitExpr_static_field_access.

@Override
public Code visitExpr_static_field_access(Expr_static_field_accessContext ctx) {
    SegmentCode static_type_name_code = (SegmentCode) ctx.static_type_name().accept(this);
    String typeName = static_type_name_code.toString();
    String name = ctx.IDENTIFIER().getText();
    Class<?> beanClass = resolver.resolveClass(typeName);
    if (beanClass == null) {
        throw reportError("java.lang.ClassNotFoundException: " + typeName, static_type_name_code.getNode());
    }
    Field field = resolver.resolveStaticField(beanClass, name);
    if (field == null) {
        throw reportError(name + " is not a static field for type " + beanClass.getName(), ctx.IDENTIFIER());
    }
    if (securityManager != null) {
        securityManager.checkMemberAccess(field);
    }
    String source = ClassUtils.getShortClassName(field.getDeclaringClass()) + '.' + name;
    TypedKlass resultKlass = TypedKlassUtils.getFieldTypedKlass(field);
    return new SegmentCode(resultKlass, source, ctx);
}
Also used : Field(java.lang.reflect.Field) TypedKlass(jetbrick.template.parser.support.TypedKlass) 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