BrainfuckReader

Struct BrainfuckReader 

Source
pub struct BrainfuckReader { /* private fields */ }
Expand description

A simple Brainfuck interpreter.

The interpreter maintains:

  • the program codes as a String,
  • a customizable capacity memory tape initialized to zeros (30,000 cells by default),
  • a data pointer indexing into that tape.

Implementations§

Source§

impl BrainfuckReader

Source

pub fn new(code: String) -> Self

Create a new interpreter from Brainfuck code.

The memory tape is initialized to 30,000 zeroed cells.

Source

pub fn new_with_memory(code: String, memory_size: usize) -> Self

Create a new interpreter from Brainfuck code but with a custom memory size.

Source

pub fn set_output_sink<F>(&mut self, sink: F)
where F: Fn(&[u8]) + Send + Sync + 'static,

Provide an output sink. When set, ‘.’ sends bytes to this sink instead of stdout. The sink receives a slice of bytes; for Brainfuck, it will be a single-byte slice per ‘.’.

Source

pub fn set_input_provider<F>(&mut self, provider: F)
where F: Fn() -> Option<u8> + Send + Sync + 'static,

Provide an input provider. When set, ‘,’ reads from this provider instead of stdin. Returning None indicates EOF (cell is set to 0).

Source

pub fn set_tape_observer<F>(&mut self, window_size: usize, observer: F)
where F: Fn(usize, usize, &[u8]) + Send + Sync + 'static,

Provide a tape observer and desired window size.

Source

pub fn run(&mut self) -> Result<(), BrainfuckReaderError>

Execute the Brainfuck program until completion.

Returns Ok(()) on success or a BrainfuckReaderError on failure.

Source

pub fn run_debug(&mut self) -> Result<(), BrainfuckReaderError>

Debug-run the Brainfuck program, printing a step-by-step table of operations instead of producing I/O side effects. The interpreter state (pointer, memory) advances exactly as it would during a real run, but:

  • ‘.’ does not print the character; we log the action instead
  • ‘,’ does not read from stdin; we simulate EOF and set the cell to 0 and log
Source

pub fn run_with_control( &mut self, step_control: StepControl, ) -> Result<(), BrainfuckReaderError>

Execute with cooperative cancellation and optional step limit.

Source

pub fn run_debug_with_control( &mut self, step_control: StepControl, ) -> Result<(), BrainfuckReaderError>

Debug-run with cooperative cancellation and optional step limit.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

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

Source§

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.