]> git.lizzy.rs Git - metalua.git/blob - src/compiler/mlp_stat.lua
compatibility with integral-variants of Lua VMs
[metalua.git] / src / compiler / mlp_stat.lua
1 ----------------------------------------------------------------------
2 -- Metalua:  $Id: mlp_stat.lua,v 1.7 2006/11/15 09:07:50 fab13n Exp $
3 --
4 -- Summary: metalua parser, statement/block parser. This is part of
5 --   the definition of module [mlp].
6 --
7 ----------------------------------------------------------------------
8 --
9 -- Copyright (c) 2006, Fabien Fleutot <metalua@gmail.com>.
10 --
11 -- This software is released under the MIT Licence, see licence.txt
12 -- for details.
13 --
14 ----------------------------------------------------------------------
15 --
16 ----------------------------------------------------------------------
17
18 --------------------------------------------------------------------------------
19 --
20 -- Exports API:
21 -- * [mlp.stat()]
22 -- * [mlp.block()]
23 -- * [mlp.for_header()]
24 --
25 --------------------------------------------------------------------------------
26
27 --------------------------------------------------------------------------------
28 -- eta-expansions to break circular dependency
29 --------------------------------------------------------------------------------
30 local expr      = function (lx) return mlp.expr     (lx) end
31 local func_val  = function (lx) return mlp.func_val (lx) end
32 local expr_list = function (lx) return mlp.expr_list(lx) end
33
34 module ("mlp", package.seeall)
35
36 --------------------------------------------------------------------------------
37 -- List of all keywords that indicate the end of a statement block. Users are
38 -- likely to extend this list when designing extensions.
39 --------------------------------------------------------------------------------
40
41
42 local block_terminators = { "else", "elseif", "end", "until", ")", "}", "]" }
43
44 -- FIXME: this must be handled from within GG!!!
45 function block_terminators:add(x) 
46    if type (x) == "table" then for _, y in ipairs(x) do self:add (y) end
47    else _G.table.insert (self, x) end
48 end
49
50 --------------------------------------------------------------------------------
51 -- list of statements, possibly followed by semicolons
52 --------------------------------------------------------------------------------
53 block = gg.list {
54    name        = "statements block",
55    terminators = block_terminators,
56    primary     = function (lx)
57       local x = stat (lx)
58       if lx:is_keyword (lx:peek(), ";") then lx:next() end
59       return x
60    end }
61
62 --------------------------------------------------------------------------------
63 -- Helper function for "return <expr_list>" parsing.
64 -- Called when parsing return statements
65 --------------------------------------------------------------------------------
66 local return_expr_list_parser = gg.list { 
67    expr, separators = ",", terminators = block_terminators }
68
69 --------------------------------------------------------------------------------
70 -- for header, between [for] and [do] (exclusive).
71 -- Return the `Forxxx{...} AST, without the body element (the last one).
72 --------------------------------------------------------------------------------
73 function for_header (lx)
74    local var = mlp.id (lx)
75    if lx:is_keyword (lx:peek(), "=") then 
76       -- Fornum: only 1 variable
77       lx:next() -- skip "="
78       local e = expr_list (lx)
79       assert (2 <= #e and #e <= 3, "2 or 3 values in a fornum")
80       return { tag="Fornum", var, unpack (e) }
81    else
82       -- Forin: there might be several vars
83       local a = lx:is_keyword (lx:next(), ",", "in")
84       if a=="in" then var_list = { var } else
85          -- several vars; first "," skipped, read other vars
86          var_list = gg.list{ 
87             primary = id, separators = ",", terminators = "in" } (lx)
88          _G.table.insert (var_list, 1, var) -- put back the first variable
89          lx:next() -- skip "in"
90       end
91       local e = expr_list (lx)
92       return { tag="Forin", var_list, e }
93    end
94 end
95
96 --------------------------------------------------------------------------------
97 -- Function def parser helper: id ( . id ) *
98 --------------------------------------------------------------------------------
99 local function fn_builder (list)
100    local r = list[1]
101    for i = 2, #list do r = { tag="Index", r, id2string(list[i]) } end
102    return r
103 end
104 local func_name = gg.list{ id, separators = ".", builder = fn_builder }
105
106 --------------------------------------------------------------------------------
107 -- Function def parser helper: ( : id )?
108 --------------------------------------------------------------------------------
109 local method_name = gg.onkeyword{ name = "method invocation", ":", id, 
110    transformers = { function(x) return x and id2string(x) end } }
111
112 --------------------------------------------------------------------------------
113 -- Function def builder
114 --------------------------------------------------------------------------------
115 local function funcdef_builder(x)
116    local name, method, func = x[1], x[2], x[3]
117    if method then 
118       name = { tag="Index", name, method }
119       _G.table.insert (func[1], 1, {tag="Id", "self"}) 
120    end
121    return { tag="Set", {name}, {func} } 
122 end 
123
124
125 --------------------------------------------------------------------------------
126 -- if statement builder
127 --------------------------------------------------------------------------------
128 local function if_builder (x)
129    local cb_pairs, else_block, r = x[1], x[2], {tag="If"}
130    for i=1,#cb_pairs do r[2*i-1]=cb_pairs[i][1]; r[2*i]=cb_pairs[i][2] end
131    if else_block then r[#r+1] = else_block end
132    return r
133 end 
134
135 --------------------------------------------------------------------------------
136 -- produce a list of (expr,block) pairs
137 --------------------------------------------------------------------------------
138 local elseifs_parser = gg.list {
139    gg.sequence { expr, "then", block },
140    separators  = "elseif",
141    terminators = { "else", "end" } }
142
143 --------------------------------------------------------------------------------
144 -- assignments and calls: statements that don't start with a keyword
145 --------------------------------------------------------------------------------
146 local function assign_or_call_stat_parser (lx)
147    local e = expr_list (lx)
148    local a = lx:is_keyword(lx:peek())
149    local op = a and stat.assignments[a]
150    if op then
151       --FIXME: check that [e] is a LHS
152       lx:next()
153       local v = expr_list (lx)
154       if type(op)=="string" then return { tag=op, e, v }
155       else return op (e, v) end
156    else 
157       assert (#e > 0)
158       if #e > 1 then 
159          gg.parse_error (lx, "comma is not a valid statement separator") end
160       if e[1].tag ~= "Call" and e[1].tag ~= "Invoke" then
161          gg.parse_error (lx, "This expression is of type '%s'; "..
162             "only function and method calls make valid statements", 
163             e[1].tag or "<list>")
164       end
165       return e[1]
166    end
167 end
168
169 local local_stat_parser = gg.multisequence{
170    -- local function <name> <func_val>
171    { "function", id, func_val, builder = 
172       function(x) return { tag="Localrec", { x[1] }, { x[2] } } end },
173    -- local <id_list> ( = <expr_list> )?
174    default = gg.sequence{ id_list, gg.onkeyword{ "=", expr_list },
175       builder = function(x) return {tag="Local", x[1], x[2] or { } } end } }
176
177 --------------------------------------------------------------------------------
178 -- statement
179 --------------------------------------------------------------------------------
180 stat = gg.multisequence { 
181    name="statement",
182    { "do", block, "end", builder = 
183       function (x) return { tag="Do", unpack (x[1]) } end },
184    { "for", for_header, "do", block, "end", builder = 
185       function (x) x[1][#x[1]+1] = x[2]; return x[1] end },
186    { "function", func_name, method_name, func_val, builder=funcdef_builder },
187    { "while", expr, "do", block, "end", builder = "While" },
188    { "repeat", block, "until", expr, builder = "Repeat" },
189    { "local", local_stat_parser, builder = fget (1) },
190    { "return", return_expr_list_parser, builder = fget (1, "Return") },
191    { "break", builder = function() return { tag="Break" } end },
192    { "-{", splice_content, "}", builder = fget(1) },
193    { "if", elseifs_parser, gg.onkeyword{ "else", block }, "end", 
194      builder = if_builder },
195    default = assign_or_call_stat_parser }
196
197 stat.assignments = {
198    ["="] = "Set" }
199
200 function stat.assignments:add(k, v) self[k] = v end