Loop primitive: NEXT FROM extension
This compiler adopts a non-standard loop primitive borrowed from CLC-INTERCAL, under the same rationale as our Label 666 syscall extension: the standard does not provide what a real self-hosted compiler needs, and a single, surgical extension is preferable to the multi-month "abstain dance" scaffolding option.
The primitive lands in stage3 (src/compiler/stage3.i) and downstream code
generated by it. It does not affect the bootstrap compiler (intercalc.sh) or
the bytecode tier.
Decision
After evaluating the three options laid out in docs/stage3-roadmap.md:
- Option A (
(label)*while-abstain extension): requires a new postfix syntax, a new runtime counter mechanism, and bespoke codegen for an "iterate with abstain bound" semantics that has no precedent in any existing INTERCAL. - Option B (NEXT FROM, from CLC-INTERCAL): existing precedent in another well-known dialect, reuses existing label and branch machinery, requires one new statement form.
- Option C (scaffolding pattern, ~30 statements per logical loop): no compiler change, but stage3.i would balloon to thousands of lines and the resulting compiler would be slow and unreadable.
We adopt Option B with a small refinement: the conditional variant
(label) NEXT FROM <expression> makes loops directly expressible without
recursing into the abstain-dance problem (where conditionally toggling
ABSTAIN itself needs the same kind of conditional jump).
Syntax
Two new statement forms are accepted:
DO (LABEL) NEXT FROM
DO (LABEL) NEXT FROM <expr>
Both forms behave like an unconditional or conditional backward b to the
labelled statement at (LABEL). Neither form pushes onto the NEXT stack;
neither needs a matching RESUME.
<expr> is any standard 16-bit INTERCAL expression. The conditional form
jumps to (LABEL) if and only if bit 0 of <expr> is set. Bit 0 is the
canonical "boolean test" position, matching the existing zero-test idiom
'.X ~ .X' ~ #1.
The unconditional form is exactly equivalent to (LABEL) NEXT FROM #1.
Semantics
(LABEL) NEXT FROM <expr> is operationally:
- Evaluate
<expr>to a 16-bit valueV. - If
V & 1 == 1, transfer control to the statement labelled(LABEL)without modifying the NEXT stack. - Otherwise, fall through to the next statement.
This is a pure intra-procedural conditional branch. It does not interact with the NEXT/RESUME stack at all, so it does not consume a stack slot (no E123 risk) and does not produce an entry to RESUME from.
It can be the target of ABSTAIN FROM (label) like any other statement
(the label here being the optional prefix on the NEXT FROM line itself),
and it participates normally in the politeness count.
Why this design
- Backward branches without stack manipulation. Standard
NEXTpushes, so a tight loop overflows the 79-entry stack on the 80th iteration.NEXT FROMdoes not push. - Conditional in a single statement. The standard "computed RESUME" idiom
only unwinds to a parent frame and fails at top level
(verified empirically; see
memory/bugs_learned.mdanddocs/intercal_patterns.md). The conditional NEXT FROM gives us a working in-frame conditional branch. - CLC-INTERCAL precedent. Adopting from an existing dialect keeps the compiler in the family of known INTERCAL extensions rather than inventing a one-off design.
- Minimal codegen. Each NEXT FROM is one ARM64
cbnz/bplus a label; no new runtime function, no new BSS, no new error code.
Codegen sketch
For ARM64 (other platforms by translation):
; DO (LABEL) NEXT FROM
b _stmt_LABEL_N
; DO (LABEL) NEXT FROM <expr>
<emit-expression-into-w0>
tbnz w0, #0, _stmt_LABEL_N
tbnz (test bit and branch if non-zero) is the natural primitive for
"branch if bit 0 is set". The fall-through is the next statement.
What this lets stage3 do
Once NEXT FROM is wired up, the substages laid out in
docs/stage3-roadmap.md become straightforward:
- Byte-by-byte tokeniser. A counter
.poswalks from 0 to source length; each iteration probes the byte at,65535 SUB .posand records a token into a parallel array. Loop test:'(.pos - .len) ~ (.pos - .len)' ~ #1is non-zero while there is more source. - Statement classifier. One pass over the token array, advancing while inside a statement, emitting one record at each statement boundary.
- Politeness check. One pass counting PLEASE statements; reject if the ratio is out of range.
- Codegen. One pass over the statement table dispatching by type.
Each pass is one NEXT FROM at the end of a body block. The body block itself is open to use any standard INTERCAL constructs, including syslib calls, STASH/RETRIEVE, and (with care) inner NEXT FROM loops.
Compatibility
NEXT FROM is non-standard. INTERCAL programs intended to be portable to
ick/CLC-INTERCAL must not use it. We document the extension here so a
later port to another compiler is a known undertaking, not a surprise.
The bootstrap compiler (intercalc.sh) gains a small parse and codegen
case for NEXT FROM; existing programs that do not use it are unaffected.
tools/lint_intercal.sh is taught to recognise it so it does not flag
the new form as an unrecognised statement.
Test plan
A single new test file tests/test_next_from.i exercises:
- Unconditional NEXT FROM: a 5-iteration counter loop.
- Conditional NEXT FROM #1: same as unconditional.
- Conditional NEXT FROM with computed expression: scan a fixed-content array for a target byte; loop until found; READ OUT the index.
- ABSTAIN FROM the NEXT FROM line: confirm the loop is broken.
Test registered in tests/run_tests.sh and tests/run_self_tests.sh.
Roadmap relation
This document satisfies task #121 in the post-v0.1.0 work plan and unblocks #122 (substage 1 tokeniser). Implementation order:
- This doc (now).
- intercalc.sh codegen for NEXT FROM (next).
- One regression test demonstrating the loop in a small program.
- stage3 substage 1 tokeniser using NEXT FROM.