diff options
| author | Sophie Forrest <git@sophieforrest.com> | 2024-08-30 23:35:45 +1200 |
|---|---|---|
| committer | Sophie Forrest <git@sophieforrest.com> | 2024-08-30 23:35:45 +1200 |
| commit | f5f789540ad7d3f7f4f855c9db69d65cfc190ee0 (patch) | |
| tree | f532988e9a35a0d2c58efbad9daf6e66288f4a1f /src/utility.rs | |
| parent | c9ab8d38765c7c80f2ea9083ce8d326f407110ac (diff) | |
feat(engine): allow choosing engine per executor call
Diffstat (limited to '')
| -rw-r--r-- | src/utility.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/utility.rs b/src/utility.rs index 58ac3e2..4eba853 100644 --- a/src/utility.rs +++ b/src/utility.rs @@ -2,7 +2,7 @@ use std::path::Path; -use crate::{constants::TapeInner, lex, parse, Error, Executor}; +use crate::{engine::Engine, executor::execute, lex, parse, Error}; /// Utility function to execute a Brainfuck file. Lexes, parses and executes the /// input file. @@ -12,7 +12,10 @@ use crate::{constants::TapeInner, lex, parse, Error, Executor}; /// This function will return an error if reading the input file, parsing or /// execution fails. See documentation for [`crate::parser::parse`] and /// [`crate::executor::execute`]. -pub fn execute_from_file(path: impl AsRef<Path>, tape: &mut [TapeInner]) -> Result<(), Error> { +pub fn execute_from_file<E: Engine>( + path: impl AsRef<Path>, + tape: &mut [E::TapeInner], +) -> Result<(), Error> { let input = fs_err::read_to_string(path.as_ref())?; let operator_codes = lex(&input); @@ -21,7 +24,7 @@ pub fn execute_from_file(path: impl AsRef<Path>, tape: &mut [TapeInner]) -> Resu let mut data_pointer = 0; - Executor::execute(&instructions, tape, &mut data_pointer)?; + execute::<E>(&instructions, tape, &mut data_pointer)?; Ok(()) } @@ -34,14 +37,17 @@ pub fn execute_from_file(path: impl AsRef<Path>, tape: &mut [TapeInner]) -> Resu /// This function will return an error if parsing or /// execution fails. See documentation for [`crate::parser::parse`] and /// [`crate::executor::execute`]. -pub fn execute_from_str(input: &str, tape: &mut [TapeInner]) -> Result<(), Error> { +pub fn execute_from_str<E: Engine<TapeInner = u8>>( + input: &str, + tape: &mut [E::TapeInner], +) -> Result<(), Error> { let operator_codes = lex(input); let instructions = parse(input, &operator_codes)?; let mut data_pointer = 0; - Executor::execute(&instructions, tape, &mut data_pointer)?; + execute::<E>(&instructions, tape, &mut data_pointer)?; Ok(()) } |