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 --releaseRun any program
$ target/release/bfrs samples/mandelbrot.bfor simply
$ cargo run --release samples/mandelbrot.bfYou can also run the interpretor in interactive mode.
dumpcommand will dump the current memory state in ahdlike formatresetcommand 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 --releaseTo run benchmarks
$ cargo benchLicensed under GPLv3. Sample programs are largely written by other authors and are under other licenses.