throw errors when necessary
This commit is contained in:
parent
80f8998e04
commit
df24b5da84
18
src/lib.rs
18
src/lib.rs
|
@ -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(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue