Search in sources :

Example 1 with ThrowStmt

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

the class AstBuilder method visitThrowStat.

@Override
public ThrowStmt visitThrowStat(KalangParser.ThrowStatContext ctx) {
    ThrowStmt ts = new ThrowStmt(visitExpression(ctx.expression()));
    mapAst(ts, ctx);
    this.returned = true;
    return ts;
}
Also used : ThrowStmt(kalang.ast.ThrowStmt)

Example 2 with ThrowStmt

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

ThrowStmt (kalang.ast.ThrowStmt)2 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 BlockStmt (kalang.ast.BlockStmt)1 ExprNode (kalang.ast.ExprNode)1 IfStmt (kalang.ast.IfStmt)1 NewObjectExpr (kalang.ast.NewObjectExpr)1 UnaryExpr (kalang.ast.UnaryExpr)1