Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/LUA/tos.lua.html
--   http://angg.twu.net/LUA/tos.lua
--           (find-angg "LUA/tos.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2021aug16
--
-- Based on:
--   (find-angg "LUA/lua50init.lua" "mytostring")
-- Superseded by:
--   (find-angg "LUA/lua50init.lua" "Tos")
--
-- This was my first function for printing the contents of tables.
-- I wrote it when I started using Lua 4.0 in dec/2000.
-- It doesn't use metatables, as they didn't exist in Lua 4.0.
-- The functions and variables were renamed many times over the years.
-- The comments and test blocks were rewritten in 2021.
--
-- Links to some historical versions:
--   (find-es "lua" "lua_yada")
--   (find-es "lua5" "mytostring")
--   (find-angg "dednat/inc.lua" "p")
--   (find-angg "LUA/tos2.lua")
--   (find-dn5 "tos.lua")
--
-- Uses:
--   (find-angg "LUA/lua50init.lua" "map")
--
-- (defun e () (interactive) (find-angg "LUA/tos.lua"))

tos_compare_keyvals = function (keyval1, keyval2)
      local key1,  key2  = keyval1.key, keyval2.key
      local type1, type2 = type(key1), type(key2)
      if type1 == type2 then
        if type1 == "number" then return key1 < key2 end
        if type1 == "string" then return key1 < key2 end
        -- return tos(key1) < tos(key2)         -- slow
        return tostring(key1) < tostring(key2)  -- fast
      else
        return type1 < type2  -- numbers before strings before tables, etc
      end
    end
tos_sorted_keyvals = function (tbl)
    local keyvaltbl = {}
    for key,val in pairs(tbl) do
      table.insert(keyvaltbl, {key=key, val=val})
    end
    table.sort(keyvaltbl, tos_compare_keyvals)
    return keyvaltbl
  end
tos_keyval = function (keyval)
    return tos(keyval.key).."="..tos(keyval.val)
  end

tos_table = function (tbl, sep, a, b)
    local keyvaltbl = tos_sorted_keyvals(tbl)
    local images = map(tos_keyval, keyvaltbl)
    return (a or "{")
        .. table.concat(images, sep or ", ")
        .. (b or "}")
  end
tos_number = function (n) return tostring(n) end
tos_string = function (s) return string.format("%q", s) end
tos_other  = function (o) return "<"..tostring(o)..">" end
tos = function (o)
    local t = type(o)
    if t == "number" then return tos_number(o) end
    if t == "string" then return tos_string(o) end
    if t == "table"  then return tos_table(o)  end
    return tos_other(o)
  end

tos_arg = function (arg, sep)
    local images = map(tos, arg, arg.n)
    return table.concat(images, sep or " ")
  end


--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "tos.lua"

= tos_compare_keyvals({key="a", val="aa"}, {key=2, val=22})
= tos_sorted_keyvals ({a="aa", [2]=22})
  --> {{key=2, val=22}, {key="a", val="aa"}}
= tos_keyval({key="a", val="aa"})
  --> '"a"="aa"'

= tos{}
= tos{a="aa", 22}
= tos({{"a", b="bb", 22}, {}, print})

--]]



--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "~/LUA/tos.lua"


--]]