This tutorial is for the programming language Brainfuċk, I used the Brainfuċked compiler, there's also a popular Brainfuċk interpreter in PHP. The Brainfuċked compiler is 799 bytes, has syntax checking, and of course is open source. The smallest compiler in the world is for Brainfuċk, and is 171 bytes.
Brainfuċk is a cross-platform programming language with only eight instructions, and no operands, no variables, no conditions, and of course no functions.
Let's go through all of the possible instructions you can use with Brainfuċk:
> move the memory pointer to the next memory block
< move the memory pointer to the previous memory block
+ increases the memory cell under the memory pointer
- decreases the memory cell under the memory pointer
[ starts a 'while' loop "c while(cur_block_value != 0)"
] ends a 'while' loop
. writes the contents of the memory cell under the memory pointer as an ASCII character "c putchar()"
, reads an ASCII character input and fills the memory cell under the memory pointer "c getchar()"
Anything that's not one of those eight characters will be ignored by the compiler and taken as comments. The memory pointer always starts on the left-most memory block, and all memory blocks start with a zero value.
Now let's do some coding, open up Notepad, and remember to save your projects as *.b's.
That's the full code for our first program in brainfuċk, normally you start with a Hello World program, but, we'll get there eventually, it's more complex than you would first imagine.
+++++[-] does several things. First we are increasing the memory cell under the memory pointer to five. Then we enter a loop that loops subtracting our value of five by one until it reaches zero, then exits the loop. This could take up to five lines of code in a language such as C, which is also less efficient.
This moves our memory pointer to the third memory block and increases the memory cell's stored value by two.
Now let's add onto this program, we'll change several memory cell's values.
Now this program moves our memory pointer to the third memory block, increases the memory cell's stored value by two, goes back to the first memory block and increases that memory cell's stored value by one. Then goes back to our third memory block and increases our existing memory cell value (of two), to five by incrementing it by three.
Until now, you haven't been able to see your own programs' calculations. Let's create a program that has some output, the 'Hello World' program.
Since we can only work with numbers, we have to use characters' ASCII values to represent the characters we want to output.
First increases the memory pointer to the first memory block, and increases the memory cell's value to nine. We then enter our loop, go back to our zero block and change it's value to eight, go back to our first memory block and decrease the memory cell's stored value to eight, and, returns back to the start of our loop until the memory cell the memory pointer is on is equal to zero. This loop continues until our zero memory block is at 72, and our first memory block is at zero. We then return our memory pointer to the zero memory block, and with our '.', we print the ASCII decimal value, which represents the letter 'H'.
As you can imagine, it then goes through the rest of the characters and does the same.
I hope you enjoy programming in Brainfuċk as much as I have. Once you get good with Brainfuċk, I suggest moving to Brainfork and Braintwist, which still use the core idea of Brainfuċk, with extra capabilities.