[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to (slightly) modify the Lua parser
- From: KHMan <keinhong@...>
- Date: Tue, 13 Oct 2009 00:36:41 +0800
steve donovan wrote:
On Mon, Oct 12, 2009 at 6:08 PM, Francesco Abbate
<francesco.bbt@gmail.com> wrote:
|x| sin(x)/x
or
|x, y| sin(x) * cos(y)
You're not the first to want this ;)
http://lua-users.org/wiki/ShortAnonymousFunctions
No direct clues there to patch the parser, however.
If you trace the call tree of the recursive descent parser in
lparser.c (starting from luaY_parser), I think it is do-able, the
call tree is pretty straightforward.
For function definitions:
statement() -> funcstat() -> funcname() -> body()
body() is where the parameter list is parsed.
So for |x,y|, you need to check for the first '|', then call out
to its own 'stat' (statement handler) function, where you follow
the style of the parameter parsing in body(), but check for '|'
instead of a closing ')'.
For the body, which is a expression (or can it be a list of
expressions?), you can look at the flow of explist1(). The
expression handling stuff is pretty compact code, so take your
time to follow the call tree.
HTH,
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia