1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-14 11:36:40 +00:00

IR codegen: state dispatch, multi-OAM, and transition support

State machine dispatch:
- IrProgram now stores states (Vec<String>) and start_state
- Lowering captures state metadata before walking the AST
- IR codegen generates a main loop with vblank wait and CMP+BNE+JMP
  dispatch table, matching the AST codegen's layout
- Each frame handler ends with JMP __ir_main_loop
- current_state initialized to the start state's index at boot

Multi-OAM support:
- next_oam_slot counter, reset at the start of each _frame function
- Sequential allocation of 4-byte OAM entries at $0200 + slot*4
- Silently drops draws beyond slot 63 (OAM full)

Transition codegen:
- IrOp::Transition now looks up the target state's index from
  state_indices, writes it to ZP $03, and JMPs back to main loop
- Previously this was a no-op placeholder

Shared constants:
- ZP_FRAME_FLAG ($00) and ZP_CURRENT_STATE ($03) match AST codegen

Tests: 271 total (5 new IR codegen tests + 2 new integration tests)
All 7 examples compile through --use-ir, including multi-state games
and programs with transitions and functions.

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 10:33:58 +00:00
parent 7e94cf37e3
commit 5660a8b7c3
No known key found for this signature in database
5 changed files with 239 additions and 16 deletions

View file

@ -26,6 +26,9 @@ struct LoweringContext {
current_label: String,
// Loop context for break/continue
loop_stack: Vec<LoopContext>,
// State metadata captured from the AST
state_names: Vec<String>,
start_state: String,
}
struct LoopContext {
@ -57,6 +60,8 @@ impl LoweringContext {
current_ops: Vec::new(),
current_label: String::new(),
loop_stack: Vec::new(),
state_names: Vec::new(),
start_state: String::new(),
}
}
@ -104,10 +109,16 @@ impl LoweringContext {
functions: self.functions,
globals: self.globals,
rom_data: self.rom_data,
states: self.state_names,
start_state: self.start_state,
}
}
fn lower_program(&mut self, program: &Program) {
// Capture state metadata before lowering
self.state_names = program.states.iter().map(|s| s.name.clone()).collect();
program.start_state.clone_into(&mut self.start_state);
// Register constants
for c in &program.constants {
if let Expr::IntLiteral(v, _) = &c.value {