befungee

Implementation of programming language Befunge

befungee is a Befunge-93 interpreter, written in Python and licensed under GNU GPL. Author is Curtis McEnroe, first version was published on 14 January 2010.

Interpreter specifics:

  • To run the interpreter, use python befungee.py <program name>.
  • To run the built-in debugger, use --debug --delay=100 option.
  • Stack elements are stored as integers of arbitrary size.
  • The contents of program cells is stored as unsigned integer 0..255.
  • When input is read character by character, #13 is ignored.

befungee debugger
befungee debugger

Examples:

Hello, World!:

Example for versions befungee 0.2.0

First part of the example pushes the required values on the stack. 25* puts 10 on the stack (ASCII-code of new line), then " toggles string mode, in which each character of the program pushes its ASCII-code on the stack, and finally second " toggles string mode off.

Second part of the example is a loop which prints all values in the stack, starting from the topmost ones. > makes the instruction pointer move right (after the end of iteration). : duplicates the topmost element of the stack (i.e., the current character); due to usage of the bridge # this command is executed only when the instruction pointer moves to the right. , prints the topmost element of the stack; due to the bridge it is executed only when the IP moves to the left. _ allows the IP to pass to @ if the topmost element of the stack is 0 (or the stack is empty) and mirrors it back to moving left otherwise. The order of events within one iteration is:

  • duplicate the topmost element of the stack.
  • check, whether it’s equal to 0; if it is, break the loop. During this check the topmost element is always deleted.
  • print the topmost element of the stack.
25*"!dlroW ,olleH" >:#,_@

Hello, World!:

Example for versions befungee 0.2.0

This example handles the task in a more straightforward way — puts ASCII-codes of characters on the stack one-by-one and prints them immediately after they get on the stack.

89*,45*99*+,39*99*+::,,56*99*+,29+4*,48*,92+8*1-,56*99*+:,3+,,25*:*,56*3+,25*,@

Factorial:

Example for versions befungee 0.2.0

This example uses iterative factorial definition.The program consists of two loops — the first one calculates numbers from 16 to 1, the second one does the actual factorial calculation.

First of all we put 16 on top of the stack (as 4*4) — this will be the maximal number which has to be processed. The first loop follows, executed in clockwise order. One iteration puts into the stack a number which equals the previous one decremented. When this number becomes zero, loop breaks (_ in the second line), and the instruction pointer goes right.

Next the top element of the stack (0) is removed and 1 is placed there instead (this is the current factorial value). The second loop follows, executed in counter-clockwise order. One iteration performs the following actions:

  • \ — two top elements of the stack (the factorial calculated earlier and the next number) are swapped (the next number becomes the topmost). If the stack contains only one element, 0 is pushed on top of it.
  • :_ — if the next number is 0 (or if the stack is empty), the loop breaks.
  • . — next number is printed (its copy stays in the stack).
  • block ,,,,"! = " pushes characters in the stack and prints them immediately. The string is scanned right-to-left, but the characters are printed from topmost to lower ones, so for the programmer the string looks exactly the way it will be printed.
  • .:* — new factorial is calculated and printed (its copy stays in the stack).
  • ,*25 — newline printed.
44* >:1-:v    v ,*25 .:* ,,,,"! = ".:_ @ 
    ^    _ $1 > \:                   ^ 

Fibonacci numbers:

Example for versions befungee 0.2.0

This example uses iterative definition of Fibonacci numbers. It is based on commands p and g which are used to modify source code of the program at the runtime, — they put a specific character at a certain cell of the program or read it from the cell to the stack, respectively. In this case they are used to store Fibonacci numbers outside of the stack. Loop counter is stored in the bottom element of the stack all the time. Since playfield cells can’t store values over 255, only the first 13 Fibonacci numbers are printed.

031p132p 94+ > 31g 32g :. + 32g v
             | :-1,,", "p23 p13 <
             > "."::,,,@

CamelCase:

Example for versions befungee 0.2.0

This program processes the input string character-by-character. The only instructions executed before the loop are 152p — this puts value 1 in cell (5, 2) which will store 1 if the last character was not a letter, and 0 otherwise.

The main part of the program is the loop. One loop iteration reads (~) and processes one character of the input string. The loop breaks as soon as the character is its ASCII is 10 (: 25*- #v_ @), otherwise the instruction pointer goes to the second line (at v command). If the character is a lowercase letter, it is converted to uppercase to unify further processing (this is done by the part of the program before the first square block and the block itself). After this we check whether the letter is an uppercase letter. If it is not (pointer is back to line 1), we mark (5, 2) cell as 1, the character is removed from the stack, and the iteration is over. Otherwise we do one more check, this time on the value of (5, 2) cell, and depending on its outcome we keep the character or convert it to lowercase. Finally, the resulting character is printed, and the iteration is over.

152p > ~ : 25*- #v_ @               >48*-v                    >152p $         v
                 > :: "`"` \"{"\` * |    > :: "@"` \"["\` * ! |    >    v
                                    >    ^                    >52g |    >052p,v
                                                                   >48*+^
     ^                                                                        <
 ( )
  |
  was last character a space (5,2)