handle backreferences and linked lists in puke

This commit is contained in:
Kistaro Windrider 2023-09-30 13:50:07 -07:00
parent f67c2da37f
commit 7c29c329b7
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -45,7 +45,9 @@ function mknew(tt, more)
end
end
linked_list = {}
-- intrusive singly-linked list.
-- cannot be nested!
linked_list = {is_linked_list=true}
mknew(linked_list, function(x)
x.next=nil
x.tail=x
@ -288,17 +290,39 @@ function fadescreen()
end
end
function puke(item, indent)
-- puke emits a verbose string
-- describing item, indented to
-- the specified depth (0 by
-- default). used for table
-- debugging. table-type keys
-- are not legible here
function puke(item, indent, seen)
if (type(item) ~= "table") return tostr(item)
seen = seen or {}
if (seen[item]) return "<<PREV>>"
seen[item] = true
indent = indent or 0
local pfx = ""
local pfx = "\n"
for _=1,indent do
pfx ..= " "
end
if (type(item) ~= "table") return pfx..tostr(item)
ret = pfx.."{"
pfx = "\n"..pfx
if item.is_linked_list then
local ret,n = "linked_list <",0
item:strip(function(x)
n += 1
local masked_x = {next="<<...>>"}
setmetatable(masked_x, {__index=x})
ret ..= pfx..tostr(n)..": "..masked_x
end)
return ret..pfx..">"
end
local ret = "{"
for k, v := pairs(item) do
ret ..= pfx..tostr(k)..": "..puke(v, indent+1)
ret ..= pfx..tostr(k)..": "..puke(v, indent+1, seen)
end
return ret..pfx.."}"
end