Convert ComplementTransformer to Kotlin

bzip2
Graham 5 years ago
parent d5cee1b2f5
commit 8ac16d722b
  1. 68
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java
  2. 58
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt

@ -1,68 +0,0 @@
package dev.openrs2.deob.ast.transform;
import java.util.Optional;
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.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.UnaryExpr;
import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class ComplementTransformer extends Transformer {
private static boolean isComplement(Expression expr) {
return expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.BITWISE_COMPLEMENT;
}
private static boolean isComplementOrLiteral(Expression expr) {
return isComplement(expr) || ExprUtils.isIntegerOrLongLiteral(expr);
}
private static Optional<BinaryExpr.Operator> complement(BinaryExpr.Operator op) {
switch (op) {
case EQUALS:
case NOT_EQUALS:
return Optional.of(op);
case GREATER:
return Optional.of(BinaryExpr.Operator.LESS);
case GREATER_EQUALS:
return Optional.of(BinaryExpr.Operator.LESS_EQUALS);
case LESS:
return Optional.of(BinaryExpr.Operator.GREATER);
case LESS_EQUALS:
return Optional.of(BinaryExpr.Operator.GREATER_EQUALS);
default:
return Optional.empty();
}
}
private static Expression complement(Expression expr) {
if (expr.isUnaryExpr()) {
return expr.asUnaryExpr().getExpression();
} else if (expr.isIntegerLiteralExpr()) {
return new IntegerLiteralExpr(~expr.asIntegerLiteralExpr().asInt());
} else if (expr.isLongLiteralExpr()) {
return ExprUtils.createLong(~expr.asLongLiteralExpr().asLong());
} else {
throw new IllegalArgumentException();
}
}
@Override
public void transform(CompilationUnit unit) {
NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> {
complement(expr.getOperator()).ifPresent(op -> {
var left = expr.getLeft();
var right = expr.getRight();
if (isComplementOrLiteral(left) && isComplementOrLiteral(right) && !(ExprUtils.isIntegerOrLongLiteral(left) && ExprUtils.isIntegerOrLongLiteral(right))) {
expr.setOperator(op);
expr.setLeft(complement(left));
expr.setRight(complement(right));
}
});
});
}
}

@ -0,0 +1,58 @@
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.IntegerLiteralExpr
import com.github.javaparser.ast.expr.UnaryExpr
import dev.openrs2.deob.ast.util.ExprUtils
import dev.openrs2.deob.ast.util.NodeUtils
class ComplementTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr ->
val op = complement(expr.operator) ?: return@walk
val left = expr.left
val right = expr.right
val bothLiteral = ExprUtils.isIntegerOrLongLiteral(left) && ExprUtils.isIntegerOrLongLiteral(right)
if (isComplementOrLiteral(left) && isComplementOrLiteral(right) && !bothLiteral) {
expr.operator = op
expr.left = complement(left)
expr.right = complement(right)
}
}
}
companion object {
private fun isComplement(expr: Expression): Boolean {
return expr.isUnaryExpr && expr.asUnaryExpr().operator == UnaryExpr.Operator.BITWISE_COMPLEMENT
}
private fun isComplementOrLiteral(expr: Expression): Boolean {
return isComplement(expr) || ExprUtils.isIntegerOrLongLiteral(expr)
}
private fun complement(op: BinaryExpr.Operator): BinaryExpr.Operator? {
return when (op) {
BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> op
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER
BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS
else -> null
}
}
private fun complement(expr: Expression): Expression {
return when {
expr.isUnaryExpr -> expr.asUnaryExpr().expression
expr.isIntegerLiteralExpr -> IntegerLiteralExpr(expr.asIntegerLiteralExpr().asInt().inv())
expr.isLongLiteralExpr -> ExprUtils.createLong(expr.asLongLiteralExpr().asLong().inv())
else -> throw IllegalArgumentException()
}
}
}
}
Loading…
Cancel
Save