From e2c65706e61be09ddb7eeb28cd1957acd4317bcf Mon Sep 17 00:00:00 2001 From: _ov4 Date: Fri, 16 Oct 2026 10:09:47 +0000 Subject: feat(constants): implement literal constants with lexical scope Co-Authored-By: Claude Fable 5.1 --- AGENTS/ARCH.md | 3 ++- AGENTS/CONVENTIONS.md | 19 +++++++++++++------ AGENTS/EXTENDING.md | 3 ++- AGENTS/PITFALLS.md | 8 +++++--- AGENTS/SUBSYSTEMS.md | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) (limited to 'AGENTS') diff --git a/AGENTS/ARCH.md b/AGENTS/ARCH.md index 4fab3ee..ffde252 100644 --- a/AGENTS/ARCH.md +++ b/AGENTS/ARCH.md @@ -264,9 +264,10 @@ do not modify the real release for local verification. | `lib/Parser/ParserExpression.cc`, `ParserControlFlow.cc` | Expressions/designators and control-flow statement parsing (including ordered CASE branches) | | `lib/Parser/ParserFunction.cc` | FUNCTION/PROCEDURE/CALL/RETURN + parameter-list parsing | | `lib/Parser/ParserFile.cc` | OPENFILE/READFILE/WRITEFILE/CLOSEFILE parsing | -| `lib/Parser/ParserType.cc` | Type names, variable/array declarations, TYPE/ENDTYPE and CLASS/ENDCLASS declarations, shared field parsing, SET/DEFINE reserved stubs | +| `lib/Parser/ParserType.cc` | Type names, variable/array/constant declarations, TYPE/ENDTYPE and CLASS/ENDCLASS declarations, shared field parsing, SET/DEFINE reserved stubs | | `lib/CodeGen/CodeGen.cc` | Initialization, symbols, call arguments and main() compilation | | `lib/CodeGen/ExpressionGen.cc`, `StatementGen.cc`, `ControlFlowGen.cc` | Expression/statement dispatch and control-flow emission | +| `lib/CodeGen/ConstantGen.cc` | Literal constants, declaration conflicts and lexical binding restoration | | `lib/CodeGen/ValueTypeGen.cc`, `OutputGen.cc`, `Builtins.h` | Expression typing, coercion/declarations, scalar output and the shared builtin table | | `lib/CodeGen/Designator.cc`, `TypeCheckGen.cc` | TYPE pre-pass and lvalue access; type admission and user-kind binary operators | | `lib/CodeGen/CTypeGen.cc` | CodeGen methods for native C scalar conversions, casts, arithmetic and input/output | diff --git a/AGENTS/CONVENTIONS.md b/AGENTS/CONVENTIONS.md index 5b16242..93c6f24 100644 --- a/AGENTS/CONVENTIONS.md +++ b/AGENTS/CONVENTIONS.md @@ -93,12 +93,19 @@ only `realloc`); `CodeGen` also keeps its own `MallocFunc` for the char→STRING and INPUT STRING buffers — declared in `SetupExternalFunctions`, never fetched with a bare `getFunction("malloc")`. -**One flat symbol namespace, one table.** `Symbols` (name → `SymbolInfo{Storage, -TypeName, IsArray}`) in CodeGen is the only symbol table; register through -`registerSymbol()` (or directly, as `FunctionGen::createArgumentAllocas` does for -parameters) and read storage through `getNamedValue()`/`getSymbolInfo()`. There is no -scope stack. `FunctionGen` saves/clears/restores the table around a function body, but -`ArrayHandler::ArrayTable` is *not* scoped (see `PITFALLS.md`). +**One symbol namespace, one table.** `Symbols` (name → `SymbolInfo{Storage, +TypeName, IsArray, ConstantValue, ScopeID}`) in CodeGen is the only symbol table. +A non-null ConstantValue denotes a read-only CONSTANT with no Storage. Read it +through `emitExpr`; all writable/address-taking paths must reject it through +`requireWritable`. Register variables through `registerSymbol()` (parameters are +also bound directly by FunctionGen). `prepareSymbolDeclaration` diagnoses +same-scope conflicts involving constants before declaration emission. Unique +scope IDs distinguish sibling blocks; `emitScopedBlock` restores only bindings +introduced by constants or hiding constants, preserving other variables' existing +function-wide visibility. FunctionGen saves/restores Symbols and inherits only +previously visible top-level constants (ScopeID 0). CodeGen isolates constant +scope bookkeeping and array metadata around each function body. Arrays also +restore metadata at control-flow block exits. **Dispatch is by `dynamic_cast` chain, not virtual methods.** `CodeGen::emitStmt` and `emitExpr` walk a linear if-chain of casts. **A statement type missing from the chain is diff --git a/AGENTS/EXTENDING.md b/AGENTS/EXTENDING.md index d03c088..5e104c8 100644 --- a/AGENTS/EXTENDING.md +++ b/AGENTS/EXTENDING.md @@ -9,7 +9,8 @@ unique_ptr children, move ctor, raw-ptr getters) → `ParseXxx` in the matching `Parser*.cc` file + branch in `ParseStatementImpl` (block statements parse their body with `ParseBlock({terminator tokens})`; control-flow parsers live in `ParserControlFlow.cc`; comma-separated expression lists use `ParseExprList(close, allowEmpty)`; add the new statement's leading keyword to -`syncToStatementStart`) → `dynamic_cast` branch in `CodeGen::emitStmt`. Guard every +`syncToStatementStart`) → `dynamic_cast` branch in `CodeGen::emitStmt`. Emit statement bodies through `emitScopedBlock` +so constants cannot escape their lexical block. Guard every fall-through branch in control-flow codegen with `if (!Builder->GetInsertBlock()->hasTerminator())` — a RETURN in the body may have already terminated the block. diff --git a/AGENTS/PITFALLS.md b/AGENTS/PITFALLS.md index 93564f1..58934ce 100644 --- a/AGENTS/PITFALLS.md +++ b/AGENTS/PITFALLS.md @@ -35,9 +35,11 @@ test programs. - **`getExprTypeInfo` returns nullptr** for expressions it can't classify (null-check it at any new call site) and internally defaults to INTEGER for arithmetic binaries and unknown-function calls; OUTPUT reports an error for a nullptr result. +- **CONSTANT** accepts only literals (including negative numeric literals), never expressions or other names. Constants have lexical block scope and may shadow outer names; bindings hiding constants are restored on block exit too. Only previously declared top-level constants are inherited by functions. Constants have no writable storage: assignment, INPUT, READFILE targets, FOR variables, BYREF and address-of are rejected. - **CASE OF** reads its selector once and tests expression labels in source order, with inclusive TO ranges and an optional final OTHERWISE. Label headers occupy their own source line; bodies may begin after the colon or on subsequent lines. Comparisons reuse ordinary operator rules; records and whole arrays are unsupported. -- The lexer emits no newline tokens; the grammar is purely keyword-delimited. Bare - `RETURN` is recognized before block terminators, including CATCH and ENDTRY. +- The lexer emits no newline tokens; most grammar is keyword-delimited, while CASE + uses token line information to recognize labels. Bare `RETURN` is recognized + before block terminators and the next CASE label. - `verifyModule` runs at the end of `compile()`; invalid modules make the driver exit non-zero without writing IR. - The FOR loop variable must be pre-DECLAREd as INTEGER; FOR reuses that alloca. The @@ -101,7 +103,7 @@ test programs. ## Not implemented (9618 features) -CONSTANT, SET types (`SET`/`DEFINE` are reserved with dedicated errors), +SET types (`SET`/`DEFINE` are reserved with dedicated errors), random files (OPENFILE...FOR RANDOM/SEEK/GETRECORD/PUTRECORD — TYPE is done, so these are unblocked now; the RANDOM keyword is reserved and gives a dedicated parse error), DATE, INPUT into array elements, string escape sequences, diff --git a/AGENTS/SUBSYSTEMS.md b/AGENTS/SUBSYSTEMS.md index f693b1c..c553f8e 100644 --- a/AGENTS/SUBSYSTEMS.md +++ b/AGENTS/SUBSYSTEMS.md @@ -34,7 +34,7 @@ map (`FunctionGen::getSignature`); `CodeGen::marshalCallArgs` dispatches call ar on those *declared* `ParamSig`s, and `getExprTypeInfo` reads return types from the same map. Top-level prototypes are pre-registered at the start of `compile()` (after the TYPE pre-pass), so calls may precede definitions; calling a name with no signature is a -compile error (no extern is fabricated). Body emission is inverted: CodeGen injects a +compile error (no extern is fabricated). Functions inherit previously declared top-level CONSTANT values; local constants use the same Symbols table with lexical binding restoration. Body emission is inverted: CodeGen injects a `StmtEmitter` callback, and must restore its own insert point (and the exchanged ArrayTable) afterwards. -- cgit v1.3.1-4-g156e