A Turing-Complete Stack-Based Programming Language
The Language
Each program has access to one piece of data: a stack of unlimited size, initially empty. The stack is implicit: the program never refers directly to it. However, each operator takes its arguments from the stack and leaves its result there.
The language includes most of the usual data types, both simple (numbers, characters, booleans) and aggregate (strings, lists, sets).
A program is a list of values and operators.
- When a program is executed, it is processed left to right.
- Each literal value is pushed onto the stack.
- Each operator pops zero or more arguments from the stack and pushes its result onto the stack.
Standard operators include:
-
The usual binary arithmetic operators such as
+
,-
,*
,/
, andrem
(remainder). -
Typical unary arithmetic operators such as
sqrt
,abs
, andneg
(negate). -
Operators that manipulate the stack, including:
-
dup
makes a copy of the value on top of the stack and pushes it. -
swap
reverses the two values on top of the stack. -
rollup
moves the value on top of the stack down to the third position.
-
Every program ends with a period.
The language also includes "quoted programs" and higher-order
operators such as map
. (More later.)
Some Programs
This program 2 3 + .
computes (2 + 3) and leaves a 5 on the stack.
This program 2 3 dup * + .
computes ((3 * 3) + 2) and leaves 11 on the stack.
What does the stack look like after executing each of these programs?
-
302 1000 500.0 / -.
-
10 6 swap dup * swap dup * - sqrt.
-
4.95 5.0 0.1 rollup - abs >=.