lua-users home
lua-l archive

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

On Sat, Sep 18, 2010 at 5:10 PM, Drake Wilson <> wrote:
> Quoth David Manura <>, on 2010-09-18 16:41:29 -0400:
>> It is possible to mechanically translate expression ifs into their statement
>> format.
> Of course it is, but (a) the inline closure approach breaks the use of
> the vararg token inside the expressions unless you do complicated
> things, and (b) hoisting the statement outside requires effort more on
> the order of doing full compilation stuff (and then preserving the
> line numbers becomes a nightmare and you might as well start patching
> Lua or writing a whole extra language on top of the runtime).
> Right now I'm using a Ragel-based mishmash which has a few pushdown
> automaton elements in it but is otherwise mostly a fancy token filter.
> Upgrading that into a full parser would be a lot of extra work
>  Patching Lua, while popular, is something I want to avoid ...

Your preprocessing approach to implementing expression-ifs would work
better if your token filter would translate

  local x = (if a then b else (...)) + c

into something like

  local x = (function() if a then return b else return (...) end end)() + c

and if somehow Lua 5.2 could avoid the overhead and vararg problem in
this special case, by going a bit beyond the anonymous closure caching
recently introduced in 5.2-work.  Lua doesn't necessarily need new
syntax to support this.  It could be done entirely in the code
generator.  Optionally it could though be written as

  local x = (do if a then return b else return (...) end end) + c

The syntax is not the tersest [1], but it does provide a fundamental
increase in expressibility of the language, more-so than the
expression-if, and preprocessors, which don't care about terseness,
can in particular take advantage of it.  Lack of this is one of the
(or "the"?) main reasons that Metalua has the disadvantage of
implementing its own, LuaJIT2-incompatible byte-code compiler.
Metalua's code generator handles statement blocks (`Stat AST nodes)
inside expressions, and these `Stat nodes are used to implement things
like expression-ifs in Metalua [2].