var _cond_0 = null
if foo():
_cond_0 = 1
else:
_cond_0 = 2
return _cond_0
If we see the pattern:
Variable declaration with assignment to a constant form
If-else statement, or a simple if statement with no else
Every clause of the if-else (or if) is a single statement: a simple assignment to the declared variable
Then we can optimize down to an application of the ternary operator
var _cond_0 = 1 if foo() else 2
Note that the ternary operator does short-circuiting correctly, so the later assignments do not have to be constant expressions (the declaration does, or it at least has to be side-effect-free, since we're planning to elide it).
Possible extension: Support elif as nested ternary applications. This should eliminate a lot of the weirdness with and / or compiling to full statements.
Currently,
(if (foo) 1 2)
compiles toIf we see the pattern:
Then we can optimize down to an application of the ternary operator
Note that the ternary operator does short-circuiting correctly, so the later assignments do not have to be constant expressions (the declaration does, or it at least has to be side-effect-free, since we're planning to elide it).
Possible extension: Support
elif
as nested ternary applications. This should eliminate a lot of the weirdness withand
/or
compiling to full statements.