a standard library in Finch: path, time, proc, json

import json   import path   import time   import proc  -- found in std/ after the program's own files,
so a program's own json.fch always wins.

path: join, base, dir, ext, stem, parts, clean, is_abs.
time: now, millis, sleep, date, local, format, days_in_month (clock_gettime, strftime through C import).
proc: run, output (an error with the command's output when it fails), env, pid, cwd.
json: parse (errors with line and column), stringify, pretty, new_*, get/at/set/push/has/names and
str_or/int_or/float_or/bool_or; \u escapes with surrogate pairs.

The C importer now leaves a plain (non-const) char * parameter a ptr[char], a buffer C writes into;
only const char * is text going in. str(ptr) and str([]u8) are in the self-hosted compiler.

boot.sh: 40 passed, 0 failed.
This commit is contained in:
Nexoniarz 2026-10-25 20:58:26 +01:00
commit ea6d2a57e5
13 changed files with 929 additions and 9 deletions

View file

@ -3729,6 +3729,14 @@ fn call(ptr[Gen] g, ast.Node c) -> Val {
} else if k == "bool" {
z := op(g, "zext i1 " + v.v + " to i32")
emit(g, "call void @finch_str_from_bool(ptr " + out + ", i32 " + z + ")")
} else if k == "ptr" {
// a C string, copied
store(g, "{ ptr, i64, i64 }", "zeroinitializer", out)
emit(g, "call void @finch_str_from_c(ptr " + out + ", ptr " + v.v + ")")
} else if k == "array" && (v.ty.elem[0].kind == "u8" || v.ty.elem[0].kind == "char") {
// bytes become text
store(g, "{ ptr, i64, i64 }", "zeroinitializer", out)
emit(g, "call void @finch_str_from_bytes(ptr " + out + ", ptr " + spill(g, v) + ")")
} else if k == "bool" || k == "char" || k == "array" || k == "map" || k == "struct" || k == "enum" {
store(g, "{ ptr, i64, i64 }", "zeroinitializer", out)
emitFormat(g, out, v.v, v.ty, false)
@ -4430,6 +4438,7 @@ fn runtimeDecls() -> str {
"declare i32 @printf(ptr, ...)",
"declare i32 @fflush(ptr)",
"declare void @finch_str_from_c(ptr, ptr)",
"declare void @finch_str_from_bytes(ptr, ptr)",
"declare void @exit(i32) noreturn",
"declare void @free(ptr)",
]