ludic/tools/ludic-cli/prelude.ludic
Orkuncakilkaya f25289db20
Some checks failed
ci / build-and-test (push) Waiting to run
commit-lint / conventional-commits (push) Waiting to run
bootstrap / cfree-fixpoint (push) Has been cancelled
docs / build-and-deploy (push) Successful in 34s
feat(gl): OpenGL 4.1 and the ludic.render3d renderer
`Gl.*` binds the whole OpenGL 4.1 core API — every entry point of the
platform gl3.h with every GL_* constant, generated by `ludic-dev glgen`
with per-call ABI thunks. Windowed builds get an NSOpenGLContext on the
existing window at Retina resolution; headless builds render into an
offscreen CGL context, so a program that uses Gl.* renders and
screenshots identically under the test harness. It links gl.ll, the
thunks and OpenGL.framework only when used; every other build stays
byte-identical.

packages/ludic.render3d is a physically based renderer written on that
surface: HDRI image-based lighting, GPU-generated terrain with scanned
PBR materials, CDLOD, cascaded shadows, glTF with skinning, instanced
vegetation with impostors, procedural grass, water, SSAO, and an HDR
pipeline with bloom, auto-exposure and ACES.

It also carries this session's work on it: the terrain at half its cost
(10.3 -> 5.4 ms of frame), the streaming hitch that got worse the longer
you played, a resize that emptied the world, and the packaging that lets
a game use the renderer from its own repository — `ludic assets`, the
material manifest shipping with the package, and shader lookup falling
back to the install root. See changes/ for each, with its numbers.

The camping game that drove all of it has moved out to its own
repository, Maroon Lake; examples/rendering/smooth.ludic stays as the
renderer's example here.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-10 03:31:12 +03:00

377 lines
13 KiB
Text

# prelude.ludic — the shared runtime for both command-line programs: `ludic`,
# which a user of the language runs, and `ludic-dev`, the contributor tool that
# builds the toolchain itself. Both are native binaries written in Ludic and
# compiled by Ludic, driving clang, the compiler and the unix tools through
# `run`. This fragment is the tiny standard library their commands lean on:
# process control, file IO, strings and a colored PASS/FAIL test harness. It
# carries no ECS, so both link as plain CLI programs.
#
# `ludic`'s commands work from any directory, against whatever toolchain is
# installed. `ludic-dev`'s tasks run relative to the current directory and expect
# the toolchain repo root.
# ---- file IO ----------------------------------------------------------------
# read a whole file into a fresh NUL-terminated buffer (null if it cannot open)
function read_file(path: pointer) -> pointer {
let f = file_open(path, "rb")
if (f == null) { return null }
file_seek(f, 0, 2)
let n = file_tell(f)
file_seek(f, 0, 0)
let buf = bytes(n + 1)
file_read(f, buf, n)
buf[n] = 0
file_close(f)
return buf
}
# overwrite `path` with `s`; returns false if it could not be opened
function write_file(path: pointer, s: pointer) -> bool {
let f = file_open(path, "wb")
if (f == null) { return false }
file_write(f, s, len(s))
file_close(f)
return true
}
# ---- byte strings ------------------------------------------------------------------
# s[a, b) as a fresh NUL-terminated string; out-of-range ends are clamped.
function str_sub(s: pointer, a: int, b: int) -> pointer {
var lo = a
if lo < 0 { lo = 0 }
var hi = b
if hi < lo { hi = lo }
let out = bytes(hi - lo + 1)
var i = lo
var k = 0
while i < hi { out[k] = s[i]; k += 1; i += 1 }
out[k] = 0
return out
}
function str_space(c: int) -> bool { return c == 32 or c == 9 or c == 13 }
function file_exists(path: pointer) -> bool { return shq(`test -e {path}`) }
function is_exec(path: pointer) -> bool { return shq(`test -x {path}`) }
# is `a` newer than `b` (like the shell's `-nt`)?
function newer(a: pointer, b: pointer) -> bool { return shq(`test {a} -nt {b}`) }
# ---- process control --------------------------------------------------------
# `run` returns the raw wait status; the program's exit code is the high byte.
function exit_code(st: int) -> int { return (st >> 8) & 255 }
# run a command, returning its exit code (0 = success)
function sh(cmd: pointer) -> int { return exit_code(run(cmd)) }
# run a command, true when it succeeded
function shq(cmd: pointer) -> bool { return exit_code(run(cmd)) == 0 }
# ---- scratch files ------------------------------------------------------------
# Every scratch file the runner writes lives under one per-process directory
# (`$TMPDIR/x_<pid>`), so `ludic-dev test` and an `x check-*` can run side by side without
# clobbering each other's captures. main removes it on the way out.
var x_tmp: pointer = null
function tmp_dir() -> pointer {
if x_tmp == null {
var base = Os.temp_dir()
if len(base) > 1 and base[len(base) - 1] == '/' { base = base[0..len(base) - 1] } # macOS: TMPDIR ends in '/'
x_tmp = `{base}/ludic_{Os.pid()}`
run(`mkdir -p {x_tmp}`)
}
return x_tmp
}
# a scratch path under tmp_dir(): tmp_path("foo.out")
function tmp_path(name: pointer) -> pointer { return `{tmp_dir()}/{name}` }
# remove the scratch directory (idempotent; a no-op when nothing was written).
# LUDIC_KEEP_TMP=1 leaves it in place, and says where, for debugging a failing case.
function tmp_cleanup() -> void {
if x_tmp == null { return }
if getenv_or("LUDIC_KEEP_TMP", "0") == "1" { print(`scratch kept: {x_tmp}`) }
else { run(`rm -rf {x_tmp}`) }
x_tmp = null
}
# run `cmd` and return its stdout (stderr discarded). Never null.
function capture(cmd: pointer) -> pointer {
let tmp = tmp_path("capture.out")
run(`{cmd} > {tmp} 2>/dev/null`)
let s = read_file(tmp)
if (s == null) { return "" }
return s
}
# run `cmd`, join its output lines with single spaces and trim — the Ludic twin
# of the shell idiom `$(cmd | tr '\n' ' ' | sed 's/ *$//')`.
function capture_line(cmd: pointer) -> pointer {
return capture(`{cmd} | tr '\n' ' ' | sed 's/ *$//'`)
}
# the last line of a command's output (for one-line error messages)
function capture_tail(cmd: pointer) -> pointer {
return capture(`{cmd} 2>&1 | tail -1`)
}
function getenv_or(name: pointer, dflt: pointer) -> pointer {
let v = getenv(name)
if (v == null) { return dflt }
return v
}
# flatten a relative example path into a filesystem-safe token: '/' -> '_', so a
# categorised path like "games/snake" yields a single-segment temp name
# ("games_snake") that never implies a missing /tmp subdirectory.
function flat(p: pointer) -> pointer {
let n = len(p)
let b = bytes(n + 1)
for i in 0 .. n {
var c = p[i]
if (c == '/') { c = 95 } # '/' (47) -> '_' (95)
b[i] = c
}
b[n] = 0
return b
}
# ---- stdout helpers ---------------------------------------------------------
# write `s` with no trailing newline (print() always adds one)
function out(s: pointer) -> void { file_write(file_stdout(), s, len(s)) }
function err(s: pointer) -> void { file_write(file_stderr(), s, len(s)) }
# an ESC byte — the lexer has no \033, so build it by hand
function esc() -> pointer { let b = bytes(2); b[0] = 27; b[1] = 0; return b }
function c_green() -> pointer { return esc() + "[32m" }
function c_red() -> pointer { return esc() + "[31m" }
function c_reset() -> pointer { return esc() + "[0m" }
# ---- the PASS/FAIL test harness ---------------------------------------------
var PASS: int = 0
var FAIL: int = 0
function ok(msg: pointer) -> void {
PASS += 1
print(` {c_green()}PASS{c_reset()} {msg}`)
}
function bad(msg: pointer) -> void {
FAIL += 1
print(` {c_red()}FAIL{c_reset()} {msg}`)
}
function bad2(msg: pointer, detail: pointer) -> void {
bad(msg)
print(` {detail}`)
}
# assert two strings equal, reporting the mismatch
function check(label: pointer, got: pointer, want: pointer) -> void {
if (got == want) { ok(label) }
else { bad2(label, `expected [{want}] got [{got}]`) }
}
# ---- host platform ----------------------------------------------------------
# A few cases exercise macOS-specific runtime ABI — Cocoa windowing, the BSD
# utsname/dirent layout — or compare against renders blessed on macOS. The
# self-hosted compiler and its C-free bootstrap are host-neutral (they produce
# byte-identical IR on any host), so the bulk of the suite runs anywhere; only
# these platform-bound cases are skipped — visibly, never silently — when the
# suite runs off Darwin. That lets a Linux CI runner gate every portable
# guarantee without red from the parts that are macOS-only today.
function host_os() -> pointer { return capture_line("uname -s") }
function is_darwin() -> bool { return host_os() == "Darwin" }
function skip(msg: pointer) -> void { print(` skip {msg}`) }
# print the "== N passed, M failed ==" footer and return the process exit code
function report() -> int {
print("")
print(`== {string(PASS)} passed, {string(FAIL)} failed ==`)
if (FAIL == 0) { return 0 }
return 1
}
# a positional argument, or a default when it is absent
function argn(i: int, dflt: pointer) -> pointer {
if (i < arg_count()) { return arg(i) }
return dflt
}
# ---- the toolchain install ---------------------------------------------------
#
# The user-facing commands run anywhere, so they cannot assume a `bin/` in the
# current directory the way the old repo-root-only task runner did. They ask
# here instead, and get the same answer in both layouts that exist: an install
# (~/.ludic/bin/ludic, root ~/.ludic) and a checkout of the toolchain repo
# (bin/ludic, root the repo). The compiler derives its own root by the identical
# rule (see ludic_home() in selfhost/frontend/parse.ludic), so the two never
# disagree about where the runtime and the bundled packages live.
var g_home: pointer = null
# the directory holding this binary, with a trailing '/'. argv[0] carries no
# directory when the CLI was found on $PATH, which is the normal case for an
# install — ask the shell where it found it.
function self_dir() -> pointer {
var a0 = arg(0)
var slash = -1
var i = 0
while a0[i] != 0 { if a0[i] == '/' { slash = i }; i += 1 }
if slash < 0 {
let found = capture_line(`command -v {a0}`)
if found == "" { return "" }
a0 = found
slash = -1
i = 0
while a0[i] != 0 { if a0[i] == '/' { slash = i }; i += 1 }
if slash < 0 { return "" }
}
return a0[0..slash + 1]
}
# The toolchain install root, with a trailing '/', or "" for the current
# directory. $LUDIC_HOME wins; otherwise the binary's own directory decides, and
# a directory named `bin` means the root is its parent.
function ludic_home() -> pointer {
if g_home != null { return g_home }
let env = getenv("LUDIC_HOME")
if env != null {
var e = env
if len(e) > 0 and e[len(e) - 1] != '/' { e = e + "/" }
g_home = e
return g_home
}
var d = self_dir()
if len(d) >= 4 and d[len(d) - 4..len(d)] == "bin/" { d = d[0..len(d) - 4] }
# a source checkout the CLI was run from elsewhere in: prefer a ./bin here
if not is_exec(`{d}bin/ludicc`) and is_exec("bin/ludicc") { d = "" }
g_home = d
return g_home
}
# the path to a toolchain binary (ludicc, ludic-fmt, ludic-lsp). Falls back to
# the bare name — on $PATH — when the install root has no bin/ of its own.
function tool(name: pointer) -> pointer {
let p = `{ludic_home()}bin/{name}`
if is_exec(p) { return p }
return name
}
function ludicc() -> pointer { return tool("ludicc") }
# true in a checkout of the toolchain repo itself, where a seed is present to
# assemble the compiler from.
function in_toolchain_repo() -> bool {
return file_exists("selfhost/ludicc.seed.ll") and file_exists("tools/ludic-cli/main.ludic")
}
# ---- strings ----------------------------------------------------------------
# Shared by every command, so they live here rather than in whichever file
# happened to need them first.
# length of a NUL-terminated buffer
function slen(s: pointer) -> int { var n = 0; while s[n] != 0 { n += 1 }; return n }
# a fresh NUL-terminated copy of s[start .. end) (end exclusive)
function sslice(s: pointer, start: int, end: int) -> pointer {
if end < start { return "" }
let n = end - start
let b = bytes(n + 1)
var i = 0
while i < n { b[i] = s[start + i]; i += 1 }
b[n] = 0
return b
}
# index of the first byte of `needle` in `hay` at or after `from`, else -1
function s_index(hay: pointer, needle: pointer, from: int) -> int {
let hn = slen(hay)
let nn = slen(needle)
if nn == 0 { return from }
var i = from
while i + nn <= hn {
var j = 0
while j < nn and hay[i + j] == needle[j] { j += 1 }
if j == nn { return i }
i += 1
}
return -1
}
function s_contains(hay: pointer, needle: pointer) -> bool { return s_index(hay, needle, 0) >= 0 }
# does `hay` contain `needle` exactly at position `at`?
function s_starts_at(hay: pointer, at: int, needle: pointer) -> bool {
let nn = slen(needle)
var i = 0
while i < nn { if hay[at + i] != needle[i] { return false }; i += 1 }
return true
}
# does `s` (a whole line) begin with `pre`?
function s_starts(s: pointer, pre: pointer) -> bool {
let pn = slen(pre)
var i = 0
while i < pn { if s[i] != pre[i] { return false }; i += 1 }
return true
}
# is byte c an ASCII space/tab?
function is_ws(c: int) -> bool { return c == ' ' or c == '\t' }
# the substring from `start` up to the next '\n' (or end)
function line_at(s: pointer, start: int) -> pointer {
var e = start
while s[e] != 0 and s[e] != '\n' { e += 1 }
return sslice(s, start, e)
}
# trim leading/trailing ASCII whitespace (space, tab, cr, nl)
function s_trim(s: pointer) -> pointer {
let n = slen(s)
var a = 0
while a < n and (is_ws(s[a]) or s[a] == '\n' or s[a] == '\r') { a += 1 }
var b = n
while b > a and (is_ws(s[b - 1]) or s[b - 1] == '\n' or s[b - 1] == '\r') { b -= 1 }
return sslice(s, a, b)
}
# lowercase ASCII A-Z
function lower_ascii(s: pointer) -> pointer {
let n = slen(s)
let b = bytes(n + 1)
var i = 0
while i < n {
var c = s[i]
if c >= 'A' and c <= 'Z' { c += 32 }
b[i] = c
i += 1
}
b[n] = 0
return b
}
# Python str.title(): capitalise the first letter of each alpha run, lower the rest
function title_case(s: pointer) -> pointer {
let n = slen(s)
let b = bytes(n + 1)
var i = 0
var prev_alpha = false
while i < n {
var c = s[i]
let al = (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z')
if al {
if prev_alpha { if c >= 'A' and c <= 'Z' { c += 32 } }
else { if c >= 'a' and c <= 'z' { c -= 32 } }
}
b[i] = c
prev_alpha = al
i += 1
}
b[n] = 0
return b
}
# ---- the C toolchain --------------------------------------------------------
# clang assembles the emitted IR and drives the linker. -Wno-override-module for
# the same reason the compiler itself passes it (see selfhost/main.ludic): the IR
# names no target triple, so clang substitutes the host's and warns every time.
function cc() -> pointer { return getenv_or("LUDIC_CC", "clang") + " -Wno-override-module" }