Struct brainfoamkit_lib::Program
source · 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
impl Program
sourcepub fn get_instruction(&self, index: usize) -> Option<Instruction>
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
sourcepub fn find_matching_bracket(&self, index: usize) -> Option<usize>
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
length()
: Get the length of the programget_instruction()
: Get an instruction from aProgram
Trait Implementations§
source§impl From<&str> for Program
impl From<&str> for Program
source§fn from(program: &str) -> Self
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 newProgram
from a series of instructions
source§impl From<Vec<Instruction>> for Program
impl From<Vec<Instruction>> for Program
source§fn from(instructions: Vec<Instruction>) -> Self
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 ofInstruction
s to load into theProgram
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 aProgram
from a string