bindings: GLFW and Vulkan, generated with snake_case names

`use glfw` and `use vulkan` load the native libraries and expose every
function with a snake_case name, plus constants, struct sizes and field
offsets.  Generated from the official headers/registry, not hand-written:

- tools/gen_glfw.py: parses glfw3.h -> std/glfw/glfw.cat (124 fns +
  GLFW_* constants + GLFWvidmode/GLFWgammaramp offsets).
- tools/gen_vulkan.py: parses vk.xml -> std/vulkan/vulkan.cat (650
  commands + 1454 struct sizes/offsets + enum constants), with a
  recursive x86-64 layout engine that resolves nested structs and
  macro-sized arrays (sizes verified against the C headers).
- ffi: `memory` values pass their address where a pointer is expected;
  `ffi.pointer(addr, name, sig)` binds a call at a raw address (Vulkan
  dispatch).  `VkResult`/enum returns are ints.
- compiler: a keyword is allowed as a member name after `.` /
  method-call position (glfw.false, glfw.visible).
- vendor/lib/libglfw.so.3: the host has no libglfw, so it is vendored.

Proved live: GLFW 3.4 window + framebuffer under xvfb-run; a Vulkan
1.0 instance + physical-device enumeration on lavapipe (headless).
tests/glfw.cat, tests/vulkan.cat wired into the suite (125 green).
This commit is contained in:
Minus committed 2026-11-17 18:48:32 +00:00
1 parent 210054f46c
commit 04009c3c5e
18 files changed
+13539 -2

No files matched your search

+9 -2
View File
@@ -400,7 +400,11 @@ static void primary(Compiler *c)
for (;;) {
if (check(c, CAT_LEXER_TOK_DOT)) {
next(c);
if (check(c, CAT_LEXER_TOK_NAME)) {
/* a member name may be a keyword (glfw.false, obj.end);
* the lexer keeps the text of keyword tokens too. */
if (c->tok.kind == CAT_LEXER_TOK_NAME ||
(c->tok.kind >= CAT_LEXER_TOK_AND &&
c->tok.kind <= CAT_LEXER_TOK_EXTENDS)) {
int k = add_string(c, c->tok.text);
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, c->tok.line);
next(c);
@@ -951,7 +955,10 @@ static void stat(Compiler *c)
for (;;) {
CAT_LEXER_Token attr, nx;
cat_lexer_next(&c->lx, &attr);
if (attr.kind != CAT_LEXER_TOK_NAME)
/* a member name may be a keyword (glfw.false = 0) */
if (attr.kind != CAT_LEXER_TOK_NAME &&
!(attr.kind >= CAT_LEXER_TOK_AND &&
attr.kind <= CAT_LEXER_TOK_EXTENDS))
break;
if (nchain < 16)
snprintf(chain[nchain], sizeof(chain[0]),
+51
View File
@@ -363,6 +363,27 @@ CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name,
return &f->base;
}
CAT_VALUE_Value *cat_types_ffi_bind_addr(unsigned long long addr,
const char *name, const char *sig)
{
CAT_TYPES_FFI_Function *f;
if (addr == 0)
return cat_types_empty();
f = calloc(1, sizeof(*f));
if (f == NULL)
return cat_types_empty();
f->base.vtable = &g_function_vtable;
f->sym = (void *)(size_t)addr;
f->name = strdup(name ? name : "?");
f->sig = sig ? strdup(sig) : NULL;
if (prepare(f, sig) != 0) {
function_destroy(&f->base);
return cat_types_empty();
}
return &f->base;
}
/* `ffi::load(path)` module function (static access).
*
* `::` sends the module as the receiver, so with `ffi::load("x")` the
@@ -390,6 +411,33 @@ static int ffi_load_native(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
return 0;
}
/* `ffi::pointer(addr, name, sig)` -- bind a call at a raw address, e.g.
* a Vulkan function pointer from vkGetDeviceProcAddr. */
static int ffi_pointer_native(CAT_VM_State *vm, CAT_VALUE_Value **args,
int nargs, CAT_VALUE_Value **out)
{
/* `ffi::pointer(...)` sends the module as receiver, so skip a
* non-number leading argument. */
int base = 0;
unsigned long long addr;
const char *name = NULL;
const char *sig = NULL;
(void)vm;
if (nargs >= 1 && args[0] != NULL && args[0]->vtable != NULL &&
args[0]->vtable->kind != CAT_VALUE_KIND_NUMBER)
base = 1;
if (nargs < base + 3) {
*out = cat_types_empty();
return 0;
}
addr = (unsigned long long)cat_types_number_as_int(args[base]);
name = cat_types_text_bytes(args[base + 1]);
sig = cat_types_text_bytes(args[base + 2]);
*out = cat_types_ffi_bind_addr(addr, name, sig);
return 0;
}
CAT_VALUE_Value *cat_types_ffi_module(void)
{
CAT_VALUE_Value *m = cat_types_table_new();
@@ -398,5 +446,8 @@ CAT_VALUE_Value *cat_types_ffi_module(void)
cat_error_clear(&e);
cat_types_table_set(m, "load",
cat_types_native_new("load", ffi_load_native), &e);
cat_types_table_set(m, "pointer",
cat_types_native_new("pointer", ffi_pointer_native),
&e);
return m;
}