Skip to content

Commit 61f37b1

Browse files
HyeockJinKimyouknowone
authored andcommitted
implement async for function in compiler
1 parent 2856aff commit 61f37b1

1 file changed

Lines changed: 32 additions & 27 deletions

File tree

‎compiler/codegen/src/compile.rs‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,11 +2656,8 @@ impl Compiler {
26562656
}
26572657

26582658
let mut loop_labels = vec![];
2659+
let mut is_async = false;
26592660
for generator in generators {
2660-
if generator.is_async {
2661-
unimplemented!("async for comprehensions");
2662-
}
2663-
26642661
let loop_block = self.new_block();
26652662
let after_block = self.new_block();
26662663

@@ -2672,35 +2669,31 @@ impl Compiler {
26722669
self.compile_expression(&generator.iter)?;
26732670

26742671
// Get iterator / turn item into an iterator
2675-
emit!(self, Instruction::GetIter);
2672+
if generator.is_async {
2673+
emit!(self, Instruction::GetAIter);
2674+
} else {
2675+
emit!(self, Instruction::GetIter);
2676+
}
26762677
}
26772678

26782679
loop_labels.push((loop_block, after_block));
2679-
2680+
self.switch_to_block(loop_block);
26802681
if generator.is_async {
2681-
let check_asynciter_block = self.new_block();
2682-
2683-
self.emit(Instruction::GetAIter);
2684-
self.switch_to_block(loop_block);
2685-
self.emit(Instruction::SetupExcept {
2686-
handler: check_asynciter_block,
2682+
is_async = true;
2683+
emit!(self, Instruction::SetupExcept {
2684+
handler: after_block,
26872685
});
2688-
self.emit(Instruction::GetANext);
2686+
emit!(self, Instruction::GetANext);
26892687
self.emit_constant(ConstantData::None);
2690-
self.emit(Instruction::YieldFrom);
2691-
self.compile_store(&generator.target)?;
2692-
self.emit(Instruction::PopBlock);
2693-
2688+
emit!(self, Instruction::YieldFrom);
2689+
emit!(self, Instruction::PopBlock);
26942690
} else {
2695-
// Get iterator / turn item into an iterator
2696-
self.emit(Instruction::GetIter);
2697-
2698-
self.switch_to_block(loop_block);
2699-
self.emit(Instruction::ForIter {
2700-
target: after_block,
2701-
});
2702-
2703-
self.compile_store(&generator.target)?;
2691+
emit!(
2692+
self,
2693+
Instruction::ForIter {
2694+
target: after_block,
2695+
}
2696+
);
27042697
}
27052698

27062699
self.compile_store(&generator.target)?;
@@ -2719,6 +2712,9 @@ impl Compiler {
27192712

27202713
// End of for loop:
27212714
self.switch_to_block(after_block);
2715+
if is_async {
2716+
emit!(self, Instruction::EndAsyncFor);
2717+
}
27222718
}
27232719

27242720
if return_none {
@@ -2755,10 +2751,19 @@ impl Compiler {
27552751
self.compile_expression(&generators[0].iter)?;
27562752

27572753
// Get iterator / turn item into an iterator
2758-
emit!(self, Instruction::GetIter);
2754+
if is_async {
2755+
emit!(self, Instruction::GetAIter);
2756+
} else {
2757+
emit!(self, Instruction::GetIter);
2758+
};
27592759

27602760
// Call just created <listcomp> function:
27612761
emit!(self, Instruction::CallFunctionPositional { nargs: 1 });
2762+
if is_async {
2763+
emit!(self, Instruction::GetAwaitable);
2764+
self.emit_constant(ConstantData::None);
2765+
emit!(self, Instruction::YieldFrom);
2766+
}
27622767
Ok(())
27632768
}
27642769

0 commit comments

Comments
 (0)