Skip to content

Commit

Permalink
fix continue in finalize-less loops
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Nov 3, 2024
1 parent 7fa5496 commit 3af8dc5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/dash_compiler/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub enum Label {
LoopEnd {
loop_id: usize,
},
LoopIncrement {
/// The end of an iteration. For `for` loops, this points to the increment code. For other loops, this aliases the condition.
LoopIterationEnd {
loop_id: usize,
},
SwitchCaseCondition {
Expand Down
8 changes: 6 additions & 2 deletions crates/dash_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,8 @@ impl<'interner> Visitor<Result<(), Error>> for FunctionCompiler<'interner> {

ib.current_function_mut()
.add_global_label(Label::LoopCondition { loop_id });
ib.current_function_mut()
.add_global_label(Label::LoopIterationEnd { loop_id });
ib.accept_expr(condition)?;
ib.build_jmpfalsep(Label::LoopEnd { loop_id }, false);

Expand Down Expand Up @@ -1071,6 +1073,8 @@ impl<'interner> Visitor<Result<(), Error>> for FunctionCompiler<'interner> {

ib.accept(*body)?;

ib.current_function_mut()
.add_global_label(Label::LoopIterationEnd { loop_id });
ib.accept_expr(condition)?;
ib.build_jmptruep(Label::LoopCondition { loop_id }, false);

Expand Down Expand Up @@ -1968,7 +1972,7 @@ impl<'interner> Visitor<Result<(), Error>> for FunctionCompiler<'interner> {

// Increment
ib.current_function_mut()
.add_global_label(Label::LoopIncrement { loop_id });
.add_global_label(Label::LoopIterationEnd { loop_id });
if let Some(finalizer) = finalizer {
ib.accept_expr(finalizer)?;
ib.build_pop();
Expand Down Expand Up @@ -2143,7 +2147,7 @@ impl<'interner> Visitor<Result<(), Error>> for FunctionCompiler<'interner> {

match breakable {
Breakable::Loop { loop_id, label: _ } => {
ib.build_jmp(Label::LoopIncrement { loop_id }, false);
ib.build_jmp(Label::LoopIterationEnd { loop_id }, false);
}
Breakable::Switch { .. } => {
// TODO: make it possible to use `continue` in loops even if its used in a switch
Expand Down

0 comments on commit 3af8dc5

Please sign in to comment.