lua-users home
lua-l archive

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


> Re. the implementation difficulties.  That's more understandable... Lua
> tends to stick to the simplest thing that works, and avoid conveniences
>  like "for" loops ).  Perhaps, though, the artibrary semantics of the syntax
> could be chosen to simplify implementation.  Since there's no wrong answer,
> pick whatever behavior is easiest to implement.

There are some subtleties in the implementation of constructors, which are 
critical for large constructors (it is not uncommon meta-files written 
in Lua that have more than 10.000 elements in a single constructor).
The code for {10,20,30} is 

  CREATEARRAY     3
  PUSHNUMBER      10
  PUSHNUMBER      20
  PUSHNUMBER      30
  SETLIST         0 3

There is a single "settable" opcode (the SETLIST), that stores the 3 
elements on the stack in successive positions of the array, starting at 
index 1. When there are too many elements in a list constructor, then we 
generate intermediate SETLIST opcodes, in blocks of 64 elements (to avoid 
too much grow of the stack). Currently, the SETLIST cannot start counting 
at arbitrary indices, only at "multiples" of 64 (this is the argument 0 in 
the SETLIST opcode. The first index should be 0*64+1, last index should be 
0*64+3). A similar approach is used for record-style fields, with SETMAP.

The new syntax would have two drawbacks: First, if we keep the current
opcodes, the only easy implementation would be to start each new series of
list-like elements in 1 again:

  { 10, 12, a=b, 13, 14} --> {[1]=10, [2]=12, a=b, [1]=13, [2]=14}

The 10, 12 at the beginning of the constructor are simply thrown away. This 
seems to be too literal an interpretation of "there's no wrong answer". 

Second, even if we create a new opcode that starts storing list elements at
arbitrary positions, the efficiency would suffer; for instance,

  {1,2,3,4; a=1, b=2, c=3, y=4}

needs only one SETLIST and one SETMAP, but

  {1, a=1, 2, b=2, 3, c=3, 4, y=4}

would need four SETLISTs and four SETMAPs. So, the new form would be
uglier (to our eyes) and slower.

-- Roberto