Language Concepts

As a programming language, PostScript is particularly simple. There are really only a few concepts that need to be sketched out.

A comment in PostScript is any text preceded by a “%”. The special comment “%!” as the first two characters of a PostScript program is seen as a tag marking the file as PostScript code by many systems (including Unix’s lpr command). It is a good idea to start every PostScript document with a “%!”... doing so will ensure that every spooler and printer the document may encounter will recognize it as PostScript code.
There are several stacks in a PostScript system, but only two are important for this guide: the operand stack, and the dictionary stack. The operand stack is where arguments to procedures (or operators, in PostScript jargon) are pushed prior to use. The dictionary stack is for dictionaries, and it provides storage for variables.
A dictionary is a collection of name-value pairs. All named variables are stored in dictionaries. Also, all available operators are stored in dictionaries along with their code. The dictionary stack is a stack of all currently open dictionaries. When a program refers to some key, the interpreter wanders down the stack looking for the first instance of that key in a dictionary. In this manner, names may be associated with variables and a simple form of scoping is implemented. Conveniently, dictionaries may be given names and be stored in other dictionaries.
A name is any sequence of characters that can not be interpreted as a number. With the exception of spaces and certain reserved characters (the characters “(”, “)”, “[”, “]”, “<”, “>”, “{”, “}”, “/”, and “%”) any character may be part of a name. The name may even start with digits (1Z is a name, for example), but you can get into problems with them (1E10 is a real number). A name is seen as being a reference to some value in a dictionary on the dictionary stack.

It should be noted that there are a couple of names that are legal in PostScript which do not follow the above definition. These are the “[” and the “]” operators. Yes, they are operators and are stored in the dictionary. Some other names that might surprise you are: “=”, “==”, “<<”, and “>>”.

If a name is preceded by a slash, PostScript will place the name on the stack as an operand. If the name has no slash, the interpreter will look up its value in the dictionary stack. If the value is a procedure object, the procedure will be evaluated. If the value is not a procedure, the value will be pushed onto the operand stack.

PostScript supports integers and reals. You can express numbers in two forms: radix form, and scientific notation. Radix form is a number of the form radix#value where radix specifies the base for value. Scientific notation is the standard mantissaEexponent form used in most languages.
Strings are, of course, just strings of characters. There are two ways of expressing strings in Level 1 PostScript. The most common way is to wrap your text in parentheses. For example the string “This is a string” would be written as (This is a string). You can also express a string as hexadecimal codes in angle brackets. For example, the string “ABC” would be expressed as <414243>. There are several escape codes that may be used in the parenthesis format of strings.
Arrays in PostScript are like arrays in any other language. Arrays may contain objects of different type, and they are written as a list of objects surrounded by brackets. For instance, [12 /Foo 5] is a three element array containing the number 12, the name Foo, and the number 5.
A procedure is your way of defining new operators. A procedure is an array that is executable and is written with braces rather than brackets. For example, a procedure to square the top element on the stack might be written as: {dup mul}. We can define this procedure to be the square operator with: /square {dup mul} def.