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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations