lua-users home
lua-l archive

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



On 8/21/07, David Given <dg@cowlark.com> wrote:
*However*, it ought to be quite straightforward to use something like metalua
or a token filter to rewrite this [...]

It is, more or less :) The clist extension in the samples shows how to mess with tables; it adds lists by comprehensions, splices, multi-indexes etc. The principle is that such extensions must produce optimized code in "normal" cases, but might have to produce much more complex code for non-standard uses.

clist implementation: http://metalua.luaforge.net/src/lib/ext-syntax/clist.lua.html; clist use sample: http://metalua.luaforge.net/src/samples/clist_test.lua.html. It might actually already provide what Mark asks for, with the ... suffix for table elements:

{ field1 = "foo",
  addLotsOfFields( $, 1, 2, 3 )...,
  addNothingNotEvenANil()...,
  addMoreFields( $, "a", "b" ) }

Genrated code is probably more naive than required, however (I'm too lazy to check).

When I say "more or less" straightforward, I mean that in order to modify an existing construction, you have to study its sources (in this case, compiler/mlp_table.lua). That's not a bad thing, because it's a barrier to entry for something which must not be done lightly: whenever you hack an existing construct, you're going to conflict with any other extension that hacks the same bit, so you lose interoperability. I'm rather glad that you're required to think twice before doing open-heart surgery! Generally speaking, the metalua's design, including gramar entry points, tries to encourage you into doing the right thing (here writing composing syntax extensions, that roughly respect Lua's grammatical style). It's easy to circumvent, but then you've been warned, you're on your own.
 
Likewise, this
>     t.foo?.baz

There's a sample implementing that, somewhere. Must be something like (untested):

mlp.lexer:add '.?'
mlp.expr.suffix:add{ '.?', mlp.id, builder = |a, b|
  +{ local x = -{a} in x ? x[-{mlp2string(b[1])}], nil } }

(modulo alpha-conversion, to be fixed with hygienic macros, hopefully for metalua 0.4). This one might be easy to implement with token filters, unlike wild literal table manipulations.

-- Fabien.