https://github.com/tomhea/bf2fj
A Brainfuck to FlipJump compiler
https://github.com/tomhea/bf2fj
Last synced: 8 months ago
JSON representation
A Brainfuck to FlipJump compiler
- Host: GitHub
- URL: https://github.com/tomhea/bf2fj
- Owner: tomhea
- License: bsd-2-clause
- Created: 2023-06-26T18:21:54.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-11T10:30:26.000Z (over 1 year ago)
- Last Synced: 2025-01-11T11:35:33.671Z (over 1 year ago)
- Language: Brainfuck
- Size: 509 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-brainfuck - bf2fj
README
[](LICENSE)
[](https://esolangs.org/wiki/FlipJump)
[](https://pypi.org/project/bf2fj/)
# bf2fj
A [Brainfuck](https://esolangs.org/wiki/Brainfuck) to [FlipJump](https://github.com/tomhea/flip-jump) Compiler.
The compiler was built to be very fast. Moreover, the flipjump programs generated by this project are generated in a way that will keep them very small, thus their compilation into an .fjm file is also fast.
See also the [C to FlipJump Compiler](https://github.com/tomhea/c2fj).
## Download:
```
>>> pip install bf2fj
```
## Run the compiler:
```
>>> bf2fj hello_world.bf
compile bf->fj: 0.008s
```
You can also run the created flipjump program.
```
>>> bf2fj hello_world.bf -r
compile bf->fj: 0.008s
parsing: 0.092s
macro resolve: 0.141s
labels resolve: 0.035s
create binary: 0.143s
loading memory: 0.017s
Hello World!
Finished by looping after 0.739s (337,484 ops executed; 85.36% flips, 98.88% jumps).
```
Note that you can modify the number of brainfuck cells that'll be reserved in the flipjump file, with `-c` / `--cells`.
## Optimizations
This compiler supports optimizations of the generated flipjump code.
The optimizations are significant. It lowered the hello_world.bf program from 2,900,990 fj ops to 337,484 fj ops (88% faster).
The major optimizations strategies:
- Optimize multiple data ops: `+++++` => `+5`, and `----++-` => `-3`.
- Optimize multiple pointer ops: `>>>>>` => `>5`, `<<<<>><` => `<3`.
- Find zeroing loops, and replace them with `*ptr = 0`. If there are data-ops before it, removes them too.
- `*ptr = 0` + `+5` => `*ptr = 5`.
## Tests:
I've gathered many brainfuck programs, and put them all inside the programs/ folder.
The tests compile each of them to flipjump:
```
>> pytest --compile-only
```
You can also run the compiled flipjump files (just omit the `--compile-only` flag), but I only added the input / expected-output files to only a portion of the tests, so it won't pass.
## Licenses:
The programs/ folder has a collection of 3rd party brainfuck programs, taken from multiple open-source websites. Each folder under programs/ has a README.md that specifies were the brainfuck files came from, and to whom we owe the credit.