Skip to content

add ast-dump subcommand and parser parity tests#76

Merged
cs01 merged 1 commit intomainfrom
worktree-parser-parity
Mar 2, 2026
Merged

add ast-dump subcommand and parser parity tests#76
cs01 merged 1 commit intomainfrom
worktree-parser-parity

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 2, 2026

Add ast-dump subcommand and parser parity tests

Summary

Add ast-dump subcommand to both JS and native compilers that dumps the parsed AST as JSON. This enables direct comparison of parser output between the TypeScript and native implementations.

Also fix JSON.stringify to handle type assertions by unwrapping the inner expression before stringification.

Changes

New Features

  • ast-dump subcommand: New command available in both chad build and ./.build/chad that takes a source file and outputs its AST as JSON
    • JS implementation (src/chad-node.ts): Uses parseWithTSAPI from parser-ts
    • Native implementation (src/chad-native.ts): Uses parseFileToAST exported from native-compiler-lib.ts
  • Parser parity test suite (tests/ast-parity.test.ts): Auto-generated test that compares ASTs from both parsers

Bug Fixes

  • JSON.stringify type assertions (src/codegen/stdlib/json.ts): Unwrap type assertions before stringification so that JSON.stringify(x as MyType) correctly stringifies the inner expression

Build/Test

  • Added npm run test:parity script to run parser parity tests

Files Changed

  • package.json - Added test:parity npm script
  • src/chad-node.ts - Added ast-dump subcommand and handler
  • src/chad-native.ts - Added ast-dump subcommand and handler
  • src/native-compiler-lib.ts - Added parseFileToAST() export with manual JSON construction for native code
  • src/codegen/stdlib/json.ts - Fix type assertion handling in stringify
  • tests/ast-parity.test.ts - New parser parity test suite
  • tests/fixtures/builtins/json-stringify-mixed-vars.ts - Test fixture
  • tests/fixtures/builtins/json-stringify-type-assertion.ts - Test fixture

Test Plan

  • Run npm run test:parity to verify parser outputs match
  • Run npm test to verify all unit tests pass
  • Run npm run verify:quick to verify self-hosting works

@cs01 cs01 merged commit 4eab828 into main Mar 2, 2026
12 checks passed
@cs01 cs01 deleted the worktree-parser-parity branch March 2, 2026 06:51
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