From 21a6314e270344e6923bb4f0a06fd09b0d9ae581 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 18 Dec 2017 16:50:31 -0800 Subject: Tweaked repr and added a depth parameter. --- utils.lua | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'utils.lua') diff --git a/utils.lua b/utils.lua index 111f25c..bab635d 100644 --- a/utils.lua +++ b/utils.lua @@ -24,22 +24,28 @@ local function size(t) return n end -local function repr(x) +local function repr(x, depth) + -- Create a string representation of the object that is close to the lua code that will + -- reproduce the object (similar to Python's "repr" function) + depth = depth or math.huge + if depth == 0 then + return tostring(x) + end local x_type = type(x) if x_type == 'table' then - local mt = getmetatable(x) - if mt and mt.__tostring then - return mt.__tostring(x) + if getmetatable(x) then + -- If this object has a weird metatable, then don't pretend like it's a regular table + return tostring(x) elseif is_list(x) then local ret = {} for i=1,#x do - ret[i] = repr(x[i]) + ret[i] = repr(x[i], depth-1) end return "{"..table.concat(ret, ", ").."}" else local ret = {} for k, v in pairs(x) do - ret[#ret+1] = "["..repr(k).."]= "..repr(v) + ret[#ret+1] = "["..repr(k,depth-1).."]= "..repr(v,depth-1) end return "{"..table.concat(ret, ", ").."}" end @@ -67,11 +73,11 @@ local function repr(x) end end -local function stringify(x) +local function stringify(x, depth) if type(x) == 'string' then return x else - return repr(x) + return repr(x, depth) end end -- cgit v1.2.3