1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 08:55:38 +00:00
nescript/examples
Claude ad951a835f
examples: adopt the friendly asset syntax
Rewrites every example with non-trivial asset declarations to use
the pleasant QoL syntax introduced in the previous commit. Every
example still compiles to a byte-identical ROM (verified by a
temp-path diff before committing), so the committed `.nes` files
and the 23 emulator goldens are unchanged.

  * platformer.ne — the centerpiece end-to-end demo:
      - `palette Main` goes to grouped form with a shared
        `universal: 0x22` (sky blue), one shared colour per
        sub-palette, and named NES colours throughout; the
        long-standing `$3F10` mirror-trap warning is now handled
        by the parser and the manual pitfall comment is gone.
      - `sprite Tileset` is 15 tiles of ASCII pixel art instead
        of 240 bytes of inline hex.
      - `background Level` uses a `legend { '.': 15, '#': 9, ... }`
        block plus `map:` strings for the 32×30 nametable, and
        `palette_map:` rows for the attribute table. The map
        reads top-down like the rendered screen.
      - SFX latch-once `pitch: 0x30` scalars + `envelope:` alias.
      - `music Theme` uses note names + `tempo: 10` default.
  * audio_demo.ne — scalar sfx pitches, `envelope:` alias, and a
    note-name `C4, E4, G4, ...` music track.
  * palette_and_background.ne — grouped CoolBlues / WarmReds
    palettes with `universal: black` + named colours, plus
    `legend` + `map:` tilemaps for the two backgrounds.
  * sprites_and_palettes.ne — Arrow and Heart sprites rewritten
    as `pixels:` ASCII art.

Along the way, two small parser extensions support the rewrites:

  - `parse_pixel_art` now accepts `a/b/c` as aliases for `#/%/@`,
    matching the vocabulary every NES editor (and our own
    gen_platformer_tiles.rs generator) uses.
  - `palette_map_to_attrs` allows up to 16 metatile rows (the
    full attribute-table coverage, including the off-screen
    bottom half) and auto-replicates row 14 → row 15 when only
    15 rows are supplied so the visible bottom of the screen
    gets consistent sub-palette assignments by default. The old
    15-row cap couldn't match a hand-packed `0xAA` attribute
    table for the last row; the platformer required this to
    stay byte-identical.

`scripts/gen_platformer_tiles.rs` is updated to emit the new
syntax directly (pixel-art `pixels:` block + `legend`/`map:`/
`palette_map:` for the background), so regenerating the
platformer tiles stays a one-liner.

474 lib tests + 64 integration tests pass (3 new parser tests
for `palette_map:` 15/16/17 rows and the `abc` alias). All 23
emulator goldens still match pixel- and sample-for-sample.

https://claude.ai/code/session_01PzaSFj3VahDzxEYTKCESkz
2026-04-13 18:04:21 +00:00
..
arrays_and_functions.ne
arrays_and_functions.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
audio_demo.ne examples: adopt the friendly asset syntax 2026-04-13 18:04:21 +00:00
audio_demo.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
bitwise_ops.ne examples: add 5 new programs covering match/loop/logic/bitwise/scanline 2026-04-12 19:05:18 +00:00
bitwise_ops.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
bouncing_ball.ne
bouncing_ball.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
coin_cavern.ne
coin_cavern.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
comparisons.ne examples: 4 new programs covering MMC3 + other e2e gaps 2026-04-12 20:48:31 +00:00
comparisons.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
friendly_assets.ne language: pleasant asset syntax for palettes, CHR, bg, sfx, music 2026-04-13 16:09:53 +00:00
friendly_assets.nes language: pleasant asset syntax for palettes, CHR, bg, sfx, music 2026-04-13 16:09:53 +00:00
function_chain.ne examples: 4 new programs covering MMC3 + other e2e gaps 2026-04-12 20:48:31 +00:00
function_chain.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
hello_sprite.ne
hello_sprite.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
inline_asm_demo.ne examples: inline_asm_demo.ne 2026-04-12 18:02:59 +00:00
inline_asm_demo.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
logic_ops.ne examples: add 5 new programs covering match/loop/logic/bitwise/scanline 2026-04-12 19:05:18 +00:00
logic_ops.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
loop_break_continue.ne examples: add 5 new programs covering match/loop/logic/bitwise/scanline 2026-04-12 19:05:18 +00:00
loop_break_continue.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
match_demo.ne examples: add 5 new programs covering match/loop/logic/bitwise/scanline 2026-04-12 19:05:18 +00:00
match_demo.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
mmc1_banked.ne
mmc1_banked.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
mmc3_per_state_split.ne examples: 4 new programs covering MMC3 + other e2e gaps 2026-04-12 20:48:31 +00:00
mmc3_per_state_split.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
palette_and_background.ne examples: adopt the friendly asset syntax 2026-04-13 18:04:21 +00:00
palette_and_background.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
platformer.ne examples: adopt the friendly asset syntax 2026-04-13 18:04:21 +00:00
platformer.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
README.md language: pleasant asset syntax for palettes, CHR, bg, sfx, music 2026-04-13 16:09:53 +00:00
scanline_split.ne examples: add 5 new programs covering match/loop/logic/bitwise/scanline 2026-04-12 19:05:18 +00:00
scanline_split.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
sprites_and_palettes.ne examples: adopt the friendly asset syntax 2026-04-13 18:04:21 +00:00
sprites_and_palettes.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
state_machine.ne
state_machine.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
structs_enums_for.ne Language: struct literals 2026-04-12 17:15:57 +00:00
structs_enums_for.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
two_player.ne examples: 4 new programs covering MMC3 + other e2e gaps 2026-04-12 20:48:31 +00:00
two_player.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00
uxrom_banked.ne examples: add uxrom_banked and jsnes golden 2026-04-13 02:04:25 +00:00
uxrom_banked.nes commit built ROMs alongside .ne sources 2026-04-13 15:12:39 +00:00

NEScript Examples

Quick Start

# Build the compiler
cargo build --release

# Compile all examples
for f in examples/*.ne; do cargo run -- build "$f"; done

# Or compile one
cargo run -- build examples/hello_sprite.ne

Open any .nes file in an NES emulator (Mesen, FCEUX, etc.)

Examples

File Features Description
hello_sprite.ne input, draw Move a sprite with the d-pad
bouncing_ball.ne if/else, variables Auto-bouncing sprite with edge detection
coin_cavern.ne states, functions, constants 3-state game with gravity and coin collection
arrays_and_functions.ne arrays, functions, while Enemy array with collision detection
state_machine.ne on enter/exit, transitions Multi-state flow with timers
sprites_and_palettes.ne sprites, scroll, cast Inline CHR data, PPU scroll writes, type casting
mmc1_banked.ne MMC1, banks, multiply Banked mapper with software multiply
palette_and_background.ne palette, background, set_palette, load_background Reset-time initial load plus vblank-safe runtime swaps
friendly_assets.ne named colours, grouped palette, pixel art, tilemap+legend, palette_map, scalar sfx pitch, note-name music Exercises every "friendlier" asset syntax at once — the palette uses bg0..sp3 + a shared universal:, the sprite is authored as ASCII pixel art, the background uses a legend { ... } + map: tilemap with a palette_map: for attributes, the sfx uses a scalar pitch: + envelope: alias, and the music uses note names (C4, E4 40, rest 10) with a tempo: default.
platformer.ne every subsystem End-to-end side-scrolling demo: custom CHR tileset, full 32×30 nametable with per-region attribute palettes, 2×2 metasprite hero with gravity/jump physics, wrap-around horizontal scrolling, enemies, coin pickups, user-declared SFX + music, and a Title → Playing state machine with an autopilot so the headless harness still hits interesting gameplay at frame 180. Regenerate the tile art with cargo run --bin gen_platformer_tiles.

Emulator Controls

NES Button Typical Key
D-pad Arrow keys
A Z
B X
Start Enter
Select Right Shift

About Sprites

Sprite names in draw Player at: (x, y) are parsed and recorded in the AST. You can define sprites with inline CHR tile data:

sprite Player {
    chr: [0x3C, 0x42, 0x81, 0x81, 0x81, 0x81, 0x42, 0x3C,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
}

If no matching sprite declaration exists, the draw uses the built-in default tile (a smiley face). See sprites_and_palettes.ne for a full example.

Compiler Commands

# Compile to ROM
cargo run -- build game.ne

# Custom output path
cargo run -- build game.ne --output my_game.nes

# Type-check only
cargo run -- check game.ne

# View generated 6502 assembly
cargo run -- build game.ne --asm-dump

# Debug mode
cargo run -- build game.ne --debug