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:
  <name>, <name>.cat, std/<name>/<name>.cat, <name>/<name>.cat,
  std/<name>.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).
This commit is contained in:
Minus committed 2026-11-17 18:40:10 +00:00
1 parent cc893ff677
commit 83629d0e1e
4 files changed
+80 -7

No files matched your search

+10 -3
View File
@@ -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);
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);
+46 -4
View File
@@ -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, `<name>.cat`, `std/<name>/\
* <name>.cat`, `<name>/<name>.cat`, and `std/<name>.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. */
+16
View File
@@ -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: <name>, <name>.cat, std/<name>/<name>.cat,
-- <name>/<name>.cat, std/<name>.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))
+8
View File
@@ -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`