esolang

Recurse

Befunge cut into boxes. Each box is a function, and a call keeps the way the pointer was going.

MagiMaster, 2006 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 5 known programs · Test method

Run it

Expected: 10 89

The idea

MagiMaster made Recurse in 2006, after Befunge and the fractal circuit mazes. A program is a pile of bordered blocks. The character in a block's top left corner is its name, and that character anywhere else calls the block. Each side of the border has one arrow, which says where the pointer starts when it comes in from that side, so a block has four entrances.

Read the story

A block returns at # or when the pointer reaches its border. The caller then goes on from the call in the direction the pointer had when it left, so a block can also turn its caller around. Blocks can call themselves too.

The machine

Two integer stacks, left and right, and one register.

CommandDoes
{ }Push the register on the left or right stack
[ ]Pop the left or right stack into the register
> ^ < vPoint the way
0 to 9Set the register
? !Read a character; print the register as one
% &Print or read a signed decimal
@Go straight on 0, turn counterclockwise when positive, clockwise when negative
#Return
a s m d rPop both stacks and set the register to left plus, minus, times, divided by or modulo right

Any other character calls the block of that name, or does nothing. Lines outside the blocks that begin with a space are comments. The run starts at the left entrance of the $ block and ends when that block returns.

Example

The Fibonacci without its comments.

The Fibonacci without its comments. R reads digits up to a space and echoes them, f calls itself twice, and % prints the result. F(0) and f(1) are both 1, so f(10) is 89.

The Ackermann function on m=2 and n=3. With the specification's own m=3 and n=5 it prints 253 after about 1.4 million steps.

Expected: 9

The 99 bottles of beer prints the whole song in about 220,000 steps. Its Hello World prints Hello, world!! with two !. The last row prints the first one inside _ and the second with the ! after [...

Patterns it teaches

Further

Two-dimensional languages

Cell-based

Write your own in Run Convert a program from Recurse

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run recurse -e ' Main function
$#######v
>3{5}k%#<
$#######^

 Ackermann(m, n)
k########################v
#......>{1}s}k_}[{{1}s{k_#
#..>{]}@1}}s{k_..........#
>[{@]}}1{a..............#<
k########################^

 Pop both stacks once while saving the register value
_#######v
>{][}[]#<
_#######^'
shib manual recurse

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/recurse.js

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

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