Open-source multiplayer game server compatible with the RuneScape client
https://www.openrs2.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.1 KiB
56 lines
2.1 KiB
5 years ago
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|