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

docs: enum type + new CLI flags in the language guide

Documents the \`enum Name { Variant, ... }\` syntax and adds
\`--dump-ir\` and \`--use-ast\` to the CLI flag table. Also adds
an integration test covering enum-variant-as-condition and variant
assignment through the full compile pipeline.

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 11:39:12 +00:00
parent dbfcb313bc
commit d4daa6d0a9
No known key found for this signature in database
2 changed files with 52 additions and 5 deletions

View file

@ -132,6 +132,29 @@ const SPEED: u8 = 3
const SIN_TABLE: u8[8] = [0, 49, 90, 117, 127, 117, 90, 49]
```
### Enums
Enums declare a named set of `u8` constants. Each variant is assigned an
index starting at 0 in declaration order:
```
enum Direction { Up, Down, Left, Right }
// Up=0, Down=1, Left=2, Right=3
var player_dir: u8 = Up
on frame {
if button.left { player_dir = Left }
if button.right { player_dir = Right }
if player_dir == Down { /* ... */ }
}
```
Variant names are global — they are flattened into the top-level symbol
table, so a variant cannot share its name with any other constant,
variable, or function (E0501). An enum cannot have more than 256
variants because each is stored as a `u8`.
### Memory Placement Hints
The NES has 256 bytes of zero-page RAM with faster access. You can hint where variables should be placed:
@ -784,13 +807,16 @@ nescript build game.ne
nescript build game.ne --output my_game.nes
nescript build game.ne --debug
nescript build game.ne --asm-dump
nescript build game.ne --dump-ir
```
| Flag | Description |
|---------------|------------------------------------------------|
| `--output` | Set output ROM file path (default: input.nes) |
| `--debug` | Enable debug mode with runtime checks |
| `--asm-dump` | Dump generated 6502 assembly to stdout |
| Flag | Description |
|---------------|----------------------------------------------------------------|
| `--output` | Set output ROM file path (default: input.nes) |
| `--debug` | Enable debug mode with runtime checks |
| `--asm-dump` | Dump generated 6502 assembly to stdout |
| `--dump-ir` | Dump the lowered IR program (after optimization) to stdout |
| `--use-ast` | Use the legacy AST-based codegen (default is the IR codegen) |
### Check

View file

@ -141,6 +141,27 @@ fn program_with_functions() {
rom::validate_ines(&rom_data).expect("should be valid iNES");
}
#[test]
fn program_with_enums() {
let source = r#"
game "Enums" { mapper: NROM }
enum Direction { Up, Down, Left, Right }
enum Mode { Idle, Running, Jumping }
var dir: u8 = 0
var mode: u8 = 0
on frame {
if button.right { dir = Right }
if button.left { dir = Left }
if dir == Right { mode = Running }
}
start Main
"#;
let rom_data = compile(source);
rom::validate_ines(&rom_data).expect("should be valid iNES");
}
#[test]
fn program_with_inline_asm() {
let source = r#"