BrainFuck : An eight-command awesome esoteric programming language

I was going through the SPOJ (SPhere Online Judge) challenge problems when i saw this problem in which the aim was to print a line using brainf**k with minimum source code size. If other well known languages were allowed, this problem would have been ridiculous. So i decided to try my hands on this esoteric language. Given its weird nature and limited features, playing with it is quite fun and the best part about it is that it takes you only around 10 minutes to learn this language.

What is Esoteric Languages?

An esoteric programming language (sometimes shortened to esolang) is a programming language designed as a test of the boundaries of computer programming language design, as a proof of concept, or as a joke.
Wikipedia on Esoteric Programming Languages

These are usually weird programming languages which are not intended to be used for any practical purpose. eg. are brainfuck, whitespace (a language which only uses whitespace characters, which are spaces , tabs and linefeeds), malbolge (a language so difficult to understand that it took two years for first malbolge program to appear which was generated by a computer!), etc.

Without going any furthur in other Esoteric Languages, lets play with brainfuck.

Brainf**k

Created by Urban Müller, brainfuck is a language that operates on only 8 very basic commands and ignores everything else. It consists of a pointer which initially points to the first byte in an array of 30000 bytes. All the bytes of this array are initially set to 0. Following are the commands which you can use in your brainfuck program along with its C equivalent statement :

Brainfuck Command C/C++ equivalent(ptr is a pointer) Explanation
> ptr++ The pointer is incremented to point to the next byte
< ptr – – The pointer is decremented to point to the previous byte
+ *ptr++ The byte pointed by the pointer is incremented by 1
*ptr – – The byte pointed by the pointer is decremented by 1
. printf(“%c”,*ptr) The ASCII equivalent character of value at byte pointed by the pointer is displayed
, scanf(“%c”,ptr) A character is stored into the current byte from the standard input
[ while(*ptr){ If the value of the current byte is 0, then the consecutive commands are not executed until a ] is encountered. If the value is greater than 0, than the execution continues.
] } If the value of the current byte is 0, the execution continues to the next command, else the execution jumps to the corresponding [

This is all about the language.

Now lets begin with a typical “Hello World” program -:

++++++++++        //increments the 0th byte value to 10
[
>+++++++        //1st byte incremented by 7
>++++++++++        //##
>+++++++++++        //##
>+++            //##
>+++++++++        //##
>+            //##
<<<<<<-            //0th byte decremented by 1
]            //when the loops exits value of 1st byte is 70;2nd is 100;3rd is 110;4th is 30;5th is 90;6th is 10
>++.            //Prints 'H'
>+.            //Prints 'e'
>--.            //##
.            //##
+++.            //##
>++.            //Prints a space
>---.            //Prints 'W'
<<.            //##
+++.            //##
------.            //##
<-.            //##
>>+.            //Prints '!'
>>.            //Prints a newline

I have neatly written the above program for sake of convenience. it can be also written by getting rid of all the new lines so that it looks like


++++++++++[>+++++++>++++++++++>+++++++++++>+++>+++++++++>+<<<<<<-] >++.>+.>--..+++.>++.>---.<<.+++.------.<-.>>+.>>.

The biggest challenge in Brainfuck is to write the shortest code. Although my above Hello World program might not be the shortest, it is still not bad. The Hello World Program is just a simple example, brainfuck can also be used to calculate factorials, generate fibonacci sequence, etc. But these programs are quite complex and justify the name of the language.

Regarding the SPOJ problem i was talking about, here is my code and it fetched me a pretty good score of 159 with the best being 129. I will try to make it shorter. Here is my code


++++++++++[>++++++++>+++>>>+++<<<<<-]>+++.---.-.-----.>++.<----[>>+++<<--]>>.>>[<++++>-]<-----.<<.>.>-----.<-----.+..-.<.>---.>+++++++++.----<++++.>.----.--.<.

Compiling and running Brainf**k code

Under Linux :

copy the content of http://www.muppetlabs.com/~breadbox/software/tiny/bf.asm.txt and save it as bf.asm. Build it using nasm :

nasm -f bin -o bf bf.asm && chmod +x bf

to compile a brainfuck program,
./bf < inputFile > outputFile
chmod +x outputFile

eg. ./bf < x.b > x && chmod +x x

To run it

./outputFile

Under Windows :
I haven’t tried any brainfuck compiler under windows, but here is a Link to a brainfuck compiler+IDE

Helpful Brainf**k Resources

Brainfuck Language
The Brainfuck Archive
Brainfuck Programs by Keymaker
Brainfuck Programs by Daniel
Short Brainfuck Programs
Compiler and IDE for Windows
Compiler For Linux

Advertisements

4 thoughts on “BrainFuck : An eight-command awesome esoteric programming language

How did you feel about this post? Push in your reply!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s