add position annotation to errors

This commit is contained in:
b1ek 2023-08-22 22:14:41 +10:00
parent df24b5da84
commit dc29847cd3
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 4 additions and 4 deletions

View File

@ -21,14 +21,14 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
if char == '>' {
if (*pointer) + 1 == 30000 {
return Err("Attempt to increase pointer exceeding memory size".into());
return Err(format!("Attempt to increase pointer exceeding memory size ({})", pos));
}
*pointer += 1;
continue
}
if char == '<' {
if *pointer == 0 {
return Err("Attempt to reduce pointer to less than 1".into());
return Err(format!("Attempt to reduce pointer to less than 1 ({})", pos));
}
*pointer -= 1;
continue
@ -73,7 +73,7 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
if char == ']' {
if loop_stack.len() == 0 {
return Err(format!("Invalid loop at position {pos}"));
return Err(format!("Unmatched ']' ({})", pos));
}
if mem[*pointer] == 0 {
@ -87,7 +87,7 @@ 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()));
return Err(format!("Unmatched '[' ({})", loop_stack.last().unwrap()));
}
Ok(())