Programming in BrainFuck (You Heard Me Right)

Hello World in BFBF is definitely an esoteric language. I cannot imagine ever needing it over any practical programming language, but it's purpose is to have an incredibly tiny interpreter. Considering the language only has 8 operators in it, it's hard NOT to write a tiny interpreter. Although my goal wasn't to write it as small as possible, I wrote an interpreter purely for the experience.

In BF, there are 8 operators: <>.,[]+- There are no variables or oprands that these operators work with. Instead it's implied that you're operating on a string of bytes, one after another and that you have a pointer to one of those bytes. Characters in the command are read one after another unless a looping structure manipulates the placement of the next command to execute.

To maneuver from one byte to another on the stream of bytes you use < and > where < moves the memory pointer one byte to the left down the stream and > moves one to the right up the stream. Upon arrival at the desired byte, you can use + or - to increment or decrement the value that the pointer is pointing at respectively.

For conditions such as while loops or if statements, you use [ and ] to define a section. Upon executing a [ the byte that you're pointing at is compared to zero. If the byte is zero, the command pointer jumps to the command immediately after the ] that matches the opening bracket. If the value is non-zero, the command pointer moves to the next character inside the brackets. A ] works just the opposite, if the value is zero it allows the command pointer to exit the bracketed section but if the byte is non-zero, then the command pointer is pushed back to the matching opening bracket. This can be seen in the Hello World app pictured above. It loops 10 times adding various numbers to the 2nd, 3rd, 4th, and 5th bytes (the first being the counter itself). In short, it's just an operator that defines "while true, do what's in the bracketed section."

For IO, BF offers , (a comma) for input and . (a period) for output. I could find little documentation about how the comma accepted the input, but with the other interpreters I found, it only seemed to accept one character and it would read in that character's ASCII value. For traditioinal BFers, my interpreter defaults to that, but if you pass extra flags to the interpreter you can change it to a more user friendly numerical input, i.e. instead of putting in 'H' to get 72, you can simply put in 72. The period works along the same lines except that it prints the byte that you're currently resting at. As with the comma, it treats the byte value as a ASCII number and prints that character. Again, for simplicity, the same flag that takes numbers in as input will spit out numbers as output. Perhaps I'll split this functionality up to allow for numerical input and character output or vice versa. Who knows. Other than these 8 operators, any other character the interpreter runs across is ignored. This makes comments quite easy, as long as they don't contain the 8 operators; you just begin writing your comments where ever and with no explicit comment operator.

All in all, it was just for the fun of it and taking baby steps towards learning how compilers and interpreters work. It probably could be optimized a little, but it works as best as I can tell.

UPDATE: NEW AND IMPROVED!!! I've put it off long enough, I finally added an Interactive Mode and a Debugger! I'm really excited about these new additions. I'll be honest, the code may not be the cleanest project I've worked on, but as far as I can tell it works. Let me know if you find any errors.

All the source code can be downloaded here.

blog comments powered by Disqus