Whitespace
Only spaces, tabs and newlines mean anything. Every visible character is a comment, so a program can hide inside any other text.
Edwin Brady and Chris Morris, 2003 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 1 example and 1 known program · Hello World on Try It Online: identical · Test method
Run it
Hello World
Expected output
Hello, world
Output
The idea
Edwin Brady and Chris Morris released Whitespace on 1 April 2003. Most people took it for an April Fools' joke. It wasn't: it is a complete, if ordinary, stack machine.
Read the story
Most languages ignore whitespace. Whitespace ignores everything else. That flips the usual idea of a comment. The visible text is the comment, and the program lives in the gaps between the words.
The practical result is the polyglot. A C or Python file can carry a Whitespace program in its indentation and line endings, and both languages run their own part.
The machine
A stack of unbounded integers, a heap (address to integer), and a call stack. Write space as S, tab as T and linefeed as L.
Each instruction starts with an IMP (instruction modification parameter) that names its group:
| IMP | Group |
|---|---|
S | Stack |
TS | Arithmetic |
TT | Heap |
L | Flow control |
TL | Input/output |
Numbers are a sign (S plus, T minus), then binary digits (S = 0, T = 1), then L. Labels are any string of S and T, ended by L.
| Code | Does |
|---|---|
SS n | Push n |
SLS SLT SLL | Duplicate, swap, drop |
STS n, STL n | Copy the *n*th item to the top; slide n items off under the top |
TSSS TSST TSSL TSTS TSTT | Add, subtract, multiply, divide, modulo |
TTS TTT | Store (address, value), retrieve |
LSS l | Mark label l |
LST l, LSL l, LTL | Call, jump, return |
LTS l, LTT l | Jump if the top is zero, if negative (pops it) |
LLL | End |
TLSS TLST | Print a character, print a number |
TLTS TLTT | Read a character, a number, into the heap address on top |
The instruction set is a prefix code: no instruction is the start of another, so the stream can be parsed without separators.
Example
Count from 1 to 3.
Count from 1 to 3. In the visible notation, S, T and L are code and everything else is a comment:
SS STL push 1
LSS SL mark label s
SLS dup
TLST print it as a number
SS STSTSL push 10 (newline)
TLSS print it as a character
SS STL push 1
TSSS add
SLS dup
SS STSSL push 4
TSST subtract
LTS TL jump to label t if zero
LSL SL jump to label s
LSS TL mark label t
LLL end
The counter stays on the stack. Each pass prints it, adds 1, and tests counter - 4. At 4 the test is zero and the program ends. It prints 1, 2, 3 on three lines. The comments avoid the capital letters S, T and L.
Quirks
- A run stops after 3,000,000 steps.
Try this
- Write a program that prints
A(65 isTSSSSST). - Write
catfor one line: read characters into the heap and print them until a newline. - Hard: hide a Whitespace program in the indentation of a working Python file.
Patterns it teaches
- Camouflage: the program is invisible. The disguise is not a theme (like Chef's recipes) but the absence of anything to see.
- Polyglot: because other languages ignore what Whitespace reads, one file can be two programs.
- Stack machines: under the disguise it is a plain stack machine with a heap, close to Forth.
- Encoding tricks: a three-symbol alphabet and a prefix code, the same idea as Ook! hiding Brainfuck, pushed to the limit.
Further
- esolangs.org/wiki/Whitespace
- The original Whitespace site and tutorial by Brady and Morris (2003), now only on web archives.
- This implementation covers the whole v0.3 instruction set.
See also
Stack-based
Write your own in Run Convert a program from Whitespace
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: whitespace.
Command line
shib run whitespace -e 'Hello, world!
'shib manual whitespaceSource
- Module
langs/classic.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org