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

@ -38,6 +38,21 @@ struct Loader {
[]ast.Program progs
[]str files
[]str loaded
str std // the standard library's folder, or ""
}
// import json: the file next to the one that imports it, else the standard library's.
// A program's own json.fch wins over the library's.
fn findModule(ptr[Loader] l, str from, str name) -> str {
local := dirOf(from) + "/" + name + ".fch"
if file_exists(local) || l.std.len == 0 {
return local
}
shared := l.std + "/" + name + ".fch"
if file_exists(shared) {
return shared
}
return local
}
fn load(ptr[Loader] l, str path, str module) {
@ -60,7 +75,7 @@ fn load(ptr[Loader] l, str path, str module) {
for name in imports {
if !l.loaded.contains(name) {
l.loaded.push(name)
load(l, dirOf(path) + "/" + name + ".fch", name)
load(l, findModule(l, path, name), name)
}
}
}
@ -149,6 +164,11 @@ fn main([]str args) -> int {
}
loader := Loader()
for cand in [dirOf(args[0]) + "/../std", dirOf(args[0]) + "/std", "std"] {
if loader.std.len == 0 && file_exists(cand + "/path.fch") {
loader.std = cand
}
}
load(addr(loader), file, "")
// C headers, from every file; their folders are searched first for "local.h"