ludic/examples/toggle.ludic
Orkuncakilkaya e1d7797e29 Phase 6f: let = immutable, var = mutable (binding-only)
Bindings now signal mutability the way Rust/Swift do, instead of `let` meaning
"local" and `var` meaning "module-level":

  - `let x = e`  -> immutable binding; a later `x = …` is a compile error
    (`cannot assign to immutable 'x' … use var`).
  - `var x = e`  -> mutable binding, at local OR module scope (position decides
    scope; the keyword decides mutability).
  - `const`      -> unchanged (compile-time).

Immutability is of the *binding*, not the object: `let n = new Node; n.kind = 1`
is fine (mutation through the reference); only rebinding `n` is rejected. The
check lives in emit_assign — a direct `name =` whose target is a `let` local
(loc_mut == 0) errors; field/index targets and `var`/param/loop bindings are
unaffected.

Delivered as three reseeds so the self-hosting compiler never had to compile
source its own rules would reject:
  A) add `var` as a local statement + per-local mutability tracking (loc_mut),
     no enforcement;
  B) migrate every reassigned `let` -> `var` across the compiler, runtime and
     examples (337 declarations), driven by a per-function, string/comment-aware
     scan (binding targets only, never `x.f =` / `x[i] =`);
  C) turn on the check. bootstrap-cfree (compiler vs its own source) and every
     golden build (which splices the runtime) then proved zero reassigned `let`
     was missed anywhere.

Also folded in: removed leftover debug instrumentation in block() (a `cur=` /
print_int(777…) trace on the separator-error path) and fixed parse.ludic's stale
header comment (no more `struct`). Reseeded (21711 lines); C-free fixpoint holds;
goldens identical; 17/17; vocab + doc-fences clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-27 23:52:59 +03:00

37 lines
1.7 KiB
Text

# toggle.ludic — enable/disable at the three ECS scopes, each a plain statement.
# Disabling never destroys data: a property's values persist in storage, so a
# later `enable` restores them. Queries already skip a cleared flag, so nothing
# else in the language needs to know. Running it prints: 6 0 7 1 0
#
# disable P on e clears one entity's has-flag (@OnDisable / @OnEnable fire)
# disable Model flips the model's enabled flag (its entities drop from queries)
# disable Handler flips the handler's enabled flag (it stops running each phase)
program Toggles {
property Health { hp: int = 0, max: int = 100 }
property Shield { amount: int = 0 }
model Player { Health, Shield }
# hooks run at the toggle point with the property's data bound by name
@OnDisable(Shield) handler Down { print_int(Shield.amount + 1) } # 5 + 1
@OnEnable(Shield) handler Up { print_int(Shield.amount + 2) } # 5 + 2
handler Seed phase Start { spawn Player { Health { max: 50 }, Shield { amount: 5 } } }
handler Run phase Render {
for (e) in query [Player] { disable Shield on self() } # @OnDisable -> 6
var n = 0
for (s) in query [Shield] { n += 1 }
print_int(n) # 0 — no live Shield now
for (e) in query [Player] { enable Shield on self() } # @OnEnable -> 7, data intact
n = 0
for (s) in query [Shield] { n += 1 }
print_int(n) # 1 — amount is still 5
disable Player # whole model off
n = 0
for (p) in query [Player] { n += 1 }
print_int(n) # 0 — model disabled
quit()
}
}