boot: C import (import "x.h", link, C functions, constants, globals, .ptr, ptr(x))

The self-hosted compiler reads headers with boot/cimport.fch and compiles calls to C
functions (variadics, char* as str, C's default promotions), enum and #define
constants, extern variables like stdout, C structs laid out as C laid them (padding
and fixed arrays, by pointer), .ptr of a str or array, ptr(x) and int(ptr). link
"lib" asks pkg-config, link "x.c" compiles the file along.

cimport and c_pointers pass. Not yet: structs by value (the calling convention) and
addr(fn) for C callbacks, which is also what stops it building itself.

boot.sh now says so when stage 2 can't be made and runs the tests on stage 1.
This commit is contained in:
Nexoniarz 2026-10-25 19:44:58 +01:00
commit 13ef63a586
6 changed files with 408 additions and 32 deletions

View file

@ -159,10 +159,18 @@ fn parseProgram(ptr[Parser] p) -> ast.Program {
while !at(p, "eof") {
if at(p, "import") {
t := next(p)
if !at(p, "ident") {
fail(p, t, "the self-hosted compiler only imports Finch modules (import name)")
if at(p, "string") {
prog.cwhere.push(p.path + ":" + str(t.line) + ":" + str(t.col))
prog.cimports.push(next(p).text)
} else {
if !at(p, "ident") {
fail(p, t, "expected a module name or a C header after 'import'")
}
prog.imports.push(next(p).text)
}
prog.imports.push(next(p).text)
} else if at(p, "link") {
next(p)
prog.links.push(expect(p, "string", "a library or C file name, like link \"glfw\"").text)
} else if at(p, "struct") {
prog.structs.push(parseStruct(p))
} else {