[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: proposal: state machine syntax also usable for continue / nested break
- From: Jim Whitehead II <jnwhiteh@...>
- Date: Fri, 28 Jan 2011 13:04:41 +0000
On Fri, Jan 28, 2011 at 4:30 AM, David Manura <dm.lua@math2.org> wrote:
> I propose extending the `do` statement to allow it to contain multiple
> labeled blocks, where each block is normally executed in sequence but
> can break to any other labeled block, like a state machine. The
> syntax is
>
> `do` block {label block} `end`
>
> Example:
>
> local x
> do
> x = 1
> :loop:
> if x > 10 then break :exit: end
> print(x)
> x = x + 1
> break :loop:
> :exit:
> end
Reading this code it is incredibly confusing to me, since it seems
that 'break' is simply the 'goto' keyword in new clothing. I also
sincerely dislike the syntax of labels at any point in the block-- I
much prefer the other proposal where the only place a label can occur
is after the beginning of a block and where the semantics of the break
keyword are clear and easy to define.
Just my two cents.
> You may recognize that as equivalent to a `for x=1,10 do print(x)
> end`. In fact, this can emulate a number of control structures. It
> can also emulate `continue` [1], breaks out of nested loops, and even
> `redo`:
>
> do
> for kk, vv in pairs(tt) do
> for k, v in pairs(t) do
> :redo:
> if condition1 then break end
> if condition2 then break :continue: end
> if condition3 then break :redo: end
> if condition4 then break :forfor: end
> :continue:
> end
> end
> :forfor:
> end
I agree that this may be more powerful, but I don't find it easy to
read or follow at all.
> As seen above, a couple syntactic simplifications are allowed. This
> same behavior is supported in the `do/end` of `while` and `for` loops,
> and an unlabeled `break` in one of these exits the containing loop as
> before. We can even make certain labels like :redo: and :continue:
> implicit in loops so that you may simply write
>
> for kk, vv in pairs(tt) do
> for k, v in pairs(t) do
> if condition1 then break end
> if condition2 then break :continue: end
> if condition3 then break :redo: end
> if condition4 then break :forfor: end
> end
> end
>
> A couple qualities of this proposal:
>
> - It looks quite versatile, perhaps nearly as versatile as goto, maybe even
> addressing David Given's recently stated needs for expressing arbitrary flow
> graphs in code generation [2].
> - It remains fairly structured despite the versatility. Each block
> can only break to
> the start of another block in the same lexical scope, and local
> variables in each
> block are private to the block.
> - It remains terse for common needs despite the versatility.
> - It extends the existing `do` statement.
> - It does not introduce new keywords but rather labels.
> - Labels perhaps could also be numbers that evaluate at runtime,
> supporting jump
> tables (like C `switch` statements) and things like Duff's device.
> - It kills multiple birds with one stone, perhaps addressing the
> criteria of Roberto [3].
> - It should be possible to write a program that translates programs with this
> syntax back to Lua 5.1 syntax for compatibility. The bytecode
> generated from that
> Lua 5.1 source might not be as efficient. However, it may be
> possible to write a
> compiler (e.g. Metalua) that directly generates efficient Lua 5.1
> bytecode for
> running under a standard Lua 5.1 VM.
> - Is this supported in `repeat/until`? It's not mandatory. We
> could require a nested
> `do/end` inside the `repeat/until`. That avoids the scoping problem.
> - It does not necessarily introduce more nesting in most cases.
>
> [1] http://article.gmane.org/gmane.comp.lang.lua.general/74476
> [2] http://lua-users.org/lists/lua-l/2011-01/msg01461.html
> [3] http://lua-users.org/lists/lua-l/2008-02/msg01183.html
>
>