public static Expr invert(Expr expr) {
if (expr instanceof UnaryExpr) {
UnaryExpr unary = (UnaryExpr) expr;
if (unary.getOperation() == UnaryOperation.NOT) {
return unary.getOperand();
}
} else if (expr instanceof BinaryExpr) {
BinaryExpr binary = (BinaryExpr) expr;
Expr a = binary.getFirstOperand();
Expr b = binary.getSecondOperand();
switch (binary.getOperation()) {
case EQUALS:
return Expr.binary(BinaryOperation.NOT_EQUALS, binary.getType(), a, b, expr.getLocation());
case NOT_EQUALS:
return Expr.binary(BinaryOperation.EQUALS, binary.getType(), a, b, expr.getLocation());
case LESS:
return Expr.binary(BinaryOperation.GREATER_OR_EQUALS, binary.getType(), a, b, expr.getLocation());
case LESS_OR_EQUALS:
return Expr.binary(BinaryOperation.GREATER, binary.getType(), a, b, expr.getLocation());
case GREATER:
return Expr.binary(BinaryOperation.LESS_OR_EQUALS, binary.getType(), a, b, expr.getLocation());
case GREATER_OR_EQUALS:
return Expr.binary(BinaryOperation.LESS, binary.getType(), a, b, expr.getLocation());
default:
break;
}
}
return Expr.invert(expr);
}
}