Stack machines
One pile of numbers. Push on top, pop off the top. Half of all esolangs keep their memory this way.
●○○ easy · where to see it
The idea
A stack is a pile; you can only add to the top (push) and take from the top (pop). A stack machine keeps its working data on a stack, so instructions don't need operands. + doesn't say what to add; it adds the top two items.
Read the story
The idea is old and in wide use. The Burroughs B5000 (1961) was a hardware stack machine. The Java virtual machine, WebAssembly and PostScript are stack machines today. Reverse Polish notation, the order 2 3 +, comes from Jan Łukasiewicz's Polish notation turned around.
Esolang authors love stacks because they make an interpreter almost trivial, and because a program becomes a flat row of symbols with no parentheses.
How it works
Evaluate 2 3 + 4 *, that is (2 + 3) × 4.
Evaluate 2 3 + 4 *, that is (2 + 3) × 4.
token stack after
2 [2]
3 [2, 3]
+ [5] pop 3, pop 2, push 2 + 3
4 [5, 4]
* [20] pop 4, pop 5, push 5 × 4
Every instruction does the same three moves. It pops its inputs, computes, and pushes the result. So the interpreter is a loop and a table.
A few standard stack words appear everywhere under different names.
| Word | Before → after | Typical spelling |
|---|---|---|
| dup | a → a a | : in Befunge, Underload and ><> |
| drop | a → | $ in Befunge |
| swap | a b → b a | \ in Befunge |
| over | a b → a b a | over in Forth |
Variations
- Two stacks. Forth keeps a separate return stack for calls; Brain-Flak gives you two data stacks and you toggle between them.
- A stack of stacks. ><> can open a new stack on top of the old one.
- One stack per type. Push has an integer stack, a boolean stack, a code stack and more. An instruction takes its inputs from the stacks of the right type.
What happens on an empty stack?
This is a design choice, and it matters for robustness.
Five machines
| Machine | Memory | Control | Examples |
|---|---|---|---|
| Tape | a row of cells and a pointer | loops that test the current cell | brainfuck, Ook!, Boolfuck |
| Stack | values piled up; only the top is easy to reach | jumps, loops or quotations that run | Underload, FALSE, the kernel |
| Queue | values in a line, added at the back and taken from the front | loops on the front value | Sceql, Qdeql, Emmental |
| Rewriting | nothing but the text itself | replace a pattern until none matches | ///, Thue, Markov algorithms |
| 2D | usually a stack; the program is a grid | a cursor that arrows and mirrors turn | Befunge-93, ><>, Labyrinth |
One task on four machines
Each program below reverses its input.
Each program below reverses its input.
Tape: walk right, then walk back
Each byte goes into its own cell. The pointer then walks back and prints. The tape remembers the order by position.
Expected output
desserts
Output
Stack and lists: fold
The kernel puts each element in front of an accumulator list. See map, filter and fold.
Expected output
desserts
Output
2D stack: pushing reverses by itself
Row 1 reads characters onto the stack until the end of input. A stack gives back the last thing first, so printing it all is already the reverse. $ drops the −1 that marked the end.
Expected output
desserts
Output
Rewriting: no input at all
/// cannot read input, so the text to change is in the program. The rule swaps ab to ba until none is left, which moves every b to the front.
Expected output
bbaa
Output
Why it matters
A tape makes "where" cheap and "copy" expensive (see moving and copying).
A tape makes "where" cheap and "copy" expensive (see moving and copying). A stack makes copying cheap and far-away values expensive. A queue keeps order. Rewriting has no variables at all. In 2D the shape of the text is the control flow.
The kernel was built to host all of them. It has a stack, memory with a pointer, quotations, a 2D reader and a rewrite reader. See how a language can evolve.
Try this
- Write
(1 + 2) × (3 + 4)in stack order. - Using only
dup,swap,dropand*, compute x³ from[x]. - Hard: write a stack program that, given
[a b], leaves[a b a b], using onlydup,swapandover. How few words do you need?
Seen in
- Forth and FALSE. The classic stack languages.
- Befunge, ><> and Piet. A 2D instruction pointer driving a stack.
- Whitespace. A stack machine with a heap, written in spaces, tabs and newlines.
- Brain-Flak. Two stacks and brackets only.
- GolfScript. A stack makes golfed programs short.
- Stack Cats. Stacks where every operation can be undone.
- Push. Typed stacks, designed for genetic programming.
Further
- esolangs.org/wiki/Category:Stack-based
- Stack machine on Wikipedia
- Lee Spector and others, papers on Push and PushGP.
- See also concatenative, two-dimensional control flow, every text runs.