lua-users home
lua-l archive

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


On Sat, Sep 10, 2011 at 1:07 AM, David Manura <dm.lua@math2.org> wrote:
> A code generation technique like in [2] could make that quite efficient:

And this is another case where it would be nice if Lua syntax allowed
string interpolation [3]:

  x = eval "
    ((${h} & 0x0F) << 0x12) |
    ((${h} & 0xF0) << 0xA) |
    ((${w} & 0x0F) << 8) |
     (${w} & 0xF0 )
  "

My preference for how Lua would handle string interpolation [4] would
be for the lexer to translate `eval" ${x} & ${y} "` into something
like `eval(" ${x} & ${y} ", x, y)`.  You could then implement eval to
compile " ${x} & ${y} " into a function and cache it using the string
as a key, so subsequent calls would be fast.  One alternative at the
moment is IMO not very readable because the brackets don't appear to
nest, and it is not as efficient to do the cache lookup when the
string is broken into smaller ones:

 x = eval([[
    ((]], h, [[ & 0x0F) << 0x12) |
    ((]], h, [[ & 0xF0) << 0xA) |
    ((]], w, [[ & 0x0F) << 8) |
     (]], w, [[ & 0xF0 )
  ]])

[3] http://lua-users.org/wiki/StringInterpolation
[4] http://lua-users.org/lists/lua-l/2009-05/msg00248.html