From Seo Wiki - Search Engine Optimization and Programming Languages
Befunge is a stack-based, reflective, esoteric programming language. It differs from conventional languages in that programs are arranged on a two-dimensional grid. "Arrow" instructions direct the control flow to the left, right, up or down, and loops are constructed by sending the control flow in a cycle.
The language was originally created by Chris Pressey in 1993 as an attempt to devise a language which is as hard to compile as possible — note that the
p command allows for self-modifying code. Nevertheless, a number of compilers have subsequently been written. A number of extensions to the original "Befunge-93" specification also exist, most notably Funge-98, which extends the concept to an arbitrary number of dimensions and can be multithreaded, with multiple instruction pointers operating simultaneously on the same space. Befunge-extensions and variants are called Fungeoids or just Funges.
The Befunge-93 specification restricts each valid program to a grid of 80 instructions horizontally by 25 instructions vertically. Program execution which exceeds these limits "wraps around" to a corresponding point on the other side of the grid; a Befunge program is in this manner topologically equivalent to a torus. Since a Befunge-93 program can only have a single stack and its storage array is bounded, the Befunge-93 language is, unlike most machine languages, not Turing-complete. The later Funge-98 specification provides Turing-completeness by removing the size restrictions on the program; rather than wrapping around at a fixed limit, the movement of a Funge-98 instruction pointer follows a model dubbed "Lahey-space" after its originator, Chris Lahey. In this model, the grid behaves like a torus of finite size with respect to wrapping, while still allowing itself to be extended indefinitely.
As stated, the design goal for Befunge was to create a language which was difficult to compile. This was attempted with the implementation of self-modifying code (the 'p' instruction can write new instructions into the playfield) and a multi-dimensional playfield (the same instruction can be executed in four different directions).
Nevertheless, these obstacles have been overcome, to some degree, and Befunge compilers have been written, using appropriate techniques.
The bef2c compiler included with the standard Befunge-93 distribution uses threaded code: each instruction is compiled to a snippet of C code, and control flows through the snippets just as it does in a Befunge interpreter (that is, conditionally on the value of some 'direction' register.) This does not result in a significant advantage over a good interpreter. Note that the bef2c compiler is not correct since it does not handle either 'p' or string mode, but it would not be impossible to make it do so (although the C language might not be well-suited for this).
The Betty compiler, for example, treats every possible straight line of instructions as a subprogram, and if a 'p' instruction alters that subprogram, that subprogram is recompiled. This is an interesting variation on just-in-time compilation, and it results in a much better advantage over an interpreter, since many instructions can be executed in native code without making intervening decisions on the 'direction' register.
The BFC (BeFunge Compiler) for Win32 written by Andrew Carter (Uranium-239), simply uses a self-executing stub and modifies the preallocated 80x25 byte matrix inside the stub to execute any given befunge program. The negative effects of this technique include having an interpretter attached to every Befunge program. However, using optimization tricks, BFC V1.1 guarantees an executable size of only 5632 bytes.
Sample Befunge-93 code
The technique of using arrows to change control flow is demonstrated in the random number generator program below. Following the arrows around, the
? instructions send the instruction pointer in random cardinal directions until the pointer hits a digit, pushing it to the stack. Then the arrows navigate to the
. to output the digit from the stack and return the pointer to the first directional randomiser. Note that there is no
@ to terminate this program so it produces an endless stream of random numbers from 1 to 9.
vv < < 2 ^ v< v1<?>3v4 ^ ^ > >?> ?>5^ v v v9<?>7v6 v v< 8 . > > ^ ^<
This is an example of the classic "Hello World!" program. First the letters "olleH" are pushed onto the stack as ASCII numbers. These are then popped from the stack in LIFO order and output as text characters to give "Hello". A space is character number 32 in ASCII, which here is constructed by multiplying 4 and 8, before being output as text. The remaining code then outputs "World!" in a similar way, followed by ASCII character 10 (a line feed character, moving the output cursor to a new line).
> v v ,,,,,"Hello"< >48*, v v,,,,,,"World!"< >25*,@
A slightly more complicated version:
>25*"!dlrow ,olleH":v v,:_@ > ^
This adds the ASCII character 10 (a line feed character) to the stack, and then pushes "!dlrow ,olleH" to the stack (the text to be outputted, backwards). It then enters a loop that first duplicates the last character on the stack (so now the stack would look like "\n!dlrow ,olleH". Then the "_" operation will pop off the last character again, and go right if it's a zero, left otherwise. When it goes left, it duplicates the first character on the stack and prints the character (which pops it off again). It then loops through this until there's a zero on the stack (then it will go right and exit the program). For this to work as expected you need a compliant interpreter that "returns" 0 when you try to pop an empty stack.
Befunge-93 instruction list
| ||Push this number on the stack|
| ||Addition: Pop a and b, then push a+b|
| ||Subtraction: Pop a and b, then push b-a|
| ||Multiplication: Pop a and b, then push a*b|
| ||Integer division: Pop a and b, then push b/a, rounded down. If a is zero, ask the user what result they want.|
| ||Modulo: Pop a and b, then push the remainder of the integer division of b/a. If a is zero, ask the user what result they want.|
| ||Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero.|
| ||Greater than: Pop a and b, then push 1 if b>a, otherwise zero.|
| ||Start moving right|
| ||Start moving left|
| ||Start moving up|
| ||Start moving down|
| ||Start moving in a random cardinal direction|
| ||Pop a value; move right if value=0, left otherwise|
| ||Pop a value; move down if value=0, up otherwise|
| || Start string mode: push each character's ASCII value all the way up to the next |
| ||Duplicate value on top of the stack|
| ||Swap two values on top of the stack|
| ||Pop value from the stack|
| ||Pop value and output as an integer|
| ||Pop value and output as ASCII character|
| ||Trampoline: Skip next cell|
| ||A "put" call (a way to store a value for later use). Pop y, x and v, then change the character at the position (x,y) in the program to the character with ASCII value v|
| ||A "get" call (a way to retrieve data in storage). Pop y and x, then push ASCII value of the character at that position in the program|
| ||Ask user for a number and push it|
| ||Ask user for a character and push its ASCII value|
| ||End program|
Most one-dimensional programming languages require some syntactic distinction between comment text and source code — although that distinction may be as trivial as Brainfuck's rule that any character not in the set
+-<>,. is a comment. Languages like Lisp and Python treat strings as comments in contexts where the values are not used. Similarly, in Befunge, there is no comment syntax: to embed documentation in the code, the programmer simply routes the control flow around the "comment" area, so that the text in that area is never executed.
- Carnage Heart - Playstation programming game using a similar language
- Esoteric programming language#Funges
- Cat's Eye Technologies, the original author's web site
- Esoteric languages wiki
- Official Funge-98 Specification
- ZBefunge, written in Inform
- Jeffrey Lee's Befunge archive
- BFC V1.1 Win32 compiler
- Yet Another Befunge93 Interpreter - An open source Befunge93 interpreter, written in Java
- Official home of the Rc/Funge-98 Interpreter
- BeQunge - An n-dimensional Funge-98 interpreter
- WASABI - A Superbly Asinine Befunge Interpreter An Open-Source Befunge93 IDE written in Java
- Conforming Concurrent Befunge-98 Interpreter Befunge-98 interpreter which conforms to the specifications and implements almost every fingerprint available
- Language::Befunge Befunge-98 Perl module at CPAN
- Language::Befunge::Debugger Graphic Befunge debugger written in Perl
- Inline::Befunge Perl module at CPAN to insert Befunge code into Perl code
- Visual Befunge Applet Visual Befunge Applet written in Java
- CoreLife, a 2 dimensional grid based programming language which predates Befunge
- Be.py, a Befunge-98 interpreter written in Pythonde:Befunge