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

@ -323,6 +323,14 @@ 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]

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
[ [
>++++ >++++