Skip to content

Commit 5499d81

Browse files
committed
Address feedback, simplify compilation code
1 parent 1be2d95 commit 5499d81

File tree

4 files changed

+31
-52
lines changed

4 files changed

+31
-52
lines changed

compiler/src/compile.rs

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,17 @@ impl<O: OutputStream> Compiler<O> {
300300
Import { names } => {
301301
// import a, b, c as d
302302
for name in names {
303-
self.compile_import(Some(&name.symbol), vec![], 0, name.alias.is_some());
303+
self.emit(Instruction::Import {
304+
name: Some(name.symbol.clone()),
305+
symbols: vec![],
306+
level: 0,
307+
});
304308
if let Some(alias) = &name.alias {
309+
for part in name.symbol.split('.').skip(1) {
310+
self.emit(Instruction::LoadAttr {
311+
name: part.to_owned(),
312+
});
313+
}
305314
self.store_name(alias);
306315
} else {
307316
self.store_name(name.symbol.split('.').next().unwrap());
@@ -317,25 +326,23 @@ impl<O: OutputStream> Compiler<O> {
317326

318327
if import_star {
319328
// from .... import *
320-
self.compile_import(
321-
module.as_ref().map(String::as_str),
322-
vec!["*".to_owned()],
323-
*level,
324-
false,
325-
);
329+
self.emit(Instruction::Import {
330+
name: module.clone(),
331+
symbols: vec!["*".to_owned()],
332+
level: *level,
333+
});
326334
self.emit(Instruction::ImportStar);
327335
} else {
328336
// from mod import a, b as c
329337
// First, determine the fromlist (for import lib):
330338
let from_list = names.iter().map(|n| n.symbol.clone()).collect();
331339

332340
// Load module once:
333-
self.compile_import(
334-
module.as_ref().map(String::as_str),
335-
from_list,
336-
*level,
337-
false,
338-
);
341+
self.emit(Instruction::Import {
342+
name: module.clone(),
343+
symbols: from_list,
344+
level: *level,
345+
});
339346

340347
for name in names {
341348
// import symbol from module:
@@ -573,30 +580,6 @@ impl<O: OutputStream> Compiler<O> {
573580
Ok(())
574581
}
575582

576-
fn compile_import(
577-
&mut self,
578-
name: Option<&str>,
579-
symbols: Vec<String>,
580-
level: usize,
581-
get_final_module: bool,
582-
) {
583-
self.emit(Instruction::Import {
584-
name: name.map(ToOwned::to_owned),
585-
symbols,
586-
level,
587-
});
588-
589-
if get_final_module {
590-
if let Some(name) = name {
591-
for part in name.split('.').skip(1) {
592-
self.emit(Instruction::LoadAttr {
593-
name: part.to_owned(),
594-
});
595-
}
596-
}
597-
}
598-
}
599-
600583
fn compile_delete(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
601584
match &expression.node {
602585
ast::ExpressionType::Identifier { name } => {

vm/src/obj/objmodule.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ impl PyModuleRef {
2828
}
2929

3030
fn getattribute(self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
31-
match vm.generic_getattribute(self.as_object().clone(), name.clone()) {
32-
Ok(Some(val)) => Ok(val),
33-
Ok(None) => Err(vm.new_attribute_error(format!(
34-
"module '{}' has no attribute '{}'",
35-
self.name, name,
36-
))),
37-
Err(err) => Err(err),
38-
}
31+
vm.generic_getattribute(self.as_object().clone(), name.clone())?
32+
.ok_or_else(|| {
33+
vm.new_attribute_error(format!(
34+
"module '{}' has no attribute '{}'",
35+
self.name, name,
36+
))
37+
})
3938
}
4039

4140
fn repr(self, vm: &VirtualMachine) -> PyResult {

vm/src/obj/objobject.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ fn object_dict_setter(
225225

226226
fn object_getattribute(obj: PyObjectRef, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
227227
vm_trace!("object.__getattribute__({:?}, {:?})", obj, name);
228-
match vm.generic_getattribute(obj.clone(), name.clone()) {
229-
Ok(Some(val)) => Ok(val),
230-
Ok(None) => Err(vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name))),
231-
Err(err) => Err(err),
232-
}
228+
vm.generic_getattribute(obj.clone(), name.clone())?
229+
.ok_or_else(|| vm.new_attribute_error(format!("{} has no attribute '{}'", obj, name)))
233230
}

vm/src/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,13 @@ impl VirtualMachine {
415415

416416
let module = self.new_str(module.to_owned());
417417

418-
let sys_module = if weird {
418+
let cached_module = if weird {
419419
None
420420
} else {
421421
let sys_modules = self.get_attribute(self.sys_module.clone(), "modules")?;
422422
sys_modules.get_item(module.clone(), self).ok()
423423
};
424-
match sys_module {
424+
match cached_module {
425425
Some(module) => Ok(module),
426426
None => {
427427
let import_func = self

0 commit comments

Comments
 (0)