#brainfuck #interpreter #brain #mind #juice

mindjuice

A simple and easy-to-use brainfuck interpreter

2 releases

Uses old Rust 2015

0.1.1 Feb 5, 2018
0.1.0 Mar 25, 2015

#256 in Emulators

38 downloads per month

MIT/Apache

12KB
133 lines

mindjuice

Mindjuice is a simple and easy-to-use brainfuck interpreter for rust.

Documentation is available at the mindjuice docs.


lib.rs:

mindjuice

Mindjuice is a simple and easy-to-use brainfuck interpreter!

Usage

Mindjuice parses and runs brainfuck programs in two stages. First, it converts an input string into a Vec<Instruction>, then it can run that instruction vector to produce output.

You can pass anything which implements Iterator<char> to the parse function.

For example, parsing and executing static string:

extern crate mindjuice;

use std::io;

// parse_instructions will return Err() if there are any unmatched left or right brackets.
// Because we know this program doesn't have any unmatched brackets, using `.unwrap()` is fine.
let instructions = mindjuice::parse_instructions("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.".chars()).unwrap();

let mut buffer = Vec::new();

// Execute the instructions!
mindjuice::execute_brainfuck(instructions, // Instructions vec
                            &mut buffer, // io::Write to send output to
                            io::empty(), // io::Read to get input from
                            30000000u64 // Maximum program iterations to run before returning
                            ).unwrap();

assert_eq!(&buffer[..], b"Hello World!\n");

Note: Because the hello world program example doesn't use the , input command, we can use io::empty() as the input. However, if we provided io::empty() for a program which did use ,, execute_brainfuck() would loop indefinitely waiting for input.

No runtime deps