% This file was converted from HTML to LaTeX with Nathan Torkington's
% html2latex program
% Version 0.9b
\chapter{Programming in PostScript}

Programming in PostScript is really pretty easy.  The fundamentals are 
that you push operands onto the operand stack by naming them, and then 
you invoke the operand to use them.  That's really all there is to it.  
The real art is knowing which operand to use.  Operators to draw and 
put text on the screen will be covered later, and these make up the 
bulk of PostScript code, but there are a couple that are used mainly 
for maintaining the program itself.
 
The first of these operators is \code{def}.  \code{def} is responsible 
for entering a definition into the top-most dictionary on the 
dictionary stack.  The top operand on the operand stack is the value, 
and the operand below the value is the key (and should be a name).  
Let's say that we wanted to define the name \code{x} to have a value 
of 5.  The PostScript to do this is: \code{/x 5 def}.  Notice the use 
of the slash on the \code{x}.  The slash ensures that the {\it name} 
\code{x} will be pushed onto the stack and not any value it may 
already have in the dictionary stack.

\code{def} is also used to define new operators.  The value in 
this case is just a procedure.  The following code defines an operator
\code{foo} which adds its top-most two operands and multiplies the result 
with the next operand on the stack: \code{/foo \{add mul\} def}.  Remember, 
operands that return results push them onto the stack, where they may 
be used later.  

An important point to know when defining procedures is that the 
elements in the procedure are not evaluated {\it until} the procedure 
is invoked.  That means that in the procedure \code{\{add mul\}}, the 
actual names \code{add} and \code{mul} are stored in the array that is 
the procedure.  This is different from an actual array in which the 
components {\it are} evaluated when the array is created.  For 
contrast, the array \code{[add mul]} contains two objects: the {\it 
procedures} defined as \code{add} and \code{mul}.

This delayed evaluation of procedure components has two important 
effects.  First, the definition of an operator used in a procedure is 
the one that is in effect when the procedure is run, {\it not } when 
it is defined.  Second, because each operator has to be looked up each 
time the procedure is invoked, things can be a little slow.  
Fortunately, PostScript provides a handy operator to replace each name 
in a procedure object with it's current definition.  This operator is 
called \code{bind}, and it can speed up your program considerably.
\code{bind} is typically used as: \code{/foo \{add mul\} bind def}.

This defines \code{foo} to be a procedure array with two components: 
the procedures for \code{add} and \code{mul}.  Note that, if 
\code{add} or \code{mul} is re-defined after defining \code{foo}, 
\code{foo} will have the {\it same } behavior as before.  Without the 
use of \code{bind}, \code{foo}'s behavior would change.
