Adds the statement program compiler - #157
Merged
Merged
Conversation
Adds compileProgram, compileProgramWithPositions and compileProgramWithSpans: the reference's statement grammar at v9.4.2 (assignment to an identifier, a property or an index, the separator, if/else and else if, while, bare expressions) parsed by parseProgram and emitted by the emitter's program walk. Each answers the result union of its expression counterpart; the located variants add a segment table per store. Every program row of the compile transcript is reproduced exactly, read through the stamped reader. ParseReason gains unexpected_else, unassignable_location and expected_open_brace. The compiler-surface record carries a proposed Amendment; the expression entry points and the conformance registry are unchanged. Refs: pts-xmz6
The Amendment's first paragraph named the commit the branch was cut from, which a rebase made false. The clause is removed; the transcript citation at its stated commit stands. Refs: pts-xmz6
johnnyt
force-pushed
the
pts-xmz6-compile-program-statement-grammar
branch
from
October 1, 2026 12:27
24e3b66 to
8aeb63f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The package compiled the expression grammar only. A statechart's script element holds a statement program, and the reference compiles one through
compile_program/1,compile_program_with_positions/1andcompile_program_with_spans/1. The lexer already tokenized the separator and the three statement keywords and the evaluator already ranstore,popand the jumps; the grammar and the emission were missing. The scope is the reference's full statement grammar, and its evidence is the program rows of the compile transcript taken from the reference atv9.4.2(both ruled by the operator, 2026-10-01).What
compileProgram,compileProgramWithPositionsandcompileProgramWithSpans, exported from the main entry and pinned intest/export-surface.json. Each answers its expression counterpart's result union; the two located variants carry one more member on the succeeding arm,segmentPositionsorsegmentSpans, keyed by eachstore's index with one annotation per location segment (the reference'ssegment_positions). A refusal is aParseErrorvalue, never a throw.parsePrograminsrc/parser.ts: statement sequences with an optional trailing separator, assignment to an identifier, a property access or a bracket access (probed as an additive expression, refused at the=otherwise),if/else/else if,while, bare expressions, and no separator needed after a statement ending in}.emitPrograminsrc/emitter.ts: the reference's instructions visitor for statements - location segments root first,store n,popafter a bare expression, the relative jumps ofif,if/elseandwhile- with the positions, spans and segment tables built in one walk. Astoreis positioned at the location's root and spanned over the whole assignment, as the reference annotates it.ParseReasongainsunexpected_else,unassignable_locationandexpected_open_brace. The program grammar's other two wordings map to members already in the union:Unexpected token X after statementtotrailing_token, andExpected '}' to close the block but found Xtoexpected_close_brace.else iflinks count against the declared source limit in both walks; a statement sequence's length costs no depth.compileProgramexample (executed by the README suite) and drops the sentence saying statement compilation was not implemented. Changelog fragmentchangelog.d/pts-xmz6.md(Added).compile,compileWithPositions,compileWithSpans,parse,evaluate,execute,executeValueanddecompileanswer as before;conformance/registry.json, the vendored corpus and the transcript are untouched.Evidence
test/reference-compile.test.tsreads everyprogramrow through the stamped reader and holdscompileProgramWithSpansto it exactly: the instruction list compared as values, the spans table and the segment table entry by entry. Everyprogram_refusalrow is held tocompileProgram's message (verbatim), position and span, with the reason a member of the closed union.Predicator.compile_program_with_positions/1in a detached export of predicator-ex atv9.4.2over every program the rows hold; every table agreed.test/compile-program.test.tspins a selection of them, each quoted as the run printed it, plus one source per refusal family pinned to its reason, the depth bound, and the unchanged expression entry points.jumpending a then block, thepopafter a bare expression, or thewhileback edge each turned the program-row suite red on an assertion; so did astorespanned from the=, a bracket segment annotated with its access node, astorepositioned at the=, a property segment annotated with its root, rewording the stray-else or the unassignable-location message, refusing a trailing separator inside a block, and giving the strayelseor the unterminated block another reason. Removing the parser's block-level depth count alone survived, because the emitter still refuses; removing it together with the emitter's level perifturned the depth test red, and the same held for theelse ifchain.Provenance
Engineering choices made in this change, each recorded in the Amendment:
segmentPositions/segmentSpans) rather than a reshapedpositions, so each located result is a subtype of its expression counterpart. No name is exported for the two result types; no new type name joins the entry.else ifchain counts a level per link, since the tree nests it; the Amendment declares this as the one flat construct of the program grammar whose length is bounded.Notes
pnpm run gateis green on the rebased head (typecheck, lint, engine neutrality, the suite with coverage, corpus check, build, cross-entry identity, legacy resolution). The whole output is on the bead rather than here, because it prints local paths.Closes pts-xmz6