Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

118
LINES

< > BotCompany Repo | #153 // tstr.lua (lua-nucleo)

Lua code

1  
--------------------------------------------------------------------------------
2  
--- Visualization of non-recursive tables.
3  
-- @module lua-nucleo.tstr
4  
-- This file is a part of lua-nucleo library
5  
-- @copyright lua-nucleo authors (see file `COPYRIGHT` for the license)
6  
--------------------------------------------------------------------------------
7  
8  
local pairs, ipairs, type, tostring = pairs, ipairs, type, tostring
9  
local table_concat = table.concat
10  
local string_match, string_format = string.match, string.format
11  
12  
--local lua51_keywords = import 'lua-nucleo/language.lua' { 'lua51_keywords' }
13  
--local serialize_number = import 'lua-nucleo/string.lua' { 'serialize_number' }
14  
local lua51_keywords = get('#155').lua51_keywords
15  
local serialize_number = get('#156').serialize_number
16  
17  
local tstr, tstr_cat
18  
do
19  
  local function impl(t, cat, visited)
20  
    local t_type = type(t)
21  
    if t_type == "table" then
22  
      if not visited[t] then
23  
        visited[t] = true
24  
25  
        cat("{")
26  
27  
        -- Serialize numeric indices
28  
29  
        local next_i = 0
30  
        for i, v in ipairs(t) do -- TODO: Lua 5.2 warning. Rewrite.
31  
          if i > 1 then -- TODO: Move condition out of the loop
32  
            cat(",")
33  
          end
34  
          impl(v, cat, visited)
35  
          next_i = i
36  
        end
37  
        next_i = next_i + 1
38  
39  
        -- Serialize hash part
40  
        -- Skipping comma only at first element if there is no numeric part.
41  
        local need_comma = (next_i > 1)
42  
        for k, v in pairs(t) do
43  
          local k_type = type(k)
44  
          if k_type == "string" then
45  
            if need_comma then
46  
              cat(",")
47  
            end
48  
            need_comma = true
49  
50  
            -- TODO: Need "%q" analogue, which would put quotes
51  
            -- only if string does not match regexp below
52  
            if not lua51_keywords[k] and string_match(k, "^[%a_][%a%d_]*$") then
53  
              cat(k) cat("=")
54  
            else
55  
              cat(string_format("[%q]=", k))
56  
            end
57  
            impl(v, cat, visited)
58  
          else
59  
            if
60  
              k_type ~= "number" or -- non-string non-number
61  
              k >= next_i or k < 1 or -- integer key in hash part of the table
62  
              k % 1 ~= 0 -- non-integer key
63  
            then
64  
              if need_comma then
65  
                cat(",")
66  
              end
67  
              need_comma = true
68  
69  
              cat("[")
70  
              impl(k, cat, visited)
71  
              cat("]=")
72  
              impl(v, cat, visited)
73  
            end
74  
          end
75  
        end
76  
77  
        cat("}")
78  
79  
        visited[t] = nil
80  
      else
81  
        -- Note this loses information on recursive tables
82  
        cat('"table (recursive)"')
83  
      end
84  
    elseif t_type == "number" then
85  
      cat(serialize_number(t))
86  
    elseif t_type == "boolean" then
87  
      cat(tostring(t))
88  
    elseif t == nil then
89  
      cat("nil")
90  
    else
91  
      -- Note this converts non-serializable types to strings
92  
      cat(string_format("%q", tostring(t)))
93  
    end
94  
  end
95  
96  
  -- This function is optimized for logging.
97  
  -- Unless you are serializing "simple" tables,
98  
  -- do not use this function for serialization.
99  
  -- This function intentionally loses information on nested recursive tables
100  
  -- and on non-serializable types like functions, threads and userdata.
101  
102  
  tstr_cat = function(cat, t)
103  
    impl(t, cat, {})
104  
  end
105  
106  
  tstr = function(t)
107  
    local buf = {}
108  
    local cat = function(v) buf[#buf + 1] = v end
109  
    impl(t, cat, {})
110  
    return table_concat(buf)
111  
  end
112  
end
113  
114  
return
115  
{
116  
  tstr = tstr;
117  
  tstr_cat = tstr_cat;
118  
}

Author comment

Prints tables in one line.

Not working in TinyBrain as-is. Try serpent (#158).

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 3622 [visualize]

Snippet ID: #153
Snippet name: tstr.lua (lua-nucleo)
Eternal ID of this version: #153/1
Text MD5: 2d2d687b3c03ad36062046c1b52701ab
Author: stefan
Category: table serializers
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-01-23 02:51:22
Source code size: 3622 bytes / 118 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 888 / 198
Referenced in: [show references]