So, I bet you've all seen in BrainF in Repl.it's list of languages and wondered, "what on Earth's this?" You clicked on it, and opened up the examples. There's a load of horribly confusing-looking code, using only 8 characters. Well, here's a tutorial to teach you how to use the programming language which is true to its name.
1. How the language works
BrainF works using memory cells. Repl.it's BrainF IDE, like most, has 30,000 memory cells, each of which can hold a single byte (some have fewer, most commonly mobile apps, like BrainFudge for Android which only has 300). There is a pointer, which always selects a single cell. The language works by moving the pointer and changing the value of the current cell - you can only ever handle the cell that is selected. When the program starts, every cell holds 0 and the pointer points to cell 0.
2. The characters
As I mentioned earlier, BrainF uses only 8 characers: > < + - [ ] , .
- > <
- Move the pointer right and left
- + -
- Increment and decrement the value of the current cell. If the value is lowered below 0 or raised above 127, the program will still function and the cells will retain their values, but they will not be printable.
- Take the value of the current cell, and print the ASCII character with that value
- Take an input of a single character, and store that character's ASCII value to the current cell, overwriting any existing data in that cell. If more than one character is provided, the Repl.it IDE will ignore any after the first.
- [ ]
- This is where it gets more complicated: these two characters are what allow you to create a loop in BrainF.
- When [ is reached, if the value of the current cell is 0, the program will skip to the corresponding ]; if the value of the current cell is not 0, the program will keep running
- When ] is reached, if the value of the current cell is 0, the program will keep running. If the value of the current cell is not 0, the program will jump back to the corresponding [.
- This means that [ ] is effectively
while current_cell != 0. There are, however, 2 main things to be careful of when creating loops:
- 99 times out of 100, you want to finish an iteration of the loop with the pointer at the same place as it started
- You also need to make sure the cell in question is actually decreasing towards 0: the program doesn't do this automatically, you need to make sure you do it, otherwise you will end up with an infinite loop, which most BrainF IDEs don't like.
There are a few little handy hints you might find useful when actually writing programs in BrainF:
- Say you want to print the character
H, which has an ASCII value of 72. Instead of doing
+s, you can do something like this:
What this does:
>at the beginning moves the pointer to the second cell
+s puts a value of 8 into the second cell
- We then enter into a loop:
<takes the pointer back to the first cell, we then add 9 to the first cell, before going back to the second cell (
>) and subtracting one (
-). This loop means we add 9 to the first cell a total of 8 times, resulting in a total value of 72.
- We then move the pointer back to the first cell (
<) and print (
.) H, because H has an ASCII value of 72.
[-]sets the current cell's value to 0
Here's an example BrainF program, with comments!
4. Upvoting this tutorial
It is a strange but true scientific fact that if you upvote this tutorial, not only will you receive vee freebucks, but you will immediately find BrainF easier. All seriousness aside though, please do upvote if you enjoyed this tutorial or found it useful.
Coming (not very) soon: How to write a BrainF interpreter in Python!
One final tip: don't take my example, never put comments in your BrainF code. If you do comment it, you'll be able to come back in a month's time and actually understand your code. If this happened, the language would need renaming: