Improve the code 💙

This commit is contained in:
Anas Elgarhy 2022-10-08 01:11:22 +02:00
parent 6c3f121111
commit 0eaf38ead0
3 changed files with 6 additions and 6 deletions

View File

@ -51,7 +51,7 @@ impl Interpreter {
Ok(()) Ok(())
} }
fn run_brainfuck_code(&mut self, bf_code: &str) -> Result<(), error::InterpreterError> { fn run_brainfuck_code(&mut self, bf_code: &str) -> Result<(), InterpreterError> {
for (i, ch) in bf_code.chars().enumerate() { for (i, ch) in bf_code.chars().enumerate() {
match BfCommand::from_char(ch, i) { match BfCommand::from_char(ch, i) {
Some(cmd) => { Some(cmd) => {
@ -66,7 +66,7 @@ impl Interpreter {
Ok(()) Ok(())
} }
fn execute(&mut self, cmd: BfCommand) -> Result<(), error::InterpreterError> { fn execute(&mut self, cmd: BfCommand) -> Result<(), InterpreterError> {
match cmd { match cmd {
BfCommand::IncPtr => { BfCommand::IncPtr => {
self.pointer += 1; self.pointer += 1;
@ -74,7 +74,7 @@ impl Interpreter {
if self.features.contains(&arguments::Feature::ReversePointer) { if self.features.contains(&arguments::Feature::ReversePointer) {
self.pointer = 0; self.pointer = 0;
} else { } else {
return Err(error::InterpreterError::new( return Err(InterpreterError::new(
format!("Pointer out of bounds {}", self.pointer), format!("Pointer out of bounds {}", self.pointer),
11, 11,
)); ));
@ -86,7 +86,7 @@ impl Interpreter {
if self.features.contains(&arguments::Feature::ReversePointer) { if self.features.contains(&arguments::Feature::ReversePointer) {
self.pointer = self.array_size - 1; self.pointer = self.array_size - 1;
} else { } else {
return Err(error::InterpreterError::new( return Err(InterpreterError::new(
format!("Pointer out of bounds {}", self.pointer), format!("Pointer out of bounds {}", self.pointer),
11, 11,
)); ));
@ -109,7 +109,7 @@ impl Interpreter {
self.cells[self.pointer] = match std::io::stdin().bytes().next() { self.cells[self.pointer] = match std::io::stdin().bytes().next() {
Some(Ok(byte)) => byte, Some(Ok(byte)) => byte,
Some(Err(e)) => { Some(Err(e)) => {
return Err(error::InterpreterError::new( return Err(InterpreterError::new(
format!("Failed to read byte from stdin: {}", e), format!("Failed to read byte from stdin: {}", e),
12, 12,
)); ));
@ -135,7 +135,7 @@ impl Interpreter {
} }
} }
_ => { _ => {
return Err(error::InterpreterError::new( return Err(InterpreterError::new(
format!("Unmatched closing bracket at position {}", i), format!("Unmatched closing bracket at position {}", i),
14, 14,
)); ));