pub struct Program { /* private fields */ }
Expand description

Structure to hold the program.

A Program is a series if instructions stored in the program stack. This struct allows us to conveniently read the program, modify it and save it back.

Examples

Loading a Program from a series of instructions

use brainfoamkit_lib::{
    Instruction,
    Program,
};

let instructions = vec![
    Instruction::IncrementPointer,
    Instruction::IncrementValue,
    Instruction::DecrementPointer,
    Instruction::DecrementValue,
];
let mut program = Program::from(instructions);

assert_eq!(program.length(), Some(4));

Load a Program from a string

// TODO: Verify this example
use brainfoamkit_lib::Program;

let program_string = ">>++<<--";
let program = Program::from(program_string);

assert_eq!(program.length(), Some(8));

Get an instruction from a Program

// TODO: Verify this example
use brainfoamkit_lib::{
    Instruction,
    Program,
};

let program_string = ">+<-";

let mut program = Program::from(program_string);

assert_eq!(
    program.get_instruction(0),
    Some(Instruction::IncrementPointer)
);
assert_eq!(
    program.get_instruction(1),
    Some(Instruction::IncrementValue)
);
assert_eq!(
    program.get_instruction(2),
    Some(Instruction::DecrementPointer)
);
assert_eq!(
    program.get_instruction(3),
    Some(Instruction::DecrementValue)
);
assert_eq!(program.get_instruction(4), None);

Implementations§

source§

impl Program

source

pub fn get_instruction(&self, index: usize) -> Option<Instruction>

Get an instruction from a Program at a specific index

This method gets an instruction from the program at a specific index.

Arguments
  • index - The index of the instruction to get
Examples
use brainfoamkit_lib::{
    Instruction,
    Program,
};

let instructions = ">>++<<--";
let program = Program::from(instructions);

assert_eq!(
    program.get_instruction(0),
    Some(Instruction::IncrementPointer)
);
assert_eq!(
    program.get_instruction(1),
    Some(Instruction::IncrementPointer)
);
assert_eq!(
    program.get_instruction(2),
    Some(Instruction::IncrementValue)
);
assert_eq!(
    program.get_instruction(3),
    Some(Instruction::IncrementValue)
);
assert_eq!(
    program.get_instruction(4),
    Some(Instruction::DecrementPointer)
);
assert_eq!(
    program.get_instruction(5),
    Some(Instruction::DecrementPointer)
);
assert_eq!(
    program.get_instruction(6),
    Some(Instruction::DecrementValue)
);
assert_eq!(
    program.get_instruction(7),
    Some(Instruction::DecrementValue)
);
assert_eq!(program.get_instruction(8), None);
Returns

The Instruction at the given index

See Also
  • length(): Get the length of the program
source

pub fn find_matching_bracket(&self, index: usize) -> Option<usize>

Find the matching JumpBackward instruction for the given JumpForward instruction

This method allows us to identify the boundaries of a given loop. It will return the index of the matching JumpBackward instruction for the given JumpForward instruction. It returns None if no matching JumpBackward instruction is found or the instruction at the given index is not a JumpForward instruction.

Examples
use brainfoamkit_lib::{
    Instruction,
    Program,
};

let instructions = "[[]]";
let mut program = Program::from(instructions);

assert_eq!(program.find_matching_bracket(0), Some(3));
assert_eq!(program.find_matching_bracket(1), Some(2));
Returns

The index of the matching bracket

See Also
source

pub fn length(&self) -> Option<usize>

Get the length of the program

This method returns the length of the program.

Examples
use brainfoamkit_lib::Program;

let program_string = ">>++<<--";
let program = Program::from(program_string);

assert_eq!(program.length(), Some(8));
Returns

The length of the program

Trait Implementations§

source§

impl Clone for Program

source§

fn clone(&self) -> Program

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Program

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Program

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Display for Program

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&str> for Program

source§

fn from(program: &str) -> Self

Load a Program from a string

This method loads a Program from a string.

Arguments
  • program - A string containing the program to load
Examples
use brainfoamkit_lib::Program;

let program_string = ">>++<<--";
let program = Program::from(program_string);

assert_eq!(program.length(), Some(8));
See Also
  • from(): Create a new Program from a series of instructions
source§

impl From<Vec<Instruction>> for Program

source§

fn from(instructions: Vec<Instruction>) -> Self

Create a new Program from a series of instructions

This method creates a new Program from a series of instructions.

Arguments
  • instructions - A vector of Instructions to load into the Program
Examples
use brainfoamkit_lib::{
    Instruction,
    Program,
};

let instructions = vec![
    Instruction::IncrementPointer,
    Instruction::IncrementValue,
    Instruction::DecrementPointer,
    Instruction::DecrementValue,
];
let program: Program = Program::from(instructions);

assert_eq!(program.length(), Some(4));
See Also
  • from(): Load a Program from a string
source§

impl Index<usize> for Program

§

type Output = Instruction

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl PartialEq for Program

source§

fn eq(&self, other: &Program) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Program

source§

impl StructuralEq for Program

source§

impl StructuralPartialEq for Program

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.