[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Ternary operator patch
- From: David Manura <dm.lua@...>
- Date: Sat, 18 Sep 2010 18:38:03 -0400
On Sat, Sep 18, 2010 at 5:10 PM, Drake Wilson <drake@begriffli.ch> wrote:
> Quoth David Manura <dm.lua@math2.org>, 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
"expression-do's":
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].
[1] http://lua-users.org/wiki/ShortAnonymousFunctions
[2] http://github.com/fab13n/metalua/blob/master/src/samples/ifexpr.mlua
- References:
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Jonathan Castello
- Re: Ternary operator patch, Roberto Ierusalimschy
- Re: Ternary operator patch, Enrico Tassi
- Re: Ternary operator patch, Doug Rogers
- Re: Ternary operator patch, HyperHacker
- Re: Ternary operator patch, GrayFace
- Re: Ternary operator patch, Jonathan Castello
- Re: Ternary operator patch, Drake Wilson
- Re: Ternary operator patch, David Manura
- Re: Ternary operator patch, Drake Wilson