nojanath / SublimeKSP

Fork of Nils Liberg's SublimeKSP plugin. See README for details.
GNU General Public License v3.0
86 stars 18 forks source link

Inline multi-line functions #397

Open mkruselj opened 11 months ago

mkruselj commented 11 months ago

Let's say we have a function like:

function math.clamp(x, min, max)
    if (x < min)
        x := min
    else if (x > max)
        x := max
    end if
end function

We currently cannot inline this function in cases like these (or at all, really - cannot even assign such a function to a variable directly):

if math.clamp(x, 0, 100) = 50

What would need to happen is:

temp := math.clamp(x, 0, 100)

if temp = 50

There would have to be a proxy variable for every expression that is bool'd together. Probably makes sense to use an array instead.

if math.min(x, 0) # 0 and math.clamp(y, 0, 100) = 50

becomes:

temp[0] := math.min(x, 0)
temp[1] := math.clamp(y, 0, 100)

if temp[0] # 0 and temp[1] = 50
    <code>
end if
eitherys commented 11 months ago

This needs to respect short-circuit evaluation. The rules are

For expression if A and B

proxies for A
if A
    proxies for B
    if B
        code

For expression if A or B

proxies for A
if A
    code
else
    proxies for B
    if B
        code
end if

Of course this would have to be written properly as part of an expression tree to build how far the if-statements expand when expressions are more complex. I would not add this feature if you can not make it respect short-circuit evaluation; it would invalidate basic rules of programming.