← board

Stackless generator: allow yield inside a case statement

Problem

; generator; stackless; currently hard-rejects a yield that is lexically inside a case statement — only for/while/if nesting is allowed (see done/feature-generators-yield.md, the "Phase 4 (v2 stackless backend)" log entry: "yield-in-try/case/repeat/with is a clear compile error", documented as intentional for the initial cut, not lifted since).

Minimal repro:

program ReproCaseYield;

function Gen(n: Integer): Integer; generator; stackless;
begin
  case n of
    1: yield 10;
    2: yield 20;
  else
    yield 99;
  end;
end;

var x: Integer;
begin
  for x in Gen(1) do
    writeln(x);
end.
$ ./pxx --target=i386 repro_case_yield.pas out
pascal26:13: error: stackless generator: yield only allowed at top level or inside for/while/if (not in this construct) ()

(Error is reported at the compiler's own source line, not the user's file line/col — a secondary diagnostics-quality gap worth fixing alongside this.)

Why it matters

The default (stackful) generator backend only targets x86-64 (generator: only the x86-64 target is supported for the stackful backend (use stackless for other targets)), so any cross-target generator use must go through stackless. examples/chess/chess.pas's GenMoves — the flagship demo's move generator — dispatches on piece kind via case k of ... end with yield in every branch. This is an idiomatic, ordinary pattern (not managed types, not exception-adjacent), so the case restriction is the first and only wall standing between the chess demo and cross-target validation (perft byte-identical across i386/aarch64/arm32/riscv32/xtensa is the ticket's whole point). Likely blocks other cross-target generator code the same way.

Scope

Acceptance

Log