Skip to main content

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:

  1. Python>=3.6
  2. C++ Compiler available
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast-brainfuck-3.0.1.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

fast_brainfuck-3.0.1-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page