Search in sources :

Example 31 with Type

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

the class AstBuilder method getVarObjectType.

private Type getVarObjectType(VarObject p) {
    Type type = this.overrideTypes.get(p);
    if (type == null)
        type = p.getType();
    // TODO handle other object type
    if (type instanceof ClassType) {
        Integer ns = nullState.get(p);
        NullableKind nullable;
        if (ns == null) {
            nullable = ((ObjectType) type).getNullable();
        } else if (ns == NULLSTATE_MUST_NONNULL) {
            nullable = NullableKind.NONNULL;
        } else if (ns == NULLSTATE_UNKNOWN) {
            nullable = NullableKind.UNKNOWN;
        } else if (ns == NULLSTATE_MUST_NULL || ns == NULLSTATE_NULLABLE) {
            nullable = NullableKind.NULLABLE;
        } else {
            throw Exceptions.unexceptedValue(ns);
        }
        return Types.getClassType((ClassType) type, nullable);
    } else {
        return type;
    }
}
Also used : 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) NullableKind(kalang.core.NullableKind) ClassType(kalang.core.ClassType)

Example 32 with Type

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

the class AstBuilder method getObjectFieldLikeExpr.

protected ExprNode getObjectFieldLikeExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        AstBuilder.this.handleSyntaxError("unsupported type", rule == null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType) && fieldName.equals("length")) {
        ret = new ArrayLengthExpr(expr);
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, thisClazz);
        } catch (FieldNotFoundException ex) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "field not found:" + fieldName, rule);
            ret = new UnknownFieldExpr(expr, exprType.getClassNode(), fieldName);
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) 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) FieldNotFoundException(kalang.FieldNotFoundException) UnknownFieldExpr(kalang.ast.UnknownFieldExpr) ArrayLengthExpr(kalang.ast.ArrayLengthExpr)

Example 33 with Type

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

the class AstBuilder method parseClassType.

@Nullable
protected ObjectType parseClassType(KalangParser.ClassTypeContext ctx) {
    NullableKind nullable = ctx.nullable == null ? NullableKind.NONNULL : NullableKind.NULLABLE;
    Token rawTypeToken = ctx.rawClass;
    List<String> classNameParts = new LinkedList();
    for (Token p : ctx.paths) {
        classNameParts.add(p.getText());
    }
    if (ctx.innerClass != null) {
        classNameParts.add(rawTypeToken.getText() + "$" + ctx.innerClass.getText());
    } else {
        classNameParts.add(rawTypeToken.getText());
    }
    String rawType = String.join(".", classNameParts);
    for (GenericType gt : thisClazz.getGenericTypes()) {
        if (rawType.equals(gt.getName()))
            return gt;
    }
    ObjectType clazzType = requireClassType(rawType, rawTypeToken);
    if (clazzType == null)
        return null;
    ClassNode clazzNode = clazzType.getClassNode();
    GenericType[] clzDeclaredGenericTypes = clazzNode.getGenericTypes();
    List<KalangParser.ParameterizedElementTypeContext> parameterTypes = ctx.parameterTypes;
    if (parameterTypes != null && !parameterTypes.isEmpty()) {
        Type[] typeArguments = new Type[parameterTypes.size()];
        if (parameterTypes != null && parameterTypes.size() > 0) {
            if (clzDeclaredGenericTypes.length != parameterTypes.size()) {
                diagnosisReporter.report(Diagnosis.Kind.ERROR, "wrong number of type arguments", ctx);
                return null;
            }
            for (int i = 0; i < typeArguments.length; i++) {
                typeArguments[i] = parseParameterizedElementType(parameterTypes.get(i));
                // TODO should return null?
                if (typeArguments[i] == null)
                    return null;
            }
        }
        return Types.getClassType(clazzType.getClassNode(), typeArguments, nullable);
    } else {
        return Types.getClassType(clazzType.getClassNode(), nullable);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) GenericType(kalang.core.GenericType) NullableKind(kalang.core.NullableKind) Token(org.antlr.v4.runtime.Token) LinkedList(java.util.LinkedList) ObjectType(kalang.core.ObjectType) 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) Nullable(javax.annotation.Nullable)

Example 34 with Type

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

the class AstBuilder method visitQuestionExpr.

@Override
public AstNode visitQuestionExpr(KalangParser.QuestionExprContext ctx) {
    List<Statement> stmts = new LinkedList<>();
    ExprNode conditionExpr = (ExprNode) visit(ctx.expression(0));
    ExprNode trueExpr = (ExprNode) visit(ctx.expression(1));
    ExprNode falseExpr = (ExprNode) visit(ctx.expression(2));
    Type trueType = trueExpr.getType();
    Type falseType = falseExpr.getType();
    Type type;
    if (trueType.equals(falseType)) {
        type = trueType;
    } else {
        type = TypeUtil.getCommonType(trueType, falseType);
    }
    LocalVarNode vo = this.declareTempLocalVar(type);
    VarDeclStmt vds = new VarDeclStmt(vo);
    stmts.add(vds);
    VarExpr ve = new VarExpr(vo);
    IfStmt is = new IfStmt(conditionExpr);
    is.getTrueBody().statements.add(new ExprStmt(new AssignExpr(ve, trueExpr)));
    is.getFalseBody().statements.add(new ExprStmt(new AssignExpr(ve, falseExpr)));
    stmts.add(is);
    MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
    mapAst(ve, ctx);
    return mse;
}
Also used : ExprNode(kalang.ast.ExprNode) MultiStmtExpr(kalang.ast.MultiStmtExpr) 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) IfStmt(kalang.ast.IfStmt) ExprStmt(kalang.ast.ExprStmt) Statement(kalang.ast.Statement) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) LocalVarNode(kalang.ast.LocalVarNode) LinkedList(java.util.LinkedList) AssignExpr(kalang.ast.AssignExpr)

Example 35 with Type

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

the class AstBuilder method visitForEachStat.

@Override
public Object visitForEachStat(KalangParser.ForEachStatContext ctx) {
    BlockStmt block = newBlock();
    ExprNode expr = this.visitExpression(ctx.expression());
    Type exprType = expr.getType();
    List<TerminalNode> idsCtx = ctx.Identifier();
    VarExpr indexVarExpr = null;
    TerminalNode varId;
    if (idsCtx.size() == 1) {
        varId = idsCtx.get(0);
    } else {
        TerminalNode indexId = idsCtx.get(0);
        LocalVarNode indexVar = this.declareLocalVar(indexId.getText(), Types.INT_TYPE, Modifier.FINAL, ctx);
        if (indexVar == null)
            return null;
        block.statements.add(new VarDeclStmt(indexVar));
        indexVarExpr = new VarExpr(indexVar);
        varId = idsCtx.get(1);
    }
    LoopStmt loopStmt;
    if (exprType instanceof ArrayType) {
        LocalVarNode localVarNode = this.declareLocalVar(varId.getText(), ((ArrayType) exprType).getComponentType(), Modifier.FINAL, ctx);
        if (localVarNode == null)
            return null;
        VarExpr localVariable = new VarExpr(localVarNode);
        block.statements.add(new VarDeclStmt(localVarNode));
        LocalVarNode lenVar = this.declareTempLocalVar(Types.INT_TYPE);
        LocalVarNode counterVar = this.declareTempLocalVar(Types.INT_TYPE);
        // var len
        block.statements.add(new VarDeclStmt(lenVar));
        // var i
        block.statements.add(new VarDeclStmt(counterVar));
        VarExpr counterVarExpr = new VarExpr(counterVar);
        VarExpr lenVarExpr = new VarExpr(lenVar);
        block.statements.add(new ExprStmt(new AssignExpr(lenVarExpr, new ArrayLengthExpr(expr))));
        // l = array.length
        block.statements.add(new ExprStmt(new AssignExpr(counterVarExpr, new ConstExpr(0))));
        // i=0
        ExprNode cnd = new CompareExpr(counterVarExpr, lenVarExpr, CompareExpr.OP_LT);
        BlockStmt loopBody = this.newBlock();
        loopBody.statements.add(new ExprStmt(new AssignExpr(localVariable, new ElementExpr(expr, counterVarExpr))));
        if (indexVarExpr != null) {
            loopBody.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, counterVarExpr)));
        }
        loopBody.statements.add(visitStat(ctx.stat()));
        popBlock();
        BlockStmt updateBs = newBlock();
        // increment counter
        updateBs.statements.add(new ExprStmt(new AssignExpr(counterVarExpr, new MathExpr(counterVarExpr, new ConstExpr(1), MathExpr.OP_ADD))));
        popBlock();
        loopStmt = new LoopStmt(cnd, null, loopBody, updateBs);
    } else {
        ObjectType iterType = Types.getIterableClassType();
        if (iterType.isAssignableFrom(exprType)) {
            ObjectInvokeExpr getIterableExpr;
            try {
                getIterableExpr = ObjectInvokeExpr.create(expr, "iterator", null);
            } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                throw Exceptions.unexceptedException(ex);
            }
            LocalVarNode iterableVarNode = this.declareTempLocalVar(getIterableExpr.getType());
            block.statements.add(new VarDeclStmt(iterableVarNode));
            VarExpr iterableVarExpr = new VarExpr(iterableVarNode);
            block.statements.add(new ExprStmt(new AssignExpr(iterableVarExpr, getIterableExpr)));
            // set index = 0
            if (indexVarExpr != null) {
                block.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, new ConstExpr(0))));
            }
            ObjectInvokeExpr cnd;
            try {
                cnd = ObjectInvokeExpr.create(iterableVarExpr, "hasNext", null);
            } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                throw Exceptions.unexceptedException(ex);
            }
            BlockStmt loopBody = this.newBlock();
            ObjectInvokeExpr nextInvokeExpr;
            try {
                nextInvokeExpr = ObjectInvokeExpr.create(iterableVarExpr, "next", null);
            } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                throw Exceptions.unexceptedException(ex);
            }
            LocalVarNode localVarNode = this.declareLocalVar(varId.getText(), nextInvokeExpr.getType(), Modifier.FINAL, ctx);
            if (localVarNode == null)
                return null;
            VarExpr localVariable = new VarExpr(localVarNode);
            loopBody.statements.add(new VarDeclStmt(localVarNode));
            loopBody.statements.add(new ExprStmt(new AssignExpr(localVariable, new CastExpr(localVariable.getType(), nextInvokeExpr))));
            loopBody.statements.add(visitStat(ctx.stat()));
            popBlock();
            BlockStmt updateBs = newBlock();
            if (indexVarExpr != null) {
                // do index++
                updateBs.statements.add(new ExprStmt(new AssignExpr(indexVarExpr, new MathExpr(indexVarExpr, new ConstExpr(1), BinaryExpr.OP_ADD))));
            }
            popBlock();
            loopStmt = new LoopStmt(cnd, null, loopBody, updateBs);
        } else {
            this.handleSyntaxError("require array type or iterable type", ctx.expression());
            loopStmt = null;
        }
    }
    popBlock();
    if (loopStmt != null)
        block.statements.add(loopStmt);
    return block;
}
Also used : ConstExpr(kalang.ast.ConstExpr) LoopStmt(kalang.ast.LoopStmt) BlockStmt(kalang.ast.BlockStmt) ArrayLengthExpr(kalang.ast.ArrayLengthExpr) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) 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) ExprStmt(kalang.ast.ExprStmt) CompareExpr(kalang.ast.CompareExpr) VarDeclStmt(kalang.ast.VarDeclStmt) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr) CastExpr(kalang.ast.CastExpr) VarExpr(kalang.ast.VarExpr) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) LocalVarNode(kalang.ast.LocalVarNode) MethodNotFoundException(kalang.MethodNotFoundException) MathExpr(kalang.ast.MathExpr) ElementExpr(kalang.ast.ElementExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

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