Simplify IfElseTransformer

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 4 years ago
parent 9c80ff1c6e
commit b20115f1a7
  1. 40
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt

@ -108,8 +108,9 @@ class IfElseTransformer : Transformer() {
*/ */
unit.walk { stmt: IfStmt -> unit.walk { stmt: IfStmt ->
stmt.elseStmt.ifPresent { elseStmt -> stmt.elseStmt.ifPresent { elseStmt ->
if (elseStmt.isIf()) { val ifStmt = elseStmt.getIf()
stmt.setElseStmt(elseStmt.getIf()) if (ifStmt != null) {
stmt.setElseStmt(ifStmt)
} }
} }
} }
@ -210,44 +211,29 @@ class IfElseTransformer : Transformer() {
} }
private fun Statement.isIf(): Boolean { private fun Statement.isIf(): Boolean {
return when (this) { return getIf() != null
is IfStmt -> true
is BlockStmt -> {
val stmts = statements
stmts.size == 1 && stmts[0] is IfStmt
}
else -> false
}
} }
private fun Statement.getIf(): Statement { private fun Statement.getIf(): Statement? {
when (this) { return when (this) {
is IfStmt -> { is IfStmt -> clone()
return clone()
}
is BlockStmt -> { is BlockStmt -> {
val stmts = statements val head = statements.singleOrNull()
if (stmts.size == 1) {
val head = stmts[0]
if (head is IfStmt) { if (head is IfStmt) {
return head.clone() head.clone()
} } else {
null
} }
} }
else -> null
} }
throw IllegalArgumentException()
} }
private fun Statement.isTailThrowOrReturn(): Boolean { private fun Statement.isTailThrowOrReturn(): Boolean {
return when (this) { return when (this) {
is ThrowStmt, is ReturnStmt -> true is ThrowStmt, is ReturnStmt -> true
is BlockStmt -> { is BlockStmt -> {
val stmts = statements val tail = statements.lastOrNull()
if (stmts.isEmpty()) {
return false
}
val tail = stmts[stmts.size - 1]
tail is ThrowStmt || tail is ReturnStmt tail is ThrowStmt || tail is ReturnStmt
} }
else -> false else -> false

Loading…
Cancel
Save