Search in sources :

Example 1 with ArrayType

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

the class AstBuilder method visitInvokeExpr.

@Override
public AstNode visitInvokeExpr(InvokeExprContext ctx) {
    Object target = visit(ctx.target);
    if (target == null)
        return null;
    String mdName = ctx.Identifier().getText();
    String refKey = ctx.refKey.getText();
    if (refKey.equals(".")) {
        if (target instanceof ClassReference) {
            return getStaticInvokeExpr((ClassReference) target, mdName, ctx.params, ctx);
        } else if (target instanceof ExprNode) {
            return getObjectInvokeExpr((ExprNode) target, mdName, ctx.params, ctx);
        } else {
            throw Exceptions.unexceptedValue(target);
        }
    } else if (refKey.equals("->")) {
        ExprNode[] invokeArgs = new ExprNode[3];
        ExprNode[] params = new ExprNode[ctx.params.size()];
        if (target instanceof ClassReference) {
            invokeArgs[0] = new ConstExpr(null);
        } else if (target instanceof ExprNode) {
            invokeArgs[0] = ((ExprNode) target);
        }
        invokeArgs[1] = new ConstExpr(mdName);
        for (int i = 0; i < params.length; i++) {
            params[i] = visitExpression(ctx.params.get(i));
        }
        invokeArgs[2] = createInitializedArray(Types.getRootType(), params);
        ClassNode dispatcherAst = getAst("kalang.runtime.dynamic.MethodDispatcher");
        if (dispatcherAst == null) {
            throw Exceptions.unexceptedException("Runtime library is required!");
        }
        return getStaticInvokeExpr(new ClassReference(dispatcherAst), "invokeMethod", invokeArgs, ctx);
    } else if (refKey.equals("*.")) {
        if (!(target instanceof ExprNode)) {
            handleSyntaxError("expression required", ctx.expression);
            return null;
        }
        ExprNode targetExpr = (ExprNode) target;
        Type targetType = targetExpr.getType();
        if (!(targetType instanceof ArrayType)) {
            handleSyntaxError("array required", ctx.expression);
            return null;
        }
        List<Statement> stats = new LinkedList();
        LocalVarNode varArrLen = this.declareTempLocalVar(Types.INT_TYPE);
        LocalVarNode varCounter = this.declareTempLocalVar(Types.INT_TYPE);
        stats.add(new VarDeclStmt(Arrays.asList(varArrLen, varCounter)));
        VarExpr varArrLenExpr = new VarExpr(varArrLen);
        VarExpr varCounterExpr = new VarExpr(varCounter);
        stats.add(new ExprStmt(new AssignExpr(varArrLenExpr, new ArrayLengthExpr(targetExpr))));
        stats.add(new ExprStmt(new AssignExpr(varCounterExpr, new ConstExpr(0))));
        CompareExpr conditionExpr = new CompareExpr(varCounterExpr, varArrLenExpr, CompareExpr.OP_LT);
        ExprNode targetEleExpr = new ElementExpr(targetExpr, varCounterExpr);
        ExprNode invokeExpr = getObjectInvokeExpr(targetEleExpr, mdName, ctx.params, ctx);
        if (invokeExpr == null)
            return null;
        LocalVarNode varRet = this.declareTempLocalVar(Types.getArrayType(invokeExpr.getType()));
        VarExpr varRetExpr = new VarExpr(varRet);
        stats.add(new VarDeclStmt(varRet));
        stats.add(new ExprStmt(new AssignExpr(varRetExpr, new NewArrayExpr(invokeExpr.getType(), varArrLenExpr))));
        BlockStmt loopBody = this.newBlock();
        loopBody.statements.add(new ExprStmt(new AssignExpr(new ElementExpr(varRetExpr, varCounterExpr), invokeExpr)));
        popBlock();
        BlockStmt updateBs = newBlock();
        updateBs.statements.add(new ExprStmt(new AssignExpr(varCounterExpr, new MathExpr(varCounterExpr, new ConstExpr(1), MathExpr.OP_ADD))));
        this.popBlock();
        LoopStmt loopStmt = new LoopStmt(conditionExpr, null, loopBody, updateBs);
        stats.add(loopStmt);
        return new MultiStmtExpr(stats, varRetExpr);
    } else {
        throw Exceptions.unexceptedException(refKey);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) ConstExpr(kalang.ast.ConstExpr) LoopStmt(kalang.ast.LoopStmt) Statement(kalang.ast.Statement) BlockStmt(kalang.ast.BlockStmt) ArrayLengthExpr(kalang.ast.ArrayLengthExpr) LinkedList(java.util.LinkedList) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) 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) ExprStmt(kalang.ast.ExprStmt) CompareExpr(kalang.ast.CompareExpr) NewArrayExpr(kalang.ast.NewArrayExpr) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) VarObject(kalang.ast.VarObject) ClassReference(kalang.ast.ClassReference) LocalVarNode(kalang.ast.LocalVarNode) MathExpr(kalang.ast.MathExpr) ElementExpr(kalang.ast.ElementExpr)

Example 2 with ArrayType

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

the class AstBuilder method getObjectFieldExpr.

@Nullable
protected ExprNode getObjectFieldExpr(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)) {
        return null;
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
        } catch (FieldNotFoundException ex) {
            return null;
        }
    }
    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) Nullable(javax.annotation.Nullable)

Example 3 with ArrayType

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

the class ClassTypeTest method test.

@Test
public void test() throws AstNotFoundException {
    AstLoader astLoader = new AstLoader();
    ClassNode listClass = astLoader.loadAst("java.util.List");
    ClassNode arrayListClass = astLoader.loadAst("java.util.ArrayList");
    Type[] paramTypes = new Type[] { Types.getRootType() };
    ObjectType listType = Types.getClassType(listClass, paramTypes);
    ArrayType listArrayType = Types.getArrayType(listType);
    ObjectType arrayListType = Types.getClassType(arrayListClass, paramTypes);
    ArrayType arrayListArrayType = Types.getArrayType(arrayListType);
    assertTrue(arrayListType.isSubTypeOf(listType));
    assertTrue(listArrayType.isAssignableFrom(arrayListArrayType));
    String mdDescriptors = Arrays.toString(listType.getMethodDescriptors(listClass, false, true));
    assertEquals("[public abstract boolean contains(java.lang.Object arg0), public abstract boolean addAll(int arg0,java.util.Collection<? extends java.lang.Object> arg1), public void sort(java.util.Comparator<? super java.lang.Object> arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(int arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(), public abstract boolean retainAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract java.lang.Object[] toArray(), public abstract java.lang.Object remove(int arg0), public abstract boolean addAll(java.util.Collection<? extends java.lang.Object> arg0), public boolean removeIf(java.util.function.Predicate<? super java.lang.Object> arg0), public abstract int hashCode(), public abstract boolean removeAll(java.util.Collection<? extends java.lang.Object> arg0), public java.util.stream.Stream<java.lang.Object> parallelStream(), public abstract int lastIndexOf(java.lang.Object arg0), public void replaceAll(java.util.function.UnaryOperator<java.lang.Object> arg0), public abstract void clear(), public abstract boolean containsAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract int indexOf(java.lang.Object arg0), public abstract boolean add(java.lang.Object arg0), public abstract boolean isEmpty(), public abstract java.util.List<java.lang.Object> subList(int arg0,int arg1), public abstract java.lang.Object get(int arg0), public abstract java.lang.Object set(int arg0,java.lang.Object arg1), public abstract void add(int arg0,java.lang.Object arg1), public abstract java.util.Iterator<java.lang.Object> iterator(), public java.util.Spliterator<java.lang.Object> spliterator(), public void forEach(java.util.function.Consumer<? super java.lang.Object> arg0), public abstract java.lang.Object[] toArray(java.lang.Object[] arg0), public abstract boolean remove(java.lang.Object arg0), public abstract boolean equals(java.lang.Object arg0), public java.util.stream.Stream<java.lang.Object> stream(), public abstract int size()]", mdDescriptors);
// assertTrue(listType.isSubTypeOf(Types.getRootType()));
}
Also used : ArrayType(kalang.core.ArrayType) AstLoader(kalang.compiler.AstLoader) ClassNode(kalang.ast.ClassNode) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) Type(kalang.core.Type) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) Test(org.junit.Test)

Example 4 with ArrayType

use of kalang.core.ArrayType 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 5 with ArrayType

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

Aggregations

ArrayType (kalang.core.ArrayType)6 ClassType (kalang.core.ClassType)6 ObjectType (kalang.core.ObjectType)6 Type (kalang.core.Type)6 GenericType (kalang.core.GenericType)5 PrimitiveType (kalang.core.PrimitiveType)5 WildcardType (kalang.core.WildcardType)5 ExprNode (kalang.ast.ExprNode)4 ArrayLengthExpr (kalang.ast.ArrayLengthExpr)3 Nullable (javax.annotation.Nullable)2 FieldNotFoundException (kalang.FieldNotFoundException)2 AssignExpr (kalang.ast.AssignExpr)2 BlockStmt (kalang.ast.BlockStmt)2 ClassNode (kalang.ast.ClassNode)2 CompareExpr (kalang.ast.CompareExpr)2 ConstExpr (kalang.ast.ConstExpr)2 ElementExpr (kalang.ast.ElementExpr)2 ExprStmt (kalang.ast.ExprStmt)2 LocalVarNode (kalang.ast.LocalVarNode)2 LoopStmt (kalang.ast.LoopStmt)2