From dc29847cd3b37fd234d32f81f088a2e8f0031aff Mon Sep 17 00:00:00 2001 From: b1ek Date: Tue, 22 Aug 2023 22:14:41 +1000 Subject: [PATCH] add position annotation to errors --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5f2cd79..22c73ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(())