esolang
Learn · Ideas152 languages show it

Two-dimensional control flow

The program is a picture, and the instruction pointer walks across it. Arrows are your goto.

●○○ easy · where to see it

The idea

Most programs are read top to bottom. In a two-dimensional language the source is a grid, and the instruction pointer (IP) moves through it right, left, up or down. Some characters run code; others turn the IP. A loop is a loop drawn on the page.

Read the story

Chris Pressey's Befunge (1993) started the family. He designed it to be as hard as possible to compile (esolangs.org), since the IP can go anywhere and the program can rewrite its own grid.

Since then, people have tried every geometry, including hexagons, mazes, images and falling marbles.

How it works

Befunge's grid wraps around at the edges.

Befunge's grid wraps around at the edges. The IP starts at the top left, moving right.

CharDoes
> < ^ vGo right, left, up, down
_Pop a value: go right if it is 0, left otherwise
\|Pop a value: go down if it is 0, up otherwise
0–9Push that digit
:Duplicate the top of the stack
-Subtract
.Pop and print as a number
@Stop

A countdown from 5.

5>:.1-:v
 ^     _@

Walk it through.

  1. 5 pushes 5. > keeps going right.
  2. :. prints a copy of the top. 1- subtracts one. : copies the result.
  3. v turns down, onto the _ below it. _ pops the copy.
  4. Not zero: go left along the bottom row to ^, which sends the IP up onto the >. The loop closes.
  5. Zero: go right onto @. Done.

The output is 5 4 3 2 1 . The loop is the rectangle you can see in the source.

Other geometries

  • ><>. Befunge-like, with a stack of stacks and mirrors (/, \) that bounce the IP.
  • SNUSP. Brainfuck's tape with 2D flow, plus subroutine calls.
  • Hexagony. The source is folded onto a hexagon, and there are six IPs.
  • Labyrinth. The code is a maze; at a junction, the sign of the top of the stack chooses the way.
  • Piet. The program is an image. Commands come from the change in colour and lightness between neighbouring blocks.
  • Funciton and Marbelous. 2D with no single IP; values flow along drawn wires or fall as marbles.

Side by side

><> is like Befunge, with mirrors that reflect the cursor and ; to stop.

Befunge, a drawn loop

Expected output

Hello World!

Output


  • Row 1 runs right, then turns down. Row 2 runs left through the string.
  • Rows 3 and 4 are the loop. : copies, v goes down, _ turns left while the value is not 0, , prints, ^ goes back up.

><> (Fish), mirrors and a stack of stacks

><> is like Befunge, with mirrors that reflect the cursor and ; to stop.

Expected output

Hello, world!

Output


PATH, brainfuck on a grid

PATH uses brainfuck’s cells. Its arrows turn only when the cell is not 0, so a loop is a closed path with an exit.

Expected output

(nothing)

Output


Labyrinth, where the maze decides

In Labyrinth there are no arrows. At a junction the top of the stack picks the way, left if negative and right if positive.

Expected output

abc

Output


See Befunge-93 and tape, stack, queue, rewriting, 2D.

Try this

  1. Change the countdown to start at 9. Then make it count down from 9 to 0, printing 0 too.
  2. Write a Befunge program that prints Hi using string mode (" pushes characters until the next ", and , prints a character).
  3. Hard: make the countdown loop run vertically, using | instead of _.

Seen in

Further

Where to see it