diff options
Diffstat (limited to '')
| -rw-r--r-- | src/executor.rs | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/executor.rs b/src/executor.rs index da7d676..c5fff93 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -25,13 +25,46 @@ pub struct U8; /// Struct for executor implementation, allows u16 Engine to be implemented. #[derive(Clone, Copy, Debug)] +#[cfg(feature = "engine-u16")] pub struct U16; /// Struct for executor implementation, allows u32 Engine to be implemented. #[derive(Clone, Copy, Debug)] +#[cfg(feature = "engine-u32")] pub struct U32; -/// Executes the provided instruction set, utilising the provided tape.. +/// Trait that must be implemented by all executors. +pub trait Executor<E: Engine> { + /// Executes the provided instruction set, utilising the provided tape. + /// + /// # Errors + /// + /// This function will return an error if the Brainfuck code indexes out of + /// bounds of the tape, or if the executor cannot read an input byte from + /// stdin. + fn execute( + instructions: &[Instruction], + tape: &mut [E::TapeInner], + data_pointer: &mut usize, + ) -> Result<(), Error>; +} + +impl<E> Executor<E> for E +where + E: Engine, +{ + #[inline] + fn execute( + instructions: &[Instruction], + tape: &mut [<E as Engine>::TapeInner], + data_pointer: &mut usize, + ) -> Result<(), Error> { + execute::<E>(instructions, tape, data_pointer) + } +} + +/// Executes the provided instruction set, utilising the provided tape. This +/// function allows specifying the Brainfuck engine implementation per call. /// /// # Errors /// |