lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 12/06/2014 01:24, Paige DePol wrote:
On Jun 11, 2014, at 10:49 PM, Thiago L. <fakedme@gmail.com> wrote:

On 12/06/2014 00:18, Paige DePol wrote:
On Jun 11, 2014, at 9:24 PM, Thiago L. <fakedme@gmail.com> wrote:

Make it so this is possible:

goto stuff
::label::
print("yay!")
goto exit
::stuff::
f = label
goto f
::exit::
With my computed goto/switch case patch[1] that is possible:

goto stuff
:|label|:
print("yay!")
goto exit
::stuff::
f = "label"
jumpto f
::exit::

As goto labels are only used during compile time I created the :|jumpto|: style label for computed gotos, see my patch documentation for full details.


And this:

t=setmetatable({label1, label2},{__index = function() return default end})
goto t[x] -- errors if t[x] doesn't evaluate to a label
::label1::
dostuff()
-- fall thru
::label2::
domorestuff()
goto exit -- break
::default::
dodefault()
::exit::
switch x
     case label1
         dostuff()
     case label2
         domorestuff()
         break
     else
         dodefault()
end


Or, if you prefer:

jumpto or continue on x
goto default
:|label1|:
dostuff()
:|label2|:
domorestuff()
goto exit
::default::
dodefault()
::exit::

Note that no meta-tables or closures were required to implement your example with my patch, you can also probably see why I added sugar for the switch statement, it looks a LOT nicer than the raw computed goto syntax! ;)

~pmd

[1] https://github.com/FizzyPop/lua-patches/tree/master/joint-patches/jump-table%2Bif-shct%2Bcont-loop


Can your thing handle objects? Something like:

t[_G] = something
goto t[_G]
Well, no, since you can't easily represent an object as a constant value. Objects in Lua are essentially tables and since you can't use a table as a jumpto point you can't use "objects" either, sorry! ;)

~pmd


Then don't do constants:

t = {}
t[_G] = function() <some stuff> end
if t[_G] then t[_G]() else <default case> end

I would like to be able to do something like this:

mode = {}
mode.x = {}
mode.y = {}
mode.z = {}
obj = mode.x
switch obj
    case mode.x
        <some stuff>
        break
    else
        <default case>
end

basically what it should do behind the scenes is make a table where you have all the cases and stuff where it would look like this: (even if it has to do it at runtime which would be kinda slow but once you have a large amount of cases it should be faster than if-then-else(if) chains)
t={} t[mode.x] = somelabel t[mode.y] = someotherlabel <etc>