]> git.lizzy.rs Git - metalua.git/blob - src/compiler/mlp_meta.lua
Made AST definition changes, in the compiler and in the libs.
[metalua.git] / src / compiler / mlp_meta.lua
1 ----------------------------------------------------------------------
2 -- Metalua:  $Id: mlp_meta.lua,v 1.4 2006/11/15 09:07:50 fab13n Exp $
3 --
4 -- Summary: Meta-operations: AST quasi-quoting and splicing
5 --
6 ----------------------------------------------------------------------
7 --
8 -- Copyright (c) 2006, Fabien Fleutot <metalua@gmail.com>.
9 --
10 -- This software is released under the MIT Licence, see licence.txt
11 -- for details.
12 --
13 ----------------------------------------------------------------------
14 -- History:
15 -- $Log: mlp_meta.lua,v $
16 -- Revision 1.4  2006/11/15 09:07:50  fab13n
17 -- debugged meta operators.
18 --
19 -- Revision 1.2  2006/11/09 09:39:57  fab13n
20 -- some cleanup
21 --
22 -- Revision 1.1  2006/11/07 21:29:02  fab13n
23 -- improved quasi-quoting
24 --
25 -- Revision 1.3  2006/11/07 04:38:00  fab13n
26 -- first bootstrapping version.
27 --
28 -- Revision 1.2  2006/11/05 15:08:34  fab13n
29 -- updated code generation, to be compliant with 5.1
30 --
31 ----------------------------------------------------------------------
32
33
34 --------------------------------------------------------------------------------
35 --
36 -- Exported API:
37 -- * [mlp.splice_content()]
38 -- * [mlp.quote_content()]
39 --
40 --------------------------------------------------------------------------------
41
42 --require "compile"
43 --require "ldump"
44
45 module ("mlp", package.seeall)
46
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
50 -- an AST).
51 --------------------------------------------------------------------------------
52
53 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."
57    local result = f()   
58    --printf " [SPLICE] Splice Evaled."
59    return result
60 end
61
62 --------------------------------------------------------------------------------
63 -- Going from an AST to an AST representing that AST
64 -- the only key being lifted in this version is ["tag"]
65 --------------------------------------------------------------------------------
66 function quote (t)
67    --print("QUOTING:", _G.table.tostring(t, 60))
68    local cases = { }
69    function cases.table (t)
70       local mt = { tag = "Table" }
71       if t.tag == "Splice" then
72          assert (#t==1, "Invalid splice")
73          return t[1]
74       elseif t.tag then
75          _G.table.insert (mt, { tag = "Pair", quote "tag", quote (t.tag) })
76       end
77       for _, v in ipairs (t) do
78          _G.table.insert (mt, quote(v))
79       end
80       return mt
81    end
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)
85 end
86
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 --------------------------------------------------------------------------------
93 in_a_quote = false
94
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
101       local a = lx:next()
102       lx:next() -- skip ":"
103       assert (a.tag=="Id", "Invalid splice parser name")
104       parser_name = a[1]
105 --       printf("this splice is a %s", parser_name)
106 --    else
107 --       printf("no splice specifier:\npeek(1)")
108 --       _G.table.print(lx:peek(1))
109 --       printf("peek(2)")
110 --       _G.table.print(lx:peek(2))
111    end
112    local ast = mlp[parser_name](lx)
113    if in_a_quote then
114       --printf("SPLICE_IN_QUOTE:\n%s", _G.table.tostring(ast, "nohash", 60))
115       return { tag="Splice", ast }
116    else
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))
122       return splice (ast)
123    end
124 end
125
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]]
133       lx:next()
134    end
135    --assert(not in_a_quote, "Nested quotes not handled yet")
136    local prev_iq = in_a_quote
137    in_a_quote = true
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))
144    in_a_quote = prev_iq
145    return q_content
146 end
147