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>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-30 00:26:19 +03:00
parent ff15c4e01d
commit a38195128f
235 changed files with 24676 additions and 7762 deletions

View file

@ -0,0 +1,10 @@
program T {
entry {
print(Color.rgb(255, 128, 64)) # 16744512
print(Color.rgba(255, 128, 64, 64)) # 1090486336
print(Color.with_alpha(0xFF8040, 64)) # 1090486336
print(Color.lerp(0x000000, 0xFFFFFF, 0.5)) # 8355711
print(Color.darken(0xFFFFFF, 0.5)) # 8355711
print(Color.lighten(0x000000, 0.5)) # 8355711
}
}

View file

@ -0,0 +1,22 @@
program T {
entry {
print(Math.floor(Ease.in(0.5) * 1000.0)) # 250
print(Math.floor(Ease.out(0.5) * 1000.0)) # 750
print(Math.floor(Ease.in_out(0.5) * 1000.0)) # 500
print(Math.floor(Ease.in(1.0) * 1000.0)) # 1000
print(Math.floor(Ease.out(1.0) * 1000.0)) # 1000
print(Math.floor(Ease.bounce(1.0) * 1000.0)) # 999
print(Math.floor(Ease.bounce(0.5) * 1000.0)) # 765
print(Math.floor(Ease.back(0.5) * 1000.0)) # -88
print(Math.floor(Ease.back(1.0) * 1000.0)) # 1000
print(Collide.rects(0, 0, 10, 10, 5, 5, 10, 10)) # 1
print(Collide.rects(0, 0, 10, 10, 20, 20, 5, 5)) # 0
print(Collide.point_rect(5, 5, 0, 0, 10, 10)) # 1
print(Collide.point_rect(15, 5, 0, 0, 10, 10)) # 0
print(Collide.point_rect(10, 5, 0, 0, 10, 10)) # 0
print(Collide.circles(0, 0, 5, 6, 0, 5)) # 1
print(Collide.circles(0, 0, 5, 20, 0, 5)) # 0
print(Collide.rect_circle(0, 0, 10, 10, 15, 5, 6)) # 1
print(Collide.rect_circle(0, 0, 10, 10, 20, 5, 6)) # 0
}
}

View file

@ -11,6 +11,6 @@ program T {
print(((4 | 1) & 6)) # 4
let p = bytes(16)
p[1] = 9999
print(p[1]) # 9999
print(p[1]) # 15 (a byte buffer truncates: 9999 & 0xFF)
}
}

27
selfhost/tests/math.ludic Normal file
View file

@ -0,0 +1,27 @@
program T {
entry {
# min / max / abs / clamp, now reachable under the Math namespace
print(Math.min(3, 7)) # 3
print(Math.max(3, 7)) # 7
print(Math.abs(0 - 5)) # 5
print(Math.clamp(12, 0, 10)) # 10
# sign -> -1 / 0 / 1
print(Math.sign(0 - 4)) # -1
print(Math.sign(0)) # 0
print(Math.sign(9)) # 1
# floor / ceil / round on Q16.16 fixed
print(Math.floor(2.7)) # 2
print(Math.ceil(2.1)) # 3
print(Math.round(2.5)) # 3
print(Math.round(2.4)) # 2
# lerp(0, 10, 0.5) = 5.0
print(Math.floor(Math.lerp(0.0, 10.0, 0.5))) # 5
# inverse_lerp(0, 10, 2.5) = 0.25 -> *100 = 25
print(Math.floor(Math.inverse_lerp(0.0, 10.0, 2.5) * 100.0)) # 25
# remap(5, [0..10] -> [0..100]) = 50.0
print(Math.floor(Math.remap(5.0, 0.0, 10.0, 0.0, 100.0))) # 50
}
}

View file

@ -0,0 +1,20 @@
program T {
entry {
print(Math.floor(Math.sqrt(2.0) * 1000.0)) # 1414
print(Math.floor(Math.sqrt(9.0))) # 3
print(Math.floor(Math.sin(1.5707963) * 1000.0)) # ~999 (sin pi/2)
print(Math.floor(Math.cos(0.0) * 1000.0)) # ~999 (cos 0)
print(Math.floor(Math.sin(0.5235987) * 1000.0)) # ~499 (sin 30deg)
print(Math.floor(Math.hypot(3.0, 4.0))) # 5
print(Math.floor(Math.dist(0.0, 0.0, 3.0, 4.0))) # 5
print(Math.floor(Math.dist2(0.0, 0.0, 3.0, 4.0))) # 25
print(Math.floor(Math.deg_to_rad(180.0) * 1000.0)) # 3142
print(Math.floor(Math.rad_to_deg(3.1415926))) # 180
print(Math.posmod(0 - 1, 5)) # 4
print(Math.wrap(12, 0, 10)) # 2
print(Math.ping_pong(13, 10)) # 7
print(Math.floor(Math.snapped(2.3, 0.5) * 10.0)) # 25
print(Math.floor(Math.move_toward(0.0, 10.0, 3.0) * 10.0)) # 30
print(Math.floor(Math.smoothstep(0.0, 10.0, 5.0) * 1000.0)) # 500
}
}

View file

@ -0,0 +1,11 @@
program T {
entry {
print(Math.round(Math.rad_to_deg(Math.atan2(1.0, 1.0)))) # 45
print(Math.round(Math.rad_to_deg(Math.atan2(1.0, 0.0)))) # 90
print(Math.round(Math.rad_to_deg(Math.atan2(0.0, 0.0 - 1.0)))) # 180
print(Math.round(Math.rad_to_deg(Math.atan2(0.0 - 1.0, 0.0 - 1.0)))) # -135
print(Math.round(Math.rad_to_deg(Math.asin(0.5)))) # 30
print(Math.round(Math.rad_to_deg(Math.acos(0.5)))) # 60
print(Math.round(Math.rad_to_deg(Math.asin(1.0)))) # 90
}
}

View file

@ -0,0 +1,19 @@
program T {
entry {
let b = Mem.bytes(8)
Mem.poke(b, 0, 65)
Mem.poke(b, 1, 66)
print(Mem.peek(b, 0)) # 65
print(Mem.peek(b, 1)) # 66
Mem.fill(b, 0, 8)
print(Mem.peek(b, 0)) # 0
let c = Mem.bytes(8)
Mem.poke(b, 3, 99)
Mem.copy(c, b, 8)
print(Mem.peek(c, 3)) # 99
let w = Mem.words(4)
w[0] = 12345
print(w[0]) # 12345
print(Sys.arg_count()) # 1
}
}

View file

@ -0,0 +1,44 @@
program T {
entry {
# ---- List.* over a []int slice ----
let xs = new []int
push(xs, 10)
push(xs, 20)
push(xs, 30)
print(List.len(xs)) # 3
print(List.first(xs)) # 10
print(List.last(xs)) # 30
print(List.contains(xs, 20)) # 1
print(List.contains(xs, 99)) # 0
print(List.index_of(xs, 30)) # 2
print(List.index_of(xs, 99)) # -1
List.swap(xs, 0, 2) # [30, 20, 10]
print(List.first(xs)) # 30
print(List.last(xs)) # 10
List.reverse(xs) # [10, 20, 30]
print(List.first(xs)) # 10
print(List.last(xs)) # 30
print(List.pop(xs)) # 30 -> [10, 20]
print(List.len(xs)) # 2
List.clear(xs)
print(List.len(xs)) # 0
# ---- Text.* over a str ----
let s = "hello world"
print(Text.length(s)) # 11
print(Text.char_at(s, 0)) # 104 ('h')
print(Text.starts_with(s, "hello")) # 1
print(Text.starts_with(s, "world")) # 0
print(Text.ends_with(s, "world")) # 1
print(Text.ends_with(s, "hello")) # 0
print(Text.contains(s, "lo w")) # 1
print(Text.contains(s, "xyz")) # 0
print(Text.index_of(s, "world")) # 6
print(Text.index_of(s, "xyz")) # -1
print(Text.to_int("42abc")) # 42
print(Text.length(Text.slice(s, 0, 5))) # 5
print(Text.equals(Text.slice(s, 0, 5), "hello")) # 1
print(Text.length(Text.concat("ab", "cd"))) # 4
print(Text.length(Text.from_int(1234))) # 4
}
}

View file

@ -0,0 +1,41 @@
program T {
entry {
# ---- Text builders (upper/lower/trim/repeat/pad) ----
print(Text.length(Text.upper("abc"))) # 3
print(Text.equals(Text.upper("aBc"), "ABC")) # 1
print(Text.equals(Text.lower("aBc"), "abc")) # 1
print(Text.equals(Text.trim(" hi "), "hi")) # 1
print(Text.length(Text.trim(" hi "))) # 2
print(Text.equals(Text.repeat("ab", 3), "ababab")) # 1
print(Text.length(Text.repeat("ab", 3))) # 6
print(Text.equals(Text.pad_left("7", 3), " 7")) # 1
print(Text.equals(Text.pad_right("7", 3), "7 ")) # 1
print(Text.length(Text.pad_left("7", 3))) # 3
print(Text.equals(Text.pad_left("toolong", 3), "toolong")) # 1
# ---- List insert/remove_at/remove/sort ----
let xs = new []int
push(xs, 3); push(xs, 1); push(xs, 2)
List.sort(xs)
print(List.first(xs)) # 1
print(List.last(xs)) # 3
print(List.index_of(xs, 2)) # 1
List.insert(xs, 1, 99)
print(List.len(xs)) # 4
print(List.index_of(xs, 99)) # 1
print(List.index_of(xs, 2)) # 2
List.remove_at(xs, 0)
print(List.first(xs)) # 99
print(List.len(xs)) # 3
List.remove(xs, 2)
print(List.len(xs)) # 2
print(List.contains(xs, 2)) # 0
print(List.last(xs)) # 3
let ys = new []int
push(ys, 5); push(ys, 2); push(ys, 8); push(ys, 1); push(ys, 9); push(ys, 3)
List.sort(ys)
print(List.first(ys)) # 1
print(List.last(ys)) # 9
print(List.index_of(ys, 5)) # 3
}
}

View file

@ -0,0 +1,17 @@
program T {
entry {
print(Text.equals(Text.replace("a.b.c", ".", "-"), "a-b-c")) # 1
print(Text.equals(Text.replace("hello", "l", "L"), "heLLo")) # 1
print(Text.equals(Text.replace("hello", "x", "y"), "hello")) # 1
let parts = Text.split("a,b,c", ",")
print(List.len(parts)) # 3
print(Text.equals(parts[0], "a")) # 1
print(Text.equals(parts[2], "c")) # 1
let one = Text.split("noseps", ",")
print(List.len(one)) # 1
print(Text.equals(one[0], "noseps")) # 1
print(Text.equals(Text.join(parts, "-"), "a-b-c")) # 1
print(Text.equals(Text.join(parts, ""), "abc")) # 1
print(Text.equals(Text.join(Text.split("x/y/z", "/"), "/"), "x/y/z")) # 1
}
}