Repository navigation
Description
Plan 2026-08-17, closed 2026-09-12, shipped.
Takes over from #166, whose step 7 (factor the three bindings onto one shared base) it replaces.
Goal
A MoonLive script is a class, written the way a compiled module is: members, defineControls() and the entry points the host calls (tick, placeLights, modifyLogical). Then the vocabulary an effect author is missing: functions and recursion, if/else, state across ticks, arrays and 16-bit values.
Outcome
Every shipped script is a class. A script calls its own functions and itself, declares its controls by calling addUint8 from defineControls(), keeps state in members, branches, and holds a uint16_t and arrays of up to 64 bytes. A serpentine layout compiles, which the docs had listed as the standing example of what the language could not express. Verified on the classic ESP32, the S3 and the S31, all running ember.mlv, an effect using every construct at once.
Measured at the end of steps 8 and 9 (16×16, the exec block is the emitted machine code):
| board | ISA | ember.mlv |
tick | grid.mlv |
|---|---|---|---|---|
| classic ESP32 | Xtensa | 1396 B | 81 µs | 499 B |
| S3 | Xtensa | 1396 B | 117 µs | 499 B |
| S31 | RISC-V | 1620 B | 33 µs | 880 B |
plasma.mlv emits 1378 B on Xtensa and 2644 B on RISC-V. The two Xtensa targets emit byte-identical code, the cross-check that nothing target-specific leaked into codegen. The recursion guard, the label table's stack cost and the flash cost of local calls are in performance.md, MoonLive.
Decisions
- A script is a class, and the declaration is mandatory. One top-level form. An optional declaration was rejected: it keeps a bare statement list in the grammar forever, a second parse path to document and test, so that two-line scripts stay two lines. The class name identifies the script in the status line and compile errors; the file name loads it, as a C translation unit and the functions in it.
- The name is just a name. Role does not come from a suffix such as
...Effect. It comes from which entry points the class defines, and later (the editor plan) from the file extension. - A name is a moment, not a role (product owner, during step 4). The host owns moments and calls whatever the script defined for each:
tickwhen a frame renders,placeLightswhen lights are placed,modifyLogicalwhen a coordinate is folded. No selection, no fallback, and a class defining bothtickandmodifyLogicalgets both. This settled step 5 before it started: the bindings differ only in which moment they own. - A held
MoonLiveScriptmember, not a shared base. The three bindings derive from three sibling bases underMoonModule, so a shared base needs virtual inheritance and changes every module's layout to serve three of them. The member removed 116 lines and settled the recompile rule the three had drifted on (shipped in A file editor control and a filesystem change seam #168). - Members live in the control arena. A variable shared between functions cannot live in a frame slot, which dies with its call. The arena outlives every call and keeps its address across a recompile. A control is a member the UI shows, decided at run time by
defineControls()callingaddUint8on it, so the parser refuses a write to a system variable only, never to a control. - Arguments by value for scalars, by reference for aggregates. Everything by reference was rejected: a scalar parameter would write through to the caller, and the host builtins are compiled C functions taking values.
- One exec block, an offset per entry point: a symbol table over one code section. A block per entry makes every script-to-script call cross-allocation and multiplies
allocExecon scarce IRAM; a selector argument puts a branch on everytick()forever. - Real calls, never inlining. Inlining saves a microsecond where
ripples.mlvspends 1695 µs in host calls, costs a call graph, cycle detection and a substitution pass on every device, and a wrong analysis inlines a mutually recursive pair forever. Recursion is a test case for local calls: a frame per activation is what the stack machine bought. - The depth guard sits in the callee's prologue, one copy per function, emitted only when the program contains a
CallScript, with the counter a byte in the arena the host zeroes before each run, so a script that hit the limit cannot leak a level. Aspbound check is cheaper but needs a per-platform stack bound. CallScriptcarries a function number, not an IR index. The spill pass inserts Reloads and shifts every later index; a number survives any rewrite.defineControls()is ordinary code. A compile-time reading ofaddUint8's arguments was built first and rejected: it would make it the one call whose arguments must be literals. That tookIrOp::ConstPtron all three backends, an engine-owned string pool (the exec block is IRAM, 32-bit stores only), andbyRef/byStrflags on the builtin descriptor rather than name special-cases in the parser.- Steps 2 and 3 swapped. Every class-scope declaration is a member, and whether the UI shows one is
defineControls's question, so members built first would have been built against the@controlcomment step 2 deletes. - Steps 8 and 9 are one step. The plan claimed the storage was settled; it was a 17-byte row whose offset is the declaration index, load-bearing in the bindings' cached slot pointers,
addUint8and persistence. A byte arena rebuilt for arrays would be rebuilt again for 16-bit values. A member's offset became a byte cursor;kCtrlBytessplit fromkMaxCtrls. LoadCtrl16/StoreCtrl16are separate ops, not a width field. Every backend switch is exhaustive overIrOp, so a backend that forgot the width fails to compile instead of emitting a byte access.- An out-of-range index clamps to the last element. Indices come from live control values, so out of range is an ordinary run-time state, and a stray write would corrupt the system variables and the depth counter beside the members.
- The ceiling is 64 bytes, sized when
ember.mle(two byte controls, auint16_tcounter, a 16-element heat buffer) was refused at the placeholder 16. iflowers onto the two branch ops the loops use,BranchGeandBranchNe, by negating the test and swapping operands: no new IR op, no backend or allocator change. Anifjumps forward, so the spill pass cannot mistake it for a loop.- String literals yes, a String type no. Every one of the 52 compiled effects uses
const char*as metadata only; a mutable String is the arrays' storage question again. - Not in the plan:
while,break,continue(completeness, not expressiveness), floating point (the render path is integer and the classic has no FPU), a scripted driver.
Where it lives
| What | Home |
|---|---|
| The language: a class, members and controls, assignment, branching, wider members and arrays, helpers and recursion | MoonLive |
| The architecture: the domain seam, the executable-memory seam, a scripted module is still a module | MoonLive architecture |
The costs: the recursion guard, local calls' flash, lowerWith's frame, the per-ISA tick table |
performance.md, MoonLive |
| The four stacked defects of local calls and why a green suite missed them | lessons |
The grammar, the member symbol table, if, the function table |
MoonLiveCompiler.cpp |
The IR ops: CallScript, ConstPtr, LoadCtrl16, LoadIdx |
MoonLiveIr.h |
| One arm per op, shared by the three backends | moonlive_lower.h |
The arena, the string pool, entry(name), kCtrlBytes |
MoonLive.h |
| The frame contract, the window-save reserve and the register map | register-and-slot-contract.md |
| The structural checker, per prologue | moonlive_structural.inc |
| The host tests: calls, recursion, members across ticks, the boundary table of six comparisons, the clamp, the arena ceiling | unit_moonlive_compiler.cpp |
| The three bindings and the held script | src/light/moonlive/ |
Steps
- The
classdeclaration and script functions, calls real from the start;crosshair.mlvthe example, verified on all four boards -
defineControls()replacing the// @controlcomment,ControlAnnogone, 16 scripts and four docs moved - Typed members: the assignment statement, a system variable refused, a control allowed
- A frame per function, the host arguments parked per function, the checker re-reading the frame at every prologue
- The remaining entry points per role:
placeLights,modifyLogical,tick, each run if defined - The three bindings onto a held
MoonLiveScript(in the editor plan) -
if/else, six comparisons on two branch ops, the lexer's two-character operators by maximal munch - Reading a light back,
get(x, y); shipped later asgetRGB(index)with the RGB type - Arrays and
uint16_tmembers, one step: a byte cursor, even alignment for a wide member, the clamp, the 64-byte ceiling - The editing loop, a card with a picker and an editor (in the editor plan)
- Verification: the recursion tests control-checked by reverting their fix; the checker shown failing on a shrunk reserve;
forever.mlv110 s at 109 fps on the classic; the bench on classic, S3 and S31
Shipped in
#66 (the class and local calls), #67 (defineControls, members, if, arrays, uint16_t, the review defects), #68 (a 16-bit control). Commits 2f4c292, 71c5dbe, 426d5cc, 762676f, 66dd789, 575b447.
Still open
- Arrays of structs: The next language steps, in order.
- A refused recursion is silent; reporting it needs a channel from the emitted block back to the binding, which the status line does not have.
- Two array codegen defects (
IrInst::c/dholding a width,sourcesOfreportingkArg4first) have no host test after three attempts: Catch device-backend operand defects on the host. - The label and fixup tables on the render task's stack: A MoonLive script's branch tables are fixed and on the stack.
- The fixed 64-byte arena: Size the MoonLive control arena to the script.
- A per-frame modifier moment: A scripted modifier that animates every frame. A fourth role: A scripted driver.
- The P4 was not reflashed after the array fixes; it is RISC-V like the S31, an assumption rather than a check. Its idle
Cache errorpanic is pre-existing: ESP32-P4 WiFi through the C6 co-processor.
Activity