Ouroboros
Every line of code is a snake biting its tail. The only way to stop a loop is for the snake to eat itself.
2015 · Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 4 known programs · Test method
Run it
The idea
Ouroboros appeared in 2015. Its reference implementation is in the dloscutoff esolangs repository. Each line of a program is a snake. The head is the first character, the tail is the last one, and when control reaches the tail it goes back to the head. Every line is a loop by default.
Read the story
There are no jumps. Instead, ( makes the snake swallow some characters of its own tail, so the loop gets shorter, and ) brings them back. If a snake swallows the very instruction it is running, it dies. When all snakes are dead, the program ends.
Several snakes run side by side, one instruction each per tick. Each has its own stack, and a shared stack lets them pass values. Most of the other commands come from ><>.
The machine
One stack per snake plus a shared stack. Empty stacks act as if full of zeros.
| Command | Does |
|---|---|
0-9 a-f | Push a number (digits join into one number); push 10 to 15 |
"..." | Push the characters, last one first, so they print in order |
\ @ ; . | Swap, rotate three, drop, dup |
+ - * / % _ I | Arithmetic, negate, truncate (/ gives a fraction) |
= < > ! | Compare (1 or 0), logical not |
( ) | Pop n; swallow or give back n characters of the tail |
w | Pop n and wait n ticks |
$ s S | Toggle, or set, whether commands use the own or the shared stack |
m M y Y | Move or copy the top between own and shared stack |
l L | Push the length of the own or the shared stack |
n o | Pop and print as a number, as a character |
r i | Read a number or a character (-1 at the end) |
? | Push a random fraction (a fixed seed in this implementation) |
Literals that reach the tail carry on at the head. Dividing by zero stops the run.
Example
The snake duplicates its counter, prints it, adds 1, and compares a copy with 9.
The snake duplicates its counter, prints it, adds 1, and compares a copy with 9. While the counter is not above 9, the comparison gives 0 and ( swallows nothing, so the snake loops. When it reaches 10, ( swallows one character: itself. The snake dies and the program ends.
Patterns it teaches
- Stack machines: a ><>-style stack per snake, plus a shared one.
- Self-modifying code: a loop ends by cutting off its own tail.
Try this
- Print the digits 9 down to 0.
- Print
Helloonce with one snake, using(to die at the end. - Hard: use two snakes, one to read numbers and one to print their running sum.
Further
See also
Two-dimensional languages
Stack-based
Write your own in Run Convert a program from Ouroboros
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: ouroboros.
Command line
shib run ouroboros -e '.n1+.9>('shib manual ouroborosSource
- Module
langs/stack/ports.js- Interpreter
langs/stack/ouroboros.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org