lua-users home
lua-l archive

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


Peter Cawley wrote:
> ... numeric for loops are restricted to an explicit argument
> list ...
> This is exactly what the complete syntax specifies:
> for Name `=´ exp `,´ exp [`,´ exp] do block end |
> for namelist in explist do block end |
> The complete syntax does not give the reason for this, so what is the
> rationale behind this decision?

I can't help but be drawn to your idea as more Lua-like, even though it
may require a modest rewrite of the 'fornum' code in lparser.c and the
accompanying support code in lcode.c.

Perhaps the syntax could be:

  for Name `=´ explist do block end

Where:

  '(for start)' = first value of explist
  '(for limit)' = second value of explist, or '(for start)'
  '(for step)'  = third value of explist, or 1

Here 'or' means 'if value is nil then...'.

As a quick little test I tried to modify the current parser and code
generator to check that the first expression was a VCALL or VVARARG (not
mentioned by you) if no ',' were found next. But then exp1() needed to
be modified to that eventually it resulted in a call to
luaK_setreturns(..., 2) rather than luaK_setoneret(). Since exp1() is
only called by the parser when handling numeric 'for' loops, it could be
modified without fear of affecting other code.

The proper approach would be to remove exp1() and call explist1(), then
use those results. After a few minutes of playing it seemed that the
problem would take a few hours for my feeble brain; I need to learn more
about how the expression lists are built. I've left the editor open to
play around with it later. Seems like a fun project.

Doug

-- 
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220