lua-users home
lua-l archive

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

On Tue, Nov 3, 2009 at 1:22 PM, Jim Pryor <> wrote:
> for var_1, ..., var_n in my_next_function,initial_key,state1,...staten do
>    stuff
> end
> which would be equivalent to this:
> do
>    local _f, _var, _s1 ... _sn = my_next_function, initial_key, state1, ..., staten
>    while true do
>        local var_1, ···, var_n = _f(_var, _s1, ... _sn)
>        _var = var_1
>        if _var == nil then break end
>        stuff
>    end
> end

Implementing the transformed code in the Lua VM would not be possible
with the current virtual machine, as it would require a variable
number of stack slots for the state variables, and so the variables
above the state variables in the stack (which may be locals within the
loop, or state variables for an embedded loop) would not be at known
positions. One approach would be to implement tuples as a new type
(visible only to the Lua internals) which would allow a variable
number of stack slots to be compressed into a single stack slot (as an
aside, tuples could also be used to implement other things, like not
truncating vararg expressions which are in the middle of a list of
expressions). Unfortunately, doing it in this manner would prevent a
generic for loop being rewritable as a do-while loop using plain Lua.