diff options
| -rw-r--r-- | compile.c | 4 | ||||
| -rw-r--r-- | environment.c | 2 | ||||
| -rw-r--r-- | stdlib/files.c | 13 | ||||
| -rw-r--r-- | stdlib/files.h | 2 | ||||
| -rw-r--r-- | typecheck.c | 6 |
5 files changed, 19 insertions, 8 deletions
@@ -1429,7 +1429,7 @@ CORD compile_statement(env_t *env, ast_t *ast) case Use: { auto use = Match(ast, Use); if (use->what == USE_LOCAL) { - CORD name = file_base_name(Match(ast, Use)->path); + CORD name = file_base_id(Match(ast, Use)->path); env->code->variable_initializers = CORD_all(env->code->variable_initializers, "$", name, "$$initialize();\n"); } else if (use->what == USE_C_CODE) { return CORD_all("#include \"", use->path, "\"\n"); @@ -1443,7 +1443,7 @@ CORD compile_statement(env_t *env, ast_t *ast) for (size_t i = 0; i < tm_files.gl_pathc; i++) { const char *filename = tm_files.gl_pathv[i]; env->code->variable_initializers = CORD_all( - env->code->variable_initializers, "$", lib_id, "$", file_base_name(filename), "$$initialize();\n"); + env->code->variable_initializers, "$", lib_id, "$", file_base_id(filename), "$$initialize();\n"); } globfree(&tm_files); } diff --git a/environment.c b/environment.c index 96bc51f6..a6b68b91 100644 --- a/environment.c +++ b/environment.c @@ -445,7 +445,7 @@ env_t *load_module_env(env_t *env, ast_t *ast) if (cached) return cached; env_t *module_env = fresh_scope(env); module_env->code = new(compilation_unit_t); - module_env->namespace = new(namespace_t, .name=file_base_name(name)); + module_env->namespace = new(namespace_t, .name=file_base_id(name)); module_env->namespace_bindings = module_env->locals; Table$str_set(module_env->imports, name, module_env); diff --git a/stdlib/files.c b/stdlib/files.c index 4a4220e7..cf777689 100644 --- a/stdlib/files.c +++ b/stdlib/files.c @@ -75,6 +75,19 @@ public char *file_base_name(const char *path) char *buf = GC_MALLOC_ATOMIC(len+1); strncpy(buf, path, len); buf[len] = '\0'; + return buf; +} + +public char *file_base_id(const char *path) +{ + const char *slash = strrchr(path, '/'); + if (slash) path = slash + 1; + assert(!isdigit(*path)); + const char *end = strchrnul(path, '.'); + size_t len = (size_t)(end - path); + char *buf = GC_MALLOC_ATOMIC(len+1); + strncpy(buf, path, len); + buf[len] = '\0'; for (char *p = buf; *p; p++) { if (!isalnum(*p)) *p = '_'; diff --git a/stdlib/files.h b/stdlib/files.h index f650f78e..68827c2a 100644 --- a/stdlib/files.h +++ b/stdlib/files.h @@ -18,6 +18,8 @@ typedef struct { char *resolve_path(const char *path, const char *relative_to, const char *system_path); __attribute__((pure, nonnull)) char *file_base_name(const char *path); +__attribute__((pure, nonnull)) +char *file_base_id(const char *path); __attribute__((nonnull)) file_t *load_file(const char *filename); __attribute__((nonnull, returns_nonnull)) diff --git a/typecheck.c b/typecheck.c index a5d0d2e8..aefd94cf 100644 --- a/typecheck.c +++ b/typecheck.c @@ -166,11 +166,7 @@ static env_t *load_module(env_t *env, ast_t *module_ast) ast_t *ast = parse_file(filename, NULL); if (!ast) errx(1, "Could not compile file %s", filename); env_t *module_file_env = fresh_scope(module_env); - char *file_prefix = GC_strdup(file_base_name(filename)); - for (char *p = file_prefix; *p; p++) { - if (!isalnum(*p) && *p != '_' && *p != '$') - *p = '_'; - } + char *file_prefix = file_base_id(filename); module_file_env->namespace = new(namespace_t, .name=file_prefix); env_t *subenv = load_module_env(module_file_env, ast); for (int64_t j = 0; j < subenv->locals->entries.length; j++) { |
