PostScriptish interpreter in Laszlo

Grig and I were discussing how to evaluate arbitrary mathematical expressions in Laszlo applications, in the absence of "eval".

The Flash runtime doesn’t support parsing math expressions with infix notation like "(1 + 2) * 10" – that would require the Laszlo compiler, or a lot of nasty JavaScript parsing code.

I think it would be easy to implement a simple rpn stack machine like PostScript, that used string.split(" ") to break a string apart into tokens, which Flash can do fast.

So I hacked up this Laszlo interpreter class that behaves almost but not quite completely unlike a PostScript interpreter.

Of course it's a bit different since it has an extremely simple space delimited syntax, it's just a thin layer around JavaScript supporting its standard data types, and it's easily extensible with JavaScript functions.

I stuck to PostScript function names and meanings since that’s pretty widely understood, and only changed their parameters and meanings when necessary to adapt to JavaScript (like dict based forall not acting differently with arrays).

In standard PostScript, each object has an executable bit that changes the way the interpreter treats it, but since this interpreter is written in JavaScript, it doesn’t have an executable bit, so it has slightly different semantics than PostScript, in that it doesn't automatically execute variables containing executable objects, so you have to call "exec" yourself after loading a function variable by saying its name, like "10 fact exec". But it does automatically execute primitives, so you can go "1 2 add print" to print out 3.

Space delimited strings of tokens take the place of PostScript executable arrays. Numeric tokens are pushed on the operand stack. String tokens are looked up on the dictionary stack, and their value pushed on the operand stack. Tokens beginning with : are pushed as literal strings (names). There’s no such thing as a PostScript inline executable array (nested string), so you have to give names to the branches of your if and ifelse and for loop operators, and call their names before the operator to push the string of tokens onto the stack.

Here’s an example of a dictionary with a factorial function, which you can call by pushing this dict on the dict stack and executing "10 fact exec":

"fact": "dup 1 eq factdone factrecur ifelse",
"factdone": "",
"factrecur": "dup 1 sub fact exec mul"

So when it executes "factdone", it just pushes the value of the factdone string onto the stack (a noop empty string), and "factrecur" pushes the string "dup 1 sub fact exec mul" onto the stack. Then ifelse looks at the Boolean parameter (dup 1 eq) and executes the nop "" if true, or recursive factorial if false. Note that the recursive function calls "fact exec" instead of just "fact" automatically executing the falue, since there is no notion of an executable bit.

It even gives the right answer!

The idea is that you can subclass the interpreter and add new primitive operators or application specific functions, by defining methods named "op_operatorname". It would be easy to flesh it out with most of the rest of the standard PostScript operators, but it would be best to keep a clean minimal core class that is useful for simple tasks.

Here is the link to run the PostScriptish interpreter written in OpenLaslzo, and view the source.

Try typing "10 fact exec stack" into the "exec" text field, to compute the factorial of 10 and print out the stack.

Gotta think of a better name than "PostScriptish". How about PostScribble? PostScratch? PostCrap? PissedScript?