summary refs log tree commit diff
path: root/src/executor.rs
diff options
context:
space:
mode:
authorSophie Forrest <git@sophieforrest.com>2024-08-30 23:35:45 +1200
committerSophie Forrest <git@sophieforrest.com>2024-08-30 23:35:45 +1200
commit17b78f8cb127817b93f7e6ced7e55d8748806a80 (patch)
tree22bccb1307c078da4fc6e0873c8a8c2d081408ab /src/executor.rs
parent13b95941183666fadd090314e4e9af33283084cd (diff)
feat: bar engines behind features, optimise release and small profiles
Diffstat (limited to '')
-rw-r--r--src/executor.rs35
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
 ///