skip the loop if current is 0

This commit is contained in:
b1ek 2023-08-23 16:34:20 +10:00
parent b69f495a3d
commit e34bb583a1
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 22 additions and 0 deletions

View File

@ -131,6 +131,28 @@ pub fn eval_mem<'a>(bf_str: &&str, mem: &'a mut [u8], pointer: &mut usize) -> Re
continue
}
if char == '[' {
// if current is 0
if mem[*pointer] == 0 {
// skip the loop completely
let mut p = pos.clone();
loop {
if p == progsize {
return Err(
BrainFuckError::new(format!("Unmatched '[' ({})", pos), mem)
);
}
if chars[p] == ']' {
pos = p + 1;
break;
}
p += 1;
}
continue
}
loop_stack.push(pos);
continue
}