I stumbled over this funny site called CodeGolf.com and tried my luck on the first problem (actually it’s the sixth, but it was the last and therefore appeared first on their page – confused?) Writing a Brainfuck Interpreter:
The brainfuck language uses a simple model of a computer consisting of an array of memory cells, a movable pointer into the array, an output stream, an input stream and the program itself. The program is formed from a sequence of the below commands :
>
– Increment the pointer to point to the next cell to the right.
<
– Decrement the pointer to point to the next cell to the left.
+
– Increment the byte pointed to by the pointer.
-
– Decrement the byte pointed to by the pointer.
[
- Jump forward to the command after the corresponding ]
if the byte at the pointer is zero.
]
– Jump back to the command after the corresponding [
if the byte at the pointer is non-zero.
.
– Output the value of the byte at the pointer.
,
– Accept one byte of input, storing its value in the byte at the pointer.
It didn’t take me very long to write a running interpreter. My first version was more than a thousand characters. Even after renaming the variables and stripping all the whitespaces I still needed around 700 characters, which is huge compared to the best Ruby solution with only 142 characters. Read the rest of this entry »