Added support for specifying a tape size limit

This commit is contained in:
tdark 2021-05-15 18:39:49 +02:00
parent 7b5ae34bfd
commit 1b195dc955

View file

@ -2,7 +2,8 @@
//! //!
//! If you just want to execute some simple brainfuck, check the [`interpret_with_io`] function. //! If you just want to execute some simple brainfuck, check the [`interpret_with_io`] function.
//! //!
//! To construct the interpreter, use the [`from_ascii`] or [`from_ascii_with_input_buffer`] methods. The latter grants access to //! To construct the interpreter, use the [`from_ascii`] or [`from_ascii_with_input_buffer`] methods
//! (or their variants that take a maximum tape size). The latter grants access to
//! the method [`add_input`], which allows for the addition of input while the interpreter is running. //! the method [`add_input`], which allows for the addition of input while the interpreter is running.
//! //!
//! [`from_ascii`]: Interpreter::from_ascii //! [`from_ascii`]: Interpreter::from_ascii
@ -24,6 +25,9 @@ use std::{
// NOTE(Able): This is the brain fuck interface // NOTE(Able): This is the brain fuck interface
/// The default limit for the tape size. This is the value used by methods that don't take it as a parameter
pub const DEFAULT_TAPE_SIZE_LIMIT: usize = 30_000;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
/// A brainfuck interpreter. Read the [module level documentation](self) for more /// A brainfuck interpreter. Read the [module level documentation](self) for more
pub struct Interpreter<'a, I> { pub struct Interpreter<'a, I> {
@ -31,17 +35,29 @@ pub struct Interpreter<'a, I> {
instr_ptr: usize, instr_ptr: usize,
tape: Vec<i8>, tape: Vec<i8>,
data_ptr: usize, data_ptr: usize,
tape_size_limit: usize,
input: I, input: I,
} }
impl<'a> Interpreter<'a, InputBuffer> { impl<'a> Interpreter<'a, InputBuffer> {
/// Construct an `Interpreter` from an ASCII string of code with an empty input buffer /// Construct an `Interpreter` from an ASCII string of code with an empty input buffer
/// This methods sets the tape size limit to [its default value](DEFAULT_TAPE_SIZE_LIMIT)
pub fn from_ascii_with_input_buffer(code: &'a [u8]) -> Self { pub fn from_ascii_with_input_buffer(code: &'a [u8]) -> Self {
Self::from_ascii_with_input_buffer_and_tape_limit(code, DEFAULT_TAPE_SIZE_LIMIT)
}
/// Construct an `Interpreter` from an ASCII string of code with an empty input buffer,
/// setting the tape size limit to the specified value
pub fn from_ascii_with_input_buffer_and_tape_limit(
code: &'a [u8],
tape_size_limit: usize,
) -> Self {
Self { Self {
code, code,
instr_ptr: 0, instr_ptr: 0,
tape: Vec::new(), tape: Vec::new(),
data_ptr: 0, data_ptr: 0,
tape_size_limit,
input: InputBuffer(VecDeque::new()), input: InputBuffer(VecDeque::new()),
} }
} }
@ -53,17 +69,23 @@ impl<'a> Interpreter<'a, InputBuffer> {
} }
impl<'a, I: BootlegRead> Interpreter<'a, I> { impl<'a, I: BootlegRead> Interpreter<'a, I> {
/// Construct an interpreter from an ASCII string of code and an source of input bytes /// Construct an interpreter from an ASCII string of code, a source of input bytes, and a tape size limit
pub fn from_ascii(code: &'a [u8], input: I) -> Self { pub fn from_ascii_with_tape_limit(code: &'a [u8], input: I, tape_size_limit: usize) -> Self {
Self { Self {
code, code,
instr_ptr: 0, instr_ptr: 0,
tape: Vec::new(), tape: Vec::new(),
data_ptr: 0, data_ptr: 0,
tape_size_limit,
input, input,
} }
} }
/// Constructs an interpreter from an ASCII string of code, a source of input bytes, and [the default tape size limit](DEFAULT_TAPE_SIZE_LIMIT)
pub fn from_ascii(code: &'a [u8], input: I) -> Self {
Self::from_ascii_with_tape_limit(code, input, DEFAULT_TAPE_SIZE_LIMIT)
}
/// Advance the interpreter by one instruction. /// Advance the interpreter by one instruction.
/// A return value of Ok(None) indicates succesful termination of the interpreter /// A return value of Ok(None) indicates succesful termination of the interpreter
pub fn advance(&mut self) -> Result<Option<Status>, ProgramError> { pub fn advance(&mut self) -> Result<Option<Status>, ProgramError> {
@ -83,12 +105,16 @@ impl<'a, I: BootlegRead> Interpreter<'a, I> {
} }
b'+' => { b'+' => {
let val = self.get_or_resize_tape_mut(); let val = self
.get_or_resize_tape_mut()
.ok_or(ProgramError::TapeSizeExceededLimit)?;
*val = val.checked_add(1).ok_or(ProgramError::IntegerOverflow)?; *val = val.checked_add(1).ok_or(ProgramError::IntegerOverflow)?;
} }
b'-' => { b'-' => {
let val = self.get_or_resize_tape_mut(); let val = self
.get_or_resize_tape_mut()
.ok_or(ProgramError::TapeSizeExceededLimit)?;
*val = val.checked_sub(1).ok_or(ProgramError::IntegerUnderflow)?; *val = val.checked_sub(1).ok_or(ProgramError::IntegerUnderflow)?;
} }
@ -98,7 +124,12 @@ impl<'a, I: BootlegRead> Interpreter<'a, I> {
} }
b',' => match self.input.bootleg_read() { b',' => match self.input.bootleg_read() {
Ok(Some(num)) => *self.get_or_resize_tape_mut() = num, Ok(Some(num)) => {
let cell = self
.get_or_resize_tape_mut()
.ok_or(ProgramError::TapeSizeExceededLimit)?;
*cell = num;
}
Ok(None) => return Ok(Some(Status::NeedsInput)), Ok(None) => return Ok(Some(Status::NeedsInput)),
Err(_) => return Err(ProgramError::InputReadError), Err(_) => return Err(ProgramError::InputReadError),
}, },
@ -157,11 +188,14 @@ impl<'a, I: BootlegRead> Interpreter<'a, I> {
Ok(()) Ok(())
} }
fn get_or_resize_tape_mut(&mut self) -> &mut i8 { fn get_or_resize_tape_mut(&mut self) -> Option<&mut i8> {
if self.data_ptr > self.tape_size_limit {
return None;
}
if self.data_ptr >= self.tape.len() { if self.data_ptr >= self.tape.len() {
self.tape.resize(self.data_ptr + 1, 0); self.tape.resize(self.data_ptr + 1, 0);
} }
&mut self.tape[self.data_ptr] Some(&mut self.tape[self.data_ptr])
} }
fn get_at_data_ptr(&self) -> i8 { fn get_at_data_ptr(&self) -> i8 {
@ -251,6 +285,7 @@ pub enum ProgramError {
InputReadError, InputReadError,
UnmatchedOpeningBracket, UnmatchedOpeningBracket,
UnmatchedClosingBracket, UnmatchedClosingBracket,
TapeSizeExceededLimit,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -308,6 +343,7 @@ mod tests {
instr_ptr: 0, instr_ptr: 0,
tape: vec![10, 5], tape: vec![10, 5],
data_ptr: 0, data_ptr: 0,
tape_size_limit: DEFAULT_TAPE_SIZE_LIMIT,
input: std::io::empty(), input: std::io::empty(),
}; };
@ -323,14 +359,10 @@ mod tests {
#[test] #[test]
fn hello_world() { fn hello_world() {
let mut interpreter = Interpreter { let mut interpreter = Interpreter::from_ascii(
//Source: https://en.wikipedia.org/wiki/Brainfuck b"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.",
code: b"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.", std::io::empty(),
instr_ptr: 0, );
tape: vec![],
data_ptr: 0,
input: std::io::empty(),
};
let mut string = Vec::new(); let mut string = Vec::new();
interpreter interpreter
@ -364,4 +396,17 @@ mod tests {
); );
assert_eq!(interpreter.advance_until_io(), Ok(None)); assert_eq!(interpreter.advance_until_io(), Ok(None));
} }
#[test]
fn hit_tape_size_limit() {
let mut interpreter =
Interpreter::from_ascii_with_tape_limit(b"+>+>+>+>+>", std::io::empty(), 1);
let result = interpreter.interpret_with_output(std::io::sink());
assert_eq!(
result,
Err(InterpretError::ProgramError(
ProgramError::TapeSizeExceededLimit
))
);
}
} }