use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitExpressions.
@Override
public List<Statement> visitExpressions(ExpressionsContext ctx) {
List<Statement> list = new LinkedList();
for (ExpressionContext e : ctx.expression()) {
ExprNode expr = visitExpression(e);
list.add(new ExprStmt(expr));
}
return list;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitNewExpr.
@Override
public AstNode visitNewExpr(NewExprContext ctx) {
ObjectType clsType = parseClassType(ctx.classType());
if (clsType == null)
return null;
ExprNode[] params = visitAll(ctx.params).toArray(new ExprNode[0]);
List<ExprNode> paramList = new LinkedList(Arrays.asList(params));
NewObjectExpr newExpr;
try {
if (this.isNonStaticInnerClass(clsType.getClassNode())) {
paramList.add(0, new ThisExpr(this.getThisType()));
}
params = paramList.toArray(new ExprNode[paramList.size()]);
newExpr = new NewObjectExpr(clsType, params);
mapAst(newExpr, ctx);
return newExpr;
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.classType().rawClass, clsType.getName(), "<init>", params);
return null;
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.classType().rawClass, ex);
return null;
}
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitCastExpr.
@Override
public AstNode visitCastExpr(CastExprContext ctx) {
ExprNode castExpr;
ExprNode expr = visitExpression(ctx.expression());
Type toType = parseType(ctx.type());
Type fromType = expr.getType();
if (fromType instanceof PrimitiveType) {
if (toType instanceof PrimitiveType) {
castExpr = new PrimitiveCastExpr((PrimitiveType) fromType, (PrimitiveType) toType, expr);
} else {
this.handleSyntaxError("unable to cast primitive type to class type", ctx);
return null;
}
} else {
if (toType instanceof PrimitiveType) {
this.handleSyntaxError("unable to cast class type to primitive type", ctx);
return null;
} else {
castExpr = new CastExpr(toType, expr);
}
}
mapAst(castExpr, ctx);
return castExpr;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitExpression.
protected ExprNode visitExpression(ExpressionContext expression) {
Object node = visit(expression);
if (node instanceof ExprNode) {
return (ExprNode) node;
} else {
ExprNode expr;
if (node instanceof AstNode) {
expr = new ErrorousExpr((AstNode) node);
} else {
expr = new ErrorousExpr();
}
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "not an expression", expression);
return expr;
}
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitAssertStmt.
@Override
public Object visitAssertStmt(KalangParser.AssertStmtContext ctx) {
ExprNode failExpr = visitExpression(ctx.testCondition);
if (failExpr == null)
return null;
failExpr = BoxUtil.assign(failExpr, failExpr.getType(), Types.BOOLEAN_TYPE);
if (failExpr == null) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "boolean type expected", ctx.testCondition);
return null;
}
failExpr = new UnaryExpr(failExpr, UnaryExpr.OPERATION_LOGIC_NOT);
ExprNode failMsgExpr = null;
if (ctx.failMessage != null) {
failMsgExpr = visitExpression(ctx.failMessage);
if (failMsgExpr == null)
return null;
if (Types.VOID_TYPE.equals(failMsgExpr.getType())) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "non-void type expected", ctx.failMessage);
return null;
}
}
BlockStmt body = this.newBlock();
NewObjectExpr newErrorExpr;
try {
newErrorExpr = new NewObjectExpr(Types.requireAssertionErrorClassType(), failMsgExpr != null ? new ExprNode[] { failMsgExpr } : new ExprNode[0]);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
body.statements.add(new ThrowStmt(newErrorExpr));
popBlock();
return new IfStmt(failExpr, body, null);
}
Aggregations