Convert AddSubTransformer to Kotlin

master
Graham 5 years ago
parent e357f07c27
commit 6425a874fd
  1. 53
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java
  2. 55
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt

@ -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));
}
});
}
}

@ -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
}
}
}
}
Loading…
Cancel
Save