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,7 @@
---
id: sys
title: Sys
order: 6
---
Process and OS access — command-line arguments, environment, exit code, running a shell command, and raw file I/O. Outside the deterministic simulation.

View file

@ -0,0 +1,14 @@
---
id: sys-arg
name: Sys.arg
category: sys
kind: namespace-method
tokens: Sys.arg
sig: Sys.arg(i) -> str
tip: A command-line argument by index.
order: 0
ns: Sys
member: arg
---
Returns the <code>i</code>-th command-line argument; index 0 is the program path.

View file

@ -0,0 +1,14 @@
---
id: sys-arg_count
name: Sys.arg_count
category: sys
kind: namespace-method
tokens: Sys.arg_count
sig: Sys.arg_count() -> int
tip: The number of command-line arguments.
order: 1
ns: Sys
member: arg_count
---
Returns how many command-line arguments were passed, including the program path.

View file

@ -0,0 +1,14 @@
---
id: sys-env
name: Sys.env
category: sys
kind: namespace-method
tokens: Sys.env
sig: Sys.env(name) -> str
tip: Read an environment variable.
order: 4
ns: Sys
member: env
---
Returns the value of environment variable <code>name</code>, or a null string if it is unset.

View file

@ -0,0 +1,14 @@
---
id: sys-exit
name: Sys.exit
category: sys
kind: namespace-method
tokens: Sys.exit
sig: Sys.exit(code) -> void
tip: End the process with a status code.
order: 2
ns: Sys
member: exit
---
Terminates the program immediately, returning <code>code</code> to the shell.

View file

@ -0,0 +1,14 @@
---
id: sys-file_close
name: Sys.file_close
category: sys
kind: namespace-method
tokens: Sys.file_close
sig: Sys.file_close(file) -> void
tip: Close a file.
order: 11
ns: Sys
member: file_close
---
Closes <code>file</code>, flushing any buffered writes.

View file

@ -0,0 +1,14 @@
---
id: sys-file_open
name: Sys.file_open
category: sys
kind: namespace-method
tokens: Sys.file_open
sig: Sys.file_open(path, mode) -> ptr
tip: Open a file.
order: 6
ns: Sys
member: file_open
---
Opens <code>path</code> with the given <code>mode</code> (as C <code>fopen</code>) and returns a file handle, or null on failure.

View file

@ -0,0 +1,14 @@
---
id: sys-file_read
name: Sys.file_read
category: sys
kind: namespace-method
tokens: Sys.file_read
sig: Sys.file_read(file, buf, n) -> int
tip: Read bytes from a file.
order: 7
ns: Sys
member: file_read
---
Reads up to <code>n</code> bytes from <code>file</code> into <code>buf</code> and returns the number read.

View file

@ -0,0 +1,14 @@
---
id: sys-file_seek
name: Sys.file_seek
category: sys
kind: namespace-method
tokens: Sys.file_seek
sig: Sys.file_seek(file, offset, whence) -> int
tip: Move a file's read/write position.
order: 9
ns: Sys
member: file_seek
---
Seeks <code>file</code> to <code>offset</code> relative to <code>whence</code> (as C <code>fseek</code>).

View file

@ -0,0 +1,14 @@
---
id: sys-file_tell
name: Sys.file_tell
category: sys
kind: namespace-method
tokens: Sys.file_tell
sig: Sys.file_tell(file) -> int
tip: The current file position.
order: 10
ns: Sys
member: file_tell
---
Returns the current read/write offset of <code>file</code>.

View file

@ -0,0 +1,14 @@
---
id: sys-file_write
name: Sys.file_write
category: sys
kind: namespace-method
tokens: Sys.file_write
sig: Sys.file_write(file, buf, n) -> int
tip: Write bytes to a file.
order: 8
ns: Sys
member: file_write
---
Writes <code>n</code> bytes from <code>buf</code> to <code>file</code> and returns the number written.

View file

@ -0,0 +1,14 @@
---
id: sys-read_char
name: Sys.read_char
category: sys
kind: namespace-method
tokens: Sys.read_char
sig: Sys.read_char() -> int
tip: Read one byte from standard input.
order: 5
ns: Sys
member: read_char
---
Reads and returns the next byte from standard input, or a negative value at end of input.

View file

@ -0,0 +1,14 @@
---
id: sys-run
name: Sys.run
category: sys
kind: namespace-method
tokens: Sys.run
sig: Sys.run(command) -> int
tip: Run a shell command.
order: 3
ns: Sys
member: run
---
Runs <code>command</code> through the system shell and returns its exit status. Blocks until it finishes.

View file

@ -0,0 +1,14 @@
---
id: sys-stderr
name: Sys.stderr
category: sys
kind: namespace-method
tokens: Sys.stderr
sig: Sys.stderr() -> ptr
tip: The standard error handle.
order: 13
ns: Sys
member: stderr
---
Returns the standard-error file handle, for <code>Sys.file_write</code>.

View file

@ -0,0 +1,14 @@
---
id: sys-stdout
name: Sys.stdout
category: sys
kind: namespace-method
tokens: Sys.stdout
sig: Sys.stdout() -> ptr
tip: The standard output handle.
order: 12
ns: Sys
member: stdout
---
Returns the standard-output file handle, for <code>Sys.file_write</code>.