forked from openrs2/openrs2
parent
38a3e752f9
commit
faec1f1a26
@ -1,27 +0,0 @@ |
||||
package dev.openrs2.deob.ast.transform; |
||||
|
||||
import com.github.javaparser.ast.CompilationUnit; |
||||
import com.github.javaparser.ast.Node; |
||||
import com.github.javaparser.ast.expr.ConditionalExpr; |
||||
import dev.openrs2.deob.ast.util.ExprUtils; |
||||
import dev.openrs2.deob.ast.util.NodeUtils; |
||||
|
||||
public final class TernaryTransformer extends Transformer { |
||||
@Override |
||||
public void transform(CompilationUnit unit) { |
||||
NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, ConditionalExpr.class, expr -> { |
||||
var condition = expr.getCondition(); |
||||
var notCondition = ExprUtils.not(condition); |
||||
if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { |
||||
return; |
||||
} |
||||
|
||||
var thenExpr = expr.getThenExpr(); |
||||
var elseExpr = expr.getElseExpr(); |
||||
|
||||
expr.setCondition(notCondition); |
||||
expr.setThenExpr(elseExpr.clone()); |
||||
expr.setElseExpr(thenExpr.clone()); |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,27 @@ |
||||
package dev.openrs2.deob.ast.transform |
||||
|
||||
import com.github.javaparser.ast.CompilationUnit |
||||
import com.github.javaparser.ast.Node |
||||
import com.github.javaparser.ast.expr.ConditionalExpr |
||||
import dev.openrs2.deob.ast.util.ExprUtils |
||||
import dev.openrs2.deob.ast.util.NodeUtils |
||||
|
||||
class TernaryTransformer : Transformer() { |
||||
override fun transform(unit: CompilationUnit) { |
||||
NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, ConditionalExpr::class.java) { expr -> |
||||
val condition = expr.condition |
||||
val notCondition = ExprUtils.not(condition) |
||||
if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { |
||||
return@walk |
||||
} |
||||
|
||||
val thenExpr = expr.thenExpr |
||||
val elseExpr = expr.elseExpr |
||||
|
||||
expr.condition = notCondition |
||||
|
||||
expr.thenExpr = elseExpr.clone() |
||||
expr.elseExpr = thenExpr.clone() |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue