mirror of
https://github.com/imjasonh/nescript
synced 2026-07-09 01:16:12 +00:00
Two correctness bugs were silently producing wrong ROMs:
- `x << n` / `x >> n` always shifted by 1, regardless of `n`, because
the IR lowering for `BinOp::ShiftLeft`/`ShiftRight` hardcoded the
count. Now eval_const the RHS into a compile-time count; fall back
to a new `IrOp::ShiftLeftVar` / `ShiftRightVar` (runtime loop) when
the amount isn't constant. Strength reduction folds the variable
form back to a fixed count once the optimizer knows the value.
- `x / n` / `x % n` always returned 0, because the lowering emitted
`LoadImm(t, 0)` for `BinOp::Div`/`Mod` with a comment saying the
runtime call was "TODO for now". Added real `IrOp::Div` and
`IrOp::Mod`, wired them through use-counting and DCE, gave codegen
`__divide`-based implementations, and taught strength reduction to
rewrite power-of-two divisors into shifts and modulo-by-2ⁿ into
AND masks. Constant folding now handles `Mul`/`Div`/`Mod`/shifts
too, which were previously left for the codegen to emit inefficient
software calls.
Dead code removed (no backward-compat shims kept):
- `src/debug/` entirely. `DebugSymbols`, `SourceMap`, and the
Mesen/.sym emitters had no callers outside their own tests;
`main.rs` never wrote a symbol file. Documented the intent in
`docs/future-work.md` so it comes back intentionally if needed.
- `ErrorCode::E0202` (invalid cast) and `E0403` (unreachable state):
defined, formatted, and marked `#[allow(dead_code)]` but never
emitted. W0104 now carries the unreachable-state semantics too.
- `Level::Info`: never constructed.
- `load_background` / `set_palette` statements and their
`BackgroundDecl` / `PaletteDecl` parser support: parsed and
silently dropped by IR lowering (`// TODO: implement in asset
pipeline`). Removed keywords, AST variants, parser paths, analyzer
arms, and tests. `docs/future-work.md` documents the runtime
palette/nametable design for when it comes back.
Doc cleanup:
- `docs/architecture.md` was describing files that don't exist
(`analyzer/types.rs`, `optimizer/const_fold.rs`, `codegen/regalloc.rs`,
`rom/header.rs`, `debug/symbols.rs`, …). Rewrote it to match the
real flat `mod.rs` + `tests.rs` layout and the real pipeline order.
- `docs/future-work.md` was a hybrid of open work and "recently
completed" entries that duplicated the active stubs at the top of
the file. Collapsed to just the gaps that are actually still open.
- `README.md` claimed Mesen symbol export and 210 tests; updated both.
- `docs/language-guide.md` and `spec.md` described `palette` decls,
`set_palette` / `load_background`, `debug.overlay`, and error codes
that were never emitted. Trimmed.
- Stale comments on `Statement::Play`/`StartMusic`/`StopMusic`
claimed the audio subsystem was "a no-op at codegen time".
Tests:
- Regression tests for every fix above (`lower_shift_left_with_literal
_count_uses_that_count`, `lower_shift_right_with_variable_count
_uses_runtime_variant`, `lower_divide_emits_div_op_not_load_imm
_zero`, `lower_modulo_emits_mod_op_not_load_imm_zero`,
`strength_reduce_div_by_power_of_two`, `strength_reduce_mod_by
_power_of_two`, `strength_reduce_shift_var_with_constant_amount`).
- Renamed the `program_with_sprites_and_palette` integration test
(which was exercising the now-removed `load_background`/`set_palette`)
to `program_with_inline_sprite_chr`.
`examples/sprites_and_palettes.ne` lost its `palette`/`set_palette`
usage. Nothing in the emulator test presses A, so the headless
jsnes render shouldn't move, but the golden may need regeneration
via `UPDATE_GOLDENS=1` if it does.
https://claude.ai/code/session_012fKB251HvEUQwG3tizFyqt
126 lines
5.2 KiB
Markdown
126 lines
5.2 KiB
Markdown
# NEScript
|
|
|
|
A statically-typed, compiled programming language for NES game development.
|
|
|
|
NEScript compiles `.ne` source files directly into playable iNES ROM files, with no external assembler or linker dependencies. The compiler handles everything from source text to a ROM you can run in any NES emulator.
|
|
|
|
## Quick Start
|
|
|
|
```bash
|
|
# Build the compiler
|
|
cargo build --release
|
|
|
|
# Compile an example
|
|
cargo run -- build examples/hello_sprite.ne
|
|
|
|
# Run the output ROM in an emulator
|
|
# (produces examples/hello_sprite.nes)
|
|
```
|
|
|
|
## Hello World
|
|
|
|
```
|
|
game "Hello" {
|
|
mapper: NROM
|
|
}
|
|
|
|
var px: u8 = 128
|
|
var py: u8 = 120
|
|
|
|
on frame {
|
|
if button.right { px += 2 }
|
|
if button.left { px -= 2 }
|
|
if button.down { py += 2 }
|
|
if button.up { py -= 2 }
|
|
|
|
draw Smiley at: (px, py)
|
|
}
|
|
|
|
start Main
|
|
```
|
|
|
|
## Features
|
|
|
|
- **Game-aware syntax** -- states, sprites, and input are first-class constructs
|
|
- **Full type system** -- `u8`, `i8`, `u16`, `bool`, fixed-size arrays (`u8[N]`), `enum`, `struct`
|
|
- **Rich control flow** -- `if`/`else`, `while`, `for i in 0..N`, `loop`, `match`
|
|
- **Functions** -- with parameters, return types, `inline` hint, recursion detection
|
|
- **State machines** -- `state` with `on enter`, `on exit`, `on frame`, `on scanline(N)` handlers
|
|
- **Compile-time safety** -- call depth limits, recursion detection, type checking, unused-var warnings
|
|
- **IR-based optimizer** -- constant folding, dead code elimination, strength reduction (incl. div/mod by power-of-two), copy propagation, peephole passes including INC/DEC fold and live-range slot recycling
|
|
- **Full 16-bit arithmetic** -- u16 add/sub/compare lower to carry-propagating paired operations
|
|
- **Multiple mappers** -- NROM, MMC1, UxROM, MMC3 (including multi-scanline IRQ dispatch per state)
|
|
- **Audio subsystem** -- frame-walking pulse driver with user-declared `sfx`/`music` blocks, builtin effects and tracks, period table, and zero-cost elision when unused
|
|
- **Asset pipeline** -- PNG-to-CHR conversion, inline tile data, sfx envelopes, music note streams
|
|
- **Inline assembly** -- `asm { ... }` with `{var}` substitution, plus `raw asm { ... }` for verbatim blocks
|
|
- **Hardware intrinsics** -- `poke(addr, value)` / `peek(addr)` for direct register access
|
|
- **Debug support** -- `--debug` flag enables `debug.log` / `debug.assert` writes to the emulator debug port
|
|
- **Compile-time diagnostics** -- `--dump-ir`, `--memory-map`, `--call-graph` flags
|
|
- **Single binary** -- no dependencies on ca65, Python, or any external tools
|
|
|
|
## Documentation
|
|
|
|
- **[Language Guide](docs/language-guide.md)** -- complete reference for every language feature
|
|
- **[Architecture](docs/architecture.md)** -- compiler internals and module overview
|
|
- **[NES Reference](docs/nes-reference.md)** -- hardware quick reference for contributors
|
|
- **[Examples README](examples/README.md)** -- how to build and run examples
|
|
|
|
## Examples
|
|
|
|
| Example | Features demonstrated |
|
|
|---------|---------------------|
|
|
| [`hello_sprite.ne`](examples/hello_sprite.ne) | D-pad input, sprite drawing |
|
|
| [`bouncing_ball.ne`](examples/bouncing_ball.ne) | Automatic movement, edge detection |
|
|
| [`coin_cavern.ne`](examples/coin_cavern.ne) | Multi-state game, functions, constants, gravity |
|
|
| [`arrays_and_functions.ne`](examples/arrays_and_functions.ne) | Arrays, functions, while loops, inline functions |
|
|
| [`state_machine.ne`](examples/state_machine.ne) | State transitions, on enter/exit, timers |
|
|
| [`sprites_and_palettes.ne`](examples/sprites_and_palettes.ne) | Inline CHR data, scroll, type casting |
|
|
| [`mmc1_banked.ne`](examples/mmc1_banked.ne) | MMC1 mapper, bank declarations, multiply |
|
|
| [`structs_enums_for.ne`](examples/structs_enums_for.ne) | Structs, enums, `for` loops, struct literals |
|
|
| [`inline_asm_demo.ne`](examples/inline_asm_demo.ne) | Inline asm with `{var}` substitution, `poke`/`peek` |
|
|
| [`audio_demo.ne`](examples/audio_demo.ne) | Audio subsystem: user `sfx`/`music` blocks, builtin effects, `play`/`start_music`/`stop_music` |
|
|
|
|
## Compiler Commands
|
|
|
|
```bash
|
|
# Compile to ROM
|
|
nescript build game.ne
|
|
|
|
# Compile with custom output path
|
|
nescript build game.ne --output my_game.nes
|
|
|
|
# Type-check only (no ROM output)
|
|
nescript check game.ne
|
|
|
|
# View generated 6502 assembly
|
|
nescript build game.ne --asm-dump
|
|
|
|
# Enable debug mode
|
|
nescript build game.ne --debug
|
|
```
|
|
|
|
## Emulator Compatibility
|
|
|
|
Output ROMs are standard iNES format and work with any NES emulator:
|
|
|
|
- **[Mesen](https://www.mesen.ca/)** -- recommended, best debugging support
|
|
- **[FCEUX](https://fceux.com/)**
|
|
- **[Nestopia](http://nestopia.sourceforge.net/)**
|
|
|
|
## Project Status
|
|
|
|
NEScript implements all five planned milestones:
|
|
|
|
| Milestone | Status | Key Features |
|
|
|-----------|--------|-------------|
|
|
| M1: Hello Sprite | Done | Full compiler pipeline, assembler, ROM builder |
|
|
| M2: Game Loop | Done | Functions, arrays, IR, optimizer, call graph analysis |
|
|
| M3: Asset Pipeline | Done | PNG-to-CHR, sprites, `debug.log` / `debug.assert` |
|
|
| M4: Optimization | Done | Strength reduction, ZP promotion, type casting, asm-dump |
|
|
| M5: Bank Switching | Done | MMC1/UxROM/MMC3, bank declarations, software mul/div |
|
|
|
|
465 tests across the lexer, parser, analyzer, IR, optimizer, codegen, assembler, linker, runtime, ROM, and asset modules, with CI running fmt, clippy, test, and example compilation on every push.
|
|
|
|
## License
|
|
|
|
[MIT](LICENSE)
|