mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-19 19:15:43 +00:00
comprehension starred expression compatibility
This commit is contained in:
parent
a77e0b8587
commit
0d139f9942
1 changed files with 15 additions and 3 deletions
|
@ -2027,21 +2027,33 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut compile_element = |element| {
|
||||||
|
self.compile_expression(element).map_err(|e| {
|
||||||
|
if matches!(e.error, CompileErrorType::InvalidStarExpr) {
|
||||||
|
self.error(CompileErrorType::SyntaxError(
|
||||||
|
"iterable unpacking cannot be used in comprehension".to_owned(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
e
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
match kind {
|
match kind {
|
||||||
ast::ComprehensionKind::GeneratorExpression { element } => {
|
ast::ComprehensionKind::GeneratorExpression { element } => {
|
||||||
self.compile_expression(element)?;
|
compile_element(element)?;
|
||||||
self.mark_generator();
|
self.mark_generator();
|
||||||
self.emit(Instruction::YieldValue);
|
self.emit(Instruction::YieldValue);
|
||||||
self.emit(Instruction::Pop);
|
self.emit(Instruction::Pop);
|
||||||
}
|
}
|
||||||
ast::ComprehensionKind::List { element } => {
|
ast::ComprehensionKind::List { element } => {
|
||||||
self.compile_expression(element)?;
|
compile_element(element)?;
|
||||||
self.emit(Instruction::ListAppend {
|
self.emit(Instruction::ListAppend {
|
||||||
i: 1 + generators.len(),
|
i: 1 + generators.len(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ast::ComprehensionKind::Set { element } => {
|
ast::ComprehensionKind::Set { element } => {
|
||||||
self.compile_expression(element)?;
|
compile_element(element)?;
|
||||||
self.emit(Instruction::SetAdd {
|
self.emit(Instruction::SetAdd {
|
||||||
i: 1 + generators.len(),
|
i: 1 + generators.len(),
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue