forked from openrs2/openrs2
parent
5b032c980f
commit
e357f07c27
@ -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<BinaryExpr.Operator> 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()); |
||||
} |
||||
}); |
||||
}); |
||||
} |
||||
} |
@ -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 |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue