diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-03-19 23:29:32 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-03-19 23:29:32 -0400 |
| commit | a47bd2d569e24b74bbb925b934bc835b4eea4de9 (patch) | |
| tree | e692a68f65b428aef889b9992473b0a7c1bc9314 | |
| parent | 80ea0f85079eaac027b930b7d54df46e2039e7c3 (diff) | |
Improvements to module imports
| -rw-r--r-- | ast.c | 2 | ||||
| -rw-r--r-- | ast.h | 2 | ||||
| -rw-r--r-- | compile.c | 16 | ||||
| -rw-r--r-- | parse.c | 5 | ||||
| -rw-r--r-- | typecheck.c | 12 |
5 files changed, 21 insertions, 16 deletions
@@ -141,7 +141,7 @@ CORD ast_to_cord(ast_t *ast) T(Index, "(indexed=%r, index=%r)", ast_to_cord(data.indexed), ast_to_cord(data.index)) T(FieldAccess, "(fielded=%r, field=%s)", ast_to_cord(data.fielded), data.field) T(DocTest, "(expr=%r, output=%r)", ast_to_cord(data.expr), Text__quoted(data.output, true)) - T(Use, "(%r)", Text__quoted(data.path, true)) + T(Use, "(%r)", Text__quoted(data.raw_path, true)) T(LinkerDirective, "(%r)", Text__quoted(data.directive, true)) T(InlineCCode, "(%r)", Text__quoted(data.code, true)) #undef T @@ -262,7 +262,7 @@ struct ast_s { bool skip_source:1; } DocTest; struct { - const char *path; + const char *raw_path; file_t *file; bool main_program; } Use; @@ -246,10 +246,15 @@ CORD compile_statement(env_t *env, ast_t *ast) auto decl = Match(ast, Declare); if (decl->value->tag == Use) { auto use = Match(decl->value, Use); - const char *path = use->path; // TODO: make relative to current file! - env->code->imports = CORD_all(env->code->imports, "#include \"", path, ".h\"\n"); - env->code->object_files = CORD_all(env->code->object_files, "'", path, ".o' "); + const char *path = use->raw_path; const char *name = file_base_name(path); + if (strncmp(path, "./", 2) == 0 || strncmp(path, "../", 3) == 0) { + env->code->imports = CORD_all(env->code->imports, "#include \"", path, ".h\"\n"); + env->code->object_files = CORD_all(env->code->object_files, "'", path, ".o' "); + } else { + env->code->imports = CORD_all(env->code->imports, "#include <", path, ".h>\n"); + env->code->object_files = CORD_all(env->code->object_files, "-l", name, " "); + } return CORD_all(name, "$use();\n"); } else { type_t *t = get_type(env, decl->value); @@ -1770,8 +1775,6 @@ module_code_t compile_file(ast_t *ast) env->file_prefix = heap_strf("%s$", name); Table_str_set(env->imports, name, env); - CORD_appendf(&env->code->imports, "#include <tomo/tomo.h>\n"); - for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { bind_statement(env, stmt->ast); CORD code = compile_statement(env, stmt->ast); @@ -1784,7 +1787,7 @@ module_code_t compile_file(ast_t *ast) .object_files=env->code->object_files, .header=CORD_all( // CORD_asprintf("#line 0 %r\n", Text__quoted(ast->file->filename, false)), - env->code->imports, "\n", + "#include <tomo/tomo.h>\n", env->code->typedefs, "\n", env->code->typecode, "\n", env->code->fndefs, "\n", @@ -1792,6 +1795,7 @@ module_code_t compile_file(ast_t *ast) ), .c_file=CORD_all( // CORD_asprintf("#line 0 %r\n", Text__quoted(ast->file->filename, false)), + env->code->imports, "\n", env->code->staticdefs, "\n", env->code->funcs, "\n", env->code->typeinfos, "\n", @@ -1871,11 +1871,8 @@ PARSER(parse_use) { parser_err(ctx, start, pos, "There is no filename here to use"); char *path = heap_strf("%.*s.tm", (int)path_len, pos); pos += path_len; - char *resolved_path = resolve_path(path, ctx->file->filename, getenv("USE_PATH")); - if (!resolved_path) - parser_err(ctx, start, pos, "No such file exists: \"%s\"", path); while (match(&pos, ";")) continue; - return NewAST(ctx->file, start, pos, Use, .path=resolved_path); + return NewAST(ctx->file, start, pos, Use, .raw_path=path); } PARSER(parse_linker) { diff --git a/typecheck.c b/typecheck.c index eee5f6f3..00e7c662 100644 --- a/typecheck.c +++ b/typecheck.c @@ -207,7 +207,7 @@ void bind_statement(env_t *env, ast_t *statement) } case Use: { auto use = Match(statement, Use); - const char *name = file_base_name(use->path); + const char *name = file_base_name(use->raw_path); if (Table_str_get(*env->imports, name)) break; @@ -217,8 +217,12 @@ void bind_statement(env_t *env, ast_t *statement) const char *my_name = heap_strn(CORD_to_const_char_star(env->file_prefix), CORD_len(env->file_prefix)-1); Table_str_set(module_env->imports, my_name, env); - file_t *f = load_file(use->path); - if (!f) errx(1, "No such file: %s", use->path); + const char *resolved_path = resolve_path(use->raw_path, statement->file->filename, getenv("USE_PATH")); + if (!resolved_path) + code_err(statement, "No such file exists: \"%s\"", use->raw_path); + + file_t *f = load_file(resolved_path); + if (!f) errx(1, "No such file: %s", resolved_path); ast_t *ast = parse_file(f, NULL); if (!ast) errx(1, "Could not compile!"); @@ -550,7 +554,7 @@ type_t *get_type(env_t *env, ast_t *ast) return Type(VoidType); } case Use: { - const char *path = Match(ast, Use)->path; + const char *path = Match(ast, Use)->raw_path; return Type(ModuleType, file_base_name(path)); } case Return: case Stop: case Skip: { |
