4 3 add 2 mul - PostScript as a programming language

PostScript uses the Reversed Polish Notation (RPN) which in its purest form, is a string with values and operators that work on a stack.

A stack is an array of values with no limited length. At the beginning, the stack is empty. You can push values on the top of the stack and pop them to get them back. The current value of a stack is the top most item.

The first PostScript machine we build here in Javascript is a RPN calculator. The machine reads the program which is a string of tokens separated by space. A token is a word in the string separated by space.

En example program would be "4 3 add 2 mul"

  • If the token is a number, it is pushed on the stack
  • If the token is a word, we read the word as operator. The operator might pop some values, make some calculations and then push some values on the stack.

In detail for our example

token stack
4 4
3 4 3
add 7
2 7 2
mul 14

This is all of it. There are no variables, no control structures, just linear execution of code, however, you still can make complex calculations.

We use a state machine to parse the program. A state machine reads each character at once and performs an action when it has identified a token.

  • A number starts with a digit or dash, and continues with digits and at most one period.
  • An operator starts with a letter and continues with letters or digits.
  • All other characters are treated as space.

This graphical representation was made with the postscript code on the right

My Journey to PostScript

In detail for our example

character action stack state current
4 - empty number 4
space stack.push(4) 4 start empty
3 - 4 number 3
space stack.push(3) 4 3 start empty
a - 4 3 operator a
d - 4 3 operator ad
d - 4 3 operator add
space add(stack) 7 start empty
2 - 7 number 2
space stack.push(2) 7 2 start empty
m - 7 2 operator m
u - 7 2 operator mu
l - 7 2 operator mul
space mul(stack) 14 start empty

Javascript

Now we just need to define some operators. We define add, sub, mul and div.
We set 0 if the stack is empty.

Javascript

You can try it out. Change the value of program and rerun the script

Javascript Editor

During our journey, our code will evolve. We will add features, fix bugs and sometimes refactor our code. On each chapter, we will reuse the code of the last chapter and only redefine functions, classes and objets when needed. To be able to do that, we always user variables to declare functions and classes.

The 94 lines of source code from this chapter are ps20240624.js

My Journey to PostScript