lua-users home
lua-l archive

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


I guess it all depends on what you think of as consistent.
It's a fairly loaded word, often meaning "consistent with
*my* expectations". :)

Here's a simple rule: anywhere you can have a list of expressions,
the last expression in the list is compiled with multireturn
semantics. Anywhere else that an expression can occur, it is
compiled with single-return semantics.

This is reasonably easy to state syntactically, with the
following slight modifications to the Lua syntax found in
the 5.0 reference manual (these do not change the language
generated by the grammar):

(changed):
  explist1   --> { exp ','} expmulti
  fieldlist  --> {field fieldsep} fieldmulti [fieldsep]

(added):
  fieldmulti --> '[' exp ']' '=' exp | name '=' exp | expmulti
  expmulti   --> exp

We can say that anything matching expmulti has multireturn semantics;
anything else matching exp has single return semantics.

Specifically:

prefixexp --> '(' exp ')'

  explains why (gsub(s, p, r)) is single return

args --> '(' [explist1' ')' --> '(' {exp ','} expmulti ')'

  explains why in print(foo(), foo()), the first foo() is a single
  return (exp) and the second one is multi return (expmulti).

I would say (based on *my* intuitions and expectations) that there
are no special cases in the above; there are simply two cases.

Whether that is intuitive to anyone else or not, I don't know.... :)

R.

PD: I'm not totally satisfied with the semantic representation
in the grammar segment above, but I think it's good enough for
didactic purposes.