Struct brainfoamkit_lib::VirtualMachine
source · pub struct VirtualMachine<R>where
R: VMReader,{ /* private fields */ }
Expand description
VirtualMachine
is a struct representing a Virtual Machine capable of
interpreting a BrainFuck
program and tracking its state.
Fields
tape
: A vector ofByte
values representing the memory of the machine. EachByte
in the vector is a cell in the memory tape.program
: AProgram
instance representing the Brainfuck program that the machine is executing.memory_pointer
: Ausize
value representing the current position of the memory pointer. The memory pointer points to a given cell in the memory tape.program_counter
: Ausize
that represents which instruction of theProgram
is being executed right now.
Example
use brainfoamkit_lib::{
VMReader,
VirtualMachine,
};
let input_device = std::io::stdin();
let machine = VirtualMachine::builder().input_device(input_device).build();
Implementations§
source§impl<R> VirtualMachine<R>where
R: VMReader,
impl<R> VirtualMachine<R>where
R: VMReader,
sourcepub fn program(&self) -> Program
pub fn program(&self) -> Program
Return the Program
of the VirtualMachine
.
This method returns the Program
of the VirtualMachine
.
Returns
A Program
instance representing the Program
of the VirtualMachine
.
Example
use brainfoamkit_lib::{
Program,
VMReader,
VirtualMachine,
};
let input_device = std::io::stdin();
let machine = VirtualMachine::builder()
.input_device(input_device)
.build()
.unwrap();
assert_eq!(machine.program(), Program::default());
sourcepub const fn builder() -> VirtualMachineBuilder<R>
pub const fn builder() -> VirtualMachineBuilder<R>
Create a new instance of VirtualMachine
using VirtualMachineBuilder
.
This method provides a convenient way to create a new instance of
VirtualMachine
using VirtualMachineBuilder
. This method returns
a VirtualMachineBuilder
instance that can be used to configure the
VirtualMachine
before building it.
Returns
A VirtualMachineBuilder
instance that can be used to configure the
VirtualMachine
before building it.
Example
use brainfoamkit_lib::{
VMReader,
VirtualMachine,
};
let input_device = std::io::stdin();
let machine = VirtualMachine::builder().input_device(input_device).build();
See Also
sourcepub fn length(&self) -> usize
pub fn length(&self) -> usize
Returns the length of the tape
inside the VirtualMachine
.
This method returns the length of the tape
vector of the
VirtualMachine
.
Returns
A usize
value representing the length of the VirtualMachine
.
Example
use brainfoamkit_lib::{
VMReader,
VirtualMachine,
};
let input_device = std::io::stdin();
let machine = VirtualMachine::builder()
.input_device(input_device)
.tape_size(10)
.build()
.unwrap();
assert_eq!(machine.length(), 10);
sourcepub const fn memory_pointer(&self) -> usize
pub const fn memory_pointer(&self) -> usize
Returns the current position of the memory pointer.
This method returns the current position of the memory pointer in the
VirtualMachine
.
Returns
A usize
value representing the current position of the memory pointer.
Example
use brainfoamkit_lib::{
VMReader,
VirtualMachine,
};
let input_device = std::io::stdin();
let machine = VirtualMachine::builder()
.input_device(input_device)
.build()
.unwrap();
assert_eq!(machine.memory_pointer(), 0);
sourcepub const fn program_counter(&self) -> usize
pub const fn program_counter(&self) -> usize
Returns the current position of the program counter.
This method returns the current position of the program counter in the
VirtualMachine
.
Returns
A usize
value representing the current position of the program
counter.
Example
use brainfoamkit_lib::{
VMReader,
VirtualMachine,
};
let input_device = std::io::stdin();
let machine = VirtualMachine::builder()
.input_device(input_device)
.build()
.unwrap();
assert_eq!(machine.program_counter(), 0);
sourcepub fn input_device(&mut self) -> &mut R
pub fn input_device(&mut self) -> &mut R
returns the current input device of the VirtualMachine
.
This method returns the current input device of the VirtualMachine
.
This allows for testing and type checking of the input device.
Returns
A reference to the current input device of the
VirtualMachine
.
Example
use brainfoamkit_lib::{
MockReader,
VMReader,
VirtualMachine,
};
let input_device = MockReader {
data: std::io::Cursor::new("A".as_bytes().to_vec()),
};
let mut machine = VirtualMachine::builder()
.input_device(input_device)
.build()
.unwrap();
assert_eq!(machine.input_device().read().unwrap(), 65);
See Also
sourcepub fn get_instruction(&self) -> Option<Instruction>
pub fn get_instruction(&self) -> Option<Instruction>
Returns the current instruction of the VirtualMachine
.
This method returns the instruction at the current position of the
program counter in the program. If the program counter is out of
bounds of the program, this method returns None
.
Returns
An Option
that contains the current instruction if the program counter
is within the bounds of the program, or None
if the program
counter is out of bounds.
Example
use brainfoamkit_lib::{
Instruction,
Program,
VMReader,
VirtualMachine,
};
let program = Program::from(vec![
Instruction::IncrementPointer,
Instruction::IncrementValue,
]);
let input_device = std::io::stdin();
let mut machine = VirtualMachine::builder()
.input_device(input_device)
.program(program)
.build()
.unwrap();
assert_eq!(
machine.get_instruction(),
Some(Instruction::IncrementPointer)
);
machine.execute_instruction();
assert_eq!(machine.get_instruction(), Some(Instruction::IncrementValue));
machine.execute_instruction();
assert_eq!(machine.get_instruction(), None);
sourcepub fn execute_instruction(&mut self)
pub fn execute_instruction(&mut self)
Executes the current instruction of the VirtualMachine
.
This method executes the instruction at the current position of the memory pointer in the program. If the memory pointer is out of bounds of the program, this method does nothing.
Example
use brainfoamkit_lib::{
Instruction,
Program,
VMReader,
VirtualMachine,
};
let program = Program::from(vec![
Instruction::IncrementPointer,
Instruction::IncrementValue,
]);
let input_device = std::io::stdin();
let mut machine = VirtualMachine::builder()
.input_device(input_device)
.program(program)
.build()
.unwrap();
assert_eq!(machine.memory_pointer(), 0);
machine.execute_instruction();
assert_eq!(machine.memory_pointer(), 1);
machine.execute_instruction();
assert_eq!(machine.memory_pointer(), 1);