Search in sources :

Example 6 with AmbiguousMethodException

use of kalang.AmbiguousMethodException in project kalang by kasonyang.

the class BoxUtil method castPrimitive2Object.

private static ExprNode castPrimitive2Object(ExprNode expr, PrimitiveType fromType) {
    ObjectType classType = Types.getClassType(fromType);
    if (classType == null) {
        throw new UnknownError("unknown primitive type:" + fromType);
    }
    InvocationExpr inv;
    try {
        inv = StaticInvokeExpr.create(new ClassReference(classType.getClassNode()), "valueOf", new ExprNode[] { expr });
    } catch (MethodNotFoundException | AmbiguousMethodException ex) {
        throw new RuntimeException(ex);
    }
    return inv;
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) ClassReference(kalang.ast.ClassReference) MethodNotFoundException(kalang.MethodNotFoundException) InvocationExpr(kalang.ast.InvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 7 with AmbiguousMethodException

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

Example 8 with AmbiguousMethodException

use of kalang.AmbiguousMethodException in project kalang by kasonyang.

the class AstBuilder method getStaticInvokeExpr.

private ExprNode getStaticInvokeExpr(ClassReference clazz, String methodName, ExprNode[] argumentsCtx, ParserRuleContext ctx) {
    ExprNode[] args = argumentsCtx;
    ExprNode expr;
    try {
        expr = StaticInvokeExpr.create(clazz, methodName, args);
    } catch (MethodNotFoundException ex) {
        methodNotFound(ctx.start, className, methodName, args);
        expr = new UnknownInvocationExpr(clazz, methodName, args);
    } catch (AmbiguousMethodException ex) {
        methodIsAmbiguous(ctx.start, ex);
        return null;
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : ExprNode(kalang.ast.ExprNode) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 9 with AmbiguousMethodException

use of kalang.AmbiguousMethodException in project kalang by kasonyang.

the class AstBuilder method visitMemberInvocationExpr.

@Override
public ExprNode visitMemberInvocationExpr(MemberInvocationExprContext ctx) {
    String methodName;
    ExprNode target;
    ObjectType clazz;
    if (ctx.key != null) {
        methodName = ctx.key.getText();
    } else {
        methodName = ctx.Identifier().getText();
    }
    if (methodName.equals("this")) {
        methodName = "<init>";
        target = new ThisExpr(this.getThisType());
        clazz = this.getThisType();
    } else if (methodName.equals("super")) {
        methodName = "<init>";
        target = new SuperExpr(thisClazz);
        clazz = thisClazz.getSuperType();
    } else {
        target = new ThisExpr(this.getThisType());
        clazz = this.getThisType();
    }
    List<Object> argsList = visitAll(ctx.params);
    if (argsList.contains(null))
        return null;
    ExprNode[] args = argsList.toArray(new ExprNode[argsList.size()]);
    ExprNode ie;
    if (methodName.equals("<init>")) {
        if (clazz == null)
            throw Exceptions.unexceptedValue(clazz);
        try {
            InvocationExpr.MethodSelection apply = InvocationExpr.applyMethod(clazz, methodName, args, clazz.getConstructorDescriptors(thisClazz));
            ie = new ObjectInvokeExpr(target, apply.selectedMethod, apply.appliedArguments);
        } catch (MethodNotFoundException | AmbiguousMethodException ex) {
            this.methodNotFound(ctx.start, clazz.getName(), methodName, args);
            return null;
        }
    } else {
        ie = getImplicitInvokeExpr(methodName, args, ctx);
    }
    return ie;
}
Also used : SuperExpr(kalang.ast.SuperExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) VarObject(kalang.ast.VarObject) MethodNotFoundException(kalang.MethodNotFoundException) ThisExpr(kalang.ast.ThisExpr) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 10 with AmbiguousMethodException

use of kalang.AmbiguousMethodException in project kalang by kasonyang.

the class AstBuilder method visitNewExpr.

@Override
public AstNode visitNewExpr(NewExprContext ctx) {
    ObjectType clsType = parseClassType(ctx.classType());
    if (clsType == null)
        return null;
    ExprNode[] params = visitAll(ctx.params).toArray(new ExprNode[0]);
    List<ExprNode> paramList = new LinkedList(Arrays.asList(params));
    NewObjectExpr newExpr;
    try {
        if (this.isNonStaticInnerClass(clsType.getClassNode())) {
            paramList.add(0, new ThisExpr(this.getThisType()));
        }
        params = paramList.toArray(new ExprNode[paramList.size()]);
        newExpr = new NewObjectExpr(clsType, params);
        mapAst(newExpr, ctx);
        return newExpr;
    } catch (MethodNotFoundException ex) {
        methodNotFound(ctx.classType().rawClass, clsType.getName(), "<init>", params);
        return null;
    } catch (AmbiguousMethodException ex) {
        methodIsAmbiguous(ctx.classType().rawClass, ex);
        return null;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) NewObjectExpr(kalang.ast.NewObjectExpr) MethodNotFoundException(kalang.MethodNotFoundException) LinkedList(java.util.LinkedList) ThisExpr(kalang.ast.ThisExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Aggregations

AmbiguousMethodException (kalang.AmbiguousMethodException)12 MethodNotFoundException (kalang.MethodNotFoundException)12 ExprNode (kalang.ast.ExprNode)9 ObjectType (kalang.core.ObjectType)8 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)5 UnknownInvocationExpr (kalang.ast.UnknownInvocationExpr)5 BlockStmt (kalang.ast.BlockStmt)4 InvocationExpr (kalang.ast.InvocationExpr)4 NewObjectExpr (kalang.ast.NewObjectExpr)4 ArrayType (kalang.core.ArrayType)4 ClassType (kalang.core.ClassType)4 GenericType (kalang.core.GenericType)4 Type (kalang.core.Type)4 ExprStmt (kalang.ast.ExprStmt)3 PrimitiveType (kalang.core.PrimitiveType)3 WildcardType (kalang.core.WildcardType)3 LinkedList (java.util.LinkedList)2 Nullable (javax.annotation.Nullable)2 AssignExpr (kalang.ast.AssignExpr)2 CastExpr (kalang.ast.CastExpr)2