Fix repl yoooooooooooooooo ❤

This commit is contained in:
Anas Elgarhy 2022-10-13 14:23:46 +02:00
parent 472496800b
commit b4ba99f52c
4 changed files with 48 additions and 27 deletions

View file

@ -217,7 +217,7 @@ mod tests {
#[test] #[test]
fn print_h_combine_repl() { fn print_h_combine_repl() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
assert_eq!( assert_eq!(
@ -230,7 +230,7 @@ mod tests {
#[test] #[test]
fn print_h_repl() { fn print_h_repl() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
assert_eq!(interpreter.run(String::from(">+++++++++")), Ok(0)); assert_eq!(interpreter.run(String::from(">+++++++++")), Ok(0));
@ -241,7 +241,7 @@ mod tests {
#[test] #[test]
fn nested_loop_level_1_combine() { fn nested_loop_level_1_combine() {
let mut interpreter = Interpreter::new(5, vec![], let mut interpreter = Interpreter::new(5, vec![],
&console::Term::stdout()); &console::Term::stdout());
assert_eq!(interpreter.run(String::from("++[>++[>+<-]<-]")), Ok(0)); assert_eq!(interpreter.run(String::from("++[>++[>+<-]<-]")), Ok(0));
@ -252,7 +252,7 @@ mod tests {
#[test] #[test]
fn execute_hello_world_from_file() { fn execute_hello_world_from_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -267,7 +267,7 @@ mod tests {
#[test] #[test]
fn execute_print_hi_from_file() { fn execute_print_hi_from_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -282,7 +282,7 @@ mod tests {
#[test] #[test]
fn execute_print_hi_yooo_from_file() { fn execute_print_hi_yooo_from_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -297,7 +297,7 @@ mod tests {
#[test] #[test]
fn execute_print_my_first_name_from_formatted_file() { fn execute_print_my_first_name_from_formatted_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -312,7 +312,7 @@ mod tests {
#[test] #[test]
fn execute_print_my_first_name_from_file() { fn execute_print_my_first_name_from_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -323,11 +323,19 @@ mod tests {
))), ))),
Ok(0) Ok(0)
); );
assert_eq!(interpreter.cells[0], Cell::default_cell(&vec![]));
assert_eq!(interpreter.cells[1], Cell::default_cell(&vec![]));
assert_eq!(interpreter.cells[2], Cell::new(115, &vec![]));
assert_eq!(interpreter.cells[3], Cell::new(96, &vec![]));
assert_eq!(interpreter.cells[4], Cell::new(112, &vec![]));
assert_eq!(interpreter.cells[5], Cell::new(32, &vec![]));
} }
#[test] #[test]
fn execute_print_my_first_name_and_last_name_from_formatted_file() { fn execute_print_my_first_name_and_last_name_from_formatted_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -342,7 +350,7 @@ mod tests {
#[test] #[test]
fn execute_print_my_first_name_and_last_name_from_file() { fn execute_print_my_first_name_and_last_name_from_file() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
println!(); println!();
@ -357,7 +365,7 @@ mod tests {
#[test] #[test]
fn reset() { fn reset() {
let mut interpreter = Interpreter::new(30000, vec![], let mut interpreter = Interpreter::new(30000, vec![],
&console::Term::stdout()); &console::Term::stdout());
assert_eq!(interpreter.run(String::from(">++++")), Ok(0)); assert_eq!(interpreter.run(String::from(">++++")), Ok(0));

View file

@ -111,25 +111,24 @@ impl Repl {
} }
pub fn process(&mut self, mut user_input: String) { pub fn process(&mut self, mut user_input: String) {
user_input.chars().for_each(|ch| {
if ch == '[' {
self.loop_depth += 1;
} else if ch == ']' {
self.loop_depth -= 1;
}
});
match user_input.find('[') { match user_input.find('[') {
Some(index) if self.loop_depth == 0 => { Some(index) if self.loop_depth != 0 && self.loop_body.is_empty() => {
self.loop_body.push_str(&user_input[index..]); self.loop_body.push_str(&user_input[index..]);
self.loop_depth = 1;
user_input = user_input[..index].to_string(); user_input = user_input[..index].to_string();
} }
Some(_) => { Some(_) if !self.loop_body.is_empty() => {
self.loop_body.push_str(&user_input); self.loop_body.push_str(&user_input);
user_input.matches('[').for_each(|_| self.loop_depth += 1);
user_input.matches(']').for_each(|_| self.loop_depth -= 1);
return; return;
} }
_ => { _ => {
if user_input.contains(']') { if user_input.contains(']') {
if self.loop_depth == 0 {
error!("Found ']' without matching '['");
return;
}
self.loop_depth -= 1;
if self.loop_depth == 0 { if self.loop_depth == 0 {
self.loop_body.push_str(&user_input); self.loop_body.push_str(&user_input);
user_input = self.loop_body.clone(); user_input = self.loop_body.clone();
@ -439,20 +438,34 @@ mod tests {
for line in code { for line in code {
repl.process(line); repl.process(line);
} }
assert_eq!(repl.interpreter.cells[0], Cell::default_cell(&vec![]));
assert_eq!(repl.interpreter.cells[1], Cell::default_cell(&vec![]));
assert_eq!(repl.interpreter.cells[2], Cell::new(115, &vec![]));
assert_eq!(repl.interpreter.cells[3], Cell::new(96, &vec![]));
assert_eq!(repl.interpreter.cells[4], Cell::new(112, &vec![]));
assert_eq!(repl.interpreter.cells[5], Cell::new(32, &vec![]));
} }
#[test] #[test]
fn print_my_first_name_in_one_command() { fn print_my_first_name_in_one_command() {
let mut term = console::Term::stdout(); let mut term = Term::stdout();
let interpreter = Interpreter::new(10, vec![], &mut term); let interpreter = Interpreter::new(10, vec![], &mut term);
let mut repl = Repl::new(interpreter, term); let mut repl = Repl::new(interpreter, term);
let code = "++++++++[>++++[>++<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.<<++++[>+++ let code = "++++++++[>++++[>++>+++>++++>+<<<<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.\
[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>." <<++++[>+++[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>."
.to_string(); .to_string();
repl.process(code); repl.process(code);
assert_eq!(repl.interpreter.cells[0], Cell::default_cell(&vec![]));
assert_eq!(repl.interpreter.cells[1], Cell::default_cell(&vec![]));
assert_eq!(repl.interpreter.cells[2], Cell::new(115, &vec![]));
assert_eq!(repl.interpreter.cells[3], Cell::new(96, &vec![]));
assert_eq!(repl.interpreter.cells[4], Cell::new(112, &vec![]));
assert_eq!(repl.interpreter.cells[5], Cell::new(32, &vec![]));
} }
#[test] #[test]

View file

@ -1,3 +1,3 @@
Print my first name (Anas) Print my first name (Anas)
Unformated: UnFormatted:
++++++++[>++++[>++<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.<<++++[>+++[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>. ++++++++[>++++[>++>+++>++++>+<<<<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.<<++++[>+++[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>.

View file

@ -1,5 +1,5 @@
My first name (Anas) My first name (Anas)
Formated: Formatted:
++++ ++++ 8 ++++ ++++ 8
[ [
>++++ >++++