diff --git a/src/bf_interpreter/interpreter.rs b/src/bf_interpreter/interpreter.rs index 842395a..eb7a649 100644 --- a/src/bf_interpreter/interpreter.rs +++ b/src/bf_interpreter/interpreter.rs @@ -217,7 +217,7 @@ mod tests { #[test] fn print_h_combine_repl() { - let mut interpreter = Interpreter::new(30000, vec![], + let mut interpreter = Interpreter::new(30000, vec![], &console::Term::stdout()); assert_eq!( @@ -230,7 +230,7 @@ mod tests { #[test] fn print_h_repl() { - let mut interpreter = Interpreter::new(30000, vec![], + let mut interpreter = Interpreter::new(30000, vec![], &console::Term::stdout()); assert_eq!(interpreter.run(String::from(">+++++++++")), Ok(0)); @@ -241,7 +241,7 @@ mod tests { #[test] fn nested_loop_level_1_combine() { - let mut interpreter = Interpreter::new(5, vec![], + let mut interpreter = Interpreter::new(5, vec![], &console::Term::stdout()); assert_eq!(interpreter.run(String::from("++[>++[>+<-]<-]")), Ok(0)); @@ -252,7 +252,7 @@ mod tests { #[test] fn execute_hello_world_from_file() { - let mut interpreter = Interpreter::new(30000, vec![], + let mut interpreter = Interpreter::new(30000, vec![], &console::Term::stdout()); println!(); @@ -267,7 +267,7 @@ mod tests { #[test] fn execute_print_hi_from_file() { - let mut interpreter = Interpreter::new(30000, vec![], + let mut interpreter = Interpreter::new(30000, vec![], &console::Term::stdout()); println!(); @@ -282,7 +282,7 @@ mod tests { #[test] fn execute_print_hi_yooo_from_file() { - let mut interpreter = Interpreter::new(30000, vec![], + let mut interpreter = Interpreter::new(30000, vec![], &console::Term::stdout()); println!(); @@ -297,7 +297,7 @@ mod tests { #[test] 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()); println!(); @@ -312,7 +312,7 @@ mod tests { #[test] 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()); println!(); @@ -323,11 +323,19 @@ mod tests { ))), 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] 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()); println!(); @@ -342,7 +350,7 @@ mod tests { #[test] 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()); println!(); @@ -357,7 +365,7 @@ mod tests { #[test] fn reset() { - let mut interpreter = Interpreter::new(30000, vec![], + let mut interpreter = Interpreter::new(30000, vec![], &console::Term::stdout()); assert_eq!(interpreter.run(String::from(">++++")), Ok(0)); diff --git a/src/repl.rs b/src/repl.rs index a572bc1..d705a64 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -111,25 +111,24 @@ impl Repl { } 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('[') { - 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_depth = 1; user_input = user_input[..index].to_string(); } - Some(_) => { + Some(_) if !self.loop_body.is_empty() => { 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; } _ => { if user_input.contains(']') { - if self.loop_depth == 0 { - error!("Found ']' without matching '['"); - return; - } - self.loop_depth -= 1; if self.loop_depth == 0 { self.loop_body.push_str(&user_input); user_input = self.loop_body.clone(); @@ -439,20 +438,34 @@ mod tests { for line in code { 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] 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 mut repl = Repl::new(interpreter, term); - let code = "++++++++[>++++[>++<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.<<++++[>+++ - [>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>." + let code = "++++++++[>++++[>++>+++>++++>+<<<<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.\ + <<++++[>+++[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>." .to_string(); 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] diff --git a/test_code/print_my_first_name.bf b/test_code/print_my_first_name.bf index fbec664..bddbfd4 100644 --- a/test_code/print_my_first_name.bf +++ b/test_code/print_my_first_name.bf @@ -1,3 +1,3 @@ Print my first name (Anas) -Unformated: -++++++++[>++++[>++<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.<<++++[>+++[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>. +UnFormatted: +++++++++[>++++[>++>+++>++++>+<<<<-]>>>>>>++[<<<->>>-]<<<<<<<-]>>+.<<++++[>+++[>+++<-]>++<<-]>>+.<<+++[>+++[>-<-]>-<<-]>>-.<<++++++[>>+++<<-]>>. diff --git a/test_code/print_my_first_name_formatted.bf b/test_code/print_my_first_name_formatted.bf index 1e2d08a..6849150 100644 --- a/test_code/print_my_first_name_formatted.bf +++ b/test_code/print_my_first_name_formatted.bf @@ -1,5 +1,5 @@ My first name (Anas) -Formated: +Formatted: ++++ ++++ 8 [ >++++