diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 7c9b3d0cd..bf6122899 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -14,6 +14,7 @@ mod import; mod objdict; mod objint; mod objlist; +mod objsequence; mod objstr; mod objtuple; mod objtype; diff --git a/vm/src/objlist.rs b/vm/src/objlist.rs index 725f96f55..fbe4e1596 100644 --- a/vm/src/objlist.rs +++ b/vm/src/objlist.rs @@ -1,48 +1,10 @@ use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult}; use super::vm::VirtualMachine; -pub fn get_pos(l: &Vec, p: i32) -> usize { - if p < 0 { - l.len() - ((-p) as usize) - } else { - p as usize - } -} - -pub fn get_slice_items(l: &Vec, slice: &PyObjectRef) -> Vec { - // TODO: we could potentially avoid this copy and use slice - match &(slice.borrow()).kind { - PyObjectKind::Slice { start, stop, step } => { - let start = match start { - &Some(start) => get_pos(l, start), - &None => 0, - }; - let stop = match stop { - &Some(stop) => get_pos(l, stop), - &None => l.len() as usize, - }; - match step { - &None | &Some(1) => l[start..stop].to_vec(), - &Some(num) => { - if num < 0 { - unimplemented!("negative step indexing not yet supported") - }; - l[start..stop] - .iter() - .step_by(num as usize) - .cloned() - .collect() - } - } - } - kind => panic!("get_slice_items called with non-slice: {:?}", kind), - } -} - pub fn get_item(vm: &mut VirtualMachine, l: &Vec, b: PyObjectRef) -> PyResult { match &(b.borrow()).kind { PyObjectKind::Integer { value } => { - let pos_index = get_pos(l, *value); + let pos_index = super::objsequence::get_pos(l, *value); if pos_index < l.len() { let obj = l[pos_index].clone(); Ok(obj) @@ -56,7 +18,7 @@ pub fn get_item(vm: &mut VirtualMachine, l: &Vec, b: PyObjectRef) - step: _, } => Ok(PyObject::new( PyObjectKind::List { - elements: get_slice_items(l, &b), + elements: super::objsequence::get_slice_items(l, &b), }, vm.get_type(), )), @@ -76,7 +38,7 @@ pub fn set_item( ) -> PyResult { match &(idx.borrow()).kind { PyObjectKind::Integer { value } => { - let pos_index = get_pos(l, *value); + let pos_index = super::objsequence::get_pos(l, *value); l[pos_index] = obj; Ok(vm.get_none()) } diff --git a/vm/src/objsequence.rs b/vm/src/objsequence.rs new file mode 100644 index 000000000..c9eaf04e7 --- /dev/null +++ b/vm/src/objsequence.rs @@ -0,0 +1,39 @@ +use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult}; + +pub fn get_pos(l: &Vec, p: i32) -> usize { + if p < 0 { + l.len() - ((-p) as usize) + } else { + p as usize + } +} + +pub fn get_slice_items(l: &Vec, slice: &PyObjectRef) -> Vec { + // TODO: we could potentially avoid this copy and use slice + match &(slice.borrow()).kind { + PyObjectKind::Slice { start, stop, step } => { + let start = match start { + &Some(start) => get_pos(l, start), + &None => 0, + }; + let stop = match stop { + &Some(stop) => get_pos(l, stop), + &None => l.len() as usize, + }; + match step { + &None | &Some(1) => l[start..stop].to_vec(), + &Some(num) => { + if num < 0 { + unimplemented!("negative step indexing not yet supported") + }; + l[start..stop] + .iter() + .step_by(num as usize) + .cloned() + .collect() + } + } + } + kind => panic!("get_slice_items called with non-slice: {:?}", kind), + } +} diff --git a/vm/src/objtuple.rs b/vm/src/objtuple.rs index 0f2458e92..36e4ac4ab 100644 --- a/vm/src/objtuple.rs +++ b/vm/src/objtuple.rs @@ -4,7 +4,7 @@ use super::vm::VirtualMachine; pub fn get_item(vm: &mut VirtualMachine, l: &Vec, b: PyObjectRef) -> PyResult { match &(b.borrow()).kind { PyObjectKind::Integer { value } => { - let pos_index = super::objlist::get_pos(l, *value); + let pos_index = super::objsequence::get_pos(l, *value); if pos_index < l.len() { let obj = l[pos_index].clone(); Ok(obj) @@ -18,7 +18,7 @@ pub fn get_item(vm: &mut VirtualMachine, l: &Vec, b: PyObjectRef) - step: _, } => Ok(PyObject::new( PyObjectKind::Tuple { - elements: super::objlist::get_slice_items(l, &b), + elements: super::objsequence::get_slice_items(l, &b), }, vm.get_type(), )),