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 of Byte values representing the memory of the machine. Each Byte in the vector is a cell in the memory tape.
  • program: A Program instance representing the Brainfuck program that the machine is executing.
  • memory_pointer: A usize value representing the current position of the memory pointer. The memory pointer points to a given cell in the memory tape.
  • program_counter: A usize that represents which instruction of the Program 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,

source

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());
source

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
source

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);
source

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);
source

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);
source

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
source

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);
source

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);

Auto Trait Implementations§

§

impl<R> RefUnwindSafe for VirtualMachine<R>
where R: RefUnwindSafe,

§

impl<R> Send for VirtualMachine<R>
where R: Send,

§

impl<R> Sync for VirtualMachine<R>
where R: Sync,

§

impl<R> Unpin for VirtualMachine<R>
where R: Unpin,

§

impl<R> UnwindSafe for VirtualMachine<R>
where R: UnwindSafe,

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, 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.