Search in sources :

Example 1 with UnaryExpr

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

the class Ast2Class method ifExpr.

private void ifExpr(boolean jumpOnTrue, ExprNode condition, Label label) {
    if (condition instanceof LogicExpr) {
        LogicExpr be = (LogicExpr) condition;
        ExprNode e1 = be.getExpr1();
        ExprNode e2 = be.getExpr2();
        String op = be.getOperation();
        switch(op) {
            case "&&":
                if (jumpOnTrue) {
                    Label stopLabel = new Label();
                    ifExpr(false, e1, stopLabel);
                    ifExpr(false, e2, stopLabel);
                    md.visitJumpInsn(GOTO, label);
                    md.visitLabel(stopLabel);
                } else {
                    ifExpr(false, e1, label);
                    ifExpr(false, e2, label);
                }
                break;
            case "||":
                if (jumpOnTrue) {
                    ifExpr(true, e1, label);
                    ifExpr(true, e2, label);
                } else {
                    Label stopLabel = new Label();
                    ifExpr(true, e1, stopLabel);
                    ifExpr(true, e2, stopLabel);
                    md.visitJumpInsn(GOTO, label);
                    md.visitLabel(stopLabel);
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported operation:" + op);
        }
    } else if (condition instanceof CompareExpr) {
        ifCompare(jumpOnTrue, ((CompareExpr) condition).getExpr1(), ((CompareExpr) condition).getExpr2(), ((CompareExpr) condition).getOperation(), label);
    } else if (condition instanceof UnaryExpr && ((UnaryExpr) condition).getOperation().equals("!")) {
        ifExpr(!jumpOnTrue, ((UnaryExpr) condition).getExpr(), label);
    } else {
        visit(condition);
        md.visitJumpInsn(jumpOnTrue ? IFNE : IFEQ, label);
    }
}
Also used : ExprNode(kalang.ast.ExprNode) LogicExpr(kalang.ast.LogicExpr) CompareExpr(kalang.ast.CompareExpr) Label(org.objectweb.asm.Label) UnaryExpr(kalang.ast.UnaryExpr)

Example 2 with UnaryExpr

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

the class AstBuilder method onIf.

protected void onIf(ExprNode expr, boolean onTrue) {
    if (expr instanceof InstanceOfExpr && onTrue) {
        InstanceOfExpr ie = (InstanceOfExpr) expr;
        changeTypeTemporarilyIfCould(ie.getExpr(), Types.getClassType(ie.getTarget().getReferencedClassNode()));
    }
    if (expr instanceof CompareExpr) {
        CompareExpr ce = (CompareExpr) expr;
        ExprNode e1 = ce.getExpr1();
        ExprNode e2 = ce.getExpr2();
        boolean isEQ = ce.getOperation().equals(CompareExpr.OP_EQ);
        if (e1.getType().equals(Types.NULL_TYPE)) {
            onNull(e2, onTrue, isEQ);
        } else if (e2.getType().equals(Types.NULL_TYPE)) {
            onNull(e1, onTrue, isEQ);
        }
    }
    if (expr instanceof UnaryExpr) {
        onIf(((UnaryExpr) expr).getExpr(), !onTrue);
    }
    if (expr instanceof LogicExpr) {
        LogicExpr le = (LogicExpr) expr;
        if (le.getOperation().equals(LogicExpr.OP_LOGIC_AND)) {
            if (onTrue) {
                onIf(le.getExpr1(), true);
                onIf(le.getExpr2(), true);
            }
        } else if (le.getOperation().equals(LogicExpr.OP_LOGIC_OR)) {
            if (!onTrue) {
                onIf(le.getExpr1(), false);
                onIf(le.getExpr2(), false);
            }
        }
    }
}
Also used : ExprNode(kalang.ast.ExprNode) CompareExpr(kalang.ast.CompareExpr) LogicExpr(kalang.ast.LogicExpr) InstanceOfExpr(kalang.ast.InstanceOfExpr) UnaryExpr(kalang.ast.UnaryExpr)

Example 3 with UnaryExpr

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

the class AstBuilder method visitUnaryExpr.

@Override
public UnaryExpr visitUnaryExpr(UnaryExprContext ctx) {
    String op = ctx.getChild(0).getText();
    UnaryExpr ue = new UnaryExpr(visitExpression(ctx.expression()), op);
    if (!semanticAnalyzer.validateUnaryExpr(ue))
        return null;
    mapAst(ue, ctx);
    return ue;
}
Also used : UnaryExpr(kalang.ast.UnaryExpr)

Example 4 with UnaryExpr

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

UnaryExpr (kalang.ast.UnaryExpr)4 ExprNode (kalang.ast.ExprNode)3 CompareExpr (kalang.ast.CompareExpr)2 LogicExpr (kalang.ast.LogicExpr)2 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 BlockStmt (kalang.ast.BlockStmt)1 IfStmt (kalang.ast.IfStmt)1 InstanceOfExpr (kalang.ast.InstanceOfExpr)1 NewObjectExpr (kalang.ast.NewObjectExpr)1 ThrowStmt (kalang.ast.ThrowStmt)1 Label (org.objectweb.asm.Label)1