ludic/selfhost/emit_ease.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

52 lines
2.5 KiB
Text

# emit_ease.ludic — the Ease.* namespace: tween curves over a normalized amount
# t in 0.0..1.0, returning an eased fixed. All pure Q16.16, deterministic. The
# "juice" layer that makes motion feel good (Robert Penner's easings).
fn is_ease_ns(meth: ptr) -> bool {
if (meth == "in") or (meth == "out") or (meth == "in_out") { return true }
if (meth == "back") or (meth == "bounce") { return true }
return false
}
# n1 * u * u (u a fixed code) -> code of a fixed i32
fn ease_bounce_seg(u: ptr) -> ptr {
let uu = fx_mul_code(u, u)
return fx_mul_code(uu, "495616") # 7.5625 * u*u
}
fn emit_ease_ns(meth: ptr, e: Node) -> Val {
let t = emit_expr(e.kids[0])
if (meth == "in") { # ease-in quad: t*t
return val(fx_mul_code(t.code, t.code), "fixed")
}
if (meth == "out") { # ease-out quad: t*(2 - t)
let inv = emit_bind(`sub i32 131072, {t.code}`)
return val(fx_mul_code(t.code, inv), "fixed")
}
if (meth == "in_out") { # smooth ease-in-out: 3t^2 - 2t^3
let t2 = fx_mul_code(t.code, t.code)
let t3 = fx_mul_code(t2, t.code)
let three = emit_bind(`mul i32 {t2}, 3`)
let two = emit_bind(`mul i32 {t3}, 2`)
return val(emit_bind(`sub i32 {three}, {two}`), "fixed")
}
if (meth == "back") { # ease-in-back (overshoots below 0)
let t2 = fx_mul_code(t.code, t.code)
let t3 = fx_mul_code(t2, t.code)
let a = fx_mul_code(t3, "177051") # 2.70158 * t^3
let b = fx_mul_code(t2, "111515") # 1.70158 * t^2
return val(emit_bind(`sub i32 {a}, {b}`), "fixed")
}
# ease-out bounce: four parabolic segments, selected by t (all computed, then
# picked branch-free). Shifts/offsets are the standard 2.75-denominator set.
let sA = ease_bounce_seg(t.code)
let uB = emit_bind(`sub i32 {t.code}, 35747`); let sB0 = ease_bounce_seg(uB); let sB = emit_bind(`add i32 {sB0}, 49152`)
let uC = emit_bind(`sub i32 {t.code}, 53620`); let sC0 = ease_bounce_seg(uC); let sC = emit_bind(`add i32 {sC0}, 61440`)
let uD = emit_bind(`sub i32 {t.code}, 62557`); let sD0 = ease_bounce_seg(uD); let sD = emit_bind(`add i32 {sD0}, 64512`)
let cCD = emit_bind(`icmp slt i32 {t.code}, 59578`)
let rCD = emit_bind(`select i1 {cCD}, i32 {sC}, i32 {sD}`)
let cB = emit_bind(`icmp slt i32 {t.code}, 47663`)
let rB = emit_bind(`select i1 {cB}, i32 {sB}, i32 {rCD}`)
let cA = emit_bind(`icmp slt i32 {t.code}, 23831`)
return val(emit_bind(`select i1 {cA}, i32 {sA}, i32 {rB}`), "fixed")
}