[BUG] break and continue don't work as expected
AkramIzz opened this issue · comments
Describe the bug
break
and continue
don't work if they are not direct children of the for loop body AST
To Reproduce
Steps to reproduce the behavior:
1- Run the following birb program
for (int j = 0; j < 5; j++) {
if (j == 2) {
break;
}
screm(j);
}
for (int k = 0; k < 5; k++) {
if (k == 3) {
continue;
}
screm(k);
}
Actual Results
first loop outputs 0 1 2 3 4
second loop outputs 0 1 2 3 4
Expected behavior
The first loop should output 0 1
The second loop should output 0 1 2 4
Additional context
The way this should be handled is by throwing an exception in visitBreak
and visitContinue
. The exception should be caught and handled by the loops (for and while). These exceptions aren't an indication of an error, but rather used as a flow control to unwind the call stack to the loops handling functions.
Done