1 ----------------------------------------------------------------------
2 -- Metalua: $Id: mlp_expr.lua,v 1.7 2006/11/15 09:07:50 fab13n Exp $
4 -- Summary: metalua parser, expression parser. This is part of the
5 -- definition of module [mlp].
7 ----------------------------------------------------------------------
9 -- Copyright (c) 2006, Fabien Fleutot <metalua@gmail.com>.
11 -- This software is released under the MIT Licence, see licence.txt
14 ----------------------------------------------------------------------
16 -- $Log: mlp_expr.lua,v $
17 -- Revision 1.7 2006/11/15 09:07:50 fab13n
18 -- debugged meta operators.
19 -- Added command line options handling.
21 -- Revision 1.6 2006/11/10 02:11:17 fab13n
22 -- compiler faithfulness to 5.1 improved
24 -- mlp.expr refactored
26 -- Revision 1.5 2006/11/09 09:39:57 fab13n
29 -- Revision 1.4 2006/11/07 21:29:02 fab13n
30 -- improved quasi-quoting
32 -- Revision 1.3 2006/11/07 04:38:00 fab13n
33 -- first bootstrapping version.
35 -- Revision 1.2 2006/11/05 15:08:34 fab13n
36 -- updated code generation, to be compliant with 5.1
38 ----------------------------------------------------------------------
40 --------------------------------------------------------------------------------
44 -- * [mlp.expr_list()]
47 --------------------------------------------------------------------------------
54 --------------------------------------------------------------------------------
55 -- These function wrappers (eta-expansions ctually) are just here to break
56 -- some circular dependencies between mlp_xxx.lua files.
57 --------------------------------------------------------------------------------
58 local function _expr (lx) return mlp.expr (lx) end
59 local function _table_content (lx) return mlp.table_content (lx) end
60 local function block (lx) return mlp.block (lx) end
61 local function stat (lx) return mlp.stat (lx) end
63 module ("mlp", package.seeall)
65 --------------------------------------------------------------------------------
66 -- Non-empty expression list. Actually, this isn't used here, but that's
67 -- handy to give to users.
68 --------------------------------------------------------------------------------
69 expr_list = gg.list{ _expr, separators = "," }
71 --------------------------------------------------------------------------------
72 -- Helpers for function applications / method applications
73 --------------------------------------------------------------------------------
74 local func_args_content = gg.list {
75 name = "function arguments",
76 _expr, separators = ",", terminators = ")" }
78 -- Used to parse methods
79 local method_args = gg.multisequence{
80 name = "function argument(s)",
81 { "{", table_content, "}" },
82 { "(", func_args_content, ")", builder = fget(1) },
83 default = function(lx) local r = opt_string(lx); return r and {r} or { } end }
85 --------------------------------------------------------------------------------
86 -- [func_val] parses a function, from opening parameters parenthese to
87 -- "end" keyword included. Used for anonymous functions as well as
88 -- function declaration statements (both local and global).
90 -- It's wrapped in a [_func_val] eta expansion, so that when expr
91 -- parser uses the latter, they will notice updates of [func_val]
93 --------------------------------------------------------------------------------
94 func_params_content = gg.list{ name="function parameters",
95 gg.multisequence{ { "...", builder = "Dots" }, default = id },
96 separators = ",", terminators = {")", "|"} }
98 local _func_params_content = function (lx) return func_params_content(lx) end
100 func_val = gg.sequence { name="function body",
101 "(", func_params_content, ")", block, "end", builder = "Function" }
103 local _func_val = function (lx) return func_val(lx) end
105 --------------------------------------------------------------------------------
106 -- Default parser for primary expressions
107 --------------------------------------------------------------------------------
108 local function id_or_literal (lx)
110 if a.tag~="Id" and a.tag~="String" and a.tag~="Number" then
111 gg.parse_error (lx, "Unexpected expr token %s", _G.table.tostring(a))
117 --------------------------------------------------------------------------------
118 -- Builder generator for operators. Wouldn't be worth it if "|x|" notation
119 -- were allowed, but then lua 5.1 wouldn't compile it
120 --------------------------------------------------------------------------------
122 -- opf1 = |op| |_,a| `Op{ op, a }
123 local function opf1 (op) return
124 function (_,a) return { tag="Op", op, a } end end
126 -- opf2 = |op| |a,_,b| `Op{ op, a, b }
127 local function opf2 (op) return
128 function (a,_,b) return { tag="Op", op, a, b } end end
130 -- opf2r = |op| |a,_,b| `Op{ op, b, a } -- (args reversed)
131 local function opf2r (op) return
132 function (a,_,b) return { tag="Op", op, b, a } end end
134 local function op_ne(a, _, b)
135 -- The first version guarantees to return the same code as Lua,
136 -- but it relies on the non-standard 'ne' operator, which has been
137 -- suppressed from the official AST grammar (although still supported
138 -- in practice by the compiler).
139 -- return { tag="Op", "ne", a, b }
140 return { tag="Op", "not", { tag="Op", "eq", a, b, lineinfo= {
141 first = a.lineinfo.first, last = b.lineinfo.last } } }
145 --------------------------------------------------------------------------------
147 -- complete expression
149 --------------------------------------------------------------------------------
151 -- FIXME: set line number. In [expr] transformers probably
153 expr = gg.expr { name = "expression",
155 primary = gg.multisequence{ name="expr primary",
156 { "(", _expr, ")", builder = "Paren" },
157 { "function", _func_val, builder = fget(1) },
158 { "-{", splice_content, "}", builder = fget(1) },
159 { "+{", quote_content, "}", builder = fget(1) },
160 { "nil", builder = "Nil" },
161 { "true", builder = "True" },
162 { "false", builder = "False" },
163 { "...", builder = "Dots" },
165 default = id_or_literal },
167 infix = { name="expr infix op",
168 { "+", prec = 60, builder = opf2 "add" },
169 { "-", prec = 60, builder = opf2 "sub" },
170 { "*", prec = 70, builder = opf2 "mul" },
171 { "/", prec = 70, builder = opf2 "div" },
172 { "%", prec = 70, builder = opf2 "mod" },
173 { "^", prec = 90, builder = opf2 "pow", assoc = "right" },
174 { "..", prec = 40, builder = opf2 "concat", assoc = "right" },
175 { "==", prec = 30, builder = opf2 "eq" },
176 { "~=", prec = 30, builder = op_ne },
177 { "<", prec = 30, builder = opf2 "lt" },
178 { "<=", prec = 30, builder = opf2 "le" },
179 { ">", prec = 30, builder = opf2r "lt" },
180 { ">=", prec = 30, builder = opf2r "le" },
181 { "and",prec = 20, builder = opf2 "and" },
182 { "or", prec = 10, builder = opf2 "or" } },
184 prefix = { name="expr prefix op",
185 { "not", prec = 80, builder = opf1 "not" },
186 { "#", prec = 80, builder = opf1 "len" },
187 { "-", prec = 80, builder = opf1 "unm" } },
189 suffix = { name="expr suffix op",
190 { "[", _expr, "]", builder = function (tab, idx)
191 return {tag="Index", tab, idx[1]} end},
192 { ".", id, builder = function (tab, field)
193 return {tag="Index", tab, id2string(field[1])} end },
194 { "(", func_args_content, ")", builder = function(f, args)
195 return {tag="Call", f, unpack(args[1])} end },
196 { "{", _table_content, "}", builder = function (f, arg)
197 return {tag="Call", f, arg[1]} end},
198 { ":", id, method_args, builder = function (obj, post)
199 return {tag="Invoke", obj, id2string(post[1]), unpack(post[2])} end},
200 { "+{", quote_content, "}", builder = function (f, arg)
201 return {tag="Call", f, arg[1] } end },
202 default = { name="opt_string_arg", parse = mlp.opt_string, builder = function(f, arg)
203 return {tag="Call", f, arg } end } } }