A simple Brainfuck interpreter written in Rust.
Although, Brainfuck is a fairly useless language, I wanted to test myself in making an easy-to-use interpreter.
It doesn't use recursion internally, it uses a stack, which entirely avoids stack overflow errors.
It doesn't use unsafe
keyword. This pretty much means that it is unlikely to cause undefined behaviour.
You can use brainfuck-rs both as a library and a standalone executable.
You can specify the input and output buffers using BufReader
and BufWriter
respectively, but you can use anything that implements Read
and Write
traits.
Use brainfuck-rs -h
to view all the options that can be used.
First-class support for scripting, allowing you to pipe input into a Brainfuck program.
Here's an example:
echo 'Hello, World!' | brainfuck-rs rot13.b
# or
brainfuck-rs rot13.b <<< 'Hello, World!'
You can find plenty of Brainfuck programs to experiment with inside examples/brainfuck-programs.
This implementation does not introduce any optimizations, which means that it simply executes instructions character by character, but it's fast enough for most use cases (if you find one). For instance, mandelbrot.b finishes in 1 minute 48 seconds on Pentium dual-core (Pentium E5200 (2) @ 2.500GHz
).
I didn't want to overcomplicate the implementation, so I tried to keep things as simple as possible.
This implementation of Brainfuck tries to comply with the spec that can be found here.