use of kalang.ast.PrimitiveCastExpr 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;
}
Aggregations