[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [ANN] LuaMacro 2 - a lexical macro preprocessor for Lua
- From: steve donovan <steve.j.donovan@...>
- Date: Sun, 1 May 2011 17:58:57 +0200
Hi all,
The LuaMacro project has been around for a while [1] and up to now has
depended on the token filter patch. So it needed a modified Lua
executable. Recently when thinking about using macros for emitting
optimal code for LuaJIT I realized that here was _another executable
to patch_. Fortunately, I wandered away from this abyss and this new,
rewritten version[2] uses Peter Odding's LPeg Lua lexer. It now acts
more like a traditional preprocessor, although the driver program luam
will load and execute the code by default. It is certainly now a good
deal easier to debug macros, since one can see exactly what the
preprocessor has generated (luam has a -d for 'dump' option)
It can do traditional C-preprocessor things like
def_ assert_(cond) assert(cond,_STR_(cond))
except that such macros are now _lexically scoped_
The real power comes from executing arbitrary Lua code at the point of
substitution.
macro.define('forall',function(get)
local var = get:name()
local t,v = get:next() -- will be 'in'
local rest = tostring(get:upto 'do')
return ('for _,%s in ipairs(%s) do'):format(var,rest)
end)
which turns 'forall x in {10,20,30} do' into 'for _,x in ipairs({10,20,30}) do'.
Do with it what you will! Personally I take a neutral stance in the
macro debate: there are times (such as designing DSLs for difficult
customers) where a little extra sugar goes a long way.[3]
steve d.
[1] http://luaforge.net/projects/luamacro/
[2] https://github.com/stevedonovan/LuaMacro
[3] including short lambda forms like \x(x+1) - I couldn't resist the
temptation.