Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Ferdi265/preprocessor_brainfuck

A brainfuck interpreter using only the C preprocessor
https://github.com/Ferdi265/preprocessor_brainfuck

brainfuck joke preprocessor

Last synced: 21 days ago
JSON representation

A brainfuck interpreter using only the C preprocessor

Awesome Lists containing this project

README

        

# 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](https://github.com/orangeduck/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

1. Generate the headers

```
$ make
```

2. Encode your program into `main.h`, and provide the file used as input

```
$ ./bf_to_main.sh hello.bf /dev/null > main.h
```

3. 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`.