lua-users home
lua-l archive

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

Tidbits of Lua parser / code generator for folks that find such things
of interest.

Lua is ostensibly a register VM. However there is a problem generating
code for a pure register machine because of the semantics of Lua
function calls. Lua functions can return unbounded multiple values -
in theory filling up stack space. So the only way to allocate
registers is to follow a stack discipline, with only one 'open' call
possible at the top of the stack.

Lua's suffixed expressions can be l-values or r-values, and it is not
known until later what it should be. Example:

x()[1][2], y, z = f()

Here x()[1][2] is an l-value as it is going to be ultimately assigned to.

The parsing and code generation of assignment statements is IMO one of
the trickiest aspects of Lua, another being the handling of multiple
return values. Lua solves the l-value / r-value dilemma by deferring
the resolution of these expressions until the expression is used -
hence the VINDEXED expression type can end up being a GET or a PUT.

The parsing of assignments follows a strange course. I believe the
vars on the left of '=' are parsed recursively first, then at the top
of recursion, the expression list to the right of '=' is parsed, and
then during the unwind, the store instructions get generated. Please
correct me if I am wrong.