-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpprint.lua
40 lines (37 loc) · 869 Bytes
/
pprint.lua
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
function print_table(table, offset, visited)
if visited[table] then
io.write("<reccursive>\n")
return
end
visited[table] = table
io.write("{\n")
local key, value = next(table)
while key do
local i = 0
while i < offset do
io.write(" ")
i = i + 1
end
io.write(tostring(key))
io.write(" = ")
pprint_impl(value, offset + 1, visited)
key, value = next(table, key)
end
local i = 0
while i < offset - 1 do
io.write(" ")
i = i + 1
end
io.write("}\n")
end
function pprint_impl(value, offset, visited)
if type(value) == "table" then
print_table(value, offset, visited)
else
io.write(tostring(value))
io.write("\n")
end
end
function pprint(value)
pprint_impl(value, 1, {})
end