Advertisement
NonSequitur

Untitled

Dec 16th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. local ex = {
  2.     hey = {
  3.         nested_shit = {
  4.             '1', '2', 3, function() end
  5.         },
  6.  
  7.         print,
  8.     },
  9.  
  10.     2, 7,
  11.  
  12.     'some random string',
  13.  
  14.     another_table = {
  15.         newproxy(),
  16.         'even some userdata',
  17.         {{{{{{'if you\'re seeing this this shit works'}}}}}}
  18.     }
  19. }
  20.  
  21. local function levelPrint(level, ...)
  22.     io.write(string.rep('\t', level))
  23.     print(...)
  24. end
  25.  
  26. local function formatPair(first, second)
  27.     return string.format('%s = %s', tostring(first), tostring(second))
  28. end
  29.  
  30. function inspect(list)
  31.     local stack = {list}
  32.     local info = {}
  33.     local level = 1
  34.     print('{')
  35.  
  36.     while #stack ~= 0 do
  37.         local current = stack[#stack]
  38.         local index, value = next(current, info[current])
  39.         if not index then
  40.             table.remove(stack, #stack)
  41.             info[current] = nil
  42.             level = level - 1
  43.             levelPrint(level, '}')
  44.         else
  45.             info[current] = index
  46.  
  47.             if type(value) == 'string' then
  48.                 levelPrint(level, formatPair(index, '"' .. value .. '"'))            
  49.             elseif type(value) == 'table' then
  50.                 table.insert(stack, value)
  51.                 levelPrint(level, formatPair(index, '{'))
  52.                 level = level + 1
  53.             else
  54.                 levelPrint(level, formatPair(index, value))    
  55.             end
  56.         end
  57.     end
  58. end
  59.  
  60. inspect(ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement