If you are here right now then you should understand that this page is still under construction. That means that it sin't complete, contains several spelling and gramatical errors, and even the total look of this site can change if you come back later.
Befunge is an stack based, 2 dimensional programming language. What does this mean? Well for starters it is stack based, which means that the language has it's data all in a line and con only be accessed one at a time. It doesn't sound to easy to use, but Befunge is an esoteric programming language (Esolang), which means it's more a concept language than one you would write an operating system in (don't worry I have a few tricks up my sleeve). Being 2D is a little differen; unlike reading a language like a book (but with a frequent jumping in lines) you instead look at it like a map that has four directions of movement (up, down left, and right). Just like multiple roads can intersect the Befunge code intersects itself and can make very "knotty" code, but I'll explain more on this later.
There are a few concepts that you have to be aware of in Befunge. First is that there are only three places that you can get data: from the stack, from the user, from the code itself. In a 2D plane you have to be aware of where you are and where you are going, we will refer to these as position (in (X, Y) format) and direction (Upward, Downward, Leftward, Rightward). By default you start at (0,0) heading Rightward. Befunge has 2 modes: normal execution, and stringmode. Until you hit a '"' you are in normal execution so everything can be described as an action (Stringmode is discussed later). Each cell is separate and only can the value in it can only perform one action (even though those actions seem to change). The befunge program is only 80 cells wide and 20 cells tall so no code can be larger than that. So, why don't we just get started?
Since Befunge is a stack based languaage you should understand how a stack works. The stack is essentially a line of data. You can only place or retreive one piece of data from this line and you only work with the newest value. The stack itself contains all the elements that were placed on it from oldest to newest, but you are stuck using only the newest values. This is called Last In First Out or LIFO.
There are two commands you have to be aware of whenever you use a stack and they are "push" and "pop". Push places an element on top of the stack. Since this value is now the newest value on the stack it is the only element you can work with. Pop on the other hand removes the newest element from the stack to use in normal execution of the program. This means that whatever element next to the newest is now obviously the newest (because the newest value is no longer in the stack). If the stack is empty normally it's empty, but in Befunge an empty stack means that the stack's newest value (and all the other values) are 0s. Let's see some actual code now.
"!dlrow olleH"#> #, :# _@
That was the "Hello World" program. If it was confusing ... tough; we aren't going over that one yet, but I will show you some other code and explain that one later. Let's try these four.
@
1357902468@
00000000000@
755755@755@755@
Okay so what do they do? The first one is the simplest and it shows the the first, and essentially the last command you need for Befunge. '@' is Terminate Program command, what its does is terminate the program. It seems simple, but do you REALLY understand it? The second shows the next ten commands and how to put data on the stack. '0', '1', '2', '3', '4', '5', '6', '7', '8', and '9' each push their respective values on the stack, so program two would push 1, then push 3, then push 5 and so on until it pushes 8. This woulld leave the stack from newest to oldest being 8,6,4,2,0,9,7,5,3,1. After that the '@' command terminated the program. Program three would push 0 onto the stack 11 times then terminate. This doesn't seem too important, but remember that an empty stack in Befunge always is filled with 0 values. Program 3 is useless by itself and could easily be shortened by only performing program 1 because no matter how many values you pop from the stack in program 3 you can always pop another 0, and no matter how many 0s you push onto an empty stack you will still have the same functionality as an empty stack. The only time it would be useful to push a zero onto the stack would be if the stack isn't empty or filled with 0s (Program 2 is an example of this).
So what is the result of Program 4's stack from Newest to oldest? If you said 5,5,7,5,5,7,5,5,7,5,5,7 then you would be wrong. Whenever the program terminates, it terminates, so the stack would remain 5,5,7,5,5,7 before termination. You may think this is useless programming like in Program 3. I agree with this assumption in this specific case, but more complex programs may needs more than one '@' to be effective.
Commands Learned '@' (Terminate Program), '0' (Push 0), '1' (Push 1), '2' (Push 2), '3' (Push 3), '4' (Push 4), '5' (Push 5), '6' (Push 6), '7' (Push 7), '8' (Push 8), '9' (Push 9)There are more commands that deal with the stack directly, the first three we will deal with are '$', '\', and ':'. The '$' is the Pop command, and all it does is pop off the newest vallue of the Stack. It doesn't pop it to anywhere specific like an output window or anywhere else elaborate. For all your purposes it's just gone and you can't get it back. There isn't any Unpop, but this is just as good as Unpush. The '\' command is the Swap command. What this does is allow you to do is take the newest member in the stack and the next-to-newest member in the stack and changes their positions (making the newest the next-to-newest, and the next-to-newest the newest). The ':' Command is the Copy Command. It is a very useful command that pushes an exact copy of newest value onto the stack. With these three commands you will be able to manipulate the stack to a degree. Here are some example programs.
89\67\45\23\01\@
1:$$\67\$38$\:\$@
\$$:$$$@
Program 5 uses the '\' command to make the stack from newest to oldest look like 0,1,2,3,4,5,6,7,8,9. First it Pushes 8 then pushes 9 and then swaps their values, this doens't do anything useful, not does it do it the most effecient way, but we'll compare that to another later. Program 6 uses both the ':' and '$' to make exercise the stack out a little. first 1 is pushes and then coppied onto the stack (the stack now looks like 1,1) Then it pops the 1 off the stacks using the '$'. This is repeated to empty the Stack. Next the 6 and seven are pushes and then swapped (the stack is now 6,7). Six is popped and 3 and 8 are pushed (the stak is now 8,3,7). Now the 8 is popped, the 3 and 7 are swapped, and the 7 coppied (the stack is now 7,7,3). The '\' is used yet again, but this time it swaps the 7 and the 7. This effectively does nothing so it is also a useless step (in this example, not all). Finally one 7 is popped and the Stack is left at 7,3 before termination. Program 7 plays with the empty stack and these commands. First it attempts to swap on an empty stack. If you forgot an empty stack is essentially filled with 0s so this would only swap 0 with 0. Then it popss a couple of times and tries to copy onto an empty stack. Yet again this isn't a problem for the insatiable 0 stack; it copies a 0 onto the stack and then proceeds in the next two commands to pop a couple of more times. Now we have to pop once more from an empty stack, this isn't a concern because we would be popping a 0. The '$' command goes nowhere so it doesn't matter what is being popped, popping on an empty stack doesn't make it any less empty, or even any less filled. Program 7 is also a useless program.
Commands Learned '/' (Swap top elemects in Stack), ':' (Push top element of stack again), '$' (Pop)Data in befunge is always an integer, and you only have specific commands to push 0-9. To push numbers like 10, -9, or 1024 you will need to do a little math. To do this there are the normal basic mathematical operations of '+', '-', '*', '/', as well as '%'. '+' is obviously addition and it takes the top two values off the stack and adds then together and pushes the result back. Addition is commutative so it doesn't matter in which order the top two members of the stack are in, the result will be the same for 106+74 as 74+106. '-' is subtraction and it pops the top value on the stack and subtracts it from the next value popped from the stack, and then pushes the result. Watch the order you push the values because if you want to take 3-7 you'll have to push 3 then 7. Unlike most of the time when we do math Befunge math isn't infix it's postfix. What this means is that 3/2+4-7*8 would have to be written 32/4+78*- where the sign follows values. The stack is mainly to blame here because the values have to exist on the stack before you can actualy manipulate them. Also another this to know is that Befunge commands are done 1 at a time, so to have 2+7/8-3*4 conform to the order of operations you would have to change the equations into four steps 7/8=A, A+2=B, 3*4=C, and then B-C. Befunge doesn't know the future so it can't be smart emough to take this into account, but more on that later.
98+::4\4/.%."/",.@
v >.v
& >1v |:<
>#v?3v @$< $
4>2>\1-:!|
> ^
^ <
>&:1`!v
^$_v# <
v :< <
7v -1<
>?1>\:|
>0^ $
v**448<
>*\88*v
v48\+*<
>**+\4v
v\+**4<
>8*+\4v
v*2\+*<
>++-:0v
v1->+v^_$ ^
@.<
God bless, Gryfang