summary refs log tree commit diff
path: root/src/executor.rs
blob: 24b2f470a2fcf827ffaa659a31742e6ba203be45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Executor implementation for Brainfuck.

use miette::Diagnostic;
use thiserror::Error;

use crate::{constants::TapeInner, engine::Engine, parser::Instruction};

/// Runtime errors that can occur in brainfuck executor.
#[derive(Debug, Diagnostic, Error)]
pub enum Error {
	/// Brainfuck code performed an out of bounds index on the tape during
	/// runtime.
	#[error("tape indexed out of bounds, attempted index at `{0}`")]
	IndexOutOfBounds(usize),

	/// Executor was unable to read an input byte from stdin.
	#[error("could not read input from stdin")]
	ReadInput(#[from] std::io::Error),
}

/// Struct for executor implementation, allows Engine to be implemented.
#[derive(Clone, Copy, Debug)]
pub struct Executor;

impl Executor {
	/// 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.
	pub fn execute(
		instructions: &[Instruction],
		tape: &mut [TapeInner],
		data_pointer: &mut usize,
	) -> Result<(), Error> {
		for instruction in instructions {
			match *instruction {
				Instruction::IncrementPointer => {
					let tape_len: usize = tape.len() - 1;

					if *data_pointer == tape_len {
						*data_pointer = 0;
					} else {
						*data_pointer += 1;
					}
				}
				Instruction::DecrementPointer => {
					*data_pointer = match *data_pointer {
						0 => tape.len() - 1,
						_ => *data_pointer - 1,
					};
				}
				Instruction::IncrementByte => match tape.get_mut(*data_pointer) {
					Some(value) => *value = value.overflowing_add(1).0,
					None => return Err(Error::IndexOutOfBounds(*data_pointer)),
				},
				Instruction::DecrementByte => match tape.get_mut(*data_pointer) {
					Some(value) => *value = value.overflowing_sub(1).0,
					None => return Err(Error::IndexOutOfBounds(*data_pointer)),
				},
				Instruction::OutputByte => {
					Self::write_byte(match tape.get(*data_pointer) {
						Some(value) => *value,
						None => return Err(Error::IndexOutOfBounds(*data_pointer)),
					})?;
				}
				Instruction::InputByte => {
					let input = Self::read_byte()?;

					match tape.get_mut(*data_pointer) {
						Some(value) => *value = input,
						None => return Err(Error::IndexOutOfBounds(*data_pointer)),
					};
				}
				Instruction::Loop(ref instructions) => {
					while match tape.get(*data_pointer) {
						Some(value) => *value,
						None => return Err(Error::IndexOutOfBounds(*data_pointer)),
					} != 0
					{
						Self::execute(instructions, tape, data_pointer)?;
					}
				}
			}
		}

		Ok(())
	}
}