lua-users home
lua-l archive

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

On Wednesday 07 November 2007, Jeremy Darling wrote:
> I've worked in pascal for many years, and I have to say that the two
> MAJOR complaints I've had about the language stem directly from the
> implementation details of the case statement.  I like the fall
> through abilities of C and wish that they existed in pascal without
> having to hack them into place.

Personally, I find the fallthrough feature of C more dangerous than 
useful. I do use it (marking the spots with /* fallthrough! */ 
comments), but only in obvious cases and performance and/or size 
critical code. IMHO, fallthrough doesn't really belong in a high 
level language - and this is coming from someone who learnt real 
programming in assembly language. ;-)

That said, I do have 'switch' in my EEL language (somewhat C-like and 
geared towards hard real time applications, but other than that, 
rather similar to Lua), and I occasionally miss the fallthrough 
logic. I'm considering implementing it as an *explicit* feature, 
using some keyword to skip/fallthrough to the next case handler. 
('continue' would be logical, but that could get nasty when 
using 'switch' inside loop constructs.)

> The return idiom is a good one as long as it could easily allow for
> the fall through statements to work.  Compiled down it seems that
> you would basically have the switch at the top that would drop into
> the proper execution code below. If a code block didn't have a
> return then it would simply keep executing till the end, if it did
> then it would work like any other code block and return to the
> caller.

Depending on memory management and other VM behavior, it may not be 
quite that simple.

In EEL, I use a very simple solution: Each case is compiled as a 
local, unnamed function, and a table (basically the same construct as 
a Lua table) is used to map cases to functions. The "wrong index" 
exception is mapped to the default case, obviously. There is a 
special VM instruction to do the look-up, call and default case 
handling as a single operation, but that's just a performance hack.

To implement fallthrough with this design, one has to actually jump 
(tail call, preferably) from one case handler function to another, as 
the normal path is to return to the instruction after the SWITCH 

The interesting side effect is that you could theoretically jump 
around as you wish, regardless of the physical order of the cases, 
but I think I'm seeing a "Disaster this way!" sign somewhere in that 
area. ;-)

One would at the very least have to restrict it to forward skipping 
only. Not that any language can keep people from writing horrible 
code, but anyway... :-)

//David Olofson - Programmer, Composer, Open Source Advocate

.------- - Games, SDL examples  -------.
| - 2.5D rendering engine       |
| - Music/audio engine          |
| - Real time scripting         |
'-- - Rheology instrumentation  --'