Improve IfElseTransformer comments

Signed-off-by: Graham <gpe@openrs2.dev>
pull/132/head
Graham 4 years ago
parent bd28638b64
commit 8e3452ef98
  1. 63
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt

@ -19,6 +19,27 @@ class IfElseTransformer : Transformer() {
val condition = stmt.condition val condition = stmt.condition
val thenStmt = stmt.thenStmt val thenStmt = stmt.thenStmt
if (thenStmt.isIf() && !elseStmt.isIf()) { if (thenStmt.isIf() && !elseStmt.isIf()) {
/*
* Rewrite:
*
* if (a) {
* if (b) {
* ...
* }
* } else {
* ...
* }
*
* to:
*
* if (!a) {
* ...
* } else {
* if (b) {
* ...
* }
* }
*/
stmt.condition = condition.not() stmt.condition = condition.not()
stmt.thenStmt = elseStmt.clone() stmt.thenStmt = elseStmt.clone()
stmt.setElseStmt(thenStmt.clone()) stmt.setElseStmt(thenStmt.clone())
@ -31,7 +52,26 @@ class IfElseTransformer : Transformer() {
return@ifPresent return@ifPresent
} }
// Prefer fewer NOTs in the if condition /*
* Prefer fewer NOTs in the if condition
*
* Rewrites:
*
* if (!a) {
* ...
* } else {
* ....
* }
*
* to:
*
* if (a) {
* ...
* } else {
* ...
* }
*
*/
val notCondition = condition.not() val notCondition = condition.not()
if (notCondition.countNots() < condition.countNots()) { if (notCondition.countNots() < condition.countNots()) {
stmt.condition = notCondition stmt.condition = notCondition
@ -47,6 +87,21 @@ class IfElseTransformer : Transformer() {
} }
} }
/*
* Rewrite:
*
* } else {
* if (a) {
* ...
* }
* }
*
* to:
*
* } else if (a) {
* ....
* }
*/
unit.walk { stmt: IfStmt -> unit.walk { stmt: IfStmt ->
stmt.elseStmt.ifPresent { elseStmt -> stmt.elseStmt.ifPresent { elseStmt ->
if (elseStmt.isIf()) { if (elseStmt.isIf()) {
@ -58,9 +113,8 @@ class IfElseTransformer : Transformer() {
/* /*
* Rewrite: * Rewrite:
* *
* ...
* } else { * } else {
* if (x != 123) { * if (!a) {
* ... * ...
* throw ...; * throw ...;
* } * }
@ -69,8 +123,7 @@ class IfElseTransformer : Transformer() {
* *
* to: * to:
* *
* ... * } else if (a) {
* } else if (x == 123) {
* ... * ...
* } else { * } else {
* ... * ...

Loading…
Cancel
Save