Tom Duff on Duff's device (1988)
NeWS had some spectacular abuses of C preprocessor macros with nested switch statement, loops and gotos.<p>Especially the conic splines code by Vaughan Pratt and James Gosling, which &quot;flattens&quot; Bezier curves into conic splines (unlike straight lines like most PostScript renderer implementations), which Vaughan Pratt&#x27;s conix package can draw really fast.<p><a href="http:&#x2F;&#x2F;www.chilton-computing.org.uk&#x2F;inf&#x2F;literature&#x2F;books&#x2F;wm&#x2F;p005.htm" rel="nofollow">http:&#x2F;&#x2F;www.chilton-computing.org.uk&#x2F;inf&#x2F;literature&#x2F;books&#x2F;wm&#x2F;...</a><p>SunDew - A Distributed and Extensible Window System<p>[...] Two pieces of work were done at SUN which provide other key components of the solution to the imaging problems. One is Vaughan Pratt&#x27;s Conix [53], a package for quickly manipulating curve bounded regions, and the other is Craig Taylor&#x27;s Pixscene [63], a package for performing graphics operations in overlapped layers of bitmaps. [...]<p>[...] Pixscene is based on a shape algebra package. The ability, provided by Conix, to do algebra very rapidly on curves should make non-rectangular windows perform well. [...]<p>[53] Pratt, V., Techniques for Conic Splines, Computer Graphics 19(3), pp.151159 (1985).<p>[63] Taylor, C. and Pratt, V., A Technique for Representing Layered Images. SUN Microsystems Inc., 2550 Garcia Avenue, Mountain View, CA94043.<p>Direct Least-Squares Fitting of Algebraic Surfaces:
Vaughan Pratt,
Sun Microsystems Inc.
and
Stanford University.
April 30, 1987.<p><a href="http:&#x2F;&#x2F;boole.stanford.edu&#x2F;pub&#x2F;fit.pdf" rel="nofollow">http:&#x2F;&#x2F;boole.stanford.edu&#x2F;pub&#x2F;fit.pdf</a><p><a href="http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;graphics&#x2F;pathtoarc.c" rel="nofollow">http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;graphics...</a><p><pre><code>    &#x2F;*-
        Convert a PostScript style path to an arclist
        pathtoarc.c, Thu Sep 19 15:45:44 1985
     *&#x2F;
</code></pre>
<a href="http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;graphics&#x2F;arctochain.c" rel="nofollow">http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;graphics...</a><p><pre><code>    &#x2F;*-
        Convert a conic arc to a curve
        arctochain.c, Tue Jun 11 14:23:17 1985
        &quot;Elegance and truth are inversely related.&quot; -- Becker&#x27;s Razor
            Vaughan Pratt
            Sun Microsystems
            This code is a version of the conix package that has been
            heavily massaged by James Gosling.
     *&#x2F;
</code></pre>
[...]<p><pre><code>    &#x2F;* macros for the conic machines *&#x2F;
    #define out(b) {		\
        if (b)			\
        xpos += dir;		\
        else			\
        *ypos++ = xpos;		\
    }

    #define across(rel,q1) {						\
            out(1);							\
            Fx += Fxx, Fy += Fyx;					\
            if ((F += Fx) rel 0)					\
                goto q1;					\
        }

    #define down(rel,q1) {							\
            out(0);							\
            if (--rem0 &lt; 0)						\
                goto final;					\
            Fx -= Fxy, Fy -= Fyy;					\
            if ((F -= Fy) rel 0)					\
                goto q1;					\
        }

    #define trans1(axis, q1)						\
        for (;;)							\
            axis(&gt;=, q1);

    #define trans2(axis, q1, rel, q2)					\
        for (;;) {							\
            axis(&lt;, q1);						\
            if (Fx rel Fx_err)					\
                goto q2;					\
        }
</code></pre>
[...]<p><pre><code>        &#x2F;* enter conic machine at appropriate state *&#x2F;
        rem0 = size.y;
        if ((size.x &gt; 0) == (side &lt; 0))
        goto EOD;
        else
        goto OED;
        &#x2F;* OE state machine *&#x2F;
    ODE:trans1(across, OED);
    DOE:trans2(across, ODE, &gt;, OED);
    OED:trans2(down, ODE, &lt;, DOE);
        &#x2F;* EO state machine *&#x2F;
    EDO:trans1(down, DEO);
    DEO:trans2(across, EDO, &gt;, EOD);
    EOD:trans2(down, EDO, &lt;, DEO);
    final:;
        &#x2F;*-	clearasil(buf0+1, bitlen); 	&#x2F;* clean out 0011&#x2F;1100 pimples *&#x2F;
        assert(ypos &lt;= curve-&gt;x + (curve-&gt;y1 - curve-&gt;y0 + 1));
</code></pre>
The main loop of the NeWS PostScript interpreter itself is the realization of Duff&#x27;s dire warning &quot;Actually, I have another revolting way to use switches to implement interrupt driven state machines but it&#x27;s too horrid to go into.&quot;<p><a href="http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;nucleus&#x2F;PostScript.c" rel="nofollow">http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;nucleus&#x2F;...</a><p><pre><code>    &#x2F;*
     * This is the PostScript interpreter.  Its implementation breaks almost every
     * rule of structured programming.  This is a result of needing to support
     * multiple PostScript processes and a paranoia about performance. There are a
     * couple of #includes that you should think of as procedure calls.  Life is
     * tough in the trenches.
     * 
     *&#x2F;

    &#x2F;*
     * The PostScript interpreter has a fairly simple basic structure:
     * 
     * while (1) { 1. Pick up an object to interpret 2. Execute it }
     * 
     * Part 1, picking up an object to interpret is handled by the statement that
     * starts &quot;switch (es-&gt;type)&quot;.  es points to the top of the execution stack: a
     * stack of sources of executable objects.  The switch determines what kind of
     * thing the object is being fetched from: an array, a string, ...
     * 
     * Part 2, executing an object, is handled by the code that starts with the label
     * &quot;execute_top&quot;.  It does some tests and switches on the type of the object
     * being executed.
     * 
     * All of this is complicated by the mechanism for blocking.  If the process
     * needs to block, say by needing to read a new buffer from a socket or by
     * needing to wait for a message, then the interpreter needs to stop working
     * on this process and leave information around that lets it restart.  This is
     * the &quot;restart_state&quot; variable: it is used as the index to a switch statement
     * that is executed when the interpreter starts up, so it won&#x27;t necessarily be
     * intering the main loop of the interpreter at the top. There are cleaner
     * ways of doing this, but they can&#x27;t be written in C!
     *&#x2F;
</code></pre>
[...]<p><pre><code>        &#x2F;*
         * Restart the interpreter where it left off last
         *&#x2F;
        switch (ee-&gt;restart_state) {
        case 1:
        goto read_1;
</code></pre>
[...]<p><pre><code>        case 24:
        goto read_24;
        }

        &#x2F;* The main loop of the interpreter *&#x2F;
        while (1) {
        struct object new;
</code></pre>
[...]<p><pre><code>    &#x2F;* Pick up an object to execute *&#x2F;
        switch (es-&gt;type) {
</code></pre>
[...]<p><pre><code>        case file_execution:{
            register PSFILE *f = es-&gt;env.file.file;
            &#x2F;* be charitable and think of this as a procedure call *&#x2F;
    #ifdef GPROF_HOOKS
    &#x2F;* gP_GetFile defined in parse_file.h *&#x2F;
    #endif
    #include    &quot;parse_file.h&quot;
            break;
            }
</code></pre>
<a href="http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;nucleus&#x2F;parse_file.h" rel="nofollow">http:&#x2F;&#x2F;donhopkins.com&#x2F;home&#x2F;code&#x2F;NeWS&#x2F;SUN&#x2F;src&#x2F;server&#x2F;nucleus&#x2F;...</a><p><pre><code>    &#x2F;*-
     * 	Parse a PostScript object from a PSFILE
     *
     * 	parse_file.h, Mon Dec 31 09:36:02 1984
     *
     * 		James Gosling,
     * 		Sun Microsystems
     *&#x2F;

    &#x2F;*
     * This is a version of getc that pauses if the buffer is empty and suspends
     * the process
     *&#x2F;</code></pre>