about summary refs log tree commit diff stats
path: root/pp.lua
blob: 9c1a6d0766d26c8fb054ab23c93e364bfb390e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
--- lam.pp

local pp = {}
table.unpack = table.unpack or unpack

pp.luadump =
	function (x)
	end

pp.luapp = function (x) print(pp.luadump(x)) end

pp.lamdump =
	function (x)
	end

pp.lampp = function (x) print(pp.lamdump(x)) end

-- The following should be at some point replaced by the preceding

function pp.dump (x, lvl)
	lvl = lvl or 0
	local space = string.rep(" ", lvl)
	local output = ""
	--[[if getmetatable(x) and getmetatable(x).__tostring then
		output = output .. tostring(x)
		else]]if type(x) == "table" then
		local subo = ""
		for k,v in pairs(x) do
			if v == x then
				v = "self"
			else
				v = pp.dump(v, lvl+2)
			end
			subo = subo .. string.format("\n%s[%s] = %s,",
				(space.."  "), k, v)
		end
		output = output .. string.format("\n%s{%s\n%s}",
			space, subo, space)
	else
		output = output .. tostring(x)
	end
	return output
end

function pp.pp (x)
	print(pp.dump(x))
end

return setmetatable(pp, { __call =
			function(_, x)
				return pp.pp(x)
			end,
})