esolang

Labyrinth

The program is a maze. There are no arrows: the instruction pointer follows corridors, and at every junction the stack decides which way to turn.

Martin Ender, 2015 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 examples and 8 known programs · Hello World on Try It Online: identical · Test method

Run it

Hello World

Expected output

Hello, World!

Output


The idea

Martin Ender made Labyrinth in 2015. In Befunge you steer with arrows. In Labyrinth you steer by shape. Every command character is a floor tile; spaces and unknown characters are walls. The instruction pointer walks along the corridors the commands form.

Read the story

Where a corridor simply bends, the IP follows it. Where it splits, the top of the stack chooses: negative turns left, zero goes straight on, positive turns right. A dead end turns the IP around. So a loop is literally a loop of characters, and an if is a fork in the road.

There is one more twist. Four commands rotate a whole row or column of the source by one cell, with the IP riding along if it is on it. The maze can reshape itself while you walk in it.

The machine

Two stacks, main and auxiliary, each with an endless supply of zeros at the bottom. The IP starts on the first command in reading order, moving right. The grid does not wrap.

Choosing a direction (after each command), by the number of open neighbours:

Open neighboursThe IP…
1goes that way (at a dead end, this means turning around)
2follows the corridor; it never turns around if it can avoid it
3 or 4looks at the top of main: < 0 turn left, 0 straight, > 0 turn right. If that is a wall, it goes the opposite way

Commands

CharDoes
0–9Top × 10 + digit (so 72 pushes 72 in two steps)
_Push 0
) (Increment, decrement the top
+ - * / %Arithmetic on the top two
` & \| $ ~ | Negate, and, or, xor, not
: ;Duplicate, drop
} {Move the top to aux, and back
=Swap the tops of main and aux
#Push the stack depth
, ?Read a byte (-1 at end), read a number
. ! \Print a byte, print a number, print a newline
"No-op: a floor tile that does nothing
< > ^ vPop n; rotate a row or column of the grid by one cell
@Stop

Example

A countdown from 5.

A countdown from 5. Read it as a ring of corridor with a tail at each end:

 5
 :!\(
 "  "
@""""

The IP starts on 5 and can only go down. It enters the ring at :, a junction: the top is 5, so it wants to turn right, but right is a wall, so it goes the other way, east. It walks the ring clockwise: : copies, ! prints the number, \ a newline, ( subtracts one, then four no-ops lead to the bottom-left corner. That corner is a junction too. While the top is positive it turns right, north, and goes round again. When it reaches 0 it goes straight on, west, into @. Output: 5 4 3 2 1, one per line.

Two small ones from the language's own examples: cat in five characters, and a Hello World without any layout at all.

,)@
.(

The countdown is saved as langs/museum/examples/count.lab. These commands run all three.

This implementation leaves out the four grid-rotating commands.

Checked examples

Example

Expected output

abc

Output


Hello World

Expected output

Hello, World!

Output


Example

Expected output

0

Output


Quirks

  • A run stops after 3,000,000 steps.

Try this

  1. Change the countdown to count from 9, and to print the numbers on one line separated by spaces.
  2. Write a truth machine: read a number; if it is 0, print 0 and stop; if it is 1, print 1 forever.
  3. Hard: print the sum of all numbers in the input, using ? until it gives 0.

Patterns it teaches

Further

Two-dimensional languages

Stack-based

Write your own in Run Convert a program from Labyrinth

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: labyrinth.

Command line

shib run labyrinth -e ',)@
.(' --input 'abc'
shib manual labyrinth

Source

Module
langs/grid/index.js
Interpreter
langs/grid/labyrinth.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org