lua-users home
lua-l archive

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


Metalua is 5.1-only because it generates Lua 5.1 bytecode. It's now packaged in two Lua Rocks actually, the parser and the compiler, and the parser is 5.2 compatible.

However, Metalua can also produce source code back, so you can make a sequence Metalua->Lua sources->Lua 5.2 bytecode. With a caveat: there's a special instruction "`Stat{ }" that's pretty handy in Metalua macros but unsupported by Lua 5.1 and 5.2 ; any macro using it would have to be rewritten. Another, easily fixed issue is that when Metalua generates fresh local var names, it generates invalid ones to make sure that it won't shadow user variables. This is trivially fixed in metalua.grammar.generator / gensym() if it bothers you.

Proof-of-concept:
$ lua51
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> src = "" constant = -{ `Number{ math.pi } }; print(constant) ]]
> compiler = require 'metalua.compiler'.new()
> ast = compiler:src_to_ast(src)
> src2 = compiler:ast_to_src(ast)
> print(src2)
constant = 3.1415926535898
print (constant)
> filename = os.tmpname()                 
> luac52 = io.popen("luac52 -o "..filename.." -", "w")
> luac52:write(src2)
> luac52:close()
> os.execute("lua52 "..filename)
3.1415926535898

Notice that depending on how complex your metaprogramming needs are, you might be better-off with a dumb text-based preprocessor.