Search in sources :

Example 41 with ExprNode

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

the class AstBuilder method visitExpressions.

@Override
public List<Statement> visitExpressions(ExpressionsContext ctx) {
    List<Statement> list = new LinkedList();
    for (ExpressionContext e : ctx.expression()) {
        ExprNode expr = visitExpression(e);
        list.add(new ExprStmt(expr));
    }
    return list;
}
Also used : ExprNode(kalang.ast.ExprNode) ExprStmt(kalang.ast.ExprStmt) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) Statement(kalang.ast.Statement) LinkedList(java.util.LinkedList)

Example 42 with ExprNode

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

Example 43 with ExprNode

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

the class AstBuilder method visitCastExpr.

@Override
public AstNode visitCastExpr(CastExprContext ctx) {
    ExprNode castExpr;
    ExprNode expr = visitExpression(ctx.expression());
    Type toType = parseType(ctx.type());
    Type fromType = expr.getType();
    if (fromType instanceof PrimitiveType) {
        if (toType instanceof PrimitiveType) {
            castExpr = new PrimitiveCastExpr((PrimitiveType) fromType, (PrimitiveType) toType, expr);
        } else {
            this.handleSyntaxError("unable to cast primitive type to class type", ctx);
            return null;
        }
    } else {
        if (toType instanceof PrimitiveType) {
            this.handleSyntaxError("unable to cast class type to primitive type", ctx);
            return null;
        } else {
            castExpr = new CastExpr(toType, expr);
        }
    }
    mapAst(castExpr, ctx);
    return castExpr;
}
Also used : ExprNode(kalang.ast.ExprNode) 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) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr) CastExpr(kalang.ast.CastExpr) PrimitiveType(kalang.core.PrimitiveType) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr)

Example 44 with ExprNode

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

the class AstBuilder method visitExpression.

protected ExprNode visitExpression(ExpressionContext expression) {
    Object node = visit(expression);
    if (node instanceof ExprNode) {
        return (ExprNode) node;
    } else {
        ExprNode expr;
        if (node instanceof AstNode) {
            expr = new ErrorousExpr((AstNode) node);
        } else {
            expr = new ErrorousExpr();
        }
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "not an expression", expression);
        return expr;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) VarObject(kalang.ast.VarObject) ErrorousExpr(kalang.ast.ErrorousExpr) AstNode(kalang.ast.AstNode)

Example 45 with ExprNode

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

the class AstBuilder method visitAssertStmt.

@Override
public Object visitAssertStmt(KalangParser.AssertStmtContext ctx) {
    ExprNode failExpr = visitExpression(ctx.testCondition);
    if (failExpr == null)
        return null;
    failExpr = BoxUtil.assign(failExpr, failExpr.getType(), Types.BOOLEAN_TYPE);
    if (failExpr == null) {
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "boolean type expected", ctx.testCondition);
        return null;
    }
    failExpr = new UnaryExpr(failExpr, UnaryExpr.OPERATION_LOGIC_NOT);
    ExprNode failMsgExpr = null;
    if (ctx.failMessage != null) {
        failMsgExpr = visitExpression(ctx.failMessage);
        if (failMsgExpr == null)
            return null;
        if (Types.VOID_TYPE.equals(failMsgExpr.getType())) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "non-void type expected", ctx.failMessage);
            return null;
        }
    }
    BlockStmt body = this.newBlock();
    NewObjectExpr newErrorExpr;
    try {
        newErrorExpr = new NewObjectExpr(Types.requireAssertionErrorClassType(), failMsgExpr != null ? new ExprNode[] { failMsgExpr } : new ExprNode[0]);
    } catch (MethodNotFoundException | AmbiguousMethodException ex) {
        throw Exceptions.unexceptedException(ex);
    }
    body.statements.add(new ThrowStmt(newErrorExpr));
    popBlock();
    return new IfStmt(failExpr, body, null);
}
Also used : ExprNode(kalang.ast.ExprNode) IfStmt(kalang.ast.IfStmt) BlockStmt(kalang.ast.BlockStmt) NewObjectExpr(kalang.ast.NewObjectExpr) UnaryExpr(kalang.ast.UnaryExpr) MethodNotFoundException(kalang.MethodNotFoundException) ThrowStmt(kalang.ast.ThrowStmt) AmbiguousMethodException(kalang.AmbiguousMethodException)

Aggregations

ExprNode (kalang.ast.ExprNode)47 ObjectType (kalang.core.ObjectType)23 ArrayType (kalang.core.ArrayType)17 GenericType (kalang.core.GenericType)17 Type (kalang.core.Type)17 ClassType (kalang.core.ClassType)16 PrimitiveType (kalang.core.PrimitiveType)15 WildcardType (kalang.core.WildcardType)14 ExprStmt (kalang.ast.ExprStmt)13 AssignExpr (kalang.ast.AssignExpr)12 BlockStmt (kalang.ast.BlockStmt)10 AmbiguousMethodException (kalang.AmbiguousMethodException)9 MethodNotFoundException (kalang.MethodNotFoundException)9 ClassReference (kalang.ast.ClassReference)9 ThisExpr (kalang.ast.ThisExpr)8 ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)7 Statement (kalang.ast.Statement)7 LocalVarNode (kalang.ast.LocalVarNode)6 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)6 VarExpr (kalang.ast.VarExpr)6