The brilliant factor about the language is that it is just made of eight symbols in total..!! Surprised.. so was I..!! :D
Whole of the language depends on just an array. Every manipulation has to be done in the array using a memory pointer, which moves through the memory blocks of the array. Now, getting over to the practical implementation of the language. The eight symbols are:
> = increases memory pointer, or moves the pointer to the right 1 block.
< = decreases memory pointer, or moves the pointer to the left 1 block.
+ = increases value stored at the block pointed to by the memory pointer
- = decreases value stored at the block pointed to by the memory pointer
[ = like c while(currentBlockValue != 0) loop.
] = if block currently pointed to's value is not zero, jump back to [
, = like c getchar(). input 1 character.
. = like c putchar(). print 1 character to the console.
I made my first program for SPOJ challenge problem BFWRITE. Before going through the code I would give you the link of an online compiler for BrainF_ck language. I prefer BrainF interpreter. So now, here's my code:
Looks quite difficult to understand but I'll make it simpler for you.
- First line assigns value 10 to a
- Second line begins a loop checking value of current block a.
- Third line moves pointer ahead once(>) and assigns value 7 to a(++++++). The same line then again pushes pointer to a and assigns value 10. 3 value is given to a. The pointer returns back to a(<<<) and decrements the value to 9.
- In the fourth line.. loop goes on until the value of a exhausts.
- Finally the values are.. a=0, a=70, a=100, a=30.
- The sixth line decreases a by 3 and makes its value equal to 67. '.' prints the value 'S', ASCII code of 67.
- Likewise the further lines print the text:- 'P','O','J',' ','i','s',' ','i','n','d','e','e','d',' ','a','w','e','s','o','m','e'. Hence on our output comes as:- SPOJ is indeed awesome.
* * * * *