Dictionary - defining new operators

Our PostScript machine uses the stack as main data structure, but it has also a dictionary. The dictionary allows to define values and to use them later.

We adapt the machine to allow for dictionaries. There is a new state "name" initiated with the slash character and "procedure" in curly brackets. We also better define whitespace.

Javascript

We did also rewrite the return. The rpn function now not only returns the top of a stack, but also the entire context. We rewrite postScriptEditor to show the elements properly.

Javascript

We add new operator def. However, we see that the operator uses not only the stack, but also the dict. We therefore refactor the operators that they work on a context which includes stack and dict.

Javascript

We try it out. We define foo, bar and then put foo on the stack.

Javascript Editor

If you define a name by another name it is not the same if you define directly the value (early binding) or if you define it in a procedure (late binding).

Javascript Editor

Javascript Editor

We can now define a new operator and use it

Javascript Editor

What if we need to use the parameter multiple times, for example if we square? We add the most PostScript stack operators dup, pop, exch.

Javascript

Javascript Editor

We add trigonometric functions to create a function with two parameters.

Javascript

So we convert polar coordinates to cartesian coordinates

Javascript Editor

This method to capture multiple parameters is propably more readable than handling the stack, though it is possible. Be aware that you must capture the stack in reverse order. Note also, that the dictionary we have implemented uses a global namespace. We will correct that later.

The dictionary can also overrule the operators, though it is not sure when this would make sense.

Javascript Editor

The current code is now 236 lines ps20240627.js

My Journey to PostScript