memory: a bounds-checked buffer + hex literals (FFI foundation)

To bind GLFW and Vulkan, the FFI needs to pass *structs*, not just
scalars.  This adds the memory-safe foundation for that.

- cat/types/memory.{h,c}: the `memory` type -- an owned byte buffer
  with named accessors (u8..u64, i8..i64, f32, f64, text, set_text,
  ptr, size, fill, copy).  Every access is bounds-checked and raises
  an error instead of corrupting memory.  `ptr` exposes the address for
  an FFI call; values outlive the call, so the pointer stays valid.
- ffi.c: a `memory` value where a pointer is expected now passes its
  buffer address -- the bridge that lets structs reach native code.
- lexer: 0x/0b/0o integer literals (0xAB), with `_` separators.
- stdlib: `memory(n)` constructor as a global.
- tests/memory.cat: reads/writes, zeroing, fill, out-of-bounds raising
  (119 total, all green).

Proved live: glfwGetVersion(major,minor,rev) called through catscript
FFI + memory -> 3, 4, 0 (GLFW 3.4).
This commit is contained in:
Minus committed 2026-11-17 18:44:32 +00:00
1 parent 83629d0e1e
commit 210054f46c
8 files changed
+476 -9

No files matched your search

+27 -1
View File
@@ -128,7 +128,33 @@ int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out)
return 0;
}
/* numbers: int or float */
/* numbers: int or float, with 0x/0b/0o integer bases */
if (c == '0' && (peek2(lx) == 'x' || peek2(lx) == 'X' ||
peek2(lx) == 'b' || peek2(lx) == 'B' ||
peek2(lx) == 'o' || peek2(lx) == 'O')) {
int base = 16;
int n = 0;
int marker = peek2(lx);
out->text[n++] = (char)advance(lx); /* '0' */
out->text[n++] = (char)advance(lx); /* x/b/o */
if (marker == 'b' || marker == 'B')
base = 2;
else if (marker == 'o' || marker == 'O')
base = 8;
while (isxdigit(peek(lx)) || peek(lx) == '_') {
int d = peek(lx);
advance(lx);
if (d == '_')
continue;
if (n < (int)sizeof(out->text) - 1)
out->text[n++] = (char)d;
}
out->text[n] = '\0';
/* skip the 0x/0b/0o prefix for strtoll */
out->kind = CAT_LEXER_TOK_INT;
out->ival = strtoll(out->text + 2, NULL, base);
return 0;
}
if (isdigit(c)) {
int isfloat = 0;
int n = 0;