]> git.lizzy.rs Git - metalua.git/blob - src/compiler/mlp_meta.lua
60a8d457704dd4f0b24ba8c824593b5d3f3937ac
[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, '=splice')
56    --printf " [SPLICE] Splice Compiled."
57    --local status, result = pcall(f)
58    --printf " [SPLICE] Splice Evaled."
59    --if not status then print 'ERROR IN SPLICE' end
60    local result=f()
61    return result
62 end
63
64 --------------------------------------------------------------------------------
65 -- Going from an AST to an AST representing that AST
66 -- the only key being lifted in this version is ["tag"]
67 --------------------------------------------------------------------------------
68 function quote (t)
69    --print("QUOTING:", _G.table.tostring(t, 60))
70    local cases = { }
71    function cases.table (t)
72       local mt = { tag = "Table" }
73       --_G.table.insert (mt, { tag = "Pair", quote "quote", { tag = "True" } })
74       if t.tag == "Splice" then
75          assert (#t==1, "Invalid splice")
76          local sp = t[1]
77          return sp
78       elseif t.tag then
79          _G.table.insert (mt, { tag = "Pair", quote "tag", quote (t.tag) })
80       end
81       for _, v in ipairs (t) do
82          _G.table.insert (mt, quote(v))
83       end
84       return mt
85    end
86    function cases.number (t) return { tag = "Number", t, quote = true } end
87    function cases.string (t) return { tag = "String", t, quote = true } end
88    return cases [ type (t) ] (t)
89 end
90
91 --------------------------------------------------------------------------------
92 -- when this variable is false, code inside [-{...}] is compiled and
93 -- avaluated immediately. When it's true (supposedly when we're
94 -- parsing data inside a quasiquote), [-{foo}] is replaced by
95 -- [`Splice{foo}], which will be unpacked by [quote()].
96 --------------------------------------------------------------------------------
97 in_a_quote = false
98
99 --------------------------------------------------------------------------------
100 -- Parse the inside of a "-{ ... }"
101 --------------------------------------------------------------------------------
102 function splice_content (lx)
103    local parser_name = "expr"
104    if lx:is_keyword (lx:peek(2), ":") then
105       local a = lx:next()
106       lx:next() -- skip ":"
107       assert (a.tag=="Id", "Invalid splice parser name")
108       parser_name = a[1]
109 --       printf("this splice is a %s", parser_name)
110 --    else
111 --       printf("no splice specifier:\npeek(1)")
112 --       _G.table.print(lx:peek(1))
113 --       printf("peek(2)")
114 --       _G.table.print(lx:peek(2))
115    end
116    local ast = mlp[parser_name](lx)
117    if in_a_quote then
118       --printf("SPLICE_IN_QUOTE:\n%s", _G.table.tostring(ast, "nohash", 60))
119       return { tag="Splice", ast }
120    else
121       if parser_name == "expr" then ast = { { tag="Return", ast } }
122       elseif parser_name == "stat"  then ast = { ast }
123       elseif parser_name ~= "block" then
124          error ("splice content must be an expr, stat or block") end
125       --printf("EXEC THIS SPLICE:\n%s", _G.table.tostring(ast, "nohash", 60))
126       return splice (ast)
127    end
128 end
129
130 --------------------------------------------------------------------------------
131 -- Parse the inside of a "+{ ... }"
132 --------------------------------------------------------------------------------
133 function quote_content (lx)
134    local parser 
135    if lx:is_keyword (lx:peek(1), ":") then -- +{:parser: content }
136       lx:next()
137       errory "NOT IMPLEMENTED"
138    elseif lx:is_keyword (lx:peek(2), ":") then -- +{parser: content }
139       parser = mlp[id(lx)[1]]
140       lx:next()
141    else -- +{ content }
142       parser = mlp.expr
143    end
144
145    --assert(not in_a_quote, "Nested quotes not handled yet")
146    local prev_iq = in_a_quote
147    in_a_quote = true
148    --print("IN_A_QUOTE")
149    local content = parser (lx)
150    local q_content = quote (content)
151 --     printf("/IN_A_QUOTE:\n* content=\n%s\n* q_content=\n%s\n",
152 --            _G.table.tostring(content, "nohash", 60),
153 --            _G.table.tostring(q_content, "nohash", 60))
154    in_a_quote = prev_iq
155    return q_content
156 end
157