Crate mindjuice [] [src]

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.

Enums

Error
ExecutionTerminationCondition
Instruction

Functions

execute_brainfuck
parse_instructions