summary refs log tree commit diff
path: root/src/lib.rs
blob: b806b4cd952948d13d11605145d5eb9c6531d1d8 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![allow(incomplete_features)]
#![feature(async_fn_in_trait)]
#![feature(custom_inner_attributes)]
#![feature(lint_reasons)]
#![feature(never_type)]
#![feature(once_cell)]
#![feature(test)]
#![deny(clippy::complexity)]
#![deny(clippy::nursery)]
#![deny(clippy::pedantic)]
#![deny(clippy::perf)]
#![deny(clippy::suspicious)]
#![deny(clippy::alloc_instead_of_core)]
#![deny(clippy::as_underscore)]
#![deny(clippy::clone_on_ref_ptr)]
#![deny(clippy::create_dir)]
#![warn(clippy::dbg_macro)]
#![deny(clippy::default_numeric_fallback)]
#![deny(clippy::default_union_representation)]
#![deny(clippy::deref_by_slicing)]
#![deny(clippy::empty_structs_with_brackets)]
#![deny(clippy::exit)]
#![deny(clippy::expect_used)]
#![deny(clippy::filetype_is_file)]
#![deny(clippy::fn_to_numeric_cast)]
#![deny(clippy::format_push_string)]
#![deny(clippy::get_unwrap)]
#![deny(clippy::if_then_some_else_none)]
#![allow(
	clippy::implicit_return,
	reason = "returns should be done implicitly, not explicitly"
)]
#![deny(clippy::indexing_slicing)]
#![deny(clippy::large_include_file)]
#![deny(clippy::let_underscore_must_use)]
#![deny(clippy::lossy_float_literal)]
#![deny(clippy::map_err_ignore)]
#![deny(clippy::mem_forget)]
#![deny(clippy::missing_docs_in_private_items)]
#![deny(clippy::missing_trait_methods)]
#![deny(clippy::mod_module_files)]
#![deny(clippy::multiple_inherent_impl)]
#![deny(clippy::mutex_atomic)]
#![deny(clippy::needless_return)]
#![deny(clippy::non_ascii_literal)]
#![deny(clippy::panic_in_result_fn)]
#![deny(clippy::pattern_type_mismatch)]
#![deny(clippy::rc_buffer)]
#![deny(clippy::rc_mutex)]
#![deny(clippy::rest_pat_in_fully_bound_structs)]
#![deny(clippy::same_name_method)]
#![deny(clippy::separated_literal_suffix)]
#![deny(clippy::str_to_string)]
#![deny(clippy::string_add)]
#![deny(clippy::string_slice)]
#![deny(clippy::string_to_string)]
#![allow(
	clippy::tabs_in_doc_comments,
	reason = "tabs are preferred for this project"
)]
#![deny(clippy::try_err)]
#![deny(clippy::undocumented_unsafe_blocks)]
#![deny(clippy::unnecessary_self_imports)]
#![deny(clippy::unneeded_field_pattern)]
#![deny(clippy::unwrap_in_result)]
#![deny(clippy::unwrap_used)]
#![warn(clippy::use_debug)]
#![deny(clippy::verbose_file_reads)]
#![deny(clippy::wildcard_dependencies)]
#![deny(clippy::wildcard_enum_match_arm)]
#![deny(missing_copy_implementations)]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![deny(single_use_lifetimes)]
#![deny(unsafe_code)]
#![deny(unused)]

//! Brainfuck RS
//!
//! Implementation of a Brainfuck interpreter written in Rust.

#[cfg(test)]
extern crate test;

mod engine;
pub mod executor;
pub mod lexer;
pub mod parser;
#[cfg(feature = "utilities")]
pub mod utility;

pub use lexer::{lex, OperatorCode};
use miette::Diagnostic;
pub use parser::{parse, Instruction};
use thiserror::Error;

/// Top-level error type for Brainfuck interpreter.
#[derive(Debug, Diagnostic, Error)]
pub enum Error {
	/// Error occurred when reading input from a file.
	#[error(transparent)]
	Io(#[from] std::io::Error),

	/// An error that occurred while parsing Brainfuck code.
	#[diagnostic(transparent)]
	#[error(transparent)]
	Parser(#[from] parser::Error),

	/// An error that occurred during runtime.
	#[error(transparent)]
	Runtime(#[from] executor::Error),
}

#[cfg(test)]
mod tests {
	use test::Bencher;

	use super::*;

	#[test]
	fn hello_world() -> Result<(), Error> {
		let mut tape: Vec<u8> = vec![0; 1024];

		utility::execute_from_file::<executor::U8>("./test_programs/hello_world.bf", &mut tape)?;

		Ok(())
	}

	#[test]
	fn hello_world_u16() -> Result<(), Error> {
		let mut tape: Vec<u16> = vec![0; 1024];

		utility::execute_from_file::<executor::U16>("./test_programs/hello_world.bf", &mut tape)?;

		Ok(())
	}

	#[test]
	fn hello_world_from_hell() -> Result<(), Error> {
		let mut tape: Vec<u16> = vec![0; 1024];

		utility::execute_from_file::<executor::U16>(
			"./test_programs/hello_world_from_hell.bf",
			&mut tape,
		)?;

		Ok(())
	}

	#[bench]
	fn hello_world_from_hell_bench_u8(b: &mut Bencher) {
		b.iter(|| {
			let mut tape: Vec<u8> = vec![0; 1024];

			#[allow(clippy::expect_used)]
			utility::execute_from_file::<executor::U8>("./test_programs/hello_world.bf", &mut tape)
				.expect("failed to run");
		});
	}

	#[bench]
	fn hello_world_from_hell_bench_u16(b: &mut Bencher) {
		b.iter(|| {
			let mut tape: Vec<u16> = vec![0; 1024];

			#[allow(clippy::expect_used)]
			utility::execute_from_file::<executor::U16>(
				"./test_programs/hello_world.bf",
				&mut tape,
			)
			.expect("failed to run");
		});
	}

	#[test]
	fn hello_world_short() -> Result<(), Error> {
		let mut tape: Vec<u8> = vec![0; 1024];

		utility::execute_from_str::<executor::U8>(
			"--[+++++++<---->>-->+>+>+<<<<]<.>++++[-<++++>>->--<<]>>-.>--..>+.<<<.<<-.>>+>->>.\
			 +++[.<]",
			&mut tape,
		)?;

		Ok(())
	}
}