The compiler carries its runtime and std inside itself; host module for the system it runs on

src/embedded.fch (made by tools/pack.fch, checked by tests/run.sh) holds runtime/finch_rt.c and std/; when they aren't
beside the binary they're written to the cache folder once. All the sh/cmd specifics of the driver (quoting, temp dir,
mkdir, rm, mv, null device, path separator, cache dir, binary stdio) moved into host.fch / host_windows.fch.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015f73FesGZGYPkmDvUcj6QX
This commit is contained in:
Nexoniarz 2026-10-25 22:32:02 +01:00
commit 837ad45177
12 changed files with 354 additions and 59 deletions

54
tools/pack.fch Normal file
View file

@ -0,0 +1,54 @@
// finch run tools/pack.fch src/embedded.fch runtime/finch_rt.c std/json.fch ...
// Writes a Finch module that holds the given files as text, so the compiler can carry its runtime and standard library
// inside itself (bootstrap/build.sh runs this; tests/run.sh checks the result is up to date).
fn literal(str text) -> str {
[]str parts = ["\""]
for c in text {
if c == '\\' {
parts.push("\\\\")
} else if c == '"' {
parts.push("\\\"")
} else if c == '\n' {
parts.push("\\n")
} else if c == '\t' {
parts.push("\\t")
} else if c == '\r' {
parts.push("\\r")
} else {
parts.push(str(c))
}
}
parts.push("\"")
return parts.join("")
}
fn main([]str args) -> int {
if args.len < 3 {
print("usage: pack out.fch file...")
return 1
}
[]str out = [
"// The runtime and the standard library, as text. Made by tools/pack.fch: don't edit it, run bootstrap/build.sh.",
"// Used when the compiler can't find runtime/ and std/ next to itself.",
"",
"fn names() -> []str {",
" []str list",
]
for i in 2..args.len {
out.push(" list.push(" + literal(args[i]) + ")")
}
out.push(" return list")
out.push("}")
out.push("")
out.push("fn texts() -> []str {")
out.push(" []str list")
for i in 2..args.len {
out.push(" list.push(" + literal(read_file(args[i])) + ")")
}
out.push(" return list")
out.push("}")
out.push("")
write_file(args[1], out.join("\n"))
return 0
}