https://github.com/interrrp/dementia
🥴 A blazingly fast Brainfuck interpreter in Python
https://github.com/interrrp/dementia
brainfuck python
Last synced: 10 months ago
JSON representation
🥴 A blazingly fast Brainfuck interpreter in Python
- Host: GitHub
- URL: https://github.com/interrrp/dementia
- Owner: interrrp
- License: mit
- Created: 2025-07-12T12:11:19.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-13T12:51:50.000Z (12 months ago)
- Last Synced: 2025-07-13T14:42:22.204Z (12 months ago)
- Topics: brainfuck, python
- Language: Brainfuck
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dementia
A blazingly fast Brainfuck interpreter written in Python.
## Usage
1. Install [Python](https://python.org/) if you haven't already.
2. Clone the repository.
3. `python -m dementia `
Example programs are provided in the [programs](programs) directory.
## Optimizations
"How is it so fast?" you may ask. Well, optimizations, of course. How else?
### 1. Process Brainfuck into IR
Identifies common patterns:
| Pattern | IR |
| ---------- | ------------- |
| `+++++--` | `+ 3` |
| `>>>>><<` | `> 3` |
| `[-]` | `clear` |
| `[-<<+>>]` | `transfer -2` |
### 2. Build Python code
Transforms the IR into Python code.
```py
# +++ (+ 3) gets transformed into this:
tape[ptr] += 3
# Similarly, >>> (> 3) gets transformed into this:
ptr += 3
# etc...
```
### 3. Execute Python code
Execute the generated Python code using `exec`.
### 4. Use PyPy (optional, 27x speed boost)
The performance difference between running this on CPython compared to PyPy is quite huge. On [mandelbrot.b](programs/mandelbrot.b), PyPy offered a 27x speed boost (1m27s vs 3s!).
## License
This project is licensed under the [MIT](LICENSE) license.