A JIT compiling brainfuck interpreter written in rust. It's fast!
The interpreter is split into two crates using a workspace:
rbf
: The executable that let's you run brainfuck codelibrbf
: This is where all the interpreter code is implemented
The way this interpreter works is:
- Parse code into an intermediate representation
- Apply some optimizations
- Generate code using dynasm-rs
- Run the generated code
This project requires rust 1.45.0 or newer.
$ cargo install https://github.com/lukad/rbf.git
$ git clone https://github.com/lukad/rbf.git
$ cd rbf
$ cargo install
Add librbf
to your depedencies in the Cargo.toml
.
[dependencies]
librbf = { git = "https://github.com/lukad/rbf.git" }
Use it in your code.
use librbf::{parse, Jit};
fn main() {
let source = "++++++++[>++++++++<-]>.".as_bytes();
let program = parse(source);
let fun = Jit::new().compile(&program);
fun.run();
}
The following optimizations are implemented:
- Fusing of adjacent
+
and-
instructions:
+-++-+
becomesAdd(2)
- Fusing of adjacent
>
and<
instructions:
>><<<<>
becomesMove(-1)
- Detection of set loops:
[-]
becomesSet(0)
- Set instructions will be fused with following
Add
instructions:
[-]+++
becomesSet(3)
- Detection of multiplication loops:
[>++++<-]
becomesMul(1, 8), Set(0)
- Detection of scan looops:
[>>]
becomesScan(2)
- Elimination of code without effects