mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-04 02:39: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 { names } => {
|
||||||
// import a, b, c as d
|
// import a, b, c as d
|
||||||
for name in names {
|
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 {
|
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);
|
self.store_name(alias);
|
||||||
} else {
|
} else {
|
||||||
self.store_name(name.symbol.split('.').next().unwrap());
|
self.store_name(name.symbol.split('.').next().unwrap());
|
||||||
|
@ -317,12 +326,11 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
|
|
||||||
if import_star {
|
if import_star {
|
||||||
// from .... import *
|
// from .... import *
|
||||||
self.compile_import(
|
self.emit(Instruction::Import {
|
||||||
module.as_ref().map(String::as_str),
|
name: module.clone(),
|
||||||
vec!["*".to_owned()],
|
symbols: vec!["*".to_owned()],
|
||||||
*level,
|
level: *level,
|
||||||
false,
|
});
|
||||||
);
|
|
||||||
self.emit(Instruction::ImportStar);
|
self.emit(Instruction::ImportStar);
|
||||||
} else {
|
} else {
|
||||||
// from mod import a, b as c
|
// 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();
|
let from_list = names.iter().map(|n| n.symbol.clone()).collect();
|
||||||
|
|
||||||
// Load module once:
|
// Load module once:
|
||||||
self.compile_import(
|
self.emit(Instruction::Import {
|
||||||
module.as_ref().map(String::as_str),
|
name: module.clone(),
|
||||||
from_list,
|
symbols: from_list,
|
||||||
*level,
|
level: *level,
|
||||||
false,
|
});
|
||||||
);
|
|
||||||
|
|
||||||
for name in names {
|
for name in names {
|
||||||
// import symbol from module:
|
// import symbol from module:
|
||||||
|
@ -573,30 +580,6 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
Ok(())
|
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> {
|
fn compile_delete(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
|
||||||
match &expression.node {
|
match &expression.node {
|
||||||
ast::ExpressionType::Identifier { name } => {
|
ast::ExpressionType::Identifier { name } => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue