lua-users home
lua-l archive

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


I'm trying to modify Lua so that a table constructed as:

  t = { weight = 9, wibble, foo }

results in a table as:

  { weight = 9, wibble = true, foo = true }

I've been playing around with the code in lparser.c, but I'm not having
much success (this is the first time I've done any hacking on Lua).

There is no need to hack the parser. You can set an __index metamethod
for _G that returns true for undefined global variables. This will do
what you wish but perhaps it's much more that we you want...
	This doesn't work.  You'll end with { weight=9, [1]=true, [2]=true }

	Following this approach, you could define a __index metamethod
for _G that returns the name of the (undefined global) variable:

setmetatable (_G, { __index = function (t,k) return k end, })
t = { weight = 9, wibble, foo }
for i,v in pairs(t) do print(i,v) end
1       wibble
2       foo
weight  9

	After that you'll have to change the keys of the table:

function C(t) for i,v in ipairs(t) do t[v] = true end return t end
t = C { weight = 9, wibble, foo }
for i,v in pairs(t) do print(i,v) end
1       wibble
2       foo
foo     true
wibble  true
weight  9

	Hope this helps!
		Tomas