the number of named formal parameters. This means that a stack-based language
can tell how many items to pop off the stack to pass to the function.
(That is, you can copy this post out of your browser into a .js file and
run it in Node.) */
/* The “Standard Library” is implemented as a
/* run is the entry point to the interpreter.
It resets the inputBuffer and stack fields. */
/* word removes one word from the inputBuffer and returns it. */
/* interpret decides how to handle each word.
If the word is in the dictionary, execute it.
If it is a number, push the number onto the stack.
Otherwise throw an exception.
It provides the top fn.length
items from the stack as arguments to the function.
If fn returns a value, the value is concatenated
back on top of the stack.
Multiple values can be returned in an array;
Array.prototype.concat will correctly concatenate
all of the values to this.stack in the intended order.
/* Unit tests to demonstrate usage */
/* An empty program leaves an empty stack. */
/* The standard library: dup, -, +, swap */
/* We can reflect on just about any part of the execution of the interpreter. */
/* For example, calling interpret without run or word
can be used to single-step: */
/* We can execute a function that isn’t even in the dictionary: */
/* Interpreting the word 'execute' pops a function off the stack */
/* Use the word tokenizer function to put single-word Strings on the stack */
/* The preexisting + operator can work with this new String data type */
/* And interpret can be called on Strings on the stack */
/* Even if the String is a result of some other computation! */