lua-users home
lua-l archive

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

A first alpha release of Metalua 0.1 is available at Luaforge:

Metalua is a metaprogramming framework over Lua, inspired among others by Template Haskell []. It consists of:

- the definition of an AST (abstract syntax tree) structure, which can represent code in a high-level way. AST are roughly isomorphic to Lisp s-exp, i.e. they can be easily walked through, analyzed, globally modified, combined, generated...
- a dynamically extensible parser, converting ASCII source code to AST;
- a compiler, based on Yueliang [], which compiles AST into Lua 5.1 bytecode;
- some meta-operators to glue all that, which allow at compile-time to turn AST into source code, source code into AST, and to execute arbitrary code during compilation (typically to build AST).

With it, you can:

- plug syntax extensions on-the-fly in the language (grammar extensions as well as reader macros)
- add new semantic constructions, typically to build embedded DSLs. As an example, ML-like pattern matching will hopefully soon be shipped with metalua. CPS transformation, a prolog inference engine, or SQL embedding a la LINQ would be other classic examples of what can be done with it.
- build code analysis and refactoring tools: stuffs like alpha-renaming, code cleaning, code redundancy checks, auto-documentation, lint-like inspection, etc.
- do most of other compile-time, code-analysis oriented metaprogramming stuffs you could think of.

For instance, adding a "foo ? iftrue, iffalse" syntax extension is done by this in your source file (the magic happens with +{...} and -{...} meta-operators):

---[BEGIN CODE]---
-{ mlp.expr.add_postfix_sequence { "?", mlp.expr, ",", mlp.expr,
      functor = |x| +{ function(x) if -{x[1]} then return -{x[2]} else return -{x[3]} end end () } } }

-- test it:
lang = "en"; print((lang=="fr") ? "Bonjour", "Hello")
lang = "fr"; print((lang=="fr") ? "Bonjour", "Hello")
---[END CODE]---

(This compiles "foo ? iftrue, iffalse" into "(function(x) if foo then return iftrue else return iffalse end end)()". Maybe not the most efficient encoding, but probaly the simplest to implement)

When compared with other metaprogramming tools such as preprocessors or token filters, the main advantage of Metalua is that working on AST is much more powerful than working on an unstructured stream of tokens or characters. The obvious counterpart is a steeper learning curve at the beginning. (Another advantage is that it's 100% Lua 5.1compatible source code, so there is no need for any patch nor C compiler, it only needs a Lua 5.1 bytecode interpreter).

The current main limit of Metalua is the lack of proper doc. There is only an extremely preliminary (and already partially obsolete) doc here:

However, I hope to fix this lack of doc issue, and to provide interesting extension samples ASAP (the very few and very simple examples included in the doc could actually have been written with token filters).

Meanwhile, if you feel like playing with what's there already, you're welcome to do so and ask questions.


Fabien Fleutot
+-- France:
| 11 rue Ferrus
| 75014 Paris
| home:   +33 1 53 80 47 44
| office: +33 1 46 29 40 39
| mobile: +33 6 28 06 09 97