2024-02-05

Two functions are defined:

compile_brainfuck: This function takes two arguments. The path to the Brainfuck source file and the path where the executable should be placed after compilation. It reads the Brainfuck code from the source file, generates equivalent C code using generate_c_code function, writes the C code to a temporary file, compiles it using gcc, and removes the temporary C file afterward.

generate_c_code: This function takes the Brainfuck code as input and generates equivalent C code. It translates each Brainfuck instruction to its C equivalent.

The script then checks if it’s being run directly (not imported as a module) using if name == “main”:.

It prompts the user to enter the path to the Brainfuck source file and the directory where they want the executable to be placed.

It extracts the filename from the source file path and combines it with the output directory to get the full path for the output file.

Finally, it calls compile_brainfuck function with the source file path and the output file path as arguments, initiating the compilation process.

import os
import subprocess


def compile_brainfuck(source_file, output_file):
    # Read Brainfuck source code from file
    with open(source_file, 'r') as file:
        code = file.read()

    # Generate C code with Brainfuck interpreter
    c_code = """
    #include <stdio.h>
    #include <stdlib.h>

    int main() {
        unsigned char tape[30000] = {0};
        unsigned char *ptr = tape;

        %s

        return 0;
    }
    """ % generate_c_code(code)

    # Write the generated C code to a temporary file
    with open('temp.c', 'w') as file:
        file.write(c_code)

    # Compile the C code into an executable
    subprocess.run(['gcc', 'temp.c', '-o', output_file])

    # Remove the temporary C file
    subprocess.run(['rm', 'temp.c'])


def generate_c_code(brainfuck_code):
    c_code = ""
    for char in brainfuck_code:
        if char == '>':
            c_code += "++ptr;\n"
        elif char == '<':
            c_code += "--ptr;\n"
        elif char == '+':
            c_code += "++(*ptr);\n"
        elif char == '-':
            c_code += "--(*ptr);\n"
        elif char == '.':
            c_code += "putchar(*ptr);\n"
        elif char == ',':
            c_code += "*ptr = getchar();\n"
        elif char == '[':
            c_code += "while (*ptr) {\n"
        elif char == ']':
            c_code += "}\n"
    return c_code


if __name__ == "__main__":
    source_file = input("Enter the path to the Brainfuck source file: ")
    output_directory = input("Enter the directory path where you want the executable to be placed: ")

    # Extracting file name from the source_file path
    source_file_name = os.path.basename(source_file)
    # Combining directory path and file name to get the full output file path
    output_file = os.path.join(output_directory, os.path.splitext(source_file_name)[0])

    compile_brainfuck(source_file, output_file)