Search in sources :

Example 1 with PrimitiveType

use of kalang.core.PrimitiveType in project kalang by kasonyang.

the class SemanticAnalyzer method getMathType.

public static PrimitiveType getMathType(Type t1, Type t2, String op) {
    PrimitiveType pt1 = getPrimitiveType(t1);
    PrimitiveType pt2 = getPrimitiveType(t2);
    if (pt1 == null) {
        throw new IllegalArgumentException(t1.getName());
    }
    if (pt2 == null)
        throw new IllegalArgumentException(t2.getName());
    String ret = MathType.getType(pt1.getName(), pt2.getName(), op);
    return Types.getPrimitiveType(ret);
}
Also used : PrimitiveType(kalang.core.PrimitiveType)

Example 2 with PrimitiveType

use of kalang.core.PrimitiveType in project kalang by kasonyang.

the class Ast2Class method typeSignature.

@Nullable
private String typeSignature(Type type) {
    if (type instanceof GenericType) {
        return "T" + type.getName() + ";";
    } else if (type instanceof ClassType) {
        ClassType pt = (ClassType) type;
        String ptypes = "";
        for (Type p : pt.getTypeArguments()) {
            ptypes += typeSignature(p);
        }
        if (!ptypes.isEmpty())
            ptypes = "<" + ptypes + ">";
        return "L" + pt.getClassNode().name.replace('.', '/') + ptypes + ";";
    } else if (type instanceof PrimitiveType) {
        return getTypeDescriptor(type);
    } else if (type instanceof ArrayType) {
        return "[" + typeSignature(((ArrayType) type).getComponentType());
    } else if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        Type[] lbs = wt.getLowerBounds();
        Type[] ubs = wt.getUpperBounds();
        if (lbs.length > 0) {
            // FIXME handle other lowerBounds
            return "-" + typeSignature(lbs[0]);
        } else if (ubs.length > 0) {
            // FIXME handle other lowerBounds
            return "+" + typeSignature(ubs[0]);
        } else {
            return "*";
        }
    } else {
        throw Exceptions.unsupportedTypeException(type);
    }
}
Also used : ArrayType(kalang.core.ArrayType) GenericType(kalang.core.GenericType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) WildcardType(kalang.core.WildcardType) PrimitiveType(kalang.core.PrimitiveType) ClassType(kalang.core.ClassType) Nullable(javax.annotation.Nullable)

Example 3 with PrimitiveType

use of kalang.core.PrimitiveType in project kalang by kasonyang.

the class AstBuilder method createBinaryExpr.

private ExprNode createBinaryExpr(String op, ExpressionContext exprCtx1, ExpressionContext exprCtx2, Token opStart, Token opEnd, ParserRuleContext ctx) {
    ExprNode expr1 = visitExpression(exprCtx1);
    ExprNode expr2 = visitExpression(exprCtx2);
    Type type1 = expr1.getType();
    Type type2 = expr2.getType();
    boolean isPrimitive1 = (type1 instanceof PrimitiveType);
    boolean isPrimitive2 = (type2 instanceof PrimitiveType);
    ExprNode expr;
    if (isPrimitive1 && isPrimitive2) {
        expr = createBinaryExpr(expr1, expr2, op);
    } else if (Types.isNumber(type1) && Types.isNumber(type2)) {
        PrimitiveType t = SemanticAnalyzer.getMathType(type1, type2, op);
        expr1 = BoxUtil.assign(expr1, type1, t);
        expr2 = BoxUtil.assign(expr2, type2, t);
        if (expr1 == null)
            throw Exceptions.unexceptedValue(expr1);
        if (expr2 == null)
            throw Exceptions.unexceptedValue(expr2);
        expr = createBinaryExpr(expr1, expr2, op);
    } else if (op.equals("==") || op.equals("!=")) {
        expr = createBinaryExpr(expr1, expr2, op);
    } else if (op.equals("+")) {
        expr = this.concatExpressionsToStringExpr(new ExprNode[] { expr1, expr2 }, new Token[] { exprCtx1.getStart(), exprCtx2.getStart() });
    } else {
        handleSyntaxError("unsupported operation", ParserRuleContext.EMPTY, opStart, opEnd);
        return null;
    }
    if (expr != null)
        mapAst(expr, ctx);
    return expr;
}
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) PrimitiveType(kalang.core.PrimitiveType)

Example 4 with PrimitiveType

use of kalang.core.PrimitiveType 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)

Example 5 with PrimitiveType

use of kalang.core.PrimitiveType in project kalang by kasonyang.

the class BoxUtil method getCastMethod.

private static int getCastMethod(Type fromType, Type toType) {
    if (toType.isAssignableFrom(fromType)) {
        return CAST_NOTHING;
    }
    if (fromType instanceof PrimitiveType && toType instanceof PrimitiveType) {
        // if ((toType) instanceof PrimitiveType) {
        if (MathType.castable(MathType.getType(fromType.getName()), MathType.getType(toType.getName()))) {
            return CAST_PRIMITIVE;
        }
    } else if (fromType instanceof PrimitiveType && toType instanceof ObjectType) {
        if (toType.equals(Types.getRootType())) {
            return CAST_PRIMITIVE_TO_OBJECT;
        }
        // if (fromType.equals(Types.NULL_TYPE)) {
        // return CAST_NOTHING;
        // }
        // //            if (toType.equals(Types.STRING_CLASS_TYPE)) {
        // //                return CAST_PRIMITIVE_TO_STRING;
        // //            }
        PrimitiveType toPriType = Types.getPrimitiveType((ObjectType) toType);
        if (toPriType == null) {
            return CAST_UNSUPPORTED;
        }
        if (toPriType.equals(fromType)) {
            return CAST_PRIMITIVE_TO_OBJECT;
        }
    } else if (fromType instanceof ObjectType && toType instanceof PrimitiveType) {
        // if() {
        ObjectType fromClassType = (ObjectType) fromType;
        PrimitiveType fromPrimitive = Types.getPrimitiveType(fromClassType);
        if (fromPrimitive == null) {
            return CAST_UNSUPPORTED;
        }
        if (fromPrimitive.equals(toType)) {
            return CAST_OBJECT_TO_PRIMITIVE;
        }
    }
    // }
    return CAST_UNSUPPORTED;
}
Also used : ObjectType(kalang.core.ObjectType) PrimitiveType(kalang.core.PrimitiveType)

Aggregations

PrimitiveType (kalang.core.PrimitiveType)5 ObjectType (kalang.core.ObjectType)4 ArrayType (kalang.core.ArrayType)3 ClassType (kalang.core.ClassType)3 GenericType (kalang.core.GenericType)3 Type (kalang.core.Type)3 WildcardType (kalang.core.WildcardType)3 ExprNode (kalang.ast.ExprNode)2 Nullable (javax.annotation.Nullable)1 CastExpr (kalang.ast.CastExpr)1 PrimitiveCastExpr (kalang.ast.PrimitiveCastExpr)1