Welcome

Welcome to part 3 of my Funge-98 tutorial. This part will cover the multi-dimensional aspect of Funge.

Funge Family of Languages

The Funge-98 specification is actually for a family of languages, the 3 that are mentioned in the official specification are: Unefunge, Befunge, and Trefunge.

Up til now, all of our examples have been in Unefunge, or 1-deminsional Funge. In Unefunge there is a single line of program code which the IP moves along.

By far the most popular is Befunge, which is 2-dimensional. Programs consist of multiple lines and the IP can move freely in 4 directions. This lesson will cover IP movement in Befunge.

The third class is Trefunge, which is 3-dimensional. Programs now consist of a vertical array of Befunge 2-dimensional spaces, now allowing the IP to move freely in 6 directions. Trefunge will be covered in a later lesson

Into the 2nd Dimension

Unlike traditional programming languages where the IP moves from left to right and top to bottom within a program with occasional jumps to other locations in the program, Funge allows for the IP to move in any direction. At the start of a program, the IP is always set to the first character of the first line of the program and is set to move left to right. The movement that an IP makes between instructions is called its delta. The initial delta of a Befunge IP is (1,0) meaning it moves 1 space to the right after each instruction executed. There are commands to actually change the direction that the IP travels. Lets look at our first example:


"olleH"v
       5
       k
       ,
       @

This program is very similar to one of the examples in the last lesson except that it is now bent downwards halfway through the program. When you run this, it will still print Hello just like in the original example. The new command v changes the delta for the IP to (0,1), or move top to bottom. From this point on the IP is going to move down the program instead of left to right, so the next instruction executed will be the 5 instruction, followed by the k instruction and so forth.

Lets look at another:


"olleH"v
   @,k5<

The new command here is <. This command will change the delta of the IP to (-1,0) or in other words, move right to left. This is now opposite of how the IP started. With the IP now moving right to left the next instruction will be the 5 followed by k and so on.

Now lets look at the next direction changing command:


"olleH"v
     @
     ,
     k
     5
     ^ <

Here the ^ command changes the direction of the IP to (0,-1) or, move bottom to top. So yet again 5 will be the next instruction executed followed by k and so on. Be sure to note that in this program the v command and < command are both in the same column! If the < were not in the same column then the IP would continue to go from top to bottom and no further work would be done.

This program also demonstrates another point, notice all the spaces in this program, everything between the v and the < command are spaces, and between the < and the ^ command there is also a space. Spaces in Funge act as space fillers and do not perform any function at all. An IP will move over any spaces it finds up til the next actually executable instrucion. Therefore spaces can be used within programs to make sure that program sequences are positioned right where they need to be, just like in the above example.

And now for the final direction changing command:


"olleH"v
     >  k,@
     5
     ^ <

The final command > will set the delta to (1,0) or from left to right. This program also demonstrates another point, program flow can cross right over the paths where it does before. The path from the > command to the k, command crosses right over the track from the v to the < command. This is perfectly acceptable to do. The only thing to keep in mind is that if there are executable commands on the track, they will be executed in the normal flow of the program.

At this point you should have a solid grasp of IP movement, if you are not sure, then reread this section and study the examples. The key to funge is this multi-dimensional movement of the IP and it is critical to understand exactly how this works.

The v, ^, <, and > commands are all considered absolute direction changing commands. It does not matter in which direction the IP was going when it hits these commands, the IP will always go the direction of the specified command.

There are also 3 relative delta changing commands. Lets take a look at this program:


 1.v
@.4[2.v
   ^.3<

The new command is the [ command. this when executed will turn the IP 90 degrees left to its current direction of travel, and not just make the IP go to the left. When this program is run it will output all 4 numbers. when the IP encounteres the [ for the first time, it is moving downward, or south. If you are moving south when you take a left hand turn you will now be heading east, or in the case of the IP, to the right! after this turn the next command sequence to be executed is:


 2.v<3.^

Does this sequence make sense to you, if not read the above sections again on the commands that change IP direction.

Now after the ^ command, the IP is moving up, or north, when it again encounters the [ command. When travelling north and you take a left hand turn you will now be travelling west, or in the case of the IP, to the left. The final 4 will be printed and the program ends.

The [ command is a relative direction changing command, the direction the IP will go is dependent upon which direction the IP was going when it encountered this command. Here is a table of the possible outcomes of the IP encountering this command:

IP enters going IP leaves going
LeftUp
RightDown
DownRight
UpLeft

Just as there is a turn left command, there is also a command to turn to the right. Lets change the program a bit:


 1.v
v.2]4.@
>3.^

Just like the last program, all 4 numbers will be printed. The ] command will turn the IP 90 degrees to the right when it is encountered.

Here is a table that describes how the ] behaves:

IP enters going IP leaves going
LeftDown
RightUp
DownLeft
UpRight

The last relative delta changing command can be seen in this program:


v
[ r
1
.
@

The command r will reflect the delta of the IP so that it is going in exactly the opposite direction of when it encountered the r command. If the IP was moving right, then after r it will be moving left. If the IP was moving down when it encounteres r then it will be moving up afterwards. You should understand how the rest of this program is working. If not, reread the above sections

Conclusion

It is absolutely critical that you understand IP movement in Funge. Many of the language features are keyed around this multi-dimensional movement of the IP. Be sure you fully understand this entire lesson since all future lessons are going to be built upon this multi-dimensional movment.

Next lesson will center on program flow control and branching.

Commands learned in this part:
Command What it does
vChange IP direction to down (0,1)
^Change IP direction to up (0,-1)
<Change IP direction to left (-1,0)
>Change IP direction to right (1,0)
[Turn IP 90 degrees left of current direction
]Turn IP 90 degrees right of current direction
rReflect IP back to where it came from