diff --git a/src/interpret.rs b/src/interpret.rs index 4c557ab..54acefd 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -29,11 +29,11 @@ pub fn interpret(code: &str) -> Vec { log::debug!("Read user input: {}", input_buf[0]); } '[' => { - // loop start + // skip loop if memory[head] == 0 { + // skip nested brackets and increment pc until the matching + // closing bracket is found let mut count_brackets = 1; - - // find nested brackets while count_brackets > 0 { pc += 1; if pc >= code_bytes.len() { @@ -48,6 +48,7 @@ pub fn interpret(code: &str) -> Vec { _ => {} } } + // execute loop } else { // save pc to jump back to log::info!("Pushing location {} onto loop stack", pc); @@ -55,7 +56,7 @@ pub fn interpret(code: &str) -> Vec { } } ']' => { - // loop terminator + // loop back if memory[head] != 0 { // jump back to loop beginning if let Some(start_pc) = loop_stack.last() { @@ -63,8 +64,8 @@ pub fn interpret(code: &str) -> Vec { } else { panic!("Loop stack empty, but still called!") } + // loop done, continue normally } else { - // loop done, continue normally let location = loop_stack.pop(); log::info!("Done with loop at location {}", location.unwrap()); }