From e1b9116c18cc3f5dae14aa99263650eca2e6a9ed Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 19 Feb 2026 00:42:29 +0200 Subject: Implement loop (infinite) and do...while/until loops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Completes the 'loop, next, break, do' TODO entry — break and next were landed in the previous commit; this adds the remaining two forms: - loop { body } — infinite loop; the only exit is break. Simpler than while/until since there is no condition expression to evaluate. - do { body } while expr; / do { body } until expr; — post-condition loop: body always executes at least once, condition is checked at the bottom of each iteration. Condition tokens are collected until ';' (mirrors _expression_get but stops at semicolon instead of '{'), then replayed each iteration using the same temp-stack/temp-iterator technique as while/until. Both break and next are supported. Token changes: TT_LOOP and TT_DO added to the keyword enum in token.h, registered in get_tt() and tt_get_name() in token.c. Add examples/loop_do.fy; expected output: 5 / 12 / 11 / 5. Co-Authored-By: Claude Sonnet 4.6 --- examples/all-examples.txt | 33 +++++++++++++++++++++++++++++++++ examples/loop_do.fy | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 examples/loop_do.fy (limited to 'examples') diff --git a/examples/all-examples.txt b/examples/all-examples.txt index 5e0f13e..6596434 100644 --- a/examples/all-examples.txt +++ b/examples/all-examples.txt @@ -263,6 +263,39 @@ put 20; ln; +# loop — infinite loop, break exits after 5 iterations +my i = 0; +loop { + i = i + 1; + if i == 5 { break; } +} +say i; # expected: 5 + +# loop with next — skips j==3, so sum = 1+2+4+5 = 12 +my sum = 0; +my j = 0; +loop { + j = j + 1; + if j > 5 { break; } + if j == 3 { next; } + sum = sum + j; +} +say sum; # expected: 12 + +# do...while — body runs once even though k >= 10 already +my k = 10; +do { + k = k + 1; +} while k < 10; +say k; # expected: 11 + +# do...until — stops when m reaches 5 +my m = 0; +do { + m = m + 1; +} until m == 5; +say m; # expected: 5 + #* * Examples of how to use procedures *# diff --git a/examples/loop_do.fy b/examples/loop_do.fy new file mode 100644 index 0000000..60cfa5a --- /dev/null +++ b/examples/loop_do.fy @@ -0,0 +1,32 @@ +# loop — infinite loop, break exits after 5 iterations +my i = 0; +loop { + i = i + 1; + if i == 5 { break; } +} +say i; # expected: 5 + +# loop with next — skips j==3, so sum = 1+2+4+5 = 12 +my sum = 0; +my j = 0; +loop { + j = j + 1; + if j > 5 { break; } + if j == 3 { next; } + sum = sum + j; +} +say sum; # expected: 12 + +# do...while — body runs once even though k >= 10 already +my k = 10; +do { + k = k + 1; +} while k < 10; +say k; # expected: 11 + +# do...until — stops when m reaches 5 +my m = 0; +do { + m = m + 1; +} until m == 5; +say m; # expected: 5 -- cgit v1.2.3