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]
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));

View File

@ -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]

View File

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

View File

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