diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-19 00:33:17 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-19 00:33:17 +0200 |
| commit | 147153bf6aadd52d32f6beffc17f610c837ce17f (patch) | |
| tree | 8041cea988dc11bc0cefeedd7d895fe9bdb02274 /examples | |
| parent | 7a0407c03a0af18a38fcea4312796f18a276e541 (diff) | |
Implement break and next for while/until loops
TT_BREAK and TT_NEXT were already tokenised; this wires them up:
- _program(): guard statement loop with ct == CONTROL_NONE so a break/next
flag set inside a loop body stops block execution and propagates upward
- _control(): add TT_BREAK and TT_NEXT cases that set CONTROL_BREAK /
CONTROL_NEXT on p_interpret->ct and return immediately
- while/until loop: replace the commented-out switch with active if/else
logic that clears the flag and either stops iteration (break) or lets
the loop re-evaluate its condition (next)
Add examples/break_next.fy to exercise both keywords in while and until
loops; expected output: 5 / 12 / 7.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/all-examples.txt | 26 | ||||
| -rw-r--r-- | examples/break_next.fy | 25 |
2 files changed, 51 insertions, 0 deletions
diff --git a/examples/all-examples.txt b/examples/all-examples.txt index f71e8a2..5e0f13e 100644 --- a/examples/all-examples.txt +++ b/examples/all-examples.txt @@ -32,6 +32,32 @@ assert 1 == (say 1 :< 5 :> 5 or 2 and (5 xor 8)); assert (neg 1) == (say neg not 0); +# break exits the while loop early when i reaches 5 +my i = 0; +while i < 10 { + i = i + 1; + if i == 5 { break; } +} +say i; # expected: 5 + +# next skips adding j when j == 3, so sum = 1+2+4+5 = 12 +my sum = 0; +my j = 0; +while j < 5 { + j = j + 1; + if j == 3 { next; } + sum = sum + j; +} +say sum; # expected: 12 + +# break inside an until loop stops when k reaches 7 +my k = 0; +until k == 10 { + k = k + 1; + if k == 7 { break; } +} +say k; # expected: 7 + #* * Simple examples how to write comments *# diff --git a/examples/break_next.fy b/examples/break_next.fy new file mode 100644 index 0000000..bd060f8 --- /dev/null +++ b/examples/break_next.fy @@ -0,0 +1,25 @@ +# break exits the while loop early when i reaches 5 +my i = 0; +while i < 10 { + i = i + 1; + if i == 5 { break; } +} +say i; # expected: 5 + +# next skips adding j when j == 3, so sum = 1+2+4+5 = 12 +my sum = 0; +my j = 0; +while j < 5 { + j = j + 1; + if j == 3 { next; } + sum = sum + j; +} +say sum; # expected: 12 + +# break inside an until loop stops when k reaches 7 +my k = 0; +until k == 10 { + k = k + 1; + if k == 7 { break; } +} +say k; # expected: 7 |
