-- This file: -- https://anggtwu.net/LUA/Trav1.lua.html -- https://anggtwu.net/LUA/Trav1.lua -- (find-angg "LUA/Trav1.lua") -- Author: Eduardo Ochs -- -- (defun e () (interactive) (find-angg "LUA/Trav1.lua")) -- -- «.Trav» (to "Trav") -- «.Trav-tests» (to "Trav-tests") -- _____ -- |_ _| __ __ ___ __ -- | || '__/ _` \ \ / / -- | || | | (_| |\ V / -- |_||_| \__,_| \_/ -- -- "Traverse". -- «Trav» (to ".Trav") Trav = Class { type = "Trav", from = function (o) return Trav { o=o } end, __call = function (tr,cmds) return tr:run(cmds) end, __tostring = function (tr) return tr:tostring() end, __index = { tostring = function (tr) return tostring(tr.o) end, run1 = function (tr,cmd) local fieldname = cmd:match("^%.(.*)") local funname = cmd:match("^(.*)%(%)$") local tablekey = cmd:match("^%[(.*)%]$") local method = "_"..cmd if fieldname then return Trav.from(tr.o[fieldname]) end if funname then return Trav.from(_G[funname](tr.o)) end if tablekey then return Trav.from(tr.o[expr(tablekey)]) end if tr[method] then return Trav.from(tr[method](tr,tr.o)) end error("Bad cmd: "..cmd) end, run = function (tr,cmds) for _,cmd in ipairs(split(cmds)) do tr = tr:run1(cmd) end return tr end, -- _mt = function (tr,o) return getmetatable(o) end, _keys = function (tr,o) return VTable(sortedkeys(o)) end, _c = function (tr,o) return mapconcat(mytostring, o, " ") end, _cv = function (tr,o) return mapconcat(mytostring, o, "\n") end, _ksc = function (tr,o) return tr:_c (tr:_keys(o)) end, _kscv = function (tr,o) return tr:_cv(tr:_keys(o)) end, _char = function (tr,o) return string.char(o) end, _type = function (tr,o) return type(o) end, _otype = function (tr,o) return otype(o) end, _PP = function (tr,o) return mytostring(o) end, _PPP = function (tr,o) return mytostringp(o) end, _PPV = function (tr,o) return mytostringv(o) end, _PPPV = function (tr,o) return mytostringpv(o) end, _PPP0 = function (tr,o) return mytostringp(tr) end, _PPPV0 = function (tr,o) return mytostringpv(tr) end, }, } -- «Trav-tests» (to ".Trav-tests") --[==[  (eepitch-lua51)  (eepitch-kill)  (eepitch-lua51) dofile "Trav1.lua" Vector = Class { type = "Vector", __add = function (V, W) return Vector {V[1]+W[1], V[2]+W[2]} end, __tostring = function (V) return "("..V[1]..","..V[2]..")" end, __index = { norm = function (V) return math.sqrt(V[1]^2 + V[2]^2) end, }, } v = Vector {3, 4} -- v = { 3, 4, __mt = Vector} PP(v) PPP(v) PPV(getmetatable(v)) PPV(getmetatable(v).type) PPV(getmetatable(v).__index) PPV(getmetatable(v).__index.norm) x = Trav.from(v) = x = x "PPP" = x "PPPV" = x "PPP0" = x "PPPV0" = x "PP" = x "ksc" = x "kscv" = x = x "mt" = x "mt PPV" = x "mt kscv" = x "mt keys" = x "mt .type" = x "mt .__index PPV" o = Trav.from(HTable {11, 22, a=33, b=44}) = o = o ".a" = o "[1]" = o "['b']" = o "['b'] type" = o "['b'] type()" = o " otype" = o ".a otype" = o "keys" = o "mt" = o "PPPV" = o "mt kscv" = o "mt keys" = o "mt PP" = o "mt .type" = o "mt .__index" = o "mt keys" = o "mt mt keys" --]==] -- Local Variables: -- coding: utf-8-unix -- End: