Here is another pretty-printer, more restriced in scope than "Pretty-printing Lua datastructures", but also generating a far more compact representation.


  -- 22/02/2001 jcw@equi4.com

  local showasstr=
    function (s)
      if type(s)=='string' then return format('%q',s) end
      return tostring(s)
    end
  horizstr= -- has to be global, because it's recursive
    function (x,n)
      if type(x)~='table' then return %showasstr(x) end
      if getn(x)==0 then return '()' end
      local o,e=x[1]
      for i=2,getn(x) do
        local s=x[i]
        if type(s)=='table' then
          s=horizstr(s,n-strlen(o))
        else
          s=%showasstr(s)
        end
        if not s then return end
        o=o..' '..s
        if strlen(o)>n then return end
      end
      return '('..o..')'
    end
  function lispy(t,l,f)
    local n=strlen(t)
    local h=horizstr(l,70-n)
    if h then
      write(t,' ',h)
    else
      assert(type(l)=='table',l)
      t=t..' ('..l[1]
      local e=''
      for i=2,getn(l) do
        write(e)
        lispy(t,l[i],1)
        t=strrep(' ',strlen(t))
        e='\n'
      end
      write(')')
    end
    if not f then write('\n') end
  end

The basic limitations of this code are:

For cases where those restrictions are all right, the ouput can be surprisingly readable. Here's sample output from a project I am working on:

  (filter (explode (view "frequents") (view "serves"))
          (AND (= (colnum 2) (colnum 7))
               (NOT (exists (filter (explode (view "likes"))
                                    (AND (= (colnum 1) (bound 5 1))
                                         (= (bound 1 1) (colnum 2))))))))

The above was generated by a call of the form: lispy("",mytable)


Last modified
2001-02-26

(216.232.136.19)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated