Lua 5.1: Question on how the Lua parser evaluates bodies

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

Lua 5.1: Question on how the Lua parser evaluates bodies

Alexander Walz
Hello,

I noticed that the Lua 5.1 parser seems to call the body() procedure
multiple times when parsing statements that are part of a Lua body, for
example statements in a `then`, `else` or `do` body.

These mutliple calls only occur when entering statements in interactive
mode, i.e. when issued from the command-line (getS function).

Is it possible to count and query this count of these frequent calls ?

I am asking this because I am currently trying to implement constants in
a Lua 5.1 fork.

Yours,

Alex
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1: Question on how the Lua parser evaluates bodies

Luiz Henrique de Figueiredo
> I noticed that the Lua 5.1 parser seems to call the body() procedure
> multiple times when parsing statements that are part of a Lua body, for
> example statements in a `then`, `else` or `do` body.

body()  is for parsing function bodies:
  /* body ->  `(' parlist `)' chunk END */

Perhaps you mean block():
  /* block -> chunk */
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1: Question on how the Lua parser evaluates bodies

Alexander Walz
Hello Luiz Henrique,

you are right with block() , I am sorry.

I get this in Lua 5.1:

C:\agena\srclua>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
 > do
Entering block
Left block
 >>    print("Lua")
Entering block
Left block
 >> end
Entering block
Left block
Lua

Alex

Luiz Henrique de Figueiredo wrote:
>> I noticed that the Lua 5.1 parser seems to call the body() procedure
>> multiple times when parsing statements that are part of a Lua body, for
>> example statements in a `then`, `else` or `do` body.
> body()  is for parsing function bodies:
>    /* body ->  `(' parlist `)' chunk END */
>
> Perhaps you mean block():
>    /* block -> chunk */
>
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1: Question on how the Lua parser evaluates bodies

Andrew Gierth
>>>>> "Alexander" == Alexander Walz <[hidden email]> writes:

 Alexander> Hello Luiz Henrique,
 Alexander> you are right with block() , I am sorry.

 Alexander> I get this in Lua 5.1:

The REPL decides whether it has a complete statement to execute by doing
a load() on whatever it has, and seeing if there's a syntax error at the
very end of the input. So in your example, the first two calls to
block() are for the strings [[do]] (which fails) and [[do print("Lua")]]
(which also fails), and then the third call for [[do print("Lua") end]]
succeeds and the REPL then runs the resulting chunk.

--
Andrew.