4c: Tank Printer

+wash

Render tank at width

Render tac with indent tab and maximum width edg. If tac cannot be rendered in-line in less than tac characters, it will be split into multiple lines if possible. If tac cannot be made to fit, wash will crash.

Accepts

[tab=@ edg=@] - tab is the number of characters of indentation and edge is the maximum number of characters of width.

tac is a tank.

Produces

A wall (list of tapes).

Source

++  wash
  |=  [[tab=@ edg=@] tac=tank]  ^-  wall
  (~(win re tac) tab edg)

Examples

> =rose `tank`[%rose [" " "[" "]"] ~['abc' 'def' 'ghi']]

> ((slog (crip (of-wall:format (wash [0 80] rose))) ~) ~)
[abc def ghi]


> ((slog (crip (of-wall:format (wash [0 8] rose))) ~) ~)
[ abc
  def
  ghi
]

> ((slog (crip (of-wall:format (wash [3 15] rose))) ~) ~)
   [ abc
     def
     ghi
   ]

+re

Pretty-printing engine

Container core for a pretty-printing engine that accepts a tank sample and contains arms that perform computation on it.

Accepts

tac is a tank.

Source

++  re
  |_  tac=tank

++ram:re

Flatten to tape

Flatten tank out into a tape.

Accepts

tac is a tank, taken from sample of ++re core.

Produces

A tape.

Source

++  ram
  ^-  tape
  ?@  tac
    (trip tac)
  ?-    -.tac
      %leaf  p.tac
  ::
  ::  flat %palm rendered as %rose with welded openers
  ::
      %palm
    =*  mid  p.p.tac
    =*  for  (weld q.p.tac r.p.tac)
    =*  end  s.p.tac
    ram(tac [%rose [mid for end] q.tac])
  ::
  ::  flat %rose rendered with open/mid/close
  ::
      %rose
    =*  mid  p.p.tac
    =*  for  q.p.tac
    =*  end  r.p.tac
    =*  lit  q.tac
    %+  weld
      for
    |-  ^-  tape
    ?~  lit
      end
    %+  weld
      ram(tac i.lit)
    =*  voz  $(lit t.lit)
    ?~(t.lit voz (weld mid voz))
  ==

Examples

> ~(ram re leaf+"foo")
"foo"

> ~(ram re [%rose [" " "[" "]"] ~['abc' 'def' 'ghi']])
"[abc def ghi]"

++win:re

Render at indent

Render at indent level tab and width edg.

Accepts

tac is a tank, taken from sample of ++re core.

tab and edg are atoms.

Produces

A wall (list of tape).

Source

++  win
  |=  [tab=@ud edg=@ud]
  ::  output stack
  ::
  =|  lug=wall
  |^  ^-  wall
      ?@  tac
        (rig (trip tac))
      ?-    -.tac
          %leaf  (rig p.tac)
      ::
          %palm
        =/  hom  ram
        ?:  (lte (lent hom) (sub edg tab))
          (rig hom)
        ::
        =*  for  q.p.tac
        =*  lit  q.tac
        ?~  lit
          (rig for)
        ?~  t.lit
          =:  tab  (add 2 tab)
              lug  $(tac i.lit)
            ==
          (rig for)
        ::
        =>  .(lit `(list tank)`lit)
        =/  lyn  (mul 2 (lent lit))
        =.  lug
          |-  ^-  wall
          ?~  lit
            lug
          =/  nyl  (sub lyn 2)
          %=  ^$
            tac  i.lit
            tab  (add tab nyl)
            lug  $(lit t.lit, lyn nyl)
          ==
        (wig for)
      ::
          %rose
        =/  hom  ram
        ?:  (lte (lent hom) (sub edg tab))
          (rig hom)
        ::
        =*  for  q.p.tac
        =*  end  r.p.tac
        =*  lit  q.tac
        =.  lug
          |-  ^-  wall
          ?~  lit
            ?~(end lug (rig end))
          %=  ^$
            tac  i.lit
            tab  (mod (add 2 tab) (mul 2 (div edg 3)))
            lug  $(lit t.lit)
          ==
        ?~(for lug (wig for))
      ==
  ::  +rig: indent tape and cons with output stack
  ::
  ++  rig
    |=  hom=tape
    ^-  wall
    [(runt [tab ' '] hom) lug]
  ::  +wig: indent tape and cons with output stack
  ::
  ::    joined with the top line if whitespace/indentation allow
  ::
  ++  wig
    |=  hom=tape
    ^-  wall
    ?~  lug
      (rig hom)
    =/  wug  :(add 1 tab (lent hom))
    ?.  =+  mir=i.lug
        |-  ^-  ?
        ?~  mir  |
        ?|  =(0 wug)
            ?&(=(' ' i.mir) $(mir t.mir, wug (dec wug)))
        ==
      (rig hom)       :: ^ XX regular form?
    :_  t.lug
    %+  runt  [tab ' ']
    (weld hom `tape`[' ' (slag wug i.lug)])
  --

Examples

> =rose `tank`[%rose [" " "[" "]"] ~['abc' 'def' 'ghi']]

> ((slog (crip (of-wall:format (~(win re rose) 0 80))) ~) ~)
[abc def ghi]

> ((slog (crip (of-wall:format (~(win re rose) 3 80))) ~) ~)
   [abc def ghi]

> ((slog (crip (of-wall:format (~(win re rose) 0 10))) ~) ~)
[ abc
  def
  ghi
]

> ((slog (crip (of-wall:format (~(win re rose) 3 15))) ~) ~)
   [ abc
     def
     ghi
   ]

+show

Pretty-printer (deprecated)

Pretty prints noun vem, producing a tank. This function is deprecated and in most cases produces outdated syntax or incorrect results.

Accepts

vem is a noun.

Produces

A tank.

Source

++  show
  |=  vem=*
  |^  ^-  tank
      ?:  ?=(@ vem)
        [%leaf (mesc (trip vem))]
      ?-    vem
          [s=~ c=*]
        [%leaf '\'' (weld (mesc (tape +.vem)) `tape`['\'' ~])]
      ::
          [s=%a c=@]        [%leaf (mesc (trip c.vem))]
          [s=%b c=*]        (shop c.vem |=(a=@ ~(rub at a)))
          [s=[%c p=@] c=*]
        :+  %palm
          [['.' ~] ['-' ~] ~ ~]
        [[%leaf (mesc (trip p.s.vem))] $(vem c.vem) ~]
      ::
          [s=%d c=*]        (shop c.vem |=(a=@ ~(rud at a)))
          [s=%k c=*]        (tank c.vem)
          [s=%h c=*]
        :+  %rose
          [['/' ~] ['/' ~] ~]
        =+  yol=((list @ta) c.vem)
        (turn yol |=(a=@ta [%leaf (trip a)]))
      ::
          [s=%l c=*]        (shol c.vem)
          [s=%o c=*]
        %=    $
            vem
          :-  [%m '%h::[%d %d].[%d %d]>']
          [-.c.vem +<-.c.vem +<+.c.vem +>-.c.vem +>+.c.vem ~]
        ==
      ::
          [s=%p c=*]        (shop c.vem |=(a=@ ~(rup at a)))
          [s=%q c=*]        (shop c.vem |=(a=@ ~(r at a)))
          [s=%r c=*]        $(vem [[%r ' ' '{' '}'] c.vem])
          [s=%t c=*]        (shop c.vem |=(a=@ ~(rt at a)))
          [s=%v c=*]        (shop c.vem |=(a=@ ~(ruv at a)))
          [s=%x c=*]        (shop c.vem |=(a=@ ~(rux at a)))
          [s=[%m p=@] c=*]  (shep p.s.vem c.vem)
          [s=[%r p=@] c=*]
        $(vem [[%r ' ' (cut 3 [0 1] p.s.vem) (cut 3 [1 1] p.s.vem)] c.vem])
      ::
          [s=[%r p=@ q=@ r=@] c=*]
        :+  %rose
          :*  p=(mesc (trip p.s.vem))
              q=(mesc (trip q.s.vem))
              r=(mesc (trip r.s.vem))
          ==
        |-  ^-  (list tank)
        ?@  c.vem
          ~
        [^$(vem -.c.vem) $(c.vem +.c.vem)]
      ::
          [s=%z c=*]        $(vem [[%r %$ %$ %$] c.vem])
          *                 !!
      ==
  ++  shep
    |=  [fom=@ gar=*]
    ^-  tank
    =+  l=(met 3 fom)
    =+  i=0
    :-  %leaf
    |-  ^-  tape
    ?:  (gte i l)
      ~
    =+  c=(cut 3 [i 1] fom)
    ?.  =(37 c)
      (weld (mesc [c ~]) $(i +(i)))
    =+  d=(cut 3 [+(i) 1] fom)
    ?.  .?(gar)
      ['\\' '#' $(i (add 2 i))]
    (weld ~(ram re (show d -.gar)) $(i (add 2 i), gar +.gar))
  ::
  ++  shop
    |=  [aug=* vel=$-(a=@ tape)]
    ^-  tank
    ?:  ?=(@ aug)
      [%leaf (vel aug)]
    :+  %rose
      [[' ' ~] ['[' ~] [']' ~]]
    =>  .(aug `*`aug)
    |-  ^-  (list tank)
    ?:  ?=(@ aug)
      [^$ ~]
    [^$(aug -.aug) $(aug +.aug)]
  ::
  ++  shol
    |=  lim=*
    :+  %rose
      [['.' ~] ~ ~]
    |-    ^-  (list tank)
    ?:  ?=(@ lim)  ~
    :_  $(lim +.lim)
    ?+  -.lim  (show '#')
        ~   (show '$')
        c=@  (show c.lim)
        [%& %1]  (show '.')
        [%& c=@]
      [%leaf '+' ~(rud at c.lim)]
    ::
        [%| @ ~]  (show ',')
        [%| n=@ ~ c=@]
      [%leaf (weld (reap n.lim '^') ?~(c.lim "$" (trip c.lim)))]
    ==
  --

Last updated