chore: add some comments
This commit is contained in:
@@ -29,11 +29,11 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
|||||||
log::debug!("Read user input: {}", input_buf[0]);
|
log::debug!("Read user input: {}", input_buf[0]);
|
||||||
}
|
}
|
||||||
'[' => {
|
'[' => {
|
||||||
// loop start
|
// skip loop
|
||||||
if memory[head] == 0 {
|
if memory[head] == 0 {
|
||||||
|
// skip nested brackets and increment pc until the matching
|
||||||
|
// closing bracket is found
|
||||||
let mut count_brackets = 1;
|
let mut count_brackets = 1;
|
||||||
|
|
||||||
// find nested brackets
|
|
||||||
while count_brackets > 0 {
|
while count_brackets > 0 {
|
||||||
pc += 1;
|
pc += 1;
|
||||||
if pc >= code_bytes.len() {
|
if pc >= code_bytes.len() {
|
||||||
@@ -48,6 +48,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// execute loop
|
||||||
} else {
|
} else {
|
||||||
// save pc to jump back to
|
// save pc to jump back to
|
||||||
log::info!("Pushing location {} onto loop stack", pc);
|
log::info!("Pushing location {} onto loop stack", pc);
|
||||||
@@ -55,7 +56,7 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
']' => {
|
']' => {
|
||||||
// loop terminator
|
// loop back
|
||||||
if memory[head] != 0 {
|
if memory[head] != 0 {
|
||||||
// jump back to loop beginning
|
// jump back to loop beginning
|
||||||
if let Some(start_pc) = loop_stack.last() {
|
if let Some(start_pc) = loop_stack.last() {
|
||||||
@@ -63,8 +64,8 @@ pub fn interpret(code: &str) -> Vec<u8> {
|
|||||||
} else {
|
} else {
|
||||||
panic!("Loop stack empty, but still called!")
|
panic!("Loop stack empty, but still called!")
|
||||||
}
|
}
|
||||||
|
// loop done, continue normally
|
||||||
} else {
|
} else {
|
||||||
// loop done, continue normally
|
|
||||||
let location = loop_stack.pop();
|
let location = loop_stack.pop();
|
||||||
log::info!("Done with loop at location {}", location.unwrap());
|
log::info!("Done with loop at location {}", location.unwrap());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user