Advertisement
NonSequitur

Untitled

Dec 16th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 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.     if type(first) == 'string' then
  28.         first = '"' .. first .. '"'
  29.     end
  30.     return string.format('[%s] = %s', tostring(first), tostring(second))
  31. end
  32.  
  33. function inspect(list)
  34.     local stack = {list}
  35.     local info = {}
  36.     local level = 1
  37.     print('{')
  38.  
  39.     while #stack ~= 0 do
  40.         local current = stack[#stack]
  41.         local index, value = next(current, info[current])
  42.         if not index then
  43.             table.remove(stack, #stack)
  44.             info[current] = nil
  45.             level = level - 1
  46.             levelPrint(level, '},')
  47.         else
  48.             info[current] = index
  49.  
  50.             if type(value) == 'string' then
  51.                 levelPrint(level, formatPair(index, '"' .. value .. '",'))            
  52.             elseif type(value) == 'table' then
  53.                 table.insert(stack, value)
  54.                 levelPrint(level, formatPair(index, '{'))
  55.                 level = level + 1
  56.             else
  57.                 levelPrint(level, formatPair(index, value) .. ', ')    
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. function recursive_inspect(list, level)
  64.     level = level or 0
  65.     if level == 0 then
  66.         print('{')
  67.     end
  68.     for index, value in pairs(list) do
  69.         if type(value) == 'string' then
  70.             levelPrint(level + 1, formatPair(index, '"' .. value .. '",'))
  71.         elseif type(value) == 'table' then
  72.             levelPrint(level + 1, formatPair(index, '{'))
  73.             recursive_inspect(value, level + 1)
  74.         else
  75.             levelPrint(level + 1, formatPair(index, value) .. ',')
  76.         end
  77.     end
  78.     levelPrint(level, '},')
  79. end
  80.  
  81. recursive_inspect(ex)
  82.  
  83. print('---')
  84.  
  85. inspect(ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement