Remove NegateExprVisitor

It's actually cleaner to do this without one!
bzip2
Graham 5 years ago
parent 12d3af48dd
commit fcc053270d
  1. 5
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java
  2. 3
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java
  3. 41
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java
  4. 71
      deob-ast/src/main/java/dev/openrs2/deob/ast/visitor/NegateExprVisitor.java

@ -5,7 +5,6 @@ import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.stmt.IfStmt; import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.stmt.Statement;
import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.visitor.NegateExprVisitor;
public final class IfElseTransformer extends Transformer { public final class IfElseTransformer extends Transformer {
private static boolean isIf(Statement stmt) { private static boolean isIf(Statement stmt) {
@ -42,7 +41,7 @@ public final class IfElseTransformer extends Transformer {
var condition = stmt.getCondition(); var condition = stmt.getCondition();
var thenStmt = stmt.getThenStmt(); var thenStmt = stmt.getThenStmt();
if (isIf(thenStmt) && !isIf(elseStmt)) { if (isIf(thenStmt) && !isIf(elseStmt)) {
stmt.setCondition(condition.accept(new NegateExprVisitor(), null)); stmt.setCondition(ExprUtils.not(condition));
stmt.setThenStmt(elseStmt); stmt.setThenStmt(elseStmt);
stmt.setElseStmt(thenStmt); stmt.setElseStmt(thenStmt);
} else if (!isIf(thenStmt) && isIf(elseStmt)) { } else if (!isIf(thenStmt) && isIf(elseStmt)) {
@ -59,7 +58,7 @@ public final class IfElseTransformer extends Transformer {
* checking bitwise flags look worse. * checking bitwise flags look worse.
*/ */
if (ExprUtils.isNot(condition)) { if (ExprUtils.isNot(condition)) {
stmt.setCondition(condition.accept(new NegateExprVisitor(), null)); stmt.setCondition(ExprUtils.not(condition));
stmt.setThenStmt(elseStmt); stmt.setThenStmt(elseStmt);
stmt.setElseStmt(thenStmt); stmt.setElseStmt(thenStmt);
} }

@ -3,7 +3,6 @@ package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ConditionalExpr; import com.github.javaparser.ast.expr.ConditionalExpr;
import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.visitor.NegateExprVisitor;
public final class TernaryTransformer extends Transformer { public final class TernaryTransformer extends Transformer {
@Override @Override
@ -17,7 +16,7 @@ public final class TernaryTransformer extends Transformer {
var thenExpr = expr.getThenExpr(); var thenExpr = expr.getThenExpr();
var elseExpr = expr.getElseExpr(); var elseExpr = expr.getElseExpr();
expr.setCondition(condition.accept(new NegateExprVisitor(), null)); expr.setCondition(ExprUtils.not(condition));
expr.setThenExpr(elseExpr.clone()); expr.setThenExpr(elseExpr.clone());
expr.setElseExpr(thenExpr.clone()); expr.setElseExpr(thenExpr.clone());
}); });

@ -1,5 +1,7 @@
package dev.openrs2.deob.ast.util; package dev.openrs2.deob.ast.util;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.LongLiteralExpr; import com.github.javaparser.ast.expr.LongLiteralExpr;
@ -30,6 +32,45 @@ public final class ExprUtils {
} }
} }
// TODO(gpe): need to be careful about operator precedence/EnclosedExpr
public static Expression not(Expression expr) {
if (expr.isUnaryExpr()) {
var unary = expr.asUnaryExpr();
if (unary.getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT) {
return unary.getExpression().clone();
}
} else if (expr.isBinaryExpr()) {
var binary = expr.asBinaryExpr();
var left = binary.getLeft();
var right = binary.getRight();
switch (binary.getOperator()) {
case EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.NOT_EQUALS);
case NOT_EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.EQUALS);
case GREATER:
return new BinaryExpr(left, right, BinaryExpr.Operator.LESS_EQUALS);
case GREATER_EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.LESS);
case LESS:
return new BinaryExpr(left, right, BinaryExpr.Operator.GREATER_EQUALS);
case LESS_EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.GREATER);
case AND:
return new BinaryExpr(not(left), not(right), BinaryExpr.Operator.OR);
case OR:
return new BinaryExpr(not(left), not(right), BinaryExpr.Operator.AND);
}
} else if (expr.isBooleanLiteralExpr()) {
return new BooleanLiteralExpr(!expr.asBooleanLiteralExpr().getValue());
} else if (expr.isEnclosedExpr()) {
return not(expr.asEnclosedExpr().getInner());
}
return new UnaryExpr(expr.clone(), UnaryExpr.Operator.LOGICAL_COMPLEMENT);
}
private ExprUtils() { private ExprUtils() {
/* empty */ /* empty */
} }

@ -1,71 +0,0 @@
package dev.openrs2.deob.ast.visitor;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.UnaryExpr;
import com.github.javaparser.ast.visitor.GenericVisitorWithDefaults;
import com.google.common.base.Preconditions;
// TODO(gpe): need to be careful about operator precedence/EnclosedExpr
public final class NegateExprVisitor extends GenericVisitorWithDefaults<Expression, Void> {
@Override
public Expression defaultAction(Node n, Void arg) {
Preconditions.checkArgument(n instanceof Expression);
return new UnaryExpr((Expression) n, UnaryExpr.Operator.LOGICAL_COMPLEMENT);
}
@Override
public Expression defaultAction(NodeList n, Void arg) {
throw new IllegalArgumentException();
}
@Override
public Expression visit(BinaryExpr n, Void arg) {
var left = n.getLeft();
var right = n.getRight();
switch (n.getOperator()) {
case EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.NOT_EQUALS);
case NOT_EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.EQUALS);
case GREATER:
return new BinaryExpr(left, right, BinaryExpr.Operator.LESS_EQUALS);
case GREATER_EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.LESS);
case LESS:
return new BinaryExpr(left, right, BinaryExpr.Operator.GREATER_EQUALS);
case LESS_EQUALS:
return new BinaryExpr(left, right, BinaryExpr.Operator.GREATER);
case AND:
return new BinaryExpr(left.accept(this, arg), right.accept(this, arg), BinaryExpr.Operator.OR);
case OR:
return new BinaryExpr(left.accept(this, arg), right.accept(this, arg), BinaryExpr.Operator.AND);
default:
return defaultAction(n, arg);
}
}
@Override
public Expression visit(BooleanLiteralExpr n, Void arg) {
return new BooleanLiteralExpr(!n.getValue());
}
@Override
public Expression visit(EnclosedExpr n, Void arg) {
return n.getInner().accept(this, arg);
}
@Override
public Expression visit(UnaryExpr n, Void arg) {
switch (n.getOperator()) {
case LOGICAL_COMPLEMENT:
return n.getExpression();
default:
return defaultAction(n, arg);
}
}
}
Loading…
Cancel
Save