From e357f07c2750aca98688c2ac4a40bd21dccfed1a Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 19:48:53 +0000 Subject: [PATCH] Convert BinaryExprOrderTransformer to Kotlin --- .../transform/BinaryExprOrderTransformer.java | 56 ------------------- .../transform/BinaryExprOrderTransformer.kt | 44 +++++++++++++++ 2 files changed, 44 insertions(+), 56 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java deleted file mode 100644 index 2211a92820..0000000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java +++ /dev/null @@ -1,56 +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 dev.openrs2.deob.ast.util.NodeUtils; -import dev.openrs2.deob.ast.util.TypeUtils; - -public final class BinaryExprOrderTransformer extends Transformer { - private static Optional flip(BinaryExpr.Operator op) { - switch (op) { - case PLUS: - case MULTIPLY: - case EQUALS: - case NOT_EQUALS: - case BINARY_AND: - case BINARY_OR: - case XOR: - case OR: - case AND: - 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(); - } - } - - @Override - public void transform(CompilationUnit unit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { - flip(expr.getOperator()).ifPresent(op -> { - var type = expr.calculateResolvedType(); - if (op == BinaryExpr.Operator.PLUS && TypeUtils.isString(type)) { - return; - } - - var left = expr.getLeft(); - var right = expr.getRight(); - if (left.isLiteralExpr() && !right.isLiteralExpr()) { - expr.setOperator(op); - expr.setLeft(right.clone()); - expr.setRight(left.clone()); - } - }); - }); - } -} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt new file mode 100644 index 0000000000..80c0fddd6a --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt @@ -0,0 +1,44 @@ +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 dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.TypeUtils + +class BinaryExprOrderTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + val op = flip(expr.operator) ?: return@walk + + val type = expr.calculateResolvedType() + if (op == BinaryExpr.Operator.PLUS && TypeUtils.isString(type)) { + return@walk + } + + val left = expr.left + val right = expr.right + if (left.isLiteralExpr && !right.isLiteralExpr) { + expr.operator = op + expr.left = right.clone() + expr.right = left.clone() + } + } + } + + companion object { + private fun flip(op: BinaryExpr.Operator): BinaryExpr.Operator? { + return when (op) { + BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> op + BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> op + BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> op + BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> 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 + } + } + } +}