Search in sources :

Example 1 with InvocationExpr

use of kalang.ast.InvocationExpr in project kalang by kasonyang.

the class AstBuilder method visitMapExpr.

@Override
public MultiStmtExpr visitMapExpr(KalangParser.MapExprContext ctx) {
    Type keyType = ctx.keyType != null ? requireClassType(ctx.keyType) : Types.getRootType();
    Type valueType = ctx.valueType != null ? requireClassType(ctx.valueType) : Types.getRootType();
    if (keyType == null || valueType == null)
        return null;
    LocalVarNode vo = declareTempLocalVar(Types.getClassType(Types.getMapImplClassType().getClassNode(), new Type[] { keyType, valueType }));
    VarDeclStmt vds = new VarDeclStmt(vo);
    NewObjectExpr newExpr;
    try {
        newExpr = new NewObjectExpr(Types.getMapImplClassType());
    } catch (MethodNotFoundException | AmbiguousMethodException ex) {
        throw Exceptions.unexceptedException(ex);
    }
    List<Statement> stmts = new LinkedList<>();
    stmts.add(vds);
    stmts.add(new ExprStmt(new AssignExpr(new VarExpr(vo), newExpr)));
    VarExpr ve = new VarExpr(vo);
    List<TerminalNode> ids = ctx.Identifier();
    for (int i = 0; i < ids.size(); i++) {
        ExpressionContext e = ctx.expression(i);
        ExprNode v = (ExprNode) visit(e);
        ConstExpr k = new ConstExpr(ctx.Identifier(i).getText());
        ExprNode[] args = new ExprNode[] { k, v };
        InvocationExpr iv;
        try {
            iv = ObjectInvokeExpr.create(ve, "put", args);
        } catch (MethodNotFoundException | AmbiguousMethodException ex) {
            throw Exceptions.unexceptedException(ex);
        }
        ExprStmt es = new ExprStmt(iv);
        stmts.add(es);
    }
    MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
    mapAst(mse, ctx);
    return mse;
}
Also used : ConstExpr(kalang.ast.ConstExpr) Statement(kalang.ast.Statement) NewObjectExpr(kalang.ast.NewObjectExpr) LinkedList(java.util.LinkedList) AssignExpr(kalang.ast.AssignExpr) 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) ExprStmt(kalang.ast.ExprStmt) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) LocalVarNode(kalang.ast.LocalVarNode) MethodNotFoundException(kalang.MethodNotFoundException) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 2 with InvocationExpr

use of kalang.ast.InvocationExpr 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 3 with InvocationExpr

use of kalang.ast.InvocationExpr in project kalang by kasonyang.

the class AstUtil method isConstructorCallStatement.

public static boolean isConstructorCallStatement(Statement stmt) {
    try {
        ExprStmt exprStmt = (ExprStmt) stmt;
        InvocationExpr invExpr = (InvocationExpr) exprStmt.getExpr();
        ExecutableDescriptor method = invExpr.getMethod();
        return method.getName().equals("<init>") && !Modifier.isStatic(method.getModifier());
    } catch (ClassCastException ex) {
        return false;
    }
}
Also used : ExprStmt(kalang.ast.ExprStmt) InvocationExpr(kalang.ast.InvocationExpr) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Aggregations

InvocationExpr (kalang.ast.InvocationExpr)3 AmbiguousMethodException (kalang.AmbiguousMethodException)2 MethodNotFoundException (kalang.MethodNotFoundException)2 ExprNode (kalang.ast.ExprNode)2 ExprStmt (kalang.ast.ExprStmt)2 ObjectType (kalang.core.ObjectType)2 LinkedList (java.util.LinkedList)1 ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)1 AssignExpr (kalang.ast.AssignExpr)1 ClassReference (kalang.ast.ClassReference)1 ConstExpr (kalang.ast.ConstExpr)1 LocalVarNode (kalang.ast.LocalVarNode)1 MultiStmtExpr (kalang.ast.MultiStmtExpr)1 NewObjectExpr (kalang.ast.NewObjectExpr)1 Statement (kalang.ast.Statement)1 UnknownInvocationExpr (kalang.ast.UnknownInvocationExpr)1 VarDeclStmt (kalang.ast.VarDeclStmt)1 VarExpr (kalang.ast.VarExpr)1 ArrayType (kalang.core.ArrayType)1 ClassType (kalang.core.ClassType)1