writeonce/docs/examples/fibers/main.wo
shoney.arickathil f465ad5751 feat(lang+wo-html): raw text literals, component layer, MVC samples
- lexer: backtick raw text literal — content verbatim, no escape
  processing, common source margin removed at lex time; `${ }` raw and
  `{{ }}` auto-escaping holes
- `{{ e }}` desugars to `esc(${e})` in parser.ml — a Call on the `esc`
  in scope, so types/owner/emit/.wob/VM are untouched
- WO-E004 unterminated raw literal; WO-E005 newline inside "..." —
  closes a hole where a missing quote silently ate the rest of the file
- wo-html: `Component` interface, `render_all`, `Layout`, README
- framework: `ok_html` joins ok_text/ok_json in http/types.wo
- site + shop restructured to one-feature-one-module MVC (view +
  controller per directory, model at the root, bootstrap-only main)
- removed the filler `pad: Int` convention — verified unnecessary for
  plain classes, interface dispatch, containers and actors
- corrected recorded claims: gap #1 blocks neither the build nor the
  layout; a class crosses module lines, only a free fn is scoped
- docs/guides/language-surface.md — the full grammar inventory
- story 37 landed and moved to done/

Gates: oop-accept MET, oop-e2e 116/0, woc-test 556/0, site 11/0,
web-app 46/0, fibers 10/0, db-actor 8/0

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-25 03:58:41 +02:00

73 lines
2 KiB
Text

use time
-- fibers — the writeonce hybrid demonstrated (BEAM's shape):
-- cooperative MECHANICS (no signals, no interrupts) with
-- reduction-budget EVICTION (loop back-edges pay one reduction; at
-- zero the fiber re-queues whether it likes it or not), actors as the
-- spawn surface (one message at a time, ownership-moving sends), and
-- blocking builtins that PARK on the shard's io_uring plane instead
-- of holding the thread.
--
-- Part 1 is deterministic by construction (budget accounting, no time):
-- its output is byte-exact. Part 2 shows a sleeping fiber not blocking
-- anyone; its ordering is asserted loosely by the acceptance gate
-- because it depends on real time.
class Tick {
n: Int
}
-- Part 1: a counting actor; main and the actor interleave one message
-- per main-yield under the default budget.
class Counter {
label: Text
total: Int
fn receive(msg: Tick) {
self.total = self.total + msg.n;
print("${self.label} +${msg.n} = ${self.total}");
}
}
-- Part 2: an actor whose receive PARKS mid-message. The park releases
-- the shard: main keeps ticking while this fiber sleeps.
class Sleeper {
fn receive(msg: Tick) {
print("sleeper: down for ${msg.n}ms");
time.sleep(msg.n);
print("sleeper: up");
}
}
fn spin(rounds: Int) {
-- burn reductions so the scheduler's eviction gets a chance: each
-- back-edge pays one reduction; the default budget is 4000
let i = 0;
while i < rounds {
i = i + 1;
}
}
fn main() -> Int {
-- ---- part 1: deterministic budget interleave ----
let c: actor Tick = spawn Counter { label: "count", total: 0 };
send(c, Tick { n: 1 });
send(c, Tick { n: 2 });
send(c, Tick { n: 3 });
-- three yields deliver exactly three messages, one per turn
spin(5000);
spin(5000);
spin(5000);
print("part1 done");
-- ---- part 2: a parked fiber blocks nobody ----
let s: actor Tick = spawn Sleeper {};
send(s, Tick { n: 150 });
let t = 0;
while t < 8 {
time.sleep(25);
print("main tick ${t}");
t = t + 1;
}
print("part2 done");
return 0;
}