1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 08:55:38 +00:00

Language: struct literals

struct Vec2 { x: u8, y: u8 }
    var pos: Vec2 = Vec2 { x: 100, y: 50 }
    on frame {
        pos = Vec2 { x: pos.x + 1, y: pos.y }
    }

- AST: new \`Expr::StructLiteral(name, fields, span)\` variant
- Parser: in expression position, \`Ident {\` enters struct-literal
  mode when the new \`restrict_struct_literals\` flag is off.
  \`if\`/\`while\`/\`for\` conditions set the flag so the \`{\` keeps
  going to the following block. Condition contexts can still use
  struct literals by parenthesizing them.
- Analyzer: validates that the struct type exists, each named field
  belongs to it, and each field value has a compatible type.
- IR lowering: desugars \`var = StructLiteral { ... }\` (both in
  assignments and variable initializers) into per-field StoreVar
  operations against the analyzer-synthesized \`var.field\`
  variables. No IR type for struct values is needed.
- AST codegen: no-op (legacy path).
- examples/structs_enums_for.ne now uses a struct literal for the
  initial \`player\` state instead of per-field assignments.

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 17:15:57 +00:00
parent f17f1e7267
commit c8ae433a7c
No known key found for this signature in database
9 changed files with 221 additions and 13 deletions

View file

@ -228,6 +228,26 @@ fn program_with_for_loop() {
rom::validate_ines(&rom_data).expect("should be valid iNES");
}
#[test]
fn program_with_struct_literals() {
let source = r#"
game "Lit" { mapper: NROM }
struct Vec2 { x: u8, y: u8 }
var pos: Vec2 = Vec2 { x: 10, y: 20 }
on frame {
pos = Vec2 { x: 100, y: 50 }
if button.right {
pos = Vec2 { x: pos.x + 1, y: pos.y }
}
draw Smiley at: (pos.x, pos.y)
wait_frame
}
start Main
"#;
let rom_data = compile(source);
rom::validate_ines(&rom_data).expect("should be valid iNES");
}
#[test]
fn program_with_structs() {
let source = r#"