throw errors when necessary

This commit is contained in:
b1ek 2023-08-22 22:12:58 +10:00
parent 80f8998e04
commit df24b5da84
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 18 additions and 0 deletions

View File

@ -20,18 +20,32 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
pos += 1;
if char == '>' {
if (*pointer) + 1 == 30000 {
return Err("Attempt to increase pointer exceeding memory size".into());
}
*pointer += 1;
continue
}
if char == '<' {
if *pointer == 0 {
return Err("Attempt to reduce pointer to less than 1".into());
}
*pointer -= 1;
continue
}
if char == '+' {
if mem[*pointer] == 255 {
mem[*pointer] = 0;
continue
}
mem[*pointer] += 1;
continue
}
if char == '-' {
if mem[*pointer] == 0 {
mem[*pointer] = 255;
continue
}
mem[*pointer] -= 1;
continue
}
@ -72,6 +86,10 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
}
}
if loop_stack.len() != 0 {
return Err(format!("Unclosed loop at position {}", loop_stack.last().unwrap()));
}
Ok(())
}