A know this is a syntactically valid Lua expression.Can someone give me a written parse for it?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

A know this is a syntactically valid Lua expression.Can someone give me a written parse for it?

Carl Glassberg
Hello:

This is an example of a syntactically valid legal expression in Lua,
taken from the PEGTL github grammar for Lua, by Dr. Colin Hirsch and
Daniel Frey (I hope they don't mind me repeating it here):
   ( a * b ).c()[ d ].e:f()

 Other than being a function (method?) call to f, what is the meaning
of the parse of it, step by step, from left to right?

Thanks for your assistance!

Carl

Reply | Threaded
Open this post in threaded view
|

Re: A know this is a syntactically valid Lua expression.Can someone give me a written parse for it?

Coda Highland
On Mon, Jul 29, 2019 at 5:33 PM Carl Glassberg <[hidden email]> wrote:
Hello:

This is an example of a syntactically valid legal expression in Lua,
taken from the PEGTL github grammar for Lua, by Dr. Colin Hirsch and
Daniel Frey (I hope they don't mind me repeating it here):
   ( a * b ).c()[ d ].e:f()

 Other than being a function (method?) call to f, what is the meaning
of the parse of it, step by step, from left to right?

Thanks for your assistance!

Carl

The names a, b, and d refer to variables.

Multiply the value in the variable a with the value in the variable b. Call this T1.

Treating T1 as an object, extract the entry named "c". Call this T2.

Invoke T2 as a function with no parameters. Call the result T3.

Treating T3 as an object, extract the entry with a key equal to the value found in the variable d. Call this T4.

Treating T4 as an object, extract the entry named "e". Call this T5.

Treating T5 as an object, extract the entry named "f". Call this T6.

Invoke T6 as a function, passing T5 as the only parameter. (By convention, this parameter should be named "self".)

/s/ Adam
v
Reply | Threaded
Open this post in threaded view
|

Re: A know this is a syntactically valid Lua expression.Can someone give me a written parse for it?

v
In reply to this post by Carl Glassberg
On Mon, 2019-07-29 at 15:32 -0700, Carl Glassberg wrote:
> Hello:
>
> This is an example of a syntactically valid legal expression in Lua,
> taken from the PEGTL github grammar for Lua, by Dr. Colin Hirsch and
> Daniel Frey (I hope they don't mind me repeating it here):
>    ( a * b ).c()[ d ].e:f()

( a * b ) Multiply variable `a` by variable `b`.
.c        Take field named `'c'` (string "c" without quotes) from
result of previous expression. Make sense if it was overloaded.
()        Call that field.
[ d ]     Take field at index stored in variable `d` from result of
last call.
.e        Same as `.c` before, take field named `'e'` from result of
last indexing.
:f()      Take entity at index `f` and call it with single argument
being result of last expression.

--
v <[hidden email]>


Reply | Threaded
Open this post in threaded view
|

Re: A know this is a syntactically valid Lua expression.Can someone give me a written parse for it?

Luiz Henrique de Figueiredo
In reply to this post by Carl Glassberg
>    ( a * b ).c()[ d ].e:f()
>
>  Other than being a function (method?) call to f, what is the meaning
> of the parse of it, step by step, from left to right?

Here is the output of luac -l -p on that code (Lua 5.1, whose opcodes
has easier to read):

1 [1] GETGLOBAL 0 -1 ; a
2 [1] GETGLOBAL 1 -2 ; b
3 [1] MUL       0 0 1
4 [1] GETTABLE 0 0 -3 ; "c"
5 [1] CALL     0 1 2
6 [1] GETGLOBAL 1 -4 ; d
7 [1] GETTABLE 0 0 1
8 [1] GETTABLE 0 0 -5 ; "e"
9 [1] SELF     0 0 -6 ; "f"
10 [1] CALL     0 2 1

Reply | Threaded
Open this post in threaded view
|

Re: A know this is a syntactically valid Lua expression.Can someone give me a written parse for it?

Abhijit Nandy
This flexible syntax support is one of the reasons I just can't get enough Lua, thanks to the writers of the language :D

I once tried to make a C++ like syntax for declaring classes: https://github.com/abhijitnandy2011/DSLsBasedOnLua/blob/master/cpp/cpp.lua#L186

Got stuck at trying to realize a syntax like:
int m
It had to be:
int 'm'

Cheers!
Abhijit

On Tue, Jul 30, 2019 at 6:21 AM Luiz Henrique de Figueiredo <[hidden email]> wrote:
>    ( a * b ).c()[ d ].e:f()
>
>  Other than being a function (method?) call to f, what is the meaning
> of the parse of it, step by step, from left to right?

Here is the output of luac -l -p on that code (Lua 5.1, whose opcodes
has easier to read):

1 [1] GETGLOBAL 0 -1 ; a
2 [1] GETGLOBAL 1 -2 ; b
3 [1] MUL       0 0 1
4 [1] GETTABLE 0 0 -3 ; "c"
5 [1] CALL     0 1 2
6 [1] GETGLOBAL 1 -4 ; d
7 [1] GETTABLE 0 0 1
8 [1] GETTABLE 0 0 -5 ; "e"
9 [1] SELF     0 0 -6 ; "f"
10 [1] CALL     0 2 1