Homoiconicity Revisited
PostScript is homoiconic, and semantically quite Lisp-like, when it&#x27;s not being syntactically somewhat Forth-like.<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21968842" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21968842</a><p>&gt;[...] PostScript and Lisp are homoiconic, but Forth is not. The PSIBER paper on medium goes into that (but doesn&#x27;t mention the word homoiconic, just describes how PS data structures are PS code, so a data editor is a code editor too).<p><a href="https:&#x2F;&#x2F;medium.com&#x2F;@donhopkins&#x2F;the-shape-of-psiber-space-october-1989-19e2dfa4d91e" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@donhopkins&#x2F;the-shape-of-psiber-space-oct...</a><p>Also, here is a metacircular PostScript interpreter, ps.ps: a PostScript interpreter written in PostScript! Since PostScript is homoiconic and so much like Lisp, it was as easy as writing a metacircular Lisp interpreter (but quite different in how it works, since PostScript and Lisp have very different execution models).<p><a href="https:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;archive&#x2F;psiber&#x2F;cyber&#x2F;ps.ps" rel="nofollow">https:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;archive&#x2F;psiber&#x2F;cyber&#x2F;ps.ps</a><p>The heart of the metacircular interpreter, &quot;iexec&quot;, uses some dynamically defined macros like MumbleFrotz, PushExec, Popexec, containing embedded literal references to data (a dictionary representing the interpreter state, and ExecStack, and array representing the execution stack).<p><pre><code>    % interpretivly execute an object

    &#x2F;iexec { % obj =&gt; ...
      100 dict begin
        % This functions &quot;end&quot;s the interpreter dict, executes an object in the
        % context of the interpreted process, and &quot;begin&quot;&#x27;s back onto the
        % interpreter dict. Note the circularity.
        &#x2F;MumbleFrotz [ % obj =&gt; ...
          &#x2F;end load &#x2F;exec load currentdict &#x2F;begin load
        ] cvx def

        &#x2F;ExecStack 32 array def
        &#x2F;ExecSP -1 def

        &#x2F;PushExec [ % obj =&gt; -
          &#x2F;ExecSP dup cvx 1 &#x2F;add load &#x2F;store load
          ExecStack &#x2F;exch load &#x2F;ExecSP cvx &#x2F;exch load &#x2F;put load
        ] cvx def

        &#x2F;PopExec [ % obj =&gt; -
          ExecStack &#x2F;ExecSP cvx &#x2F;get load
          &#x2F;ExecSP dup cvx 1 &#x2F;sub load &#x2F;store load
        ] cvx def

        &#x2F;TraceStep {
          iexec-step
        } def

        PushExec

        { ExecSP 0 lt { nullproc exit } if % nothing left to execute? goodbye.

          ExecStack 0 ExecSP 1 add getinterval
          TraceStep pop

          % pop top of exec stack onto the operand stack
          PopExec

          % is it executable? (else just push literal)
          dup xcheck { % obj
            % do we know how to execute it?
            dup type
            &#x2F;&#x2F;iexec-types 1 index known { % obj type
              &#x2F;&#x2F;iexec-types exch get exec % ...
            } { % obj type
              % some random type. just push it.
              pop % obj
            } ifelse
          } if % else: obj

        } loop % goodbye-proc

        currentdict &#x2F;MumbleFrotz undef % Clean up circular reference
      end
      exec % whoever exited the above loop left a goodbye proc on the stack.
    } def

</code></pre>
It also has a &quot;vexec&quot; function that executes arbitrary PostScript code, and prints out another text PostScript program to draw an animated trace diagram of the operand stack depth -vs- execution stack depth. That&#x27;s another simpler kind of macro that produces text instead of structures. So I was using the metacircular PostScript interpreter to visualize one structural PostScript program&#x27;s execution, by producing and executing the resulting text PostScript program in another PostScript interpreter!<p>I don&#x27;t currently have the PS output or rendered images decoded and online, but here&#x27;s a readme:<p><a href="https:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;archive&#x2F;psiber&#x2F;cyber&#x2F;twist.readme" rel="nofollow">https:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;archive&#x2F;psiber&#x2F;cyber&#x2F;twist.readm...</a><p>This is a plot of the execution stack (x-axis) and the operand stack (y-axis)
during the execution of the PostScript &#x2F;quicksort routine. Each successive
picture is more twisted than the last. Twisting is accomplished by rotating
the coordinate system clockwise slightly around the center of each of the
dots as they are drawn. The rotation around each plotted point accumulates
to make the whole drawing curl up. The more twisted away from the original
orientation a point is, the later it occurred in time. 
In the first picture, the untwisted version, up corresponds to a deeper
operand stack (pushing things on the stack moves you up), and right
corresponds to a deeper execution stack (procedure calls and control
structures move you right). The lines follow changes in the state of
the stack between steps of the interpreter. (This was made possible by
a PostScript interpreter written in PostScript.)<p>To see the twist animation, run monochrome NeWS, type &quot;psh&quot; to the shell,
then type &quot;(&#x2F;wherever&#x2F;you&#x2F;put&#x2F;it&#x2F;twist.ps)run&quot;.
The reason you can&#x27;t just psh the file directly is that NeWS 1.1 psh
does $1 $2 $3 arg substitution, even on binary data! (X11&#x2F;NeWS psh should 
work, so you can just go &quot;psh twist.ps&quot;)<p>What the file twist.ps contains is a short header defining the function
&quot;c&quot;, which reads a canvas and displayed it on the framebuffer. That is 
followed by a series of &quot;c&quot;&#x27;s each followed by a 1 bit deep 1152x900 
sun raster files.<p>-Don Hopkins
  (don@brillig.umd.edu)