From 4ada085234a6e83e17d9cb5679949afbefcce1be Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Tue, 14 Aug 2018 21:21:47 +0200 Subject: [PATCH] Format code properly and try to implement append function --- vm/src/objlist.rs | 18 +++++++++++------- vm/src/pyobject.rs | 4 ++-- vm/src/vm.rs | 6 ++---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/vm/src/objlist.rs b/vm/src/objlist.rs index be714c785..fbdb1002a 100644 --- a/vm/src/objlist.rs +++ b/vm/src/objlist.rs @@ -1,5 +1,5 @@ use super::objsequence::PySliceableSequence; -use super::pyobject::{PyObjectKind, PyObjectRef, PyResult, PyObject, PyFuncArgs}; +use super::pyobject::{PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult}; use super::vm::VirtualMachine; use std::collections::HashMap; @@ -25,10 +25,16 @@ pub fn set_item( fn append(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { // TODO: Implement objlist::append - // println!("{:?}", args); - // if let PyObjectKind + trace!("list.append called with: {:?}", args); + if let PyObjectKind::List { ref elements } = args.args[0].borrow().kind { + warn!("implement append here: {:?}", elements); + // elements.push(args.args[1].clone()); + Ok(vm.get_none()) + } else { + unimplemented!("Raise error when list.append is called with no list") + // Err() + } // Ok(vm.new_bound_method(args.args[0].clone(), args.args[1].clone())) - Ok(vm.get_none()) } pub fn create_type(type_type: PyObjectRef) -> PyObjectRef { @@ -36,9 +42,7 @@ pub fn create_type(type_type: PyObjectRef) -> PyObjectRef { dict.insert( "append".to_string(), PyObject::new( - PyObjectKind::RustFunction { - function: append, - }, + PyObjectKind::RustFunction { function: append }, type_type.clone(), ), ); diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 9cf62d352..9d77731f4 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -91,10 +91,10 @@ impl PyContext { PyObject::new(PyObjectKind::Boolean { value: b }, self.type_type.clone()) } - pub fn new_tuple(&self) -> PyObjectRef { + pub fn new_tuple(&self, elements: Option>) -> PyObjectRef { PyObject::new( PyObjectKind::Tuple { - elements: Vec::new(), + elements: elements.unwrap_or(Vec::new()), }, self.type_type.clone(), ) diff --git a/vm/src/vm.rs b/vm/src/vm.rs index f2e1d0b59..b5bf92556 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -594,15 +594,13 @@ impl VirtualMachine { } bytecode::Instruction::BuildList { size } => { let elements = self.pop_multiple(*size); - let list_obj = - self.context().new_list( Some(elements) ); + let list_obj = self.context().new_list(Some(elements)); self.push_value(list_obj); None } bytecode::Instruction::BuildTuple { size } => { let elements = self.pop_multiple(*size); - let list_obj = - PyObject::new(PyObjectKind::Tuple { elements: elements }, self.get_type()); + let list_obj = self.context().new_tuple(Some(elements)); self.push_value(list_obj); None }