Skip to content

Accept slices to function calls #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn parse(filename: &Path) -> Result<ast::Program, String> {
}
}

pub fn parse_program(source: &String) -> Result<ast::Program, String> {
pub fn parse_program(source: &str) -> Result<ast::Program, String> {
let lxr = lexer::Lexer::new(&source);
match python::ProgramParser::new().parse(lxr) {
Err(lalrpop_util::ParseError::UnrecognizedToken {
Expand All @@ -52,15 +52,15 @@ pub fn parse_program(source: &String) -> Result<ast::Program, String> {
}
}

pub fn parse_statement(source: &String) -> Result<ast::LocatedStatement, String> {
pub fn parse_statement(source: &str) -> Result<ast::LocatedStatement, String> {
let lxr = lexer::Lexer::new(&source);
match python::StatementParser::new().parse(lxr) {
Err(why) => Err(String::from(format!("{:?}", why))),
Ok(p) => Ok(p),
}
}

pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
pub fn parse_expression(source: &str) -> Result<ast::Expression, String> {
let lxr = lexer::Lexer::new(&source);
match python::ExpressionParser::new().parse(lxr) {
Err(why) => Err(String::from(format!("{:?}", why))),
Expand Down
4 changes: 2 additions & 2 deletions vm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Compiler {

pub fn compile(
vm: &mut VirtualMachine,
source: &String,
source: &str,
mode: Mode,
source_path: Option<String>,
) -> Result<PyObjectRef, String> {
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Compiler {
self.emit(Instruction::ReturnValue);
}

fn compile_statements(&mut self, statements: &Vec<ast::LocatedStatement>) {
fn compile_statements(&mut self, statements: &[ast::LocatedStatement]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this variation is better rust code, but still I feel like the Vec syntax is clearer. At least to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows non-Vec data structures which can produce slices for free to call the function with less overhead.

for statement in statements {
self.compile_statement(statement)
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::compile;
use super::pyobject::{PyObjectRef, PyResult};
use super::vm::VirtualMachine;

pub fn eval(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> PyResult {
pub fn eval(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> PyResult {
match compile::compile(vm, source, compile::Mode::Eval, None) {
Ok(bytecode) => {
debug!("Code object: {:?}", bytecode);
Expand Down
8 changes: 4 additions & 4 deletions vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::compile;
use super::pyobject::{DictProtocol, PyObjectKind, PyResult};
use super::vm::VirtualMachine;

fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
fn import_uncached_module(vm: &mut VirtualMachine, module: &str) -> PyResult {
// Check for Rust-native modules
if let Some(module) = vm.stdlib_inits.get(module) {
return Ok(module(&vm.ctx).clone());
Expand Down Expand Up @@ -53,7 +53,7 @@ fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult
Ok(vm.ctx.new_module(module, scope))
}

fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
fn import_module(vm: &mut VirtualMachine, module_name: &str) -> PyResult {
// First, see if we already loaded the module:
let sys_modules = vm.sys_module.get_item("modules").unwrap();
if let Some(module) = sys_modules.get_item(module_name) {
Expand All @@ -64,7 +64,7 @@ fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
Ok(module)
}

pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<String>) -> PyResult {
pub fn import(vm: &mut VirtualMachine, module_name: &str, symbol: &Option<String>) -> PyResult {
let module = import_module(vm, module_name)?;
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
// that
Expand All @@ -75,7 +75,7 @@ pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<Str
Ok(obj)
}

fn find_source(vm: &VirtualMachine, name: &String) -> io::Result<PathBuf> {
fn find_source(vm: &VirtualMachine, name: &str) -> io::Result<PathBuf> {
let sys_path = vm.sys_module.get_item("path").unwrap();
let mut paths: Vec<PathBuf> = match sys_path.borrow().kind {
PyObjectKind::List { ref elements } => elements
Expand Down
8 changes: 4 additions & 4 deletions vm/src/obj/objsequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ impl PySliceableSequence for Vec<PyObjectRef> {
pub fn get_item(
vm: &mut VirtualMachine,
sequence: &PyObjectRef,
elements: &Vec<PyObjectRef>,
elements: &[PyObjectRef],
subscript: PyObjectRef,
) -> PyResult {
match &(subscript.borrow()).kind {
PyObjectKind::Integer { value } => {
let pos_index = elements.get_pos(*value);
let pos_index = elements.to_vec().get_pos(*value);
if pos_index < elements.len() {
let obj = elements[pos_index].clone();
Ok(obj)
Expand All @@ -84,10 +84,10 @@ pub fn get_item(
} => Ok(PyObject::new(
match &(sequence.borrow()).kind {
PyObjectKind::Tuple { elements: _ } => PyObjectKind::Tuple {
elements: elements.get_slice_items(&subscript),
elements: elements.to_vec().get_slice_items(&subscript),
},
PyObjectKind::List { elements: _ } => PyObjectKind::List {
elements: elements.get_slice_items(&subscript),
elements: elements.to_vec().get_slice_items(&subscript),
},
ref kind => panic!("sequence get_item called for non-sequence: {:?}", kind),
},
Expand Down
6 changes: 3 additions & 3 deletions vm/src/obj/objstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,18 @@ impl PySliceableSequence for String {
}
}

pub fn subscript(vm: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyResult {
pub fn subscript(vm: &mut VirtualMachine, value: &str, b: PyObjectRef) -> PyResult {
// let value = a
match &(*b.borrow()).kind {
&PyObjectKind::Integer { value: ref pos } => {
let idx = value.get_pos(*pos);
let idx = value.to_string().get_pos(*pos);
Ok(vm.new_str(value[idx..idx + 1].to_string()))
}
&PyObjectKind::Slice {
start: _,
stop: _,
step: _,
} => Ok(vm.new_str(value.get_slice_items(&b))),
} => Ok(vm.new_str(value.to_string().get_slice_items(&b).to_string())),
_ => panic!(
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",
value, b
Expand Down
6 changes: 3 additions & 3 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl PyContext {
)
}

pub fn new_class(&self, name: &String, base: PyObjectRef) -> PyObjectRef {
pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef {
objtype::new(self.type_type(), name, vec![base], self.new_dict()).unwrap()
}

Expand All @@ -289,10 +289,10 @@ impl PyContext {
}.into_ref()
}

pub fn new_module(&self, name: &String, scope: PyObjectRef) -> PyObjectRef {
pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef {
PyObject::new(
PyObjectKind::Module {
name: name.clone(),
name: name.to_string(),
dict: scope.clone(),
},
self.module_type.clone(),
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn create_node(ctx: &PyContext, _name: &str) -> PyObjectRef {
node
}

fn statements_to_ast(ctx: &PyContext, statements: &Vec<ast::LocatedStatement>) -> PyObjectRef {
fn statements_to_ast(ctx: &PyContext, statements: &[ast::LocatedStatement]) -> PyObjectRef {
let mut py_statements = vec![];
for statement in statements {
py_statements.push(statement_to_ast(ctx, statement));
Expand Down
12 changes: 6 additions & 6 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ impl VirtualMachine {
self.current_frame().last_value()
}

fn store_name(&mut self, name: &String) -> Option<PyResult> {
fn store_name(&mut self, name: &str) -> Option<PyResult> {
let obj = self.pop_value();
self.current_frame_mut().locals.set_item(name, obj);
None
}

fn load_name(&mut self, name: &String) -> Option<PyResult> {
fn load_name(&mut self, name: &str) -> Option<PyResult> {
// Lookup name in scope and put it onto the stack!
let mut scope = self.current_frame().locals.clone();
loop {
Expand Down Expand Up @@ -628,8 +628,8 @@ impl VirtualMachine {
}
}

fn import(&mut self, module: &String, symbol: &Option<String>) -> Option<PyResult> {
let obj = match import(self, module, symbol) {
fn import(&mut self, module: &str, symbol: &Option<String>) -> Option<PyResult> {
let obj = match import(self, &module.to_string(), symbol) {
Ok(value) => value,
Err(value) => return Some(Err(value)),
};
Expand All @@ -643,7 +643,7 @@ impl VirtualMachine {
objtype::get_attribute(self, obj.clone(), attr_name)
}

fn load_attr(&mut self, attr_name: &String) -> Option<PyResult> {
fn load_attr(&mut self, attr_name: &str) -> Option<PyResult> {
let parent = self.pop_value();
match self.get_attribute(parent, attr_name) {
Ok(obj) => {
Expand All @@ -654,7 +654,7 @@ impl VirtualMachine {
}
}

fn store_attr(&mut self, attr_name: &String) -> Option<PyResult> {
fn store_attr(&mut self, attr_name: &str) -> Option<PyResult> {
let parent = self.pop_value();
let value = self.pop_value();
parent.set_attr(attr_name, value);
Expand Down