Search in sources :

Example 1 with LogicExpr

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

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

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

the class AstBuilder method createBinaryExpr.

private BinaryExpr createBinaryExpr(ExprNode expr1, ExprNode expr2, String op) {
    BinaryExpr binExpr;
    switch(op) {
        case "==":
        case "!=":
        case ">":
        case ">=":
        case "<":
        case "<=":
            binExpr = new CompareExpr(expr1, expr2, op);
            break;
        case "&&":
        case "||":
            binExpr = new LogicExpr(expr1, expr2, op);
            break;
        default:
            binExpr = new MathExpr(expr1, expr2, op);
            break;
    }
    semanticAnalyzer.validateBinaryExpr(binExpr);
    return binExpr;
}
Also used : CompareExpr(kalang.ast.CompareExpr) LogicExpr(kalang.ast.LogicExpr) BinaryExpr(kalang.ast.BinaryExpr) MathExpr(kalang.ast.MathExpr)

Aggregations

CompareExpr (kalang.ast.CompareExpr)3 LogicExpr (kalang.ast.LogicExpr)3 ExprNode (kalang.ast.ExprNode)2 UnaryExpr (kalang.ast.UnaryExpr)2 BinaryExpr (kalang.ast.BinaryExpr)1 InstanceOfExpr (kalang.ast.InstanceOfExpr)1 MathExpr (kalang.ast.MathExpr)1 Label (org.objectweb.asm.Label)1