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
+81 -8

No files matched your search

+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. */