From 8ac16d722b4558073d115a9bb4794735f39f1899 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 19:42:20 +0000 Subject: [PATCH] Convert ComplementTransformer to Kotlin --- .../ast/transform/ComplementTransformer.java | 68 ------------------- .../ast/transform/ComplementTransformer.kt | 58 ++++++++++++++++ 2 files changed, 58 insertions(+), 68 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java deleted file mode 100644 index 424b9d62..00000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java +++ /dev/null @@ -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 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)); - } - }); - }); - } -} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt new file mode 100644 index 00000000..38686c76 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt @@ -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() + } + } + } +}