mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
Address feedback, simplify compilation code
This commit is contained in:
parent
7aac5b634f
commit
623472c366
1 changed files with 20 additions and 37 deletions
|
@ -300,8 +300,17 @@ impl<O: OutputStream> Compiler<O> {
|
|||
Import { names } => {
|
||||
// import a, b, c as d
|
||||
for name in names {
|
||||
self.compile_import(Some(&name.symbol), vec![], 0, name.alias.is_some());
|
||||
self.emit(Instruction::Import {
|
||||
name: Some(name.symbol.clone()),
|
||||
symbols: vec![],
|
||||
level: 0,
|
||||
});
|
||||
if let Some(alias) = &name.alias {
|
||||
for part in name.symbol.split('.').skip(1) {
|
||||
self.emit(Instruction::LoadAttr {
|
||||
name: part.to_owned(),
|
||||
});
|
||||
}
|
||||
self.store_name(alias);
|
||||
} else {
|
||||
self.store_name(name.symbol.split('.').next().unwrap());
|
||||
|
@ -317,12 +326,11 @@ impl<O: OutputStream> Compiler<O> {
|
|||
|
||||
if import_star {
|
||||
// from .... import *
|
||||
self.compile_import(
|
||||
module.as_ref().map(String::as_str),
|
||||
vec!["*".to_owned()],
|
||||
*level,
|
||||
false,
|
||||
);
|
||||
self.emit(Instruction::Import {
|
||||
name: module.clone(),
|
||||
symbols: vec!["*".to_owned()],
|
||||
level: *level,
|
||||
});
|
||||
self.emit(Instruction::ImportStar);
|
||||
} else {
|
||||
// from mod import a, b as c
|
||||
|
@ -330,12 +338,11 @@ impl<O: OutputStream> Compiler<O> {
|
|||
let from_list = names.iter().map(|n| n.symbol.clone()).collect();
|
||||
|
||||
// Load module once:
|
||||
self.compile_import(
|
||||
module.as_ref().map(String::as_str),
|
||||
from_list,
|
||||
*level,
|
||||
false,
|
||||
);
|
||||
self.emit(Instruction::Import {
|
||||
name: module.clone(),
|
||||
symbols: from_list,
|
||||
level: *level,
|
||||
});
|
||||
|
||||
for name in names {
|
||||
// import symbol from module:
|
||||
|
@ -573,30 +580,6 @@ impl<O: OutputStream> Compiler<O> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_import(
|
||||
&mut self,
|
||||
name: Option<&str>,
|
||||
symbols: Vec<String>,
|
||||
level: usize,
|
||||
get_final_module: bool,
|
||||
) {
|
||||
self.emit(Instruction::Import {
|
||||
name: name.map(ToOwned::to_owned),
|
||||
symbols,
|
||||
level,
|
||||
});
|
||||
|
||||
if get_final_module {
|
||||
if let Some(name) = name {
|
||||
for part in name.split('.').skip(1) {
|
||||
self.emit(Instruction::LoadAttr {
|
||||
name: part.to_owned(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_delete(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
|
||||
match &expression.node {
|
||||
ast::ExpressionType::Identifier { name } => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue