C2BF

C2BF is a partially implemented C compiler targetting Brainfuck, written by Gregor Richards. The source is available via SVN: svn co https://c2bf.svn.sourceforge.net/svnroot/brainfuck/c2bf/trunk c2bf

=Implementation= Brainfuck's tape-based system is very different from the stack-and-heap view of a conventional computer. C is tailored to fit a stack-and-heap view, and so, a stack and heap are emulated in the tape. This is done by dividing the tape into repeating five-cell chunks. Each cell serves a different purpose:

1) Stack 2) Heap 3) Stack location marker / top marker 4) Walk 5) Carry

The stack grows from the bottom up (unlike many computers). The stack and heap are addressable in the order they appear: That is, 0x00 is stack, 0x01 is heap. To rectify this situation, all pointer math in C is multiplied by 2. Since the pointer math is already assumed to be implicitly multiplied, this (while breaking the spec) shouldn't break much code.

The stack location marker is used to determine the current location in the stack, and whether the pointer is at the top of the stack. This is mostly necessary because of how functions are implemented.

Functions
The function style was suggested by calamari, and is used in his BFBASIC.

Basically, at the top of the stack, there are a number of reserved spaces, precisely equal to the number of functions. At the beginning of execution, a default one of these is set (the one referring to the main function). A loop checks which one is set and runs an appropriate block of code. That block of code conventionally ends by setting another one, thus causing that block to be "called." Finally, it leaves in the stack the address for the next block in the current function when calling another function. The function being called, in turn, reads this address and marks that block to be called.