[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [announce] Metalua: Lua + full macros
- From: Karel Tuma <ktuma@...>
- Date: Fri, 10 Nov 2006 16:18:09 +0100
hi,
so finally someone managed yueliang to do something meaningful!
definitively looks promising! .. i always wished for ASTs in lua
of some sort but was too lazy to dig into yueliang that deep.
(not to mention it's 5.0 based iirc)
and now we've entirely new compiler with rather cool features.
oh wait. it's inspired by haskell, isn't it that language useful only
for writing compilers of itself or it was ocaml?
isn't some conspiracy going on here?
j/k.
maybe some day, lua will be mere microVM where metalua will do the
rest. size is the only real rationale for lexer/parser being in
C, basically without a way for user to tap into it (TF aside...),
performance of lua implementation will be like ~20x slower, but i dont
know anyone who'd notice...
let's see:
------------------------------------------------------------------
kt@brm:metalua$ cat compact.lua
local done = {}
function do_compact(name)
for l in io.lines(name) do
local req = string.match(l, "^require \"([^\"]*)\"")
if req then
if not done[req] then
print("do")
io.stderr:write("concenating"..req.."\n")
do_compact(req..".lua")
done[req] = 1
print("end")
end
else
print(l)
end
end
end
do_compact("mlc")
kt@brm:metalua$ lua compact.lua > meta.lua
concenating strict
...
concenating disp
kt@brm:metalua$ luac -s meta.lua
kt@brm:metalua$ ls -lah luac.out
-rw-r--r-- 1 kt kt 86K Nov 10 16:11 luac.out
kt@brm:metalua$ wow, far less than expected
bash: wow,: command not found
kt@brm:metalua$ gzip -9 luac.out
kt@brm:metalua$ ls -lah luac.out.gz
-rw-r--r-- 1 kt kt 33K Nov 10 16:11 luac.out.gz
------------------------------------------------------------------
maybe this could be rationale for adding gzip inflate support (cca 2k
compiled) to lua_undump? with compression metalua closes to stock lua
pretty well...
... and now off to go to play with it some more ...
you've my deep respect, good job.
//kt
On Fri, Nov 10, 2006 at 03:14:00PM +0100, Fabien wrote:
> A first alpha release of Metalua 0.1 is available at Luaforge:
>
> [1]http://luaforge.net/frs/download.php/1850/metalua.zip
>
> Metalua is a metaprogramming framework over Lua, inspired among others by
> Template Haskell [ [2]http://www.haskell.org/th]. 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 [
> [3]http://luaforge.net/projects/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:
>
> [4]http://luaforge.net/docman/view.php/227/158/tutorial.pdf
>
> 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.
>
> --
> 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
>
> References
>
> Visible links
> 1. http://luaforge.net/frs/download.php/1850/metalua.zip
> 2. http://www.haskell.org/th
> 3. http://luaforge.net/projects/yueliang
> 4. http://luaforge.net/docman/view.php/227/158/tutorial.pdf