PostScript Interpreter
PostScript is a programming language for describing printed pages. This interpreter, built in Python, runs a subset of PostScript so you can see how operators use the stack, how procedures run, and how identifiers are resolved.
Try Static vs. dynamic scope to see how changing the lookup rule changes the result of the same program.
Source editor
Results
Ready to run
Final operand stack
Top firstValues left on the stack when execution ends. There can be zero, one, or several.
Run a program to see what remains.
Printed output
Text printed by commands in your PostScript program, such as = and stack. Read a stack dump.
Run a program to see what it prints.
How the interpreter works
This interpreter explores how PostScript moves values through a stack and resolves identifiers as a program runs. PostScript uses postfix notation: operands come before the operator, as in 8 3 sub. The interpreter pushes 8, then 3, then runs sub to compute 8 − 3. A binding associates a name with a value or procedure. Scope determines which binding an identifier refers to. Lexical scope follows where a procedure is defined. Dynamic scope follows the active calls. The same identifier can therefore refer to different bindings depending on the lookup rule and, with dynamic scope, the runtime call path.
Static vs. dynamic scoping
Scope determines where the interpreter looks for the definition of an identifier. PostScript uses dynamic lookup. This interpreter also supports static lookup so you can run the same program under both rules.
Dynamic lookup can make a program harder to understand because the value of an identifier may depend on which procedures called it. You may need to follow the program's execution to know which definition is found. Static, or lexical, scope ties each reference to the surrounding source code, making it easier to reason about a procedure without tracing every possible caller. For that predictability, most modern general-purpose languages use static scope by default, though some languages still support dynamic bindings in specific cases.
In the Static vs. dynamic scope example, g reads x and f calls g. The outer dictionary defines x as 4, and f defines a local x as 7.
g finds a different x under each lookup rule.Lexicalg (2) → definition link → outer (0): x = 4
Dynamicg (2) → active caller → f (1): x = 7
Static scope finds 4
Lookup starts in g's local dictionary and follows a link to the dictionary where g was defined. That link leads directly to the outer x.
Dynamic scope finds 7
Lookup starts in g's local dictionary and then searches the active calls, most recent first. Since f called g, its local x is found first.
Return to the editor and run Static vs. dynamic scope with each scope setting. Compare the top of the final operand stack. The source and call order stay the same.
Syntax key
PostScript calls identifiers such as x names.
/x- Push the identifier
xitself. The slash makes it a literal name, so the interpreter skips lookup. x- Look up
x. Push its value, or run the stored procedure if it refers to one. def- Take a name and value off the stack and store the definition in the current dictionary.
/x 4 defstores4underx. { … }- Push a block of instructions for later execution.
/g { x stack } defdefinesgas a procedure. [ … ]- Push an array as one stack item. The examples use integer arrays such as
[1 2 3 4]. dup- Duplicate the top operand. For example,
5 dupleaves two copies of5. lt- Compare the two top operands and push
trueif the second-from-top value is less than the top value. In3 5 lt, it pushestrue. add,sub,mul,div- Take two numbers from the stack and push their sum, difference, product, or quotient.
if,ifelse- Run one or two procedure blocks depending on a boolean value.
for,forall- Run a procedure for a sequence of numbers or for each item in an array.
=- Remove the top operand and print it.
stack- Print the operand stack and dictionary stack as they are at that point in the program, leaving their contents in place.
Working with a stack
The operand stack is where operators find their inputs and leave their results. The last value pushed is the first one an operator takes. In 8 3 sub, the stack grows from 8 to 3, 8 (top first). sub pops 3 first, then 8, and computes 8 − 3, leaving 5.
8 3 sub, top value shown first8push 83
8push 35sub computes 8 − 3A block of instructions is itself a value. In Sum of squares, def saves {dup mul} as the procedure square. Then forall pushes each array element in turn and runs {square} on it. The same stack carries numbers, arrays, and code.
Reading the output of Static vs. dynamic scope
The stack inside g prints a snapshot while g is running. The operand stack holds values that operators work on. The dictionary stack holds definitions. Each dictionary maps identifiers to values or procedures.
In this interpreter, each procedure call creates a local dictionary and removes it when the call returns. This per-call dictionary rule is part of this interpreter’s simplified design. Full PostScript instead uses its dictionary stack, which programs can change explicitly. def writes to the current dictionary, so f can give x a local value while the outer dictionary keeps its own definition. Read the snapshot from top to bottom.
staticordynamicis the lookup mode for that run.- The first
==============section lists the operands, top first. Here it shows4or7. - Below the next divider are the dictionaries, most recent first. Dictionary
2belongs togand is empty. Dictionary1belongs tofand contains/x 7. Dictionary0is the outer dictionary and contains/x 4and the definitions offandg. - The two numbers in
----2----0----identify the dictionary (2) and where static lookup goes next (0, wheregwas defined). Dynamic lookup checks the active dictionaries in order, so from2it checks1next.
The Final operand stack shows the same 4 or 7 here because stack leaves the value in place and no later instruction removes it. By comparison, 2 3 add = prints 5 and leaves the stack empty.