Skip to content
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