Mercerenies / gdlisp

Lisp on the Godot platform
GNU General Public License v3.0
141 stars 1 forks source link

"While true break" optimization pattern #27

Open Mercerenies opened 3 years ago

Mercerenies commented 3 years ago

In the course of performing other optimizations (such as, in particular, #26), it is possible to end up with the following

while true:
    if <simple expression>:
        break
    ...

This can be converted into

while !<simple expression>:
    ...
Mercerenies commented 3 years ago

Consider using

(while (if 1 2 3))

as a benchmark, which currently compiles to

while true:
    if !2:
        break
return null

(Note that the substance of the if gets optimized out by other passes, but we can't get rid of the compound while yet; that's what this issue is meant to address)

Mercerenies commented 3 years ago

Also consider

(while (and 1 2))

which currently compiles to

while true:
    var _cond_0 = null
    if !1:
        _cond_0 = false
    else:
        _cond_0 = 2
    if !_cond_0:
        break
return null

But will hopefully become eligible for this optimization after #26 is resolved.