--------------------------------------------------------------------------------
-- ASCII table generator (Lua 5.3.2) by Tony Papadimitriou <tonyp@acm.org>
--------------------------------------------------------------------------------

--==============================================================================
-- REQUIRED LIBRARY CODE COPIED HERE FOR CONVENIENCE
--==============================================================================

function list(t)                        --iterator to make a value list from a table
  local index,ans = nil,nil
  if type(t) == 'function' then return t end --assuming another iterator function
  if type(t) == 'table' then
    return function ()
             index,ans = next(t,index)
             return ans
           end
  end
  return t
end

--------------------------------------------------------------------------------

function isin(s,set)                    --return true if set contains object
  assert(s ~= nil,'1st part should not be nil')
  assert(type(set) == 'table')
  for x in list(set) do
    if x == s then return true end
  end
  return false
end

--------------------------------------------------------------------------------

function Iff(condition,a,b)   --returns 1st object on true condition, 2nd on false
  assert(type(condition) == 'boolean','Boolean expected')
  if condition then return a else return b end
end

--==============================================================================

NON_PRINTABLE = {0,7,8,9,10,13}

print [[ASCII Table * Copyright (c) 1993-2020 by Tony G. Papadimitriou *FREEWARE*
(Warning: ASCII codes 0, 7, 8, 9, 10, 13 are not displayed)
]]

--------------------------------------------------------------------------------

local
function separator(ch)
  print(string.rep(ch,78))
end

--------------------------------------------------------------------------------

function ShowTable(hexflag)
  local ans = ''
  for code = 0, 255 do
    if hexflag then
      ans = ans .. '$'
    else
      if code < 10 then ans = ans .. ' ' end
      if code < 100 then ans = ans .. ' ' end
    end
    ans = ans .. Iff(hexflag,string.format('%2X',code),code) .. ' ' ..
          string.char(Iff(isin(code,NON_PRINTABLE),32,code)) .. '|'
    if (code+1) % 13 == 0 then
      print(ans)
      ans = ''
    end
  end

  if ans ~= '' then print(ans) end
end

--------------------------------------------------------------------------------

separator('=')
ShowTable(false)    --show table with decimal numbers
separator('-')
ShowTable(true)     --show table with hex numbers
separator('=')