Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- The object model. -- (find-luamanualw3m "#pdf-setmetatable") -- (find-es "lua5" "metatable-reductions") -- Dnode(o) -~-> Dnode.mt.__call(Dnode, o) -- -~-> Metatable.__call(Dnode, o) -- -~-> setmetatable(o, Dnode) Metatable = { type = "Metatable", specialkeys = {"type"}, __call = function (mt, T) return setmetatable(T, mt) end, } setmetatable(Metatable, Metatable) Dnode = Metatable { type = "Dnode", specialkeys = {"foo", "bar"}, } node = Dnode {tag = "a"} otype = function (o) if type(o) ~= "table" then return type(o) end local mt = getmetatable(o) if mt and mt.type then return mt.type end return type(o) end --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) smt = setmetatable gmt = getmetatable PP(node) PP(gmt(node)) PP(gmt(gmt(node))) PP(otype(node)) PP(otype(gmt(node))) PP(otype(gmt(gmt(node)))) --]] --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) ee_dofile "~/LUA/tostring.lua" Primtype = Metatable { type = "Primtype", __index = { toptype = function (o) return o[1] end, }, } Array = Metatable { type = "Array", __index = { toptype = function (o) return o[1]:toptype().."["..(o[2] or "").."]" end, }, } Ptr = Metatable { type = "Ptr", __index = { toptype = function (o) return o[1]:toptype().."*" end, }, } char = Primtype {"char", size=1, align=1} int = Primtype {"int", size=4, align=4} short = Primtype {"short", size=2, align=2} char4 = Array {char, 4} char4_ = Array {char4, nil} = char:tostype() = char4:tostype() = char4_:tostype() --]] -- (find-angg "LUA/lua50init.lua" "mytostring") -- (find-elnode "Index" "* mapconcat:") -- Faltam: otype, ... oformatq = function (str) return format("%q", str) end oangle = function (o) return "<"..tostring(o)..">" end otabletos = function (T) return ospecialtos(T, {"foo", "bar"}, "nums", tos) end otosfunctions = { ["number"] = tostring, ["nil"] = tostring, ["boolean"] = tostring, ["string"] = oformatq, ["function"] = oangle, } rawtos = function (o, fallback) local t = type(o) local f = otosfunctions[t] or (t=="table" and otabletos) or tostring return f(o) end ocomp = function (key1, key2) local t1, t2 = type(key1), type(key2) if t1~=t2 then return t1 < t2 end if t1=="number" then return key1 < key2 end if t1=="string" then return key1 < key2 end return tostring(key1) < tostring(key2) end ospecialtos = function (T, specialkeys, nums, tos) local keys, T2 = {}, {} for k,v in pairs(T) do keys[k] = k end local del = function (k) keys[k] = nil end local has = function (k) return keys[k]~=nil end local put = function (str) table.insert(T2, str) end for _,sk in ipairs(specialkeys) do if has(sk) then put(sk.."="..tos(T[sk])); del(sk) end end if nums then for i=1,#T do if has(i) then put(tos(T[i])); del(i) else break end end end local keylist = {} for k,v in pairs(keys) do table.insert(keylist, k) end table.sort(keylist, ocomp) for _,k in ipairs(keylist) do put(tos(k).."="..tos(T[k])) end return "{"..table.concat(T2, ", ").."}" end --[[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) ee_dofile "~/LUA/tostring.lua" = tos{22, 33} = tos{foo="f", bar="b", plic="p", 22, 33, nil, 55} --]] mapconcat = function (f, T, sep) local T2 = {} for i=1,(T.n or #T) do table.insert(T2, f(T[i])) end return table.concat(T2, sep or " ") end otablesize = function (T) for i=1,#T+1 do if T[i]==nil then return i-1 end end end pmapconcat = function (f, T, sep) print(" " .. mapconcat(f, T, sep)) end PPwith = function (f, ...) pmapconcat(\ (o) => otostringwith(f, o) end, pack(...), " ") end -- Tests: -- PPwith(oang, 22, nil, true, false, "22", print, {22, 33}) -- PPwith(mytostring, 22, nil, true, false, "22", print, {22, 33}) okeyset = function (T) -- o is a table local keyset = {} for key,val in pairs(T) do keyset[key] = key end return keyset -- return the "set" of all keys of o end -- Local Variables: -- coding: raw-text-unix -- modes: (fundamental-mode lua-mode) -- End: