lua-users home
lua-l archive

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


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