C2BF (2025)

From Esolang
(Redirected from C2bf (2025))
Jump to navigation Jump to search

C2bf (not to be confused with C2BF) is a compiler from a large subset of C into Brainfuck written by Ian Graham Martinez. The project is written in Rust, and the source code is available at [1].

The project supports: - Integer Arithmetic - Loops & if-statements - Arrays - Pointers - Functions (including recursive ones) - Function Pointers

Implementation

C2bf compiles C code to a stack-based intermediate language, so that each instruction can then be translated into a Brainfuck snippet. For example,


putchar('0' + 5);

... is compiled into the sequence:

Push(48)
Push(5)
Add
PutChar

Then, each of these is translated into Brainfuck (the convention is that each instruction begins execution pointing at the top of the stack, which grows rightward, and that all cells to the right of the tape head are zero):

Push(48) --> ++++++++++++++++++++++++++++++++++++++++++++++++
Push(5)  --> +++++
Add      --> [-<+>]<
PutChar  --> .

For control flow, the IR is split into basic blocks, and the entire program is placed in a [] loop. See [2] for more details about the implementation and its limitations.