Skip to content

Commit c5456bd

Browse files
Julius Bierbaumliketechnik
authored andcommitted
feat(executor): handle GoTo case of Update
1 parent 4d80105 commit c5456bd

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/executor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use crate::{
2121
heap::Heap,
2222
};
2323

24+
use self::op_code::OffsetDirection;
25+
2426
pub struct ExecutorFrame {
2527
frame: Frame,
2628
pc: ProgramCounter,
@@ -119,6 +121,12 @@ pub fn run(code: &Code, heap: &mut Heap) {
119121
};
120122
current_pc.next(1).unwrap();
121123
},
124+
Update::GoTo(offset, direction) => {
125+
match direction {
126+
OffsetDirection::Forward => current_pc.next(offset).unwrap(),
127+
OffsetDirection::Backward => current_pc.(offset).unwrap(),
128+
}
129+
}
122130
}
123131
}
124132
}
@@ -168,6 +176,7 @@ the len is {length} but the index is {index}"
168176
pub enum Update {
169177
None,
170178
Return,
179+
GoTo(usize, OffsetDirection),
171180
MethodCall(Rc<Method>),
172181
}
173182

src/executor/program_counter.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum ProgramCounterError {
1212
#[error("Index {requested_pos} out of {actual_len}")]
1313
OutOfBoundsError {
1414
actual_len: usize,
15-
requested_pos: usize,
15+
requested_pos: isize,
1616
},
1717
}
1818

@@ -33,14 +33,23 @@ impl ProgramCounter {
3333
if self.current_op_codes.len() <= self.current_op_code + offset {
3434
return Err(ProgramCounterError::OutOfBoundsError {
3535
actual_len: self.current_op_codes.len(),
36-
requested_pos: self.current_op_code + offset,
36+
requested_pos: (self.current_op_code + offset) as isize,
3737
});
3838
}
3939
self.current_op_code += offset;
4040
Ok(())
4141
}
4242

43-
// todo: fn previous() might be needed as offset as usize cannot be negative
43+
pub fn previous(&mut self, offset: usize) -> Result<(), ProgramCounterError> {
44+
if offset > self.current_op_code {
45+
return Err(ProgramCounterError::OutOfBoundsError {
46+
actual_len: self.current_op_codes.len(),
47+
requested_pos: (self.current_op_code as isize) - (offset as isize),
48+
});
49+
}
50+
self.current_op_code -= offset;
51+
Ok(())
52+
}
4453

4554
/// absolute
4655
pub fn set(&mut self, position: usize) -> Result<(), ProgramCounterError> {

0 commit comments

Comments
 (0)