Awesome
Preprocessor Brainfuck Interpreter
This project contains an interpreter for brainfuck programs that is written solely in C preprocessor macros.
This implementation only uses simple preprocessor conditionals (#if some-expression
), preprocessor defines without arguments (#define FOO some-expression
), and inclusion of header files (#include HEADER
). There exist other (IMO better) implementations of brainfuck for the C preprocessor that use other features, such as token pasting (such as CPP_COMPLETE).
This implementation also makes use of generated header files to create modifiable variables whose size is dependent on the max number of steps, the max size of the tape, and the max value of each value on the tape. The only generated file that is dependent on the input brainfuck program is the file that defines the code tape and the input tape (main.h
, generated by bf_to_main.sh
), which is a simple 1:1 translation of the brainfuck code and input to a sequence of defines.
Since the number of steps is limited and the size of variables is finite, this implementation is not turing complete. A recursive inclusion of the main run loop header file doesn't really help, because every major compiler has limits on the recursion depth of header files.
How to use
- Generate the headers
$ make
- Encode your program into
main.h
, and provide the file used as input
$ ./bf_to_main.sh hello.bf /dev/null > main.h
- Run the brainfuck interpreter
$ make run
or
$ make run DEBUG=1
Note
The preprocessor brainfuck interpreter is very very inefficient and will use
around 16 GIGABYTES of memory and 15 to 20 minutes of processing time
while running hello.bf
.