BFC is a simple compiler that translates Brainfuck code into C code. It is built to convert Brainfuck programs into executable C programs, making it easier to run, debug, and optimize Brainfuck code.
- Supports all standard Brainfuck operations
- Generates a complete C program with main function and necessary includes
- C++ compiler (e.g., g++, clang++)
To compile the BFC compiler, use your C++ compiler of choice. For example:
g++ -o bfc bfc.cpp
This will create an executable named bfc
.
./bfc <input-bf-file> <output-c-file>
<input-bf-file>
: Path to the input Brainfuck file<output-c-file>
: Path where the output C file will be saved
-
Create a Brainfuck file (e.g.,
hello.bf
) with the following content:++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
This program prints "Hello World!".
-
Compile the Brainfuck code to C:
./bfc hello.bf hello.c
-
Compile the generated C code:
gcc -o hello hello.c
-
Run the compiled program:
./hello
This should output:
Hello World!
BFC translates each Brainfuck operation into equivalent C code:
+
becomesmem[p]++;
-
becomesmem[p]--;
>
becomesp++;
<
becomesp--;
.
becomesputchar(mem[p]);
,
becomesmem[p] = getchar();
[
becomeswhile (mem[p]) {
]
becomes}
The generated C program uses a character array mem
to simulate the Brainfuck memory tape and an integer p
as the memory pointer.
- The memory tape is fixed at 65,536 cells.
- Error handling for invalid Brainfuck syntax is not implemented.
- BFC does not perform any optimizations on the Brainfuck code (currently). It relies on the C compiler to optimize the generated C.
Contributions to improve BFC are welcome. Please feel free to submit issues or pull requests on the project repository. This project is open-source and available under the MIT License.