Direction
A 2D language where every arrow is gone once you have stepped on it.
2024 · Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Direction is a grid language with a stack, like Befunge, and one catch. An arrow vanishes as soon as the pointer has used it, so a path can be walked only once. To build a loop, a program puts an S in front of each arrow it wants to keep. N does the opposite and makes the next command vanish after it runs.
The machine
| Char | Does |
|---|---|
< > ^ v | Point the pointer, then vanish |
? | Pop; go west on 0, east on more than 0 |
I O | Read a number and push it; pop one and print it |
D | Duplicate the top |
+ - * / | Pop a, then b, and push b+a, b-a, b*a, b/a |
1 0 | Push 1 or 0 |
S N | The next command stays for good, or vanishes after it runs |
E | Stop |
The pointer starts in the top-left corner going right. The specification says ? turns left on 0 and right above 0. Its three programs all meet ? going down and need 0 to go west, so ? sends the pointer west or east from any direction.
Example
The countdown pushes 3, then prints, subtracts 1 and loops back up through kept arrows.
The countdown pushes 3, then prints, subtracts 1 and loops back up through kept arrows. On 0 the ? sends it west onto a plain v, which takes it down to E.
The truth machine prints its input once. On 1 the ? sends it east into a loop of kept arrows that prints 1 forever. On 0 it goes west to E.
The numeric cat reads and prints until a 0. Its first S keeps the v in the top-left corner, which every later pass needs, so a kept arrow has to stay kept.
Patterns it teaches
- Two-dimensional control flow: with a path that wears away.
- Self-modifying code: every arrow without an
Sdeletes itself. - Stack machines: the stack decides every turn.
Further
See also
Two-dimensional languages
Stack-based
Write your own in Run Convert a program from Direction
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: direction.
Command line
shib run direction -e 'IDOSvSOD<
D S
E?S>S^' --input '0'shib manual directionSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/direction.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org