ludic/examples/toggle.ludic
Orkuncakilkaya 0bded528df Add enable/disable across ECS scopes + @OnEnable/@OnDisable hooks
Three enable/disable statement scopes, each a reversible flag flip:
  - `disable P on e` / `enable P on e` — one property on one entity. Clears
    the has-flag so queries stop matching; field data persists in storage, so
    enable restores it untouched. @OnDisable(P)/@OnEnable(P) handler hooks run
    at the toggle point with the property bound by name.
  - `disable Model` / `enable Model` — @ME_<Model> global flag; the model's
    entities drop out of every query while disabled.
  - `disable Handler` / `enable Handler` — @HE_<Handler> global flag; the
    handler stops being called each phase while disabled.

Nothing is copied or freed — each toggle is one global store or one has-flag
store. Reduces entirely to existing ECS machinery (has-flags, kind filter,
per-phase call guards), so the data-oriented model is untouched.

New AST node S_TOGGLE; emit_toggle lowers it. Query {Model} filter now ANDs
@ME_; phase calls now guard on @HE_. Parser gains enable/disable statements
and @OnEnable/@OnDisable annotations.

Vocabulary: `on` promoted from RESERVED to CLAUSE (parser now dispatches on
it); enable/disable added as STMT keywords — synced across ludic_syntax.h,
the TextMate grammar, and LudicTokens.kt (check-vocabulary.py clean).

examples/toggle.ludic demonstrates all three scopes (prints 6 0 7 1 0);
test.sh smoke asserts it. Reseeded; C-free fixpoint holds; goldens identical.
Also: stop tracking tools/.idea/ (gitignored).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-27 23:05:47 +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
let 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()
}
}