summary refs log tree commit diff
path: root/src/parser.rs
blob: cffa6db842629d0ac056ab352b1a51f38a50ce0b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Parser implementation for Brainfuck. Parses operator codes into instruction
//! sets.

use miette::{Diagnostic, SourceSpan};
use thiserror::Error;

use crate::lexer::OperatorCode;

/// Parsed instructions for Brainfuck.
#[derive(Clone, Debug)]
pub enum Instruction {
	/// `>`
	///
	/// Increment the data pointer by one (to point to the next cell to the
	/// right).
	IncrementPointer,

	/// `<`
	///
	/// Decrement the data pointer by one (to point to the next cell to the
	/// left).
	DecrementPointer,

	/// `+`
	///
	/// Increment the byte at the data pointer by one.
	IncrementByte,

	/// `-`
	///
	/// Decrement the byte at the data pointer by one.
	DecrementByte,

	/// `.`
	///
	/// Output the byte at the data pointer.
	OutputByte,

	/// `,`
	///
	/// Accept one byte of input, storing its value in the byte at the data
	/// pointer.
	InputByte,

	/// `[]`
	///
	/// Loops through the inner instructions.
	Loop(Vec<Instruction>),
}

/// Error type for errors that occur during parsing from [`OperatorCode`]s to
/// [`Instruction`]s.
#[derive(Debug, Diagnostic, Error)]
pub enum Error {
	/// Parser encountered a loop with no beginning.
	#[diagnostic(help("try closing the loop"))]
	#[error("loop closed at {loop_src:?} has no beginning")]
	LoopWithNoBeginning {
		/// Source code associated with diagnostic
		#[source_code]
		input: String,

		/// SourceSpan of the loop bracket.
		#[label("loop ending")]
		loop_src: SourceSpan,
	},

	/// Parser encountered a loop with no ending.
	#[diagnostic(help("try closing the loop"))]
	#[error("loop beginning at {loop_src:?} has no ending")]
	LoopWithNoEnding {
		/// Source code associated with diagnostic
		#[source_code]
		input: String,

		/// SourceSpan of the loop bracket.
		#[label("loop beginning")]
		loop_src: SourceSpan,
	},

	/// Parser sliced out of bounds.
	#[error("parser sliced out of bounds")]
	SliceOutOfBounds(std::ops::Range<usize>),
}

/// Parses the operator codes into instruction codes.
///
/// # Parameters
///
/// * `src` - The source the operator codes originate from. This is used for
///   error reporting.
/// * `operator_codes` - The operator codes reveiced from the lexer.
///
/// # Errors
///
/// This function will return an error if a loop is encountered with no
/// beginning, a loop is encountered with no ending, or if the parser attempts
/// to slice out of bounds.
pub fn parse(src: &str, operator_codes: &[OperatorCode]) -> Result<Vec<Instruction>, Error> {
	let mut program: Vec<Instruction> = Vec::new();
	let mut loop_stack: i32 = 0;
	let mut loop_start = 0;
	let mut loop_source_offset: usize = 0;

	operator_codes
		.iter()
		.enumerate()
		.try_for_each(|(i, operator_code)| -> Result<(), Error> {
			match (loop_stack, *operator_code) {
				(0i32, OperatorCode::StartLoop { offset }) => {
					loop_start = i;
					loop_source_offset = offset;
					loop_stack += 1i32;
				}
				(0i32, _) => {
					if let Some(instruction) = match *operator_code {
						OperatorCode::IncrementPointer => Some(Instruction::IncrementPointer),
						OperatorCode::DecrementPointer => Some(Instruction::DecrementPointer),
						OperatorCode::IncrementByte => Some(Instruction::IncrementByte),
						OperatorCode::DecrementByte => Some(Instruction::DecrementByte),
						OperatorCode::OutputByte => Some(Instruction::OutputByte),
						OperatorCode::InputByte => Some(Instruction::InputByte),
						OperatorCode::EndLoop { offset } => {
							return Err(Error::LoopWithNoBeginning {
								input: src.to_owned(),
								loop_src: (offset, 1).into(),
							})
						}
						// We don't care about this variant as it is handled in a subsequent arm
						OperatorCode::StartLoop { .. } => None,
					} {
						program.push(instruction);
					}
				}
				(_, OperatorCode::StartLoop { .. }) => loop_stack += 1i32,
				(_, OperatorCode::EndLoop { .. }) => {
					loop_stack -= 1i32;
					if loop_stack == 0i32 {
						let loop_program = parse(
							src,
							match operator_codes.get(loop_start + 1..i) {
								Some(value) => value,
								None => return Err(Error::SliceOutOfBounds(loop_start + 1..i)),
							},
						)?;

						program.push(Instruction::Loop(loop_program));
					}
				}
				_ => (),
			};

			Ok(())
		})?;

	if loop_stack == 0i32 {
		Ok(program)
	} else {
		Err(Error::LoopWithNoEnding {
			input: src.to_owned(),
			loop_src: (loop_source_offset, 1).into(),
		})
	}
}