Skip to content

feat: Add struct initialization syntax#7

Open
fotcorn wants to merge 1 commit intomasterfrom
feat/struct-init-syntax
Open

feat: Add struct initialization syntax#7
fotcorn wants to merge 1 commit intomasterfrom
feat/struct-init-syntax

Conversation

@fotcorn
Copy link
Owner

@fotcorn fotcorn commented Jun 24, 2025

This commit introduces a new syntax for initializing structs directly upon declaration, e.g., var v: Vec = Vec{x:1, y:2};.

Key changes:

  • Updated grammar (xlang/grammar.lark) to parse the new syntax.
  • Added StructInitializer and StructInitializerMember AST nodes in xlang/xl_ast.py.
  • Modified ASTTransformer (xlang/transformer.py) to correctly construct these AST nodes, including a fix for collecting all members when multiple are present.
  • Updated Interpreter (xlang/interpreter.py) to handle the StructInitializer node, correctly assigning provided values and falling back to defaults.
  • Enhanced ValidationPass (xlang/validation_pass.py) to validate StructInitializer nodes, ensuring:
    • Correct types for initialized members.
    • All members without struct-defined defaults are initialized.
  • Added comprehensive lit tests for the new syntax in tests/lit/structs.xl, covering various initialization scenarios, default value handling, and member order.
  • Fixed minor parsing issues in test struct definitions by ensuring all members end with a comma as required by the grammar.

This commit introduces a new syntax for initializing structs directly
upon declaration, e.g., `var v: Vec = Vec{x:1, y:2};`.

Key changes:
- Updated grammar (`xlang/grammar.lark`) to parse the new syntax.
- Added `StructInitializer` and `StructInitializerMember` AST nodes
  in `xlang/xl_ast.py`.
- Modified `ASTTransformer` (`xlang/transformer.py`) to correctly
  construct these AST nodes, including a fix for collecting all
  members when multiple are present.
- Updated `Interpreter` (`xlang/interpreter.py`) to handle the
  `StructInitializer` node, correctly assigning provided values and
  falling back to defaults.
- Enhanced `ValidationPass` (`xlang/validation_pass.py`) to validate
  `StructInitializer` nodes, ensuring:
    - Correct types for initialized members.
    - All members without struct-defined defaults are initialized.
- Added comprehensive lit tests for the new syntax in
  `tests/lit/structs.xl`, covering various initialization scenarios,
  default value handling, and member order.
- Fixed minor parsing issues in test struct definitions by ensuring
  all members end with a comma as required by the grammar.
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