Skip to content

Formalize AST format - #11

Merged
liontiger23 merged 11 commits into
mainfrom
ast-format
Sep 21, 2026
Merged

liontiger23 merged 11 commits into
mainfrom
ast-format

Conversation

@liontiger23

Copy link
Copy Markdown
Member
  • Add schema for tokens.json and ast.json
  • Add proper documentation for JSON formats to both grammar files and separate document
  • Fix line/column in AST goldens
  • Fix top-level declaration in bnf for later grammars

…hema validation

- meta.schema.json now requires 'grammar', 'stages', 'exit' properties
- Create test/validate_schemas.py — validates config.json and all meta.json
  files against their schemas, with clear error messages
- Add schema validation step to both CI workflows (ci.yml, tests.yml)
  before running tests, ensuring invalid meta.json is caught early
Add tokens.schema.json to validate test tokens.json files.
Mandatory fields: kind (string), line (integer >=1), column (integer >=1).
Optional field: value (string).
The validate_schemas.py script now also loads tokens.schema.json and
validates every tokens.json found under test/. The schema requires:
  - kind (string), line (integer >=1), column (integer >=1) — mandatory
  - value (string) — optional

This runs in CI as part of the 'Validate JSON schemas' step in both
ci.yml and tests.yml workflows.
- Create ast.schema.json for AST tree validation: each node requires
  line, column, kind; allows any other fields (value, elems, body, …).
- Update tokens.schema.json: remove 'value' from properties (no longer
  tracked in schema), remove 'additionalProperties: false' so any token
  fields are accepted. Only 'kind', 'line', 'column' remain required.
Each AST node now has:
- elems: ordered list of child nodes (previously in named fields like
  body, left, right, cond, then, else, init, etc.)
- value: scalar payload (name, op, message, field, type, literal value)
  or null for nodes without one
- Extra fields (mut, type, returnType, etc.) preserved
All ast.json golden files regenerated using the fixed compiler that:
- Removes AST_EXPR_STMT wrapper from expression statements
- Produces correct elems arrays per documented format
  (Declare: [name, init], FuncDecl: [name, params..., body],
   ExternDecl: [name, params...], StructDecl: [name, fields...],
   Param: [name], FieldAccess: [base, name])
- Reports correct line/column positions (first token of each node)
- Drops the value field (not compared during golden validation)
Verify that top-level declarations (extern, def, struct) can appear
in any order at the top level:

- grammar3: def and extern interleaved
- grammar4: def and extern with typed params/return types
- grammar5: struct, extern, and def all interleaved
The run_tests.py output format recently changed:
- list command now groups tests under === STAGE === headers with
  indented names (no stage/ prefix)
- test/update commands now group results under === STAGE === headers
  and print names without stage prefix
- config stage key is 'compiler' not 'compile'

Also fix test_compiler_update_regenerates_and_exit_warning to
produce non-empty stdout so the golden file is written.

All 54 self-tests now pass.
@liontiger23
liontiger23 merged commit 7daa441 into main Sep 21, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant