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.
This commit is contained in:
Sei Lisa 2017-12-14 21:19:13 +01:00
parent ed05a2e022
commit aad4cf3bee

View file

@ -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'