lua-users home
lua-l archive

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

On 8/21/07, David Given <> 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:; clist use sample: It might actually already provide what Mark asks for, with the ... suffix for table elements:

{ field1 = "foo",
  addLotsOfFields( $, 1, 2, 3 )...,
  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

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

mlp.lexer:add '.?'
mlp.expr.suffix:add{ '.?',, 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.