1 ----------------------------------------------------------------------
2 -- Metalua: $Id: mlp_meta.lua,v 1.4 2006/11/15 09:07:50 fab13n Exp $
4 -- Summary: Meta-operations: AST quasi-quoting and splicing
6 ----------------------------------------------------------------------
8 -- Copyright (c) 2006, Fabien Fleutot <metalua@gmail.com>.
10 -- This software is released under the MIT Licence, see licence.txt
13 ----------------------------------------------------------------------
15 -- $Log: mlp_meta.lua,v $
16 -- Revision 1.4 2006/11/15 09:07:50 fab13n
17 -- debugged meta operators.
19 -- Revision 1.2 2006/11/09 09:39:57 fab13n
22 -- Revision 1.1 2006/11/07 21:29:02 fab13n
23 -- improved quasi-quoting
25 -- Revision 1.3 2006/11/07 04:38:00 fab13n
26 -- first bootstrapping version.
28 -- Revision 1.2 2006/11/05 15:08:34 fab13n
29 -- updated code generation, to be compliant with 5.1
31 ----------------------------------------------------------------------
34 --------------------------------------------------------------------------------
37 -- * [mlp.splice_content()]
38 -- * [mlp.quote_content()]
40 --------------------------------------------------------------------------------
45 module ("mlp", package.seeall)
47 --------------------------------------------------------------------------------
48 -- External splicing: compile an AST into a chunk, load and evaluate
49 -- that chunk, and replace the chunk by its result (which must also be
51 --------------------------------------------------------------------------------
53 local function splice (ast)
54 --printf(" [SPLICE] Ready to compile:\n%s", _G.table.tostring (ast, "nohash", 60))
55 local f = mlc.function_of_ast(ast)
56 --printf " [SPLICE] Splice Compiled."
58 --printf " [SPLICE] Splice Evaled."
62 --------------------------------------------------------------------------------
63 -- Going from an AST to an AST representing that AST
64 -- the only key being lifted in this version is ["tag"]
65 --------------------------------------------------------------------------------
67 --print("QUOTING:", _G.table.tostring(t, 60))
69 function cases.table (t)
70 local mt = { tag = "Table" }
71 if t.tag == "Splice" then
72 assert (#t==1, "Invalid splice")
75 _G.table.insert (mt, { tag = "Key", quote "tag", quote (t.tag) })
77 for _, v in ipairs (t) do
78 _G.table.insert (mt, quote(v))
82 function cases.number (t) return { tag = "Number", t } end
83 function cases.string (t) return { tag = "String", t } end
84 return cases [ type (t) ] (t)
87 --------------------------------------------------------------------------------
88 -- when this variable is false, code inside [-{...}] is compiled and
89 -- avaluated immediately. When it's true (supposedly when we're
90 -- parsing data inside a quasiquote), [-{foo}] is replaced by
91 -- [`Splice{foo}], which will be unpacked by [quote()].
92 --------------------------------------------------------------------------------
95 --------------------------------------------------------------------------------
96 -- Parse the inside of a "-{ ... }"
97 --------------------------------------------------------------------------------
98 function splice_content (lx)
99 local parser_name = "expr"
100 if lx:is_keyword (lx:peek(2), ":") then
102 lx:next() -- skip ":"
103 assert (a.tag=="Id", "Invalid splice parser name")
105 -- printf("this splice is a %s", parser_name)
107 -- printf("no splice specifier:\npeek(1)")
108 -- _G.table.print(lx:peek(1))
110 -- _G.table.print(lx:peek(2))
112 local ast = mlp[parser_name](lx)
114 --printf("SPLICE_IN_QUOTE:\n%s", _G.table.tostring(ast, "nohash", 60))
115 return { tag="Splice", ast }
117 if parser_name == "expr" then ast = { { tag="Return", ast } }
118 elseif parser_name == "stat" then ast = { ast }
119 elseif parser_name ~= "block" then
120 error ("splice content must be an expr, stat or block") end
121 --printf("EXEC THIS SPLICE:\n%s", _G.table.tostring(ast, "nohash", 60))
126 --------------------------------------------------------------------------------
127 -- Parse the inside of a "+{ ... }"
128 --------------------------------------------------------------------------------
129 function quote_content (lx)
130 local parser = mlp.expr
131 if lx:is_keyword (lx:peek(2), ":") then
132 parser = mlp[id(lx)[1]]
135 --assert(not in_a_quote, "Nested quotes not handled yet")
136 local prev_iq = in_a_quote
138 --print("IN_A_QUOTE")
139 local content = parser (lx)
140 local q_content = quote (content)
141 --printf("/IN_A_QUOTE:\n* content=\n%s\n* q_content=\n%s\n",
142 -- _G.table.tostring(content, "nohash", 60),
143 -- _G.table.tostring(q_content, "nohash", 60))