Brainfuck!
This article is going to teach you the worlds most simple and hilarious language: BrainFuck.
Basically, all BrainFuck does is manipulate a huge line of 30000 bytes, with only 8 commands. Just imagine a string of 30000 bytes. At the start of the program, there is a pointer at the one right at the left. Now, we can move this pointer along the string of bytes with two operators:
> - move the pointer to the next byte on the right
< - move the pointer to the next byte on the left
Whilst at these bytes, we may either increase their values or decrease their values with these two:
Commands
+ - increase the value of the byte at the pointer by one
- - decrease the value at the byte at the pointer by one
Also, we can either input data in to these pointers, or out put data from these pointers, with two more.
Operators
, - input ASCII value of key pressed in to byte at the pointer.
. - output ASCII value of the pointer as a character.
So, a program to say 'Hello' could look something like this in BrainFuck:
++++++++++++++++++++++++++++++++++
Changes the value of the byte at the pointer to 72, or 'H' in ASCII.
. - Outputs ASCII the character of the byte at the pointer.
Changes the value of the byte at the pointer to 101, or 'e' in ASCII.
. - Outputs ASCII the character of the byte at the pointer.
Changes the value of the byte at the pointer to 108, or 'l' in ASCII.
.. - Outputs ASCII the character of the byte at the pointer twice.
Changes the value of the byte at the pointer to 111, or 'o' in ASCII.
. - Outputs ASCII the character of the byte at the pointer.
In BrainFuck, there is no need for comment marks, as any other characters other that the eight characters used as commands are simply ignored.
Now, there are two more commands in BrainFuck, and these are loop commands, they are rather difficult to get your head around at first, but soon become clear. They are:
[ and ]
These work by the commands inside the [] commands being repeated until the byte the pointer was at just before the [ operator becomes zero.
So, to print out a capital H thirty times, the BrainFuck code would look like this:
Make the value of the first pointer 30.
> - Go to the next pointer on the right
+++++++++++++++++++++++++++++++
Make the value of the byte at this pointer 72, or 'H' in ASCII.
< - Go back to the previous pointer.
[ - Start the loop.
>.<-] - Make it so the value of the next pointer is displayed as an ASCII character and the value of the previous pointer is decreased by one, so when the letter has been displayed thirty times the first pointer reaches zero and the loop stops.
And finally, for one last BrainFuck program, we will take user input of one letter and the display it back again. The code is this:
,.
That's it. As soon as , assigns the ASCII value of the key pressed to the pointer . displays it back again.
A good BrainFuck compiler can be found at: http://www.iamcal.com/misc/bf_debug/
That's all there is to this wonderful language.
Tutorial by Raduce