feat: add break statement support with loop tracking and error handling
Add TOKEN_BREAK keyword recognition and a loopBody field to the compiler to track the innermost loop's first instruction index. The break statement emits a jump to the end of the current loop, and compile-time errors are reported when break appears outside a loop body, inside a function defined within a loop, or inside a method defined within a loop. New test files verify correct behavior for break in while loops, nested loops, and error cases for invalid break usage.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
var done = false
|
||||
while (!done) {
|
||||
fn {
|
||||
break // expect error
|
||||
}
|
||||
done = true
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
var done = false
|
||||
while (!done) {
|
||||
class Foo {
|
||||
method {
|
||||
break // expect error
|
||||
}
|
||||
}
|
||||
done = true
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
var i = 0
|
||||
while (true) {
|
||||
i = i + 1
|
||||
IO.write(i)
|
||||
if (i > 2) {
|
||||
// TODO: Should not require block for break.
|
||||
break
|
||||
}
|
||||
IO.write(i)
|
||||
}
|
||||
// expect: 1
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
@@ -0,0 +1,31 @@
|
||||
var i = 0
|
||||
while (true) {
|
||||
IO.write("outer " + i.toString)
|
||||
if (i > 1) {
|
||||
// TODO: Should not require block for break.
|
||||
break
|
||||
}
|
||||
|
||||
var j = 0
|
||||
while (true) {
|
||||
IO.write("inner " + j.toString)
|
||||
if (j > 1) {
|
||||
// TODO: Should not require block for break.
|
||||
break
|
||||
}
|
||||
|
||||
j = j + 1
|
||||
}
|
||||
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
// expect: outer 0
|
||||
// expect: inner 0
|
||||
// expect: inner 1
|
||||
// expect: inner 2
|
||||
// expect: outer 1
|
||||
// expect: inner 0
|
||||
// expect: inner 1
|
||||
// expect: inner 2
|
||||
// expect: outer 2
|
||||
@@ -0,0 +1 @@
|
||||
break // expect error
|
||||
Reference in New Issue
Block a user