From fcc053270d173bc3c1c44c8dda90bb8fe3992b5b Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 12 Aug 2019 19:49:20 +0100 Subject: [PATCH] Remove NegateExprVisitor It's actually cleaner to do this without one! --- .../deob/ast/transform/IfElseTransformer.java | 5 +- .../ast/transform/TernaryTransformer.java | 3 +- .../dev/openrs2/deob/ast/util/ExprUtils.java | 41 +++++++++++ .../deob/ast/visitor/NegateExprVisitor.java | 71 ------------------- 4 files changed, 44 insertions(+), 76 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/visitor/NegateExprVisitor.java diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java index bc179a08..9c6e651d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java @@ -5,7 +5,6 @@ import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.stmt.IfStmt; import com.github.javaparser.ast.stmt.Statement; import dev.openrs2.deob.ast.util.ExprUtils; -import dev.openrs2.deob.ast.visitor.NegateExprVisitor; public final class IfElseTransformer extends Transformer { private static boolean isIf(Statement stmt) { @@ -42,7 +41,7 @@ public final class IfElseTransformer extends Transformer { var condition = stmt.getCondition(); var thenStmt = stmt.getThenStmt(); if (isIf(thenStmt) && !isIf(elseStmt)) { - stmt.setCondition(condition.accept(new NegateExprVisitor(), null)); + stmt.setCondition(ExprUtils.not(condition)); stmt.setThenStmt(elseStmt); stmt.setElseStmt(thenStmt); } else if (!isIf(thenStmt) && isIf(elseStmt)) { @@ -59,7 +58,7 @@ public final class IfElseTransformer extends Transformer { * checking bitwise flags look worse. */ if (ExprUtils.isNot(condition)) { - stmt.setCondition(condition.accept(new NegateExprVisitor(), null)); + stmt.setCondition(ExprUtils.not(condition)); stmt.setThenStmt(elseStmt); stmt.setElseStmt(thenStmt); } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java index d1335b09..5a466d4d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java @@ -3,7 +3,6 @@ package dev.openrs2.deob.ast.transform; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.expr.ConditionalExpr; import dev.openrs2.deob.ast.util.ExprUtils; -import dev.openrs2.deob.ast.visitor.NegateExprVisitor; public final class TernaryTransformer extends Transformer { @Override @@ -17,7 +16,7 @@ public final class TernaryTransformer extends Transformer { var thenExpr = expr.getThenExpr(); var elseExpr = expr.getElseExpr(); - expr.setCondition(condition.accept(new NegateExprVisitor(), null)); + expr.setCondition(ExprUtils.not(condition)); expr.setThenExpr(elseExpr.clone()); expr.setElseExpr(thenExpr.clone()); }); diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java index d21d8e33..ca766f33 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java @@ -1,5 +1,7 @@ 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.IntegerLiteralExpr; 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() { /* empty */ } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/visitor/NegateExprVisitor.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/visitor/NegateExprVisitor.java deleted file mode 100644 index f9fc46d5..00000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/visitor/NegateExprVisitor.java +++ /dev/null @@ -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 { - @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); - } - } -}