[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Custom extensions to Lua
- From: Rici Lake <lua@...>
- Date: Wed, 10 Aug 2005 15:23:27 -0500
On 10-Aug-05, at 2:57 PM, Rici Lake wrote:
In fact, you could do it without patching the Lua core at all, using
only a preprocessor ( :) ), by representing a box as a table with one
distinguished key. The transformation would turn
a <- *a + 7
into
a.value = a.value + 7
or some variation on that theme. Mind you, you couldn't call the
preprocessor input Lua either, but you could certainly call it a
"language which can be compiled into Lua".
Just in case it amuses someone other than me:
do
local function donothing() end
local function show(_, k, v)
if type(v) == "string" then
v = string.format("%q", v)
else
v = tostring(v)
end
print(string.format("Global `%s' is being changed to %s",
tostring(k), v))
end
function ref(tab, key, preset, postset)
preset, postset = preset or donothing, postset or donothing
return setmetatable({}, {
__index = function() return tab[key] end,
__newindex = function(_, _, new)
preset(tab, key, new)
tab[key] = new
postset(tab, key, new)
end
})
end
-- reference global by name, assuming we have the
-- same environment table as our caller
function gref(name, preset, postset)
return ref(getfenv(), name, preset, postset)
end
function traceref(name)
return gref(name, show)
end
end
> a = "Hello"
> b = gref "a"
> c = traceref "a"
> b.value = b.value .. ", world"
> print(a)
Hello, world
> c.value = string.gsub(c.value, ", world", "")
Global `a' is being changed to "Hello"
> print(a)
Hello