Brainfuck "compiler" for Python (fast)
Project description
A big bugfix is applied to this package due to the technique Python uses to import modules.
In previous versions, when brainfuck_to_function is called, it creates a module named _foo, which is imported. But Python also stores it into a cache, that means when you call brainfuck_to_function again, the new module would not be imported, which causes a function the same as the previous function is returned.
However, in this version and higher, a module with a random name is created instead, which fixed the bug.
This is a very fast brainfuck "compiler".
When used, it builds brainfuck code into Python extension modules, and imports it.
Requires:
- Python>=3.6
- C++ Compiler available
- pybind11
Running brainfuck
Use the 'brainfuck_to_function' function to convert brainfuck into function. It will take several seconds for the C++ compiler to build the module, but after build, you can call it anytime without building, which will be faster, and about 3x faster than directly using a brainfuck interpreter implemented in C++.
from fastbf import brainfuck_to_function
code='++++++++[>++++++++<-]>++++++++.>++++++++++++[>++++++++<-]>+++++.+++++++..+++.>++++[>++++++++<-]>.<<<<+++++++++++++++.>>.+++.------.--------.>>+.'
func=brainfuck_to_function(code)
func() # Hello World!
Freezing brainfuck into executables
fast-brainfuck is not supposed to be frozen into executables (e.g. using PyInstaller). But fortunately, you can use the 'dist_brainfuck' function to convert brainfuck into Python modules. You can write setup.py like this:
from fastbf import dist_brainfuck
code='++++++++[>++++++++<-]>++++++++.>++++++++++++[>++++++++<-]>+++++.+++++++..+++.>++++[>++++++++<-]>.<<<<+++++++++++++++.>>.+++.------.--------.>>+.'
dist_brainfuck(
code,'hello'
)
Then run python setup.py build_ext --inplace, the files hello.py and hello.xxx.pyd (hello.xxx.so) will be created, you can now use
import hello
hello.run()
to run the brainfuck in your script (which CAN be frozen into executables).
Note that fast-brainfuck does NOT clean up the temporary files it creates. You have to manually delete them if you want.
fast-brainfuck can run on CPython or PyPy.
ImportError: xxx
If you encountered this error when using this package in PyPy in Windows, that means your username has non-ASCII characters. You can run
import os
os.environ['TMP']='C:\\Somedir'
before using fast-brainfuck to fix the error.
Note that moving the pointer to the left of the origin causes UNDEFINED BEHAVIOR in this version
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
File details
Details for the file fast-brainfuck-3.0.1.tar.gz
.
File metadata
- Download URL: fast-brainfuck-3.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e96b7429699db147b0d441ad368bc46697b810d3a7c8840765c48c926e62832b |
|
MD5 | d00ea9e46ea978f6af41b90c1584569c |
|
BLAKE2b-256 | c1999284949e1ead0da83f1561510c81d8823db99e18a1f22c6789893a042e5a |
File details
Details for the file fast_brainfuck-3.0.1-py3-none-any.whl
.
File metadata
- Download URL: fast_brainfuck-3.0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9819837bba8d0ffc61719906c60151421e4278ae2fcf0f54d622bbff0ef0413 |
|
MD5 | dc41cde4a1788ae84820f393289e9386 |
|
BLAKE2b-256 | 87716a690048335bcd49859ce51bb8a0db8a423feb8db5c1c625bcb3355f1627 |