diff options
Diffstat (limited to 'pp.lua')
-rw-r--r-- | pp.lua | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/pp.lua b/pp.lua new file mode 100644 index 0000000..3710b07 --- /dev/null +++ b/pp.lua | |||
@@ -0,0 +1,36 @@ | |||
1 | --- lam.pp | ||
2 | |||
3 | local pp = {} | ||
4 | |||
5 | function pp.dump (x, lvl) | ||
6 | lvl = lvl or 0 | ||
7 | local space = string.rep(" ", lvl) | ||
8 | local output = "" | ||
9 | if type(x) == "table" then | ||
10 | local subo = "" | ||
11 | for k,v in pairs(x) do | ||
12 | if v == x then | ||
13 | v = "self" | ||
14 | else | ||
15 | v = pp.dump(v, lvl+2) | ||
16 | end | ||
17 | subo = subo .. string.format("\n%s[%s] = %s,", | ||
18 | (space.." "), k, v) | ||
19 | end | ||
20 | output = output .. string.format("\n%s{%s\n%s}", | ||
21 | space, subo, space) | ||
22 | else | ||
23 | output = output .. string.format("%s", x) | ||
24 | end | ||
25 | return output | ||
26 | end | ||
27 | |||
28 | function pp.pp (x) | ||
29 | print(pp.dump(x)) | ||
30 | end | ||
31 | |||
32 | return setmetatable(pp, { __call = | ||
33 | function(_, x) | ||
34 | return pp.pp(x) | ||
35 | end, | ||
36 | }) | ||