lua-users home
lua-l archive

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


Consider the following Lua code:

   SomeVariable = 1

   function ClearSomeVariable()
      SomeVariabl = 0  -- NOTE THE TYPO!
   end

If the user accidentally writes the above code, typo and all, I want 
to be able to catch this as a bug. However, the default 
implementation of Lua allows global variables to be defined anywhere. 
As such, the above code is quite valid and simply creates a second 
global variable, "SomeVariabl", alongside the original 
global, "SomeVariable".

My question is: is there an easy way to modify the Lua source to only 
allow global variable definitions at the top level? (i.e.: outside 
all code blocks, like C/C++) How would one go about doing this? There 
must be some way to determine in the parser how many levels deep you 
are. Even a simple assert to disallow the above code would be fine 
for my purposes.