esolang

Reversible-2D

Threads crawl over a grid and trade values with it. Every step is meant to run backwards as well as forwards.

TehZ, 2010 · Turing complete: no · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

Expected: Hello, World!

The idea

TehZ made Reversible-2D in 2010. Each arrow in the source starts a thread, and each thread carries one 16-bit value. Every cell of the grid holds a value too, so the program text is also the memory.

Read the story

Every command has an opposite. P swaps the thread's value with a cell, and swapping again undoes it. + is undone by -. Printing is undone by taking the last character back off the screen, and an ended thread starts again, turned around, after as many steps as it was stopped for. The specification lists how each command reverses but not how to start a reverse run, so this implementation runs programs forward only.

The machine

CommandDoes
V > ^ <Start a thread going down, right, up or left
\ /Mirrors
PSwap the thread's value with the cell to the right
OPrint the thread's value as a character
NPrint a line break
+ -Add 1 to the value, or take 1 away
IRead a byte into the cell to the right, which must be a space
¤End the thread

Each round, every thread moves one cell and does what it finds there. Threads take turns in the order of their arrows, the top row first and then left to right. The run ends when every thread has ended.

Example

The Hello World.

The Hello World. Each P swaps a letter into the thread, and the next O prints it.

The two threads run in step. The second prints Hi! and a line break while the first is still on Hel, so their letters mix until the second one ends.

Expected: HHeil! lo, World!

A mirror turns the thread down to print.

Expected: A

The cat does not run. Its I has a mirror on its right, which the rules forbid. The specification itself says the cat leans on undefined behavior that the original interpreter happens to allow.

Patterns it teaches

Further

Reversible computing

Concurrent programming

Write your own in Run Convert a program from Reversible-2D

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: reversible-2d.

Command line

shib run reversible-2d -e '>PHOPeOPlOPlOPoOP,OP OPWOPoOPrOPlOPdOP!ON¤'
shib manual reversible-2d

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/reversible-2d.js

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

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