lua-users home
lua-l archive

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


Can your parser tell, lower down in the function, whether a name not declared in the function is a global variable or an upvalue?

It's not my parser, it's just a minor modification to the standard one which pre-populates and then (optionally) locks the table of upvalues.

The standard functioning of the Lua parser is that is scans the table of declared local variables in scope, and then if is doesn't find what it is looking for scans the table of upvalues.  If that fails as well then it looks to the locals and then upvalues of the enclosing function, promoting to an upvalue if it finds something, otherwise continuing up through enclosing functions until it hits the main chunk.

At that point if it still can't find what it is looking for it starts over from the original scope looking for _ENV in locals and upvalues and once it has that it inserts a table lookup opcode using the variable name as a string.  This final step requires that _ENV exist somewhere so this is where I inserted the syntax error for undeclared variables (since I allow that _ENV may not exist).