ludic/examples/events/recurse.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

23 lines
832 B
Text

# recurse.ludic — EV6: re-entrant emit is bounded. Ping's listener emits Pong,
# Pong's listener emits Ping — an event cycle that would hang the frame forever.
# The depth bound (EV_DEPTH_CAP) makes the nesting trap as an early return, so the
# program terminates with a bounded count instead of spinning. This is the "no
# runaway event cycle" guarantee.
#
# Running it prints: 16 (the cycle bottoms out at the depth cap, deterministically)
program Recurse {
state RecurseState {
n: int = 0
}
event Ping { }
event Pong { }
@On(Ping) handler A(recurse_st: mut RecurseState) { recurse_st.n += 1; emit Pong() } # each Ping deepens by one Pong
@On(Pong) handler B { emit Ping() } # ...and each Pong by one Ping
entry (recurse_st: RecurseState) {
emit Ping()
print(recurse_st.n)
}
}