From 83629d0e1efde98f25d4f648141345d10d3b524b Mon Sep 17 00:00:00 2001 From: Minus Date: Tue, 17 Nov 2026 18:40:10 +0000 Subject: [PATCH] imports: `use name` imports by module name `use` only accepted a path expression, so `use math` read `math` as a global and failed. Now the operand may be a bare module name. - compiler: `use NAME` emits the name as a constant (a path expression still works, e.g. `use "std/io/io.cat"` or `use (expr)`). - stdlib: `use_resolve` maps a name to a path, trying, in order: , .cat, std//.cat, /.cat, std/.cat. A path (contains `.cat` or `/`) is used as-is. - stdlib: a missing module now raises (per project rule: no silent failure) -- "cannot import module 'X'" -- instead of doing nothing. - tests/import.cat: `use math`, `use io` and a path string (109 total, all green). --- src/cat/compiler.c | 15 +++++++++---- src/cat/stdlib/stdlib.c | 50 +++++++++++++++++++++++++++++++++++++---- tests/import.cat | 16 +++++++++++++ tests/run_tests.sh | 8 +++++++ 4 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 tests/import.cat diff --git a/src/cat/compiler.c b/src/cat/compiler.c index c83371b..10feaf3 100644 --- a/src/cat/compiler.c +++ b/src/cat/compiler.c @@ -744,12 +744,19 @@ static void stat(Compiler *c) } case CAT_LEXER_TOK_USE: - /* `use expr` imports another catscript source file: it reads, - * compiles and runs that file in the current VM (like dofile), - * so its definitions become available here. */ + /* `use name` imports a module: its source is read, compiled and + * run in the current VM, so its definitions become available + * here. The operand is a module name (`math`) or a path + * (`"std/math/math.cat"`); the runtime resolves names. */ next(c); emit(c, CAT_BYTECODE_OP_GETGLOBAL, add_string(c, "use"), 0, line); - expr(c); + if (check(c, CAT_LEXER_TOK_NAME)) { + emit(c, CAT_BYTECODE_OP_CONST, add_string(c, c->tok.text), + 0, line); + next(c); + } else { + expr(c); + } emit(c, CAT_BYTECODE_OP_CALL, 1, 0, line); emit(c, CAT_BYTECODE_OP_POP, 0, 0, line); accept(c, CAT_LEXER_TOK_SEMI); diff --git a/src/cat/stdlib/stdlib.c b/src/cat/stdlib/stdlib.c index f74e18e..92e2851 100644 --- a/src/cat/stdlib/stdlib.c +++ b/src/cat/stdlib/stdlib.c @@ -446,12 +446,47 @@ static int lf_substr(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, return 0; } +/* Resolve a `use` operand to a readable path. A name like `math` + * resolves (in order) to: the name itself, `.cat`, `std//\ + * .cat`, `/.cat`, and `std/.cat`. A path that + * already contains a `.cat` or a `/` is used as-is. Returns a malloc'd + * path, or NULL when nothing matches. */ +static char *use_resolve(const char *name) +{ + static const char *fmts[] = { + "%s", "%s.cat", "std/%s/%s.cat", "%s/%s.cat", "std/%s.cat", + }; + char buf[1024]; + int i; + + if (strstr(name, ".cat") != NULL || strchr(name, '/') != NULL) { + FILE *f = fopen(name, "rb"); + if (f != NULL) { + fclose(f); + return strdup(name); + } + return NULL; + } + for (i = 0; i < (int)(sizeof(fmts) / sizeof(fmts[0])); i++) { + FILE *f; + snprintf(buf, sizeof(buf), fmts[i], name, name); + f = fopen(buf, "rb"); + if (f != NULL) { + fclose(f); + return strdup(buf); + } + } + return NULL; +} + static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value **out) { FILE *f; long sz; char *src; + char *resolved = NULL; + const char *path; CAT_BYTECODE_Function *fn; CAT_ERROR_Error e; @@ -459,10 +494,16 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, *out = cat_types_empty(); return 0; } - f = fopen(cat_types_text_bytes(args[0]), "rb"); + /* Resolve a module name (`math`) to a path; a path is used as-is. */ + resolved = use_resolve(cat_types_text_bytes(args[0])); + path = (resolved != NULL) ? resolved + : cat_types_text_bytes(args[0]); + f = fopen(path, "rb"); if (f == NULL) { - *out = cat_types_empty(); - return 0; + free(resolved); + return (cat_error_raise(&vm->err, + "cannot import module '%s'", + cat_types_text_bytes(args[0])), -1); } fseek(f, 0, SEEK_END); sz = ftell(f); @@ -487,7 +528,7 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, /* `module` reflects the imported unit while it runs, then the * caller's `module` is restored. The name is the basename. */ { - const char *p = cat_types_text_bytes(args[0]); + const char *p = path; const char *base = strrchr(p, '/'); base = (base != NULL) ? base + 1 : p; cat_module_enter(vm, base, p); @@ -496,6 +537,7 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, cat_vm_run(vm, fn, out); cat_module_leave(vm); free(src); + free(resolved); /* The compiled chunk is intentionally NOT freed: any `function` * definitions in it became globals whose closures point into this * bytecode tree. The chunk lives as long as the VM does. */ diff --git a/tests/import.cat b/tests/import.cat new file mode 100644 index 0000000..8a696c0 --- /dev/null +++ b/tests/import.cat @@ -0,0 +1,16 @@ +-- tests/import.cat -- `use name` imports by module name. +-- +-- The operand is a bare module name (`math`) or a path string. A name +-- resolves (in order) to: , .cat, std//.cat, +-- /.cat, std/.cat. A path is used as-is. + +use math +print("double 21 =", double(21)) +print("square 7 =", square(7)) + +use io +print("join:", join({"a", "b", "c"}, "-")) + +-- a path string still works (used as-is) +use "std/math/math.cat" +print("abs(-5) =", abs(-5)) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index cbc9743..df5d654 100644 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -174,6 +174,14 @@ check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed - out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'` check "repl-echo" "50" "`echo "$out" | sed -n 1p`" check "repl-state" "55" "`echo "$out" | sed -n 2p`" +# ---- imports: `use name` --------------------------------------------- + +out=`$BIN tests/import.cat` +check "import-name" "double 21 = 42" "`echo "$out" | sed -n 1p`" +check "import-sq" "square 7 = 49" "`echo "$out" | sed -n 2p`" +check "import-io" "join: a-b-c" "`echo "$out" | sed -n 3p`" +check "import-path" "abs(-5) = 5" "`echo "$out" | sed -n 4p`" + # ---- the `module` global --------------------------------------------- out=`$BIN tests/module.cat`