← board

Local typed constants (initialized const inside a routine)

Problem

A typed constant declared in a routine's const section is rejected:

pascal26:846: error: local typed constant not supported; use a const expression or a var

Repro (examples/chess/chess.pas, PieceGlyph):

function PieceGlyph(const pc: TPiece): Char;
const
  W: array[pkNone..pkKing] of Char = ('.', 'P', 'N', 'B', 'R', 'Q', 'K');
  B: array[pkNone..pkKing] of Char = ('.', 'p', 'n', 'b', 'r', 'q', 'k');
begin
  ...

Global typed constants (incl. array initializers) already work (feature-typed-const-arrays, 54d5dda) via the pending-init machinery that allocates storage and compiles the initializer as assignments before main begin. The local form hits an explicit "not supported" guard.

Direction

Acceptance

Resolution (2026-06-21, Track A)

Routine-local typed constants now lower as routine-scoped read-only initialized locals: ParseConstSection allocates storage via the existing AllocArray/ AllocVar (scope-aware from CurProc) and, when CurProc >= 0, records the element/scalar initializers into a new per-routine LocalInit* list (defs.inc) instead of the global PendingInit list. CompilePendingLocalInits emits those as assignment AST in the routine prologue — after managed-local zero-init, before the body — so the stack table is re-initialized on each call. Wired at both routine-body sites (ParseSubroutine and the unit __init_* section); reset at routine entry.

Also fixed a pre-existing gap that blocked the chess shape: ConstEval rejected a single-char string literal ('a', lexed as tkString); now a length-1 tkString evaluates to its character code, so array[..] of Char = ('.', 'P', ...) initializers work (globals too).