A cryptic and pretty limited 2D programming language. The main
purpose of the language is to be
hard to compile. Someone managed to
do it, so the cryptic and pretty limited nD programming language Funge98 was created as a replacement.

Actually, most of the above is true. But, Befunge has never actually been compiled. True, it's been TRANSLATED into C, and the result has been compiled, but that doesn't mean Befunge itself has been compiled. If I translate a JavaScript into C and then compile that, does that mean I've compiled JavaScript!? I think not. Anyway, the old befunge page has died, but there is a new one at:


Also, there is a perl module named Befunge::Interpreter floating around on CPAN written by yours truly that is compliant with the older befunge 93 spec.
Here's a useless and obfuscated piece of Befunge code (is there any other kind?) and an explanation of what it does.

<fu>2]  vu'<^_^>0avj3
"dead m"]o'?"nkey"]#[
:,!^ [/jvi'<@v>;bar;>
What this code does: prints either "dead monkey", "dead munkey", "dead minkey", or "dead mm daed yeknnkey" (don't ask why) with a 25% probability of each.

The instruction pointer (IP) starts at the < next to the 'fu' (which is entirely there for decoration; neither the f nor the u is ever executed.), which tells the IP to go left. It does so, and as such it wraps around to the end of that line. It encounters the number 3, and puts that on the stack. It then encounters a j for "jump" - it pops the 3 off the stack, telling it to jump over 3 spaces, landing on the > which tells it to go right again. It encounters a 0 which it puts on the stack, and an a which puts 10 on the stack (think hexadecimal). Then the v points the IP downward.

It comes across the ] which means "turn right" - not the same "right" that > would point it to, though. This turns it right relative to the direction the IP was going, which looks like left to us. The next instruction is ", which makes things temporarily simpler.

The " enters "string mode", where any character except another " gets put on the stack. y, e, k, and n get put on the stack - but they're now in reverse order, as the first item onto the stack will be the last to be popped off.

The ? in the middle of everything sends the pointer off in a random orthogonal direction. If it goes up or down, the < it meets immediately points it left, and if it goes left, well, then it's going left. Here, it encounters a single quote, which puts the next character onto the stack, which is either the u, o, or i which determines how the program will misspell (or not) "monkey".

If the IP went up, it meets a down arrow which points it at the right-turn bracket, resulting in it going left.
If the IP went down, it meets a down arrow which makes it wrap back around to the top, keep going down, and reach the right-turn bracket which too makes it go left.
If the IP went left, it hits the right-turn bracket right away, which makes it go up. It bounces off the down arrow, hits the bracket again, and goes left.

All that was entirely frivolous, as a < would have sufficed to make the pointer go left. But there's no use writing Befunge if you're not going to be silly.

Here it reaches "dead m", the rest of the string, which as before it pushes onto the stack backwards. It then wraps around to the far right end of the middle row, where it hits a left-turn bracket and thus goes down to the bottom row, where it is pointed right so it wraps around to the left again.

Here, the program goes on to print out the string in the strangest way possible (no, I take that back, it would be stranger to write it in Trefunge.)

The : duplicates the last item on the stack, which this first time around is the letter "d". The , takes the character which is at the bottom of the stack and prints it. So yay! We've got a d on the screen! The ! is a logical not, which changes the last item on the stack to zero if it is non-zero, or one otherwise. "d" is not 0. So the remaining duplicate of the "d" is changed to 0. The IP then reaches the ^ which points it up, across the text. The a it hits as it crosses puts 10 on the stack, it goes right, puts the 2 on the stack, hits a right-turn bracket to make it go down (through the space in "dead m"), does a left-turn to go right, and encounters / which divides the bottom two numbers on the stack (10 and 2) and puts the result (5) back on the stack, just in time to tell it how far to jump when it hits the j. Jumping over five spaces, it lands on the v after the next one, sending it back around to the top. There is a _ there, which is an "east-west if"; if the bottom number on the stack is 1, it goes left, and if it's zero, it goes right.

If the bottom number on the stack were 1, it would go left and then up, reaching the @ which ends the program. But it's not. It's the 0 that was the result of negating the letter "d". So it goes right, to the other half of the anime smileyface, and ends up on the > on the bottom.

A ; tells the IP to skip instructions until it reaches another semicolon. Thus, the IP jumps over the word "bar", and keeps going back around to the left end of the bottom row, where it does the preceding few paragraphs all over again, except with the letter "e" instead of "d"! It repeats this for each letter until it reaches the 10 and the 0 which stayed on the stack since several paragraphs ago. Bet you forgot about them. The 10 prints a line feed (character 10), and the 0 goes through that whole process except that when it is negated, it becomes 1, which sends the IP left at the "anime smiley" and up to the @ which ends the program.

But wait! There's more! What if the IP went back to the right when it hit the question mark? Then it reads the string "nkey" again, except forwards this time, so it'll get printed out backwards. And it turns right again, with the result of making the pointer go down, and hits the "r" in "bar". (The "b" and the "a" were, again, totally unnecessary). The r reflects the pointer back where it came from, where it turns right one more time and really is going to the right now. The # is a "trampoline" which makes the pointer skip the left-turn bracket which comes next. So it hits "dead m" forwards, then turns right which causes it to go down and wrap around, then turn right again to come back where it came from and hit "dead m" backwards again. Then it wraps around to the right where it hits the left-turn bracket it skipped before, making it go down to the bottom row where it starts the whole runaround with printing the string, with the result being that the enigmatic and sorta palindromic phrase "dead mm daed yeknnkey" is displayed on the screen.

Could this have been simplified? Of course. This could have been accomplished with this Befunge code:

v?0a"yeknu">"m daed" >:#,_@
 >0a"yekni"^         <
> 0a"yeknnkey dead m"^

with a much more obvious program flow. But for that matter, I could write it in C. Befunge's dubious strength is that it is one of the easier languages to obfuscate, and why not take advantage of this?

And now, a befunged quine: <@,g09,k9"

beep = B = beige toaster

Befunge n.

A worthy companion to INTERCAL; a computer language family which escapes the quotidian limitation of linear control flow and embraces program counters flying through multiple dimensions with exotic topologies. The Befunge home page is at http://www.catseye.mb.ca/esoteric/befunge/.

--The Jargon File version 4.3.1, ed. ESR, autonoded by rescdsk.

Log in or register to write something here or to contact authors.