Struct bfi::BfMachine [] [src]

pub struct BfMachine { /* fields omitted */ }

Brainfuck machine, consisting of tape, a Vec<u8>, and the pointer to a cell.

Methods

impl BfMachine
[src]

Returns a new machine.

Examples

use bfi::BfMachine;

let machine = BfMachine::new();
assert_eq!(machine.tape(), &vec![0]);

Returns a new machine with preallocated tape of some size. The tape is guaranteed to not reallocate if the number of used cells does not exceed capacity. If you know how many cells the program will use, this saves memory and computations.

Arguments

  • capacity - Number of preallocated cells

Examples

use bfi::BfMachine;

let capacity = 10;
let machine = BfMachine::with_capacity(capacity);
assert_eq!(machine.tape(), &vec![0; capacity]);

Returns a reference to the machine's tape.

Examples

use bfi::BfMachine;

let machine = BfMachine::new();
assert_eq!(machine.tape(), &vec![0]);

Returns a mutable reference to the machine's tape.

Examples

use bfi::BfMachine;

let machine = BfMachine::new();
assert_eq!(machine.tape(), &mut vec![0]);

Execute Brainfuck. If successful, return code.len(), else return a BfError, wrapped around the index of the byte that caused the error.

Arguments

  • code - ref to Vec<u8> to be interpreted, each u8 treated as a char
  • input - ref to Vec<u8> with input bytes
  • output - mutable ref to output string

Examples

Executing correct code:

use bfi::{BfMachine, b};

let code = b("++++[->,.<]");
let input = b("adversarial");
let mut output = Vec::new();

let mut machine = BfMachine::new();
let res = machine.exec(&code, &input, &mut output);

assert_eq!(output, b("adve"));
assert_eq!(res, Ok(code.len()));

Erroneous code:

use bfi::{BfMachine, BfError, b};

let code = b("+]");  // uh-oh, a mismatched `]`
let input = b("");
let mut output = Vec::new();

let mut machine = BfMachine::new();
assert_eq!(
    machine.exec(&code, &input, &mut output),
    Err(BfError::Syntax(1))  // syntax error at byte 1
);

You can also encounter a BfError::Input(_). This one occurs if , is found while all of the available input has been used.

use bfi::{BfMachine, BfError, b};

let input = b("spam");
let code = b(",,,, ,");  // this fifth comma leads to nothing good
let mut output = Vec::new();

let mut machine = BfMachine::new();
assert_eq!(
    machine.exec(&code, &input, &mut output),
    Err(BfError::Input(5))  // the input was not enough, error on byte 5
);

And there is also BfError::Memory(_) that works the very similar way. It is raised when the code tries to visit the cell no. -1, i.e. one to the left of the starting position.

Trait Implementations

impl Debug for BfMachine
[src]

Formats the value using the given formatter.

impl Eq for BfMachine
[src]

impl PartialEq for BfMachine
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Hash for BfMachine
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl Default for BfMachine
[src]

Returns the "default value" for a type. Read more