ludic/examples/detach.ludic
Orkuncakilkaya a38195128f feat(stdlib): namespaced standard library (issue #2)
Implement the bulk of the namespaced-stdlib proposal (workshopsoft/ludic#2):
156 namespace methods across Math, Text, List, Ease, Collide, World, Net,
Sys, Save, Mem, extended Screen, Color functions, extended Random, and Time.
All deterministic fixed-point; self-hosting (C-free bootstrap fixpoint holds).

Compiler (selfhost/):
- Math.*: sqrt/sin/cos/tan/atan2/asin/acos (fixed-point runtime prelude —
  bit-by-bit isqrt, 256-entry interpolated sine table, Ross atan2), plus
  hypot/dist/dist2/deg_to_rad/rad_to_deg/posmod/wrap/ping_pong/snapped/
  move_toward/smoothstep/lerp/remap/sign/floor/ceil/round.
- Text.* (complete): upper/lower/trim/repeat/pad, split/join/replace,
  and the libc-backed queries.
- List.* (complete): insert/remove_at/remove/sort plus the earlier ops.
- Ease.* (in/out/in_out/back/bounce) and Collide.* (rects/point_rect/
  circles/rect_circle).
- Phase 3: World/Net/Sys/Save namespaced over the bare builtins (byte-
  identical IR) and Mem.* (bytes/words/copy/fill/peek/poke).
- Screen.* extended (line/circle/fill_circle/triangle/fill_triangle via new
  runtime primitives; sprite/sprite_scaled aliases), Color.* functions,
  Random.* (value/int/sign), Time.* (frame/delta/elapsed/now — new
  game-loop frame counter).
- Fix a lexer bug: fixed-point literals with >4 fractional digits overflowed.

Docs & tooling:
- 129 new per-symbol doc pages; gen.py made data-driven (namespaces
  discovered from the docs, no hardcoded list); new check-impl.py enforces
  that every implemented namespace method / keyword / type / phase has a
  doc page, wired into `x test-tools`. Document the previously-undocumented
  keywords (break/continue/where/entry/new/public + and/or/not tokens).
- LSP: namespaced signature help (ns_method_sig) covering every namespace.

Tests: 12 new self-host/regression tests + a golden render for the drawing
primitives. All suites green (selfhost 21, regression 45, tools 29).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-30 00:26:19 +03:00

38 lines
1.6 KiB
Text

# detach.ludic — the structural attach/detach pair and its @OnAttach / @OnDetach
# hooks. `attach P on e` adds a property to a LIVE entity (seeding its fields and
# firing @OnAttach); `detach P on e` removes it (firing @OnDetach, which still
# reads the outgoing value before the has-flag clears). This is the structural
# counterpart to enable/disable — attach/detach create and destroy the property's
# presence, whereas disable/enable only pause it while keeping the data.
#
# Running it prints: 15 1 25 0
# 15 @OnAttach(Shield): amount seeded to 5, prints 5 + 10
# 1 one live Shield now matches the query
# 25 @OnDetach(Shield): reads the outgoing amount 5, prints 5 + 20
# 0 the Shield is gone — nothing matches
#
# bin/x game-build bin/ludicc examples/detach.ludic /tmp/detach
# /tmp/detach </dev/null
program Detach {
property Tag { v: int = 0 }
property Shield { amount: int = 0 }
model Unit { Tag }
@OnAttach(Shield) handler Up { print(Shield.amount + 10) } # structural: property born
@OnDetach(Shield) handler Down { print(Shield.amount + 20) } # structural: property dies
handler Seed phase Start { spawn Unit { Tag { v: 1 } } }
handler Run phase Render {
for (u) in query [Unit] { attach Shield on self() { amount: 5 } } # @OnAttach -> 15
var n = 0
for (s) in query [Shield] { n += 1 }
print(n) # 1
for (s) in query [Shield] { detach Shield on self() } # @OnDetach -> 25
n = 0
for (s) in query [Shield] { n += 1 }
print(n) # 0
quit()
}
}