Friday, 6 September 2013

Adding Barewords to Lua

Adding Barewords to Lua

Ok, to implement a domain specific language, within lua, I want to add
barewords to the language. So that print("foo") could be written as
print(foo)
The way I have done this is by changing the metatable of the enviroment
table _G.
mt = {__index = function(tbl,key) return key end}
setmetatable(__G, mt)
And that works, because retrieving the value of variable foo is done by
_G.foo which is equivalent to _G["foo"]
Is this a good approach? Are there hidden downsides? Is there a better
way? Can I do it so that barewords only work inside a certain file?
(Perhaps by executing that file, from another luascript, using loadstring)

No comments:

Post a Comment