Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WIP: 2510 Async list Comprehension #2513

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

Closed
Closed
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
38 changes: 27 additions & 11 deletions compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2258,9 +2258,9 @@ impl Compiler {

let mut loop_labels = vec![];
for generator in generators {
if generator.is_async {
unimplemented!("async for comprehensions");
}
// if generator.is_async {
// unimplemented!("async for comprehensions");
// }

// Setup for loop:
self.emit(Instruction::SetupLoop);
Expand All @@ -2271,21 +2271,37 @@ impl Compiler {
} else {
// Evaluate iterated item:
self.compile_expression(&generator.iter)?;

// Get iterator / turn item into an iterator
self.emit(Instruction::GetIter);
}

let loop_block = self.new_block();
let after_block = self.new_block();
loop_labels.push((loop_block, after_block));

self.switch_to_block(loop_block);
self.emit(Instruction::ForIter {
target: after_block,
});
if generator.is_async {
let check_asynciter_block = self.new_block();

self.emit(Instruction::GetAIter);
self.switch_to_block(loop_block);
self.emit(Instruction::SetupExcept {
handler: check_asynciter_block,
});
self.emit(Instruction::GetANext);
self.emit_constant(ConstantData::None);
self.emit(Instruction::YieldFrom);
self.compile_store(&generator.target)?;
self.emit(Instruction::PopBlock);

} else {
// Get iterator / turn item into an iterator
self.emit(Instruction::GetIter);

self.compile_store(&generator.target)?;
self.switch_to_block(loop_block);
self.emit(Instruction::ForIter {
target: after_block,
});

self.compile_store(&generator.target)?;
}

// Now evaluate the ifs:
for if_condition in &generator.ifs {
Expand Down