Brainfuck parser
Project description
Brainfuck
Pure python brainfuck parser, interpreter Including optimizations, translation into python AST and python code
Installation
pip install brainfuck-venomlab
Usage
From code
from brainfuck import Brainfuck
from brainfuck.contrib import execute, to_python_ast, to_python_code
source_code = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++."
bf = Brainfuck.compile(source_code)
# Execute code directly
execute(bf)
# Optimize
bf = bf.optimize()
# Get python code
python_code = to_python_code(bf) # type: str
# Get python AST
program = to_python_ast(bf) # type: ast.AST
# Compile AST info code object with `exec` mode - it's module
executable = compile(program, "program.py", "exec")
# Either exec or eval
exec(executable)
From command line
Execute brainfuck from *.bf file:
brainfuck -f examples/hw.bf
# Hello World!
Execute brainfuck from commandline:
brainfuck -c "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++."
# Hello
Translate brainfuck to python with -py
flag:
brainfuck -py -c "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++."
# buffer = bytearray(30000)
# index = 0
# buffer[index] += 1
# buffer[index] += 1
# buffer[index] += 1
# ...
# print(chr(buffer[index]), end='')
Enable optimizations with -o
flag:
brainfuck -py -o -c "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++."
# buffer = bytearray(30000)
# index = 0
# buffer[index] += 10
# ...
# buffer[index] += 3
# print(chr(buffer[index]), end='')
Write python output to file:
brainfuck -py -out "hw.py" -o -c "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++."
# file `hw.py` is created
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for brainfuck_venomlab-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a2e0dca663738176588815126538f1eec878b040fbf39c78d5a8e99e77f4506 |
|
MD5 | a5573d0d4850ab9918dcd4527d74c6f9 |
|
BLAKE2b-256 | 03cceef0a8fce3ac9f91b652a5498bc64c6294cd02c819b861378d2980f1ee6f |