Optimizing Brainf*** interpreter written in Rust
- Unlimited number of memory cells are available
- Memory cell increments and decrements wrap around
You need Rust and Cargo installed to build this. Clone this repository and build.
$ cargo build --release
Run any program
$ target/release/bfrs samples/mandelbrot.bf
or simply
$ cargo run --release samples/mandelbrot.bf
You can also run the interpretor in interactive mode.
dump
command will dump the current memory state in ahd
like formatreset
command will reset the internal memory state
$ cargo run --release
>>> dump
-----------
Memory Dump
-----------
00000000 00 |. |
00000010
>>> [-][Following will print capital 'A' followed by a new line character]
>>> ++++++++[>++++++++<-]>+.>++++++++++.
A
>>> dump
-----------
Memory Dump
-----------
00000000 00 41 0a |.A. |
00000010
>>> [-][use 'reset' command to reset memory state]
>>> reset
>>> dump
-----------
Memory Dump
-----------
00000000 00 |. |
00000010
>>>
The interpretor makes some optimizations inspired by the excellent bfc project.
-
Combine successive additions and subtractions i.e.
+++-
is done in a single step asincrement 2
-
Combine successive memory cell pointer increment and decrement i.e.
>>><
is done in a single step asshift right twice
-
The biggest performance gain over the naive implementation was caching the loop start and end index so that input file is scanned only once.
Some fairly comprehensive tests are availabel in tests/
.
$ cargo test --release
To run benchmarks
$ cargo bench
Licensed under GPLv3. Sample programs are largely written by other authors and are under other licenses.