Transpiler for compiling Brainfuck to Python.
- Brainfuck code is tokenized using a lexer
- A parser constructs an abstract syntax tree from the tokens
- The constructed Brainfuck syntax tree is translated to a Python syntax tree using the Python ast module.
The following example reads a Brainfuck program from stdin, transpiles it to Python and prints the resulting program to stdout:
echo '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.' | poetry run transpiler/transpile.py
memory = [0] * 30000
pointer = 0
memory[pointer] += 8
while memory[pointer] != 0:
pointer += 1
memory[pointer] += 4
while memory[pointer] != 0:
pointer += 1
memory[pointer] += 2
pointer += 1
memory[pointer] += 3
pointer += 1
memory[pointer] += 3
pointer += 1
memory[pointer] += 1
pointer -= 4
memory[pointer] -= 1
pointer += 1
memory[pointer] += 1
pointer += 1
memory[pointer] += 1
pointer += 1
memory[pointer] -= 1
pointer += 2
memory[pointer] += 1
while memory[pointer] != 0:
pointer -= 1
pointer -= 1
memory[pointer] -= 1
pointer += 2
print(chr(memory[pointer]), end='')
pointer += 1
memory[pointer] -= 3
print(chr(memory[pointer]), end='')
memory[pointer] += 7
print(chr(memory[pointer]), end='')
print(chr(memory[pointer]), end='')
memory[pointer] += 3
print(chr(memory[pointer]), end='')
pointer += 2
print(chr(memory[pointer]), end='')
pointer -= 1
memory[pointer] -= 1
print(chr(memory[pointer]), end='')
pointer -= 1
print(chr(memory[pointer]), end='')
memory[pointer] += 3
print(chr(memory[pointer]), end='')
memory[pointer] -= 6
print(chr(memory[pointer]), end='')
memory[pointer] -= 8
print(chr(memory[pointer]), end='')
pointer += 2
memory[pointer] += 1
print(chr(memory[pointer]), end='')
pointer += 1
memory[pointer] += 2
print(chr(memory[pointer]), end='')
Since the transpiled program is written to stdout, you can execute it directly by piping the result to the Python interpreter:
echo '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.' | poetry run transpiler/transpile.py | python3
Hello World!