Search in sources :

Example 1 with Type

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

the class SemanticAnalyzer method validateAssign.

public boolean validateAssign(AssignableExpr to, ExprNode from, OffsetRange offset) {
    if (to instanceof VarExpr) {
        LocalVarNode varObject = ((VarExpr) to).getVar();
        if (Modifier.isFinal(varObject.modifier)) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("%s is readonly", varObject.getName()), offset);
            return false;
        }
    } else if (to instanceof FieldExpr) {
        FieldDescriptor field = ((FieldExpr) to).getField();
        if (Modifier.isFinal(field.getModifier())) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("%s is readonly", field.getName()), offset);
            return false;
        }
    }
    Type toType = to.getType();
    Type fromType = from.getType();
    if (!toType.isAssignableFrom(fromType)) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("incompatible types: %s cannot be converted to %s", fromType, toType), offset);
        return false;
    }
    return true;
}
Also used : ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) MathType(kalang.util.MathType) PrimitiveType(kalang.core.PrimitiveType) VarExpr(kalang.ast.VarExpr) FieldExpr(kalang.ast.FieldExpr) LocalVarNode(kalang.ast.LocalVarNode) FieldDescriptor(kalang.core.FieldDescriptor)

Example 2 with Type

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

the class SemanticAnalyzer method validateUnaryExpr.

public boolean validateUnaryExpr(UnaryExpr node) {
    String op = node.getOperation();
    Type et = node.getExpr().getType();
    switch(op) {
        case UnaryExpr.OPERATION_LOGIC_NOT:
            return requireBoolean(node, et);
        case UnaryExpr.OPERATION_NEG:
        case UnaryExpr.OPERATION_POS:
        case UnaryExpr.OPERATION_NOT:
            return requireNumber(node, et);
        default:
            throw Exceptions.unexceptedValue(op);
    }
}
Also used : ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) MathType(kalang.util.MathType) PrimitiveType(kalang.core.PrimitiveType)

Example 3 with Type

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

the class SemanticAnalyzer method validateBinaryExpr.

public boolean validateBinaryExpr(BinaryExpr node) {
    ExprNode expr1 = node.getExpr1();
    ExprNode expr2 = node.getExpr2();
    Type t1 = expr1.getType();
    Type t2 = expr2.getType();
    String op = node.getOperation();
    switch(op) {
        case "==":
        case "!=":
            if (Types.isNumber(t1)) {
                return this.requireNumber(expr2, t2);
            } else {
                return true;
            // TODO pass anything.may be Object needed?
            }
        case "+":
        case "-":
        case "*":
        case "/":
        case "%":
        case ">=":
        case "<=":
        case ">":
        case "<":
        case "&":
        case "|":
        case "^":
        case BinaryExpr.OP_SHIFT_LEFT:
        case BinaryExpr.OP_SHIFT_RIGHT:
            return this.requireNumber(expr1, t1) && this.requireNumber(expr2, t2);
        case "&&":
        case "||":
            return requireBoolean(expr1, t1) && requireBoolean(expr2, t2);
        default:
            diagnosisReporter.report(Diagnosis.Kind.ERROR, "unsupport operation:" + op, node.offset);
            return false;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) MathType(kalang.util.MathType) PrimitiveType(kalang.core.PrimitiveType)

Example 4 with Type

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

the class SemanticAnalyzer method validateReturnStmt.

public boolean validateReturnStmt(MethodNode method, ReturnStmt node) {
    Type retType = method.getType();
    if (node.expr == null) {
        if (!retType.equals(Types.VOID_TYPE)) {
            diagnosisReporter.report(Diagnosis.Kind.ERROR, "missing return value", node.offset);
            return false;
        }
        return true;
    } else {
        Type exType = node.expr.getType();
        node.expr = this.checkAssign(node.expr, exType, retType, node);
        if (node.expr == null)
            return false;
        return true;
    }
}
Also used : ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) MathType(kalang.util.MathType) PrimitiveType(kalang.core.PrimitiveType)

Example 5 with Type

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

the class Ast2Class method getPrimitiveCastOpc.

private int getPrimitiveCastOpc(Type fromType, Type toType) {
    Type f = fromType;
    Type tt = toType;
    if (f.equals(INT_TYPE)) {
        if (tt.equals(LONG_TYPE))
            return I2L;
        else if (tt.equals(FLOAT_TYPE))
            return I2F;
        else if (tt.equals(DOUBLE_TYPE))
            return I2D;
        else if (tt.equals(SHORT_TYPE))
            return I2S;
        else if (tt.equals(BYTE_TYPE))
            return I2B;
        else if (tt.equals(CHAR_TYPE))
            return I2C;
    } else if (f.equals(FLOAT_TYPE)) {
        if (tt.equals(INT_TYPE))
            return F2I;
        else if (tt.equals(LONG_TYPE))
            return F2L;
        else if (tt.equals(DOUBLE_TYPE))
            return F2D;
    } else if (f.equals(LONG_TYPE)) {
        if (tt.equals(INT_TYPE))
            return L2I;
        else if (tt.equals(FLOAT_TYPE))
            return L2F;
        else if (tt.equals(DOUBLE_TYPE))
            return L2D;
    } else if (f.equals(DOUBLE_TYPE)) {
        if (tt.equals(INT_TYPE))
            return D2I;
        else if (tt.equals(LONG_TYPE))
            return D2L;
        else if (tt.equals(FLOAT_TYPE))
            return D2F;
    } else if (f.equals(BYTE_TYPE)) {
        if (tt.equals(SHORT_TYPE))
            return 0;
        else if (tt.equals(INT_TYPE))
            return 0;
        else if (tt.equals(LONG_TYPE))
            return I2L;
        else if (tt.equals(FLOAT_TYPE))
            return I2F;
        else if (tt.equals(DOUBLE_TYPE))
            return I2D;
    } else if (f.equals(CHAR_TYPE) || f.equals(SHORT_TYPE)) {
        if (tt.equals(INT_TYPE))
            return 0;
        else if (tt.equals(LONG_TYPE))
            return I2L;
        else if (tt.equals(FLOAT_TYPE))
            return I2F;
        else if (tt.equals(DOUBLE_TYPE))
            return I2D;
    }
    throw Exceptions.unexceptedException("It is unable to cast " + fromType + " to " + toType);
}
Also used : 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)

Aggregations

ObjectType (kalang.core.ObjectType)44 Type (kalang.core.Type)44 ArrayType (kalang.core.ArrayType)37 ClassType (kalang.core.ClassType)36 GenericType (kalang.core.GenericType)36 PrimitiveType (kalang.core.PrimitiveType)33 WildcardType (kalang.core.WildcardType)30 ExprNode (kalang.ast.ExprNode)17 LinkedList (java.util.LinkedList)7 ExprStmt (kalang.ast.ExprStmt)7 VarExpr (kalang.ast.VarExpr)7 Nullable (javax.annotation.Nullable)6 AssignExpr (kalang.ast.AssignExpr)6 LocalVarNode (kalang.ast.LocalVarNode)6 BlockStmt (kalang.ast.BlockStmt)5 VarDeclStmt (kalang.ast.VarDeclStmt)5 AmbiguousMethodException (kalang.AmbiguousMethodException)4 MethodNotFoundException (kalang.MethodNotFoundException)4 ConstExpr (kalang.ast.ConstExpr)4 MethodNode (kalang.ast.MethodNode)4