Improve the repl 🥰

This commit is contained in:
Anas Elgarhy 2022-10-13 19:58:02 +02:00
parent 96175f78bf
commit 0581d0a2e1
1 changed files with 6 additions and 0 deletions

View File

@ -102,11 +102,14 @@ impl Repl {
} }
pub fn process(&mut self, mut user_input: String) { pub fn process(&mut self, mut user_input: String) {
let mut print = false;
user_input.chars().for_each(|ch| { user_input.chars().for_each(|ch| {
if ch == '[' { if ch == '[' {
self.loop_depth += 1; self.loop_depth += 1;
} else if ch == ']' { } else if ch == ']' {
self.loop_depth -= 1; self.loop_depth -= 1;
} else if ch == '.' {
print = true;
} }
}); });
match user_input.find('[') { match user_input.find('[') {
@ -143,6 +146,9 @@ impl Repl {
match self.interpreter.run(user_input) { match self.interpreter.run(user_input) {
Ok(_) => { Ok(_) => {
info!("Successfully ran brainfuck source code from REPL"); info!("Successfully ran brainfuck source code from REPL");
if print {
println!(); // Print newline after output, if user requested print any output
}
} }
Err(e) => { Err(e) => {
error!("Failed to run brainfuck source code from REPL: {}", e); error!("Failed to run brainfuck source code from REPL: {}", e);