Skip to content

Commit

Permalink
Fixed while continue in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Oct 6, 2023
1 parent 0932d6a commit c9288b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/compiler/grammar/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,24 @@ impl Compiler {
token_info.line_num,
token_info.line_pos,
)?;
match (&state.block.btype, state.block_stack.last()) {
(Word::While, Some(prev_block)) => {
let mut found_while = 0;
for block in [&state.block]
.into_iter()
.chain(state.block_stack.iter().rev())
{
if let Word::While = &block.btype {
found_while += 1;
} else if found_while == 1 {
state
.instructions
.push(Instruction::Jmp(prev_block.last_block_start));
}
_ => {
return Err(token_info.custom(ErrorType::ContinueOutsideLoop));
.push(Instruction::Jmp(block.last_block_start));
found_while += 1;
break;
}
}
if found_while != 2 {
return Err(token_info.custom(ErrorType::ContinueOutsideLoop));
}
}

_ => {
Expand Down
15 changes: 15 additions & 0 deletions tests/stalwart/while.svtest
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ test "While - continue" {
let "j" "j+1";
}

if eval "j != 0 || i == 0" {
test_fail "while continue failed";
}

let "i" "0";
let "j" "0";

while "i < 5" {
let "i" "i+1";
if eval "true" {
continue;
}
let "j" "j+1";
}

if eval "j != 0 || i == 0" {
test_fail "while continue failed";
}
Expand Down

0 comments on commit c9288b6

Please sign in to comment.