From aad4cf3bee1279feb042bce13c6089ed78d76fec Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Thu, 14 Dec 2017 21:19:13 +0100 Subject: [PATCH] Fix the fix for the bug introduced in 097c054 Our previous fix was incomplete, because it failed to detect the last IF in a chain of ELSE IFs. For example: if (a == 2) llDie(); else if (a) llDie(); else if (a == 3) llDie(); That would be transformed by the IF swapper into: if (a ^ 2) if (a) llDie(); else if (a == 3) llDie(); else llDie(); Note that the last 'else' would bind to the last 'if', not to the first one. So the condition is actually like this: child[1] of an 'if' statement needs to be guarded in {} if the 'else' may belong to the wrong 'if'. It will belong to the wrong 'if' if child[1] is a (possibly empty) chain of 'if {whatever} else ...', followed by an 'if' without 'else', that is: if (cond) stmt; (which was what our previous check did), but also e.g.: if (cond) stmt; else if (cond) stmt; else if (cond) stmt; which we neglected to consider in our previous fix. --- lslopt/lsloutput.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lslopt/lsloutput.py b/lslopt/lsloutput.py index e0c2fcc..ea6f46f 100644 --- a/lslopt/lsloutput.py +++ b/lslopt/lsloutput.py @@ -385,9 +385,17 @@ class outscript(object): ret = self.dent() while True: ret += 'if (' + self.OutExpr(child[0]) + ')\n' - if (len(child) == 3 - and child[1]['nt'] == 'IF' and len(child[1]['ch']) < 3 - ): + # Do we need to add braces around the THEN side? + needs_braces = False + if len(child) == 3: + testnode = child[1] + # Find last IF in an ELSE IF chain + while testnode['nt'] == 'IF' and len(testnode['ch']) == 3: + testnode = testnode['ch'][2] + if testnode['nt'] == 'IF': + # hit an IF without ELSE at the end of the chain + needs_braces = True + if needs_braces: ret += self.dent() + '{\n' ret += self.OutIndented(child[1]) ret += self.dent() + '}\n'