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]
fn new() -> Self
Returns a new machine.
Examples
use bfi::BfMachine; let machine = BfMachine::new(); assert_eq!(machine.tape(), &vec![0]);
fn with_capacity(capacity: usize) -> Self
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]);
fn tape(&self) -> &Vec<u8>
Returns a reference to the machine's tape.
Examples
use bfi::BfMachine; let machine = BfMachine::new(); assert_eq!(machine.tape(), &vec![0]);
fn tape_mut(&mut self) -> &mut Vec<u8>
Returns a mutable reference to the machine's tape.
Examples
use bfi::BfMachine; let machine = BfMachine::new(); assert_eq!(machine.tape(), &mut vec![0]);
fn exec(
&mut self,
code: &Vec<u8>,
input: &Vec<u8>,
output: &mut Vec<u8>
) -> Result<usize, BfError>
&mut self,
code: &Vec<u8>,
input: &Vec<u8>,
output: &mut Vec<u8>
) -> Result<usize, BfError>
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 toVec<u8>
to be interpreted, eachu8
treated as achar
input
- ref toVec<u8>
with input bytesoutput
- 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]
impl Eq for BfMachine
[src]
impl PartialEq for BfMachine
[src]
fn eq(&self, __arg_0: &BfMachine) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &BfMachine) -> bool
This method tests for !=
.
impl Hash for BfMachine
[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more