[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PATCH] Jump Tables for Switch/Case and Computed Goto
- From: Hisham <h@...>
- Date: Mon, 5 May 2014 23:11:59 -0300
On 5 May 2014 22:26, Paige DePol <lual@serfnet.org> wrote:
> I appreciate any and all feedback, I hope anyone using a custom Lua will try out this patch and let me know what they think of it! :)
Hi! Here's some feedback:
"The `and break` modifier automatically places a `break` statement at
the end of every `case` block. The default is `and continue`, which is
also the default in most implementations of the switch/case
expression. You may also use the `break` statement anywhere in the
`case` block to jump to the end of the entire `switch` statement.
Additionally, the `continue` keyword may be used in a case block to
jump execution immediately to the next case block."
First, it took me a bit to realize that inside switch you're using
`continue` to mean what is usually called fallthrough.
Also, this seemed strange to me: "is also the default in most
implementations of the switch/case expression" ← Is it really? I've
only seen this behavior in C and languages that copy its switch
construct verbatim (ie, languages with a syntax directly derived from
C). In every other language with a switch/case/match
statement/expression, fallthrough is not the default (and most often
it's not even available).
https://en.wikipedia.org/wiki/Switch_statement#Fallthrough
Given that Lua's syntax is more Pascal-style than C-style,
fallthrough-by-default is truly surprising.
"With this patch the if statement now has a new shortcut style:
if {cond} (goto {label}|jumpto {point}|break|continue|return [...])
With this form of the if statement no end statement is required, nor
are the else or elseif statements valid. The shorcut if statement was
developed for the jumpto statement, which can only reference jump
points within the same lexical block as the jumpto itself, however, I
quickly realised it was usable for other forms of flow control."
Interestingly, this instantly reminded me of AppleSoft BASIC for the
Apple II. It also had two forms of IF: `IF exp THEN stmt` and `IF exp
GOTO stmt`. I haven't thought about this structure in many, many...
many years! :)
A curiosity: could you share with us what was your motivation for this
jump-tables patch? Was it primarily to support switch-case? And also,
what was the motivation for switch-case: syntax clarity or
performance? (if the latter, some numbers would be nice). In my Lua
years, I actually learned not to miss switch/case much (certainly not
as much as I (still) miss continue (even with goto available)). I've
never used computed goto's much so my brain is not even trained to
think up algorithms using them. Some examples of use would be fun (I
assume the tests don't make it justice).
-- Hisham