Search in sources :

Example 1 with IfStmt

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

the class AstBuilder method visitPostIfStmt.

@Override
public AstNode visitPostIfStmt(KalangParser.PostIfStmtContext ctx) {
    ExprNode leftExpr = visitExpression(ctx.expression(0));
    if (!(leftExpr instanceof AssignExpr)) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, "AssignExpr required", ctx);
    }
    AssignExpr assignExpr = (AssignExpr) leftExpr;
    AssignableExpr to = assignExpr.getTo();
    ExprNode from = assignExpr.getFrom();
    ExprNode cond = visitExpression(ctx.expression(1));
    Token op = ctx.op;
    if (op != null) {
        String opStr = op.getText();
        BinaryExpr be = createBinaryExpr(to, cond, opStr);
        cond = be;
    }
    AssignExpr as = new AssignExpr(to, from);
    IfStmt is = new IfStmt(cond);
    is.getTrueBody().statements.add(new ExprStmt(as));
    mapAst(is, ctx);
    return is;
}
Also used : ExprNode(kalang.ast.ExprNode) IfStmt(kalang.ast.IfStmt) ExprStmt(kalang.ast.ExprStmt) AssignableExpr(kalang.ast.AssignableExpr) BinaryExpr(kalang.ast.BinaryExpr) Token(org.antlr.v4.runtime.Token) AssignExpr(kalang.ast.AssignExpr)

Example 2 with IfStmt

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

the class AstBuilder method visitIfStat.

@Override
public AstNode visitIfStat(IfStatContext ctx) {
    ExprNode expr = visitExpression(ctx.expression());
    if (expr == null) {
        return null;
    }
    Type exprType = expr.getType();
    expr = BoxUtil.assign(expr, expr.getType(), Types.BOOLEAN_TYPE);
    if (expr == null) {
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, exprType + " cannot be converted to boolean", ctx.expression());
        return null;
    }
    BlockStmt trueBody = null;
    BlockStmt falseBody = null;
    VarTable<VarObject, Integer> trueAssigned, falseAssigned;
    this.nullState = trueAssigned = this.nullState.newStack();
    newOverrideTypeStack();
    onIf(expr, true);
    if (ctx.trueStmt != null) {
        trueBody = requireBlock(ctx.trueStmt);
    }
    popOverrideTypeStack();
    this.nullState = this.nullState.popStack();
    boolean trueReturned = this.returned;
    this.returned = false;
    this.nullState = falseAssigned = this.nullState.newStack();
    newOverrideTypeStack();
    onIf(expr, false);
    if (ctx.falseStmt != null) {
        falseBody = requireBlock(ctx.falseStmt);
    }
    popOverrideTypeStack();
    this.nullState = this.nullState.popStack();
    handleMultiBranchedAssign(trueAssigned.vars(), falseAssigned.vars());
    boolean falseReturned = this.returned;
    if (trueReturned)
        onIf(expr, false);
    if (falseReturned)
        onIf(expr, true);
    this.returned = falseReturned && trueReturned;
    IfStmt ifStmt = new IfStmt(expr, trueBody, falseBody);
    mapAst(ifStmt, ctx);
    return ifStmt;
}
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) IfStmt(kalang.ast.IfStmt) BlockStmt(kalang.ast.BlockStmt) VarObject(kalang.ast.VarObject)

Example 3 with IfStmt

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

the class AstBuilder method visitQuestionExpr.

@Override
public AstNode visitQuestionExpr(KalangParser.QuestionExprContext ctx) {
    List<Statement> stmts = new LinkedList<>();
    ExprNode conditionExpr = (ExprNode) visit(ctx.expression(0));
    ExprNode trueExpr = (ExprNode) visit(ctx.expression(1));
    ExprNode falseExpr = (ExprNode) visit(ctx.expression(2));
    Type trueType = trueExpr.getType();
    Type falseType = falseExpr.getType();
    Type type;
    if (trueType.equals(falseType)) {
        type = trueType;
    } else {
        type = TypeUtil.getCommonType(trueType, falseType);
    }
    LocalVarNode vo = this.declareTempLocalVar(type);
    VarDeclStmt vds = new VarDeclStmt(vo);
    stmts.add(vds);
    VarExpr ve = new VarExpr(vo);
    IfStmt is = new IfStmt(conditionExpr);
    is.getTrueBody().statements.add(new ExprStmt(new AssignExpr(ve, trueExpr)));
    is.getFalseBody().statements.add(new ExprStmt(new AssignExpr(ve, falseExpr)));
    stmts.add(is);
    MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
    mapAst(ve, ctx);
    return mse;
}
Also used : 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) IfStmt(kalang.ast.IfStmt) ExprStmt(kalang.ast.ExprStmt) Statement(kalang.ast.Statement) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) LocalVarNode(kalang.ast.LocalVarNode) LinkedList(java.util.LinkedList) AssignExpr(kalang.ast.AssignExpr)

Example 4 with IfStmt

use of kalang.ast.IfStmt 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)4 IfStmt (kalang.ast.IfStmt)4 AssignExpr (kalang.ast.AssignExpr)2 BlockStmt (kalang.ast.BlockStmt)2 ExprStmt (kalang.ast.ExprStmt)2 ArrayType (kalang.core.ArrayType)2 ClassType (kalang.core.ClassType)2 GenericType (kalang.core.GenericType)2 ObjectType (kalang.core.ObjectType)2 PrimitiveType (kalang.core.PrimitiveType)2 Type (kalang.core.Type)2 WildcardType (kalang.core.WildcardType)2 LinkedList (java.util.LinkedList)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 AssignableExpr (kalang.ast.AssignableExpr)1 BinaryExpr (kalang.ast.BinaryExpr)1 LocalVarNode (kalang.ast.LocalVarNode)1 MultiStmtExpr (kalang.ast.MultiStmtExpr)1 NewObjectExpr (kalang.ast.NewObjectExpr)1