Search in sources :

Example 1 with PrimitiveCastExpr

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;
}
Also used : ExprNode(kalang.ast.ExprNode) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr) CastExpr(kalang.ast.CastExpr) PrimitiveType(kalang.core.PrimitiveType) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr)

Aggregations

CastExpr (kalang.ast.CastExpr)1 ExprNode (kalang.ast.ExprNode)1 PrimitiveCastExpr (kalang.ast.PrimitiveCastExpr)1 ArrayType (kalang.core.ArrayType)1 ClassType (kalang.core.ClassType)1 GenericType (kalang.core.GenericType)1 ObjectType (kalang.core.ObjectType)1 PrimitiveType (kalang.core.PrimitiveType)1 Type (kalang.core.Type)1 WildcardType (kalang.core.WildcardType)1