lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




Le mer. 5 juin 2019 à 16:42, Dibyendu Majumdar <mobile@majumdar.org.uk> a écrit :
On Wed, 5 Jun 2019 at 15:35, Matthew Wild <mwild1@gmail.com> wrote:
>
>   local resource x = foo()
>
> to be interpreted as
>   local <toclose> x = foo()
> or
>   local resource; x = foo()
>

The former as the latter is impossible.
So if you say:

local resource = 0

Then resource is a variable.

But if you say:
local resource x = 0
Then x is a variable, and resource is a qualifier.
No because it is also equivalent to
  local resource
  x = 0
which declares a new variable named "resource" (without an initialiser, so initialized to nil) and then performs an assignment to the variable named "x" in the current scope.
Writing it on one line as
  local resource x = 0
does not change things.

Let's remember that ";" separators of statements are already optional in Lua; they are suggested only in some cases where disambiguation is needed (caused by the already permitted currification of function calls):
  print f; (x)(y)[0] = 1
  print f(x); (y)[0] = 1
where you have to wonder what is the meaning of:
   print f(x)(y)[0] = 1
It's true that Lua instructions must either start by a keyword, otherwise they are assignments or function calls; currification without required ";" complicates the separation when there are multiple assignments or function calls. So the ";" was finally introduced (the Lua designer most probably wanted to avoid the ";" need everywhere, but introducing the currified syntax required the addition of ";" to separate ambiguous statements.

Ambiguous statements are not permitted in Lua syntax, or have a mandatory associativity (in a LR parser, normally the parser should should choose the "shift" action instead of "reduce", to favor longer instructions (however the parser can still rollback if needed to retry with a "reduce" action); if it chooses the "reduce" action, it favors smaller instructions but once the reduce action has been performed, the parser can no longer rollback to use a "shift" instead, because "reduce" actions cannot be easily rolled back; rolling back from a "shift" is trivial, it just means pushing back the unprocess tokens in excess to a cache of the token input stream)

Adding the ";" (which remains optional) allows the parser to "reduce" immediately without trying a "shift" and processing a (possibly infinite) stream, which it will do by default. And no rollback is ever needed. The currification of function calls becomes possible and safe.