diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java deleted file mode 100644 index e65b566593..0000000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java +++ /dev/null @@ -1,53 +0,0 @@ -package dev.openrs2.deob.ast.transform; - -import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.ast.Node; -import com.github.javaparser.ast.expr.BinaryExpr; -import com.github.javaparser.ast.expr.Expression; -import com.github.javaparser.ast.expr.UnaryExpr; -import dev.openrs2.deob.ast.util.ExprUtils; -import dev.openrs2.deob.ast.util.NodeUtils; -import dev.openrs2.deob.ast.util.TypeUtils; - -public final class AddSubTransformer extends Transformer { - private static boolean isNegative(Expression expr) { - if (expr.isUnaryExpr()) { - return expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS; - } else if (expr.isIntegerLiteralExpr()) { - return expr.asIntegerLiteralExpr().asInt() < 0; - } else if (expr.isLongLiteralExpr()) { - return expr.asLongLiteralExpr().asLong() < 0; - } else { - return false; - } - } - - @Override - public void transform(CompilationUnit unit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { - var op = expr.getOperator(); - var left = expr.getLeft(); - var right = expr.getRight(); - - var type = expr.calculateResolvedType(); - if (op == BinaryExpr.Operator.PLUS && TypeUtils.isString(type)) { - return; - } - - if (op == BinaryExpr.Operator.PLUS && isNegative(right)) { - /* x + -y => x - y */ - expr.setOperator(BinaryExpr.Operator.MINUS); - expr.setRight(ExprUtils.negate(right)); - } else if (op == BinaryExpr.Operator.PLUS && isNegative(left) && !(ExprUtils.hasSideEffects(left) && ExprUtils.hasSideEffects(right))) { - /* -x + y => y - x */ - expr.setOperator(BinaryExpr.Operator.MINUS); - expr.setLeft(right.clone()); - expr.setRight(ExprUtils.negate(left)); - } else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) { - /* x - -y => x + y */ - expr.setOperator(BinaryExpr.Operator.PLUS); - expr.setRight(ExprUtils.negate(right)); - } - }); - } -} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt new file mode 100644 index 0000000000..75f1d6b901 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt @@ -0,0 +1,55 @@ +package dev.openrs2.deob.ast.transform + +import com.github.javaparser.ast.CompilationUnit +import com.github.javaparser.ast.Node +import com.github.javaparser.ast.expr.BinaryExpr +import com.github.javaparser.ast.expr.Expression +import com.github.javaparser.ast.expr.UnaryExpr +import dev.openrs2.deob.ast.util.ExprUtils +import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.TypeUtils + +class AddSubTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + val op = expr.operator + val left = expr.left + val right = expr.right + val type = expr.calculateResolvedType() + + if (op == BinaryExpr.Operator.PLUS && TypeUtils.isString(type)) { + return@walk + } + + if (op == BinaryExpr.Operator.PLUS && isNegative(right)) { + // x + -y => x - y + expr.operator = BinaryExpr.Operator.MINUS + expr.right = ExprUtils.negate(right) + } else if (op == BinaryExpr.Operator.PLUS && isNegative(left)) { + if (ExprUtils.hasSideEffects(left) || ExprUtils.hasSideEffects(right)) { + return@walk + } + + // -x + y => y - x + expr.operator = BinaryExpr.Operator.MINUS + expr.left = right.clone() + expr.right = ExprUtils.negate(left) + } else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) { + // x - -y => x + y + expr.operator = BinaryExpr.Operator.PLUS + expr.right = ExprUtils.negate(right) + } + } + } + + companion object { + private fun isNegative(expr: Expression): Boolean { + return when { + expr.isUnaryExpr -> expr.asUnaryExpr().operator == UnaryExpr.Operator.MINUS + expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asInt() < 0 + expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asLong() < 0 + else -> false + } + } + } +}