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:
parent
f17f1e7267
commit
c8ae433a7c
9 changed files with 221 additions and 13 deletions
|
|
@ -28,7 +28,16 @@ struct Player {
|
|||
alive: bool,
|
||||
}
|
||||
|
||||
var player: Player
|
||||
// Struct literal initializer in declaration.
|
||||
var player: Player = Player {
|
||||
x: 120,
|
||||
y: 112,
|
||||
vx: 0,
|
||||
vy: 0,
|
||||
facing: Down,
|
||||
frame: Idle,
|
||||
alive: true,
|
||||
}
|
||||
|
||||
// A small fixed-size array of enemy x-positions. In a real game this
|
||||
// would be an array of structs once those are supported.
|
||||
|
|
@ -38,15 +47,6 @@ var enemy_y: u8 = 100
|
|||
const SPEED: u8 = 1
|
||||
|
||||
on frame {
|
||||
// Initialize the player once on the very first frame.
|
||||
if player.alive == false {
|
||||
player.x = 120
|
||||
player.y = 112
|
||||
player.facing = Down
|
||||
player.frame = Idle
|
||||
player.alive = true
|
||||
}
|
||||
|
||||
// Read controls and update position. Velocities are u8 so we
|
||||
// treat them as signed by adding/subtracting SPEED.
|
||||
if button.left {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue