From 8e3452ef9872e03978a5b5df934103ee3cab7640 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 3 Aug 2020 08:50:39 +0100 Subject: [PATCH] Improve IfElseTransformer comments Signed-off-by: Graham --- .../deob/ast/transform/IfElseTransformer.kt | 63 +++++++++++++++++-- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt index 30a6188051..2d185a7201 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt @@ -19,6 +19,27 @@ class IfElseTransformer : Transformer() { val condition = stmt.condition val thenStmt = stmt.thenStmt if (thenStmt.isIf() && !elseStmt.isIf()) { + /* + * Rewrite: + * + * if (a) { + * if (b) { + * ... + * } + * } else { + * ... + * } + * + * to: + * + * if (!a) { + * ... + * } else { + * if (b) { + * ... + * } + * } + */ stmt.condition = condition.not() stmt.thenStmt = elseStmt.clone() stmt.setElseStmt(thenStmt.clone()) @@ -31,7 +52,26 @@ class IfElseTransformer : Transformer() { 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() if (notCondition.countNots() < condition.countNots()) { stmt.condition = notCondition @@ -47,6 +87,21 @@ class IfElseTransformer : Transformer() { } } + /* + * Rewrite: + * + * } else { + * if (a) { + * ... + * } + * } + * + * to: + * + * } else if (a) { + * .... + * } + */ unit.walk { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt -> if (elseStmt.isIf()) { @@ -58,9 +113,8 @@ class IfElseTransformer : Transformer() { /* * Rewrite: * - * ... * } else { - * if (x != 123) { + * if (!a) { * ... * throw ...; * } @@ -69,8 +123,7 @@ class IfElseTransformer : Transformer() { * * to: * - * ... - * } else if (x == 123) { + * } else if (a) { * ... * } else { * ...