esolang

Befunge

Code on a grid. The instruction pointer walks in four directions, wraps at the edges, and can rewrite the grid it walks on.

Chris Pressey, 1993 · Turing complete: with unbounded memory · ●●○ moderate · esolangs.org

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

Run it

Hello World in a loop

String mode pushes the text backwards, so H ends on top. Then a loop prints until the stack is empty.

Expected output

Hello World!

Output


  • "!dlroW olleH" pushes the 12 characters; the last pushed, H, is on top.
  • > keeps going right. : copies the top.
  • # jumps over , on the way right. _ pops the copy; if it is not 0 the cursor turns left.
  • Going left it lands on ,, prints one character, then # jumps over : … the wrap brings it back to >. When the stack is empty the copy is 0, _ sends it right, onto @.
  • Each character is printed by the same ,.
all 250 steps
The pointer walks right, pushing the text, then loops left and right through the printer. Brighter cells were visited more often.

The idea

Chris Pressey designed Befunge in 1993 with one goal: make a language that is as hard as possible to compile. His trick was to put the program on a two-dimensional grid and let the instruction pointer travel left, right, up or down. A compiler cannot know in advance which cell runs after which.

Read the story

To make it worse for compilers (and better for players), Befunge can read and write its own grid while it runs. Code is data, and data is code.

The first version, Befunge-93, has a fixed 80 × 25 playfield. Befunge-98 (the "Funge-98" family) lifts that limit and adds much more, including 1D and 3D cousins. This entry covers Befunge-93.

The machine

An 80 × 25 grid of characters that wraps around like a torus, one stack of integers, and an instruction pointer that starts in the top-left corner moving right. Popping an empty stack gives 0.

CharDoes
0–9Push that digit
+ - * / %Pop a, pop b, push b op a
!Logical not: 0 becomes 1, anything else 0
` | Pop a, pop b, push 1 if b > a, else 0
> < ^ vGo right, left, up, down
?Go in a random direction
_Pop; go right if 0, left otherwise
\|Pop; go down if 0, up otherwise
"String mode: push every character until the next "
: \ $Duplicate, swap, drop
. ,Pop and print as a number, or as a character
& ~Read a number, or a character
#Bridge: jump over the next cell
gPop y, x; push the character at (x, y)
pPop y, x, v; write v into the grid at (x, y)
@Stop

Space does nothing, so it is the wire you lay paths with.

Example

The classic Hello World pushes the text backwards, then loops through a print-until-zero gadget:

The classic Hello World pushes the text backwards, then loops through a print-until-zero gadget:

"!dlroW ,olleH">:#,_@

>:#,_ is the loop. : copies the top, # jumps over ,, and _ sends the IP left while the copy is non-zero. Going left it hits , (print), then # jumps over : and lands on > again. When the stack is empty, _ pops 0, goes right, and meets @.

Self-modification in one line. This program has no @ at first:

"!iH",,,"@"76+0p

It prints Hi!, then p writes @ into cell (13, 0), which is the + it has just run. The IP runs off the right edge, wraps to column 0, prints Hi! again, and stops at the new @. Output: Hi!Hi!.

A quine that cheats by reading its own grid with g:

:0g,1+:96+-!#@_

It prints cell 0, 1, 2, … of row 0 until the counter reaches 15, the program's length.

Commands

The dictionary’s first line says reader=2d size=80x25.
Befunge-93’s commands and their kernel tokens
CommandWhat it doesKernel tokens
0-9push the digit (0 to 9)0
+add the top twoADD
-subtract: second minus topSUB
*multiplyMUL
/divide: second by topDIV
%remainderMOD
!not: 1 if the top is 0, else 0NOT
`greater: 1 if second is greater than topSWAP LT
>go right0 FACE
vgo down1 FACE
<go left2 FACE
^go up3 FACE
?go in a random direction4 ROLL FACE
_pop; go right if 0, left otherwiseIF 2 ELSE 0 THEN FACE
|pop; go down if 0, up otherwiseIF 3 ELSE 1 THEN FACE
"string mode: push characters until the next "34 STRING
:copy the topDUP
\swap the top twoSWAP
$drop the topDROP
.print the top as a number and a spacenumber
,print the top as a character0 ADD EMIT
#bridge: skip the next cellSTEP
gget: push the character at (x, y)PEEK
pput: write a value at (x, y)POKE
&read a numberreadnum
~read a characterREAD
@stopHALT

The dictionary’s first line says reader=2d size=80x25. With that setting the kernel does not read the text from left to right. It lays it on a page and walks it with a grid cursor. FACE turns, STEP skips a cell, STRING pushes cells up to the next quote, and PEEK and POKE read and write the page. The words number, digits and readnum print and read decimal numbers.

Step by step

The cursor runs right along the first line, down, left through the string, and into a two-line loop.

Arrows draw the control flow

The cursor runs right along the first line, down, left through the string, and into a two-line loop.

Expected output

Hello World!

Output


Numbers

55+ pushes 5 and 5 and adds them; . prints a number followed by a space. 89* is 72.

Expected output

10 72 

Output


A program that writes its own stop

This program has no @ of its own. "@" pushes 64, 60 pushes 6 and 0, and p writes 64 at column 6, row 0. The cursor reaches that cell two steps later and stops there.

Expected output

(nothing)

Output


  • Without the p the cursor would go down, then left along the second line, and wrap around forever.
  • See self-modifying Befunge for more.

Two machines and more relatives

Run by a Befunge subset interpreter

The same kind of program, run by a Befunge subset interpreter instead of the kernel.

Expected output

Hello World!

Output


Run by a full Befunge-93 interpreter

The same kind of program, run by a full Befunge-93 interpreter instead of the kernel.

Expected output

Hello World!

Output


The Funge family grows in both directions. Unefunge and Trefunge are Befunge in one and three dimensions. ><> has a stack of stacks and mirrors. Hexagony walks a hexagon, and Labyrinth decides its turns at junctions of a maze. See 2D control flow.

Quirks

  • Empty stack. Popping an empty stack gives 0. So _ on an empty stack goes right.
  • Integer division. / and % round toward zero in the kernel, as JavaScript does.
  • Random direction. ? uses the kernel’s ROLL, which is seeded, so a run repeats exactly.
  • Page size. Only 80 by 25 cells exist. Text beyond that is cut off.

Try this

  1. Write a program that reads a number with & and prints it doubled.
  2. Print the numbers 9 down to 1 using a loop and _.
  3. Hard: write a quine that does not use g. Hint: start with " and let string mode wrap around.

Patterns it teaches

  • Two-dimensional control flow: the original. Arrows, bridges and wrapping became the vocabulary of every grid language after it.
  • Self-modifying code and code as data: p writes instructions; g reads them as numbers.
  • Quine: with g, a quine can just read itself. Without g, the " string mode wraps around the torus and pushes the whole row, which gives very short honest quines.
  • Stack machines: all arithmetic happens on one stack.
  • Turing completeness: the fixed 80 × 25 grid makes Befunge-93 a bounded machine. Befunge-98, with unbounded space, is generally taken to be universal.

Further

Self-modifying

Two-dimensional languages

Write your own in Run Convert a program from Befunge

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run befunge -e '"!dlroW olleH">:#,_@'
shib run shib:befunge -e '"!dlroW olleH">:#,_@'
shib manual befunge

Kernel translation

Each command and its kernel instructions
CommandKernel instructions
00
11
22
33
44
55
66
77
88
99
+ADD
-SUB
*MUL
/DIV
%MOD
!NOT
`SWAP LT
>0 FACE
v1 FACE
<2 FACE
^3 FACE
?4 ROLL FACE
_IF 2 ELSE 0 THEN FACE
|IF 3 ELSE 1 THEN FACE
"34 STRING
:DUP
\SWAP
$DROP
.number
,0 ADD EMIT
#STEP
gPEEK
pPOKE
&readnum
~READ
@HALT

Kernel definition

#! shibboleth 3 seed=befunge syllables= plain=off reader=2d size=80x25
Befunge-93 as a front end: the program text is the page, the grid cursor walks it; FACE turns, STEP bridges,
STRING pushes cells up to the next quote, PEEK and POKE read and write the program itself
"0" => 0
"1" => 1
"2" => 2
"3" => 3
"4" => 4
"5" => 5
"6" => 6
"7" => 7
"8" => 8
"9" => 9
"+" => ADD
"-" => SUB
"*" => MUL
"/" => DIV
"%" => MOD
"!" => NOT
"`" => SWAP LT
">" => 0 FACE
"v" => 1 FACE
"<" => 2 FACE
"^" => 3 FACE
"?" => 4 ROLL FACE
"_" => IF 2 ELSE 0 THEN FACE
"|" => IF 3 ELSE 1 THEN FACE
"\"" => 34 STRING
":" => DUP
"\\" => SWAP
"$" => DROP
"." => number
"," => 0 ADD EMIT
"#" => STEP
"g" => PEEK
"p" => POKE
"&" => readnum
"~" => READ
"@" => HALT
rot => [ SWAP ] DIP SWAP
abs => DUP 0 LT IF 0 SWAP SUB THEN
digits => [ ] SWAP BEGIN DUP 10 MOD abs 48 ADD rot CAT SWAP 10 DIV DUP NOT UNTIL DROP
number => DUP 0 LT IF 45 EMIT THEN digits EMIT 32 EMIT
digit => DUP 48 LT NOT SWAP 58 LT MUL
skip => BEGIN READ DUP DUP digit SWAP -1 EQ ADD IF 1 ELSE 1020 STORE 0 THEN UNTIL
more => BEGIN READ DUP digit IF 48 SUB SWAP 10 MUL ADD 0 ELSE DROP 1 THEN UNTIL
readnum => 0 1020 STORE skip DUP -1 EQ NOT IF 48 SUB more 1020 LOAD 45 EQ IF 0 SWAP SUB THEN THEN

Source

Module
langs/classic.js
Kernel definition
shibboleth/witness/befunge.shib on shibboleth/kernel.js

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

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