Skip to content

MoonLive scripts are classes #167

Closed
Closed

Description

@MoonModules

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: tick when a frame renders, placeLights when lights are placed, modifyLogical when a coordinate is folded. No selection, no fallback, and a class defining both tick and modifyLogical gets both. This settled step 5 before it started: the bindings differ only in which moment they own.
  • A held MoonLiveScript member, not a shared base. The three bindings derive from three sibling bases under MoonModule, 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() calling addUint8 on 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 allocExec on scarce IRAM; a selector argument puts a branch on every tick() forever.
  • Real calls, never inlining. Inlining saves a microsecond where ripples.mlv spends 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. A sp bound check is cheaper but needs a per-platform stack bound.
  • CallScript carries 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 of addUint8's arguments was built first and rejected: it would make it the one call whose arguments must be literals. That took IrOp::ConstPtr on all three backends, an engine-owned string pool (the exec block is IRAM, 32-bit stores only), and byRef/byStr flags 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 @control comment 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, addUint8 and persistence. A byte arena rebuilt for arrays would be rebuilt again for 16-bit values. A member's offset became a byte cursor; kCtrlBytes split from kMaxCtrls.
  • LoadCtrl16/StoreCtrl16 are separate ops, not a width field. Every backend switch is exhaustive over IrOp, 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, a uint16_t counter, a 16-element heat buffer) was refused at the placeholder 16.
  • if lowers onto the two branch ops the loops use, BranchGe and BranchNe, by negating the test and swapping operands: no new IR op, no backend or allocator change. An if jumps 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 class declaration and script functions, calls real from the start; crosshair.mlv the example, verified on all four boards
  • defineControls() replacing the // @control comment, ControlAnno gone, 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 as getRGB(index) with the RGB type
  • Arrays and uint16_t members, 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.mlv 110 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

Activity

  1. added
    plan
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plan

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions