Home /Modules/docs/EXTENDED-FOR-LOOPS.md

EXTENDED-FOR-LOOPS.md

docs/EXTENDED-FOR-LOOPS.md · 67 line(s) · markdown

Extended for Loops: Thread and VM-Heap Health

Status: parser, AST, semantic Boolean checks, and bytecode lowering are implemented on this feature branch. Runtime health signals remain supplied by the caller/VM host; the compiler does not fabricate thread or heap state. Regression builds and end-to-end tests are still required before merge.

Compatibility

The existing three-term form remains unchanged:

for (let i = 0; i < limit; i = i + 1) {
    work(i);
}

The extended forms append one or two Boolean expressions, separated by semicolons:

// Four terms: standard iterator + thread-loop stability signal.
for (let i = 0; i < limit; i = i + 1; threadStable) {
    work(i);
}

// Five terms: thread stability + VM GC/heap health.
for (let i = 0; i < limit; i = i + 1; threadStable; heapHealthy) {
    work(i);
}

Grammar sketch:

forStatement = "for", "(", init, ";", condition, ";", update,
               [ ";", threadStable, [ ";", heapHealthy ] ],
               ")", statement ;
threadStable = expression ;  (* must have Boolean type *)
heapHealthy  = expression ;  (* must have Boolean type *)

The first three terms retain their current meaning. The fourth and fifth terms are Boolean guard expressions evaluated before each body execution, after the loop initializer and before entering the body. A false health guard exits the loop normally unless an explicit runtime policy says otherwise. Health probes must not be evaluated more than once per iteration.

Runtime contract

Required implementation checklist

  1. Lexer: no new reserved word is required if guards are ordinary expressions; preserve semicolon tokenization.
  2. Parser/AST: accept optional fourth and fifth terms and store them distinctly from the standard condition/update.
  3. Semantic analysis: require Boolean types for both guards and enforce the selected syntax-version gate.
  4. Code generation / VM: evaluate guards once at the top of each iteration; connect heap health to a documented runtime probe and flag/exception policy.
  5. Tests: preserve three-term behavior and cover four-/five-term parsing, Boolean type errors, false guards, exception/flag behavior, break, continue, and version rejection.
  6. Documentation: update SLEELA.syntax, the syntax version table, and compiler/VM compatibility notes only when the end-to-end implementation is available.

Suggested regression cases