A human-readable transpiler that converts C-like syntax to Brainfuck code. Features variables, control flow, and I/O operations.
BrainfuckScript (BFS) is a human-readable programming language that transpiles to Brainfuck. While Brainfuck is notoriously difficult to write directly, BFS provides familiar syntax similar to C, making it accessible to write programs that ultimately run as Brainfuck code.
'A' to their ASCII values#define for constantsif/else) and loops (while)== and != for comparisons (other operators not supported)# Clone the repository
git clone https://github.com/ImGajeed76/brainfuck_transpiler
cd brainfuck_transpiler
# Install dependencies
poetry install
# Basic usage
poetry run python main.py your_code.bfs -o output.bf
# To see the intermediate instruction set (for debugging)
poetry run python main.py your_code.bfs -o output.bf --debug
The following example creates a pyramid of asterisks:
#define SPACE ' '
#define STAR '*'
#define NEWLINE '\n'
#define HEIGHT 7
// Pyramid height
var height = HEIGHT;
var row = 0;
while (height) {
// Print spaces (height-1 spaces per row)
var spaces = height - 1;
while (spaces) {
output(SPACE);
spaces = spaces - 1;
}
// Print stars (2*row+1 stars per row)
// Using only addition: row + row + 1
var stars = row;
stars = stars + row;
stars = stars + 1;
while (stars) {
output(STAR);
stars = stars - 1;
}
// Print newline
output(NEWLINE);
// Move to next row
height = height - 1;
row = row + 1;
}
Output:
*
***
*****
*******
*********
***********
*************
var name = value; // Declare and initialize
name = value; // Assignment
All variables are unsigned 8-bit integers (0-255).
#define NAME value // Define constants
#include "file.bfs" // Include another file
// While loop
while (condition) {
// Code block
}
// If statement
if (condition) {
// Code block
}
// If-else statement
if (condition) {
// Code block
} else {
// Code block
}
Note: Conditions are considered true if non-zero, false if zero.
output(variable); // Output ASCII character
input(variable); // Read ASCII character from input
The transpiler works in two stages:
Parsing and Intermediate Code Generation: The BFS code is parsed using the Lark parser, generating an intermediate instruction set.
Brainfuck Code Generation: The intermediate instructions are converted to Brainfuck.
Using the --debug flag shows the intermediate instruction set:
LOAD_A_IMM 7
STORE_A 2
LOAD_A_IMM 0
STORE_A 3
LOAD_A_MEM 2
LOOP_START
...
The transpiler manages memory allocation for variables. For example:
# Memory map:
# height: address 2
# row: address 3
# spaces: address 4
# stars: address 5
The transpiler doesn't include a Brainfuck interpreter. To run the generated code:
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributions are welcome! Feel free to submit issues or pull requests.