File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,8 @@ use crate::{
21
21
heap:: Heap ,
22
22
} ;
23
23
24
+ use self :: op_code:: OffsetDirection ;
25
+
24
26
pub struct ExecutorFrame {
25
27
frame : Frame ,
26
28
pc : ProgramCounter ,
@@ -119,6 +121,12 @@ pub fn run(code: &Code, heap: &mut Heap) {
119
121
} ;
120
122
current_pc. next ( 1 ) . unwrap ( ) ;
121
123
} ,
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
+ }
122
130
}
123
131
}
124
132
}
@@ -168,6 +176,7 @@ the len is {length} but the index is {index}"
168
176
pub enum Update {
169
177
None ,
170
178
Return ,
179
+ GoTo ( usize , OffsetDirection ) ,
171
180
MethodCall ( Rc < Method > ) ,
172
181
}
173
182
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ pub enum ProgramCounterError {
12
12
#[ error( "Index {requested_pos} out of {actual_len}" ) ]
13
13
OutOfBoundsError {
14
14
actual_len : usize ,
15
- requested_pos : usize ,
15
+ requested_pos : isize ,
16
16
} ,
17
17
}
18
18
@@ -33,14 +33,23 @@ impl ProgramCounter {
33
33
if self . current_op_codes . len ( ) <= self . current_op_code + offset {
34
34
return Err ( ProgramCounterError :: OutOfBoundsError {
35
35
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 ,
37
37
} ) ;
38
38
}
39
39
self . current_op_code += offset;
40
40
Ok ( ( ) )
41
41
}
42
42
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
+ }
44
53
45
54
/// absolute
46
55
pub fn set ( & mut self , position : usize ) -> Result < ( ) , ProgramCounterError > {
You can’t perform that action at this time.
0 commit comments