esolang

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

Expected: 0123456789

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.

CommandDoes
0-9 a-fPush 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
+ - * / % _ IArithmetic, negate, truncate (/ gives a fraction)
= < > !Compare (1 or 0), logical not
( )Pop n; swallow or give back n characters of the tail
wPop n and wait n ticks
$ s SToggle, or set, whether commands use the own or the shared stack
m M y YMove or copy the top between own and shared stack
l LPush the length of the own or the shared stack
n oPop and print as a number, as a character
r iRead 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

Try this

  1. Print the digits 9 down to 0.
  2. Print Hello once with one snake, using ( to die at the end.
  3. Hard: use two snakes, one to read numbers and one to print their running sum.

Further

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 ouroboros

Source

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