aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ast.c2
-rw-r--r--ast.h2
-rw-r--r--compile.c16
-rw-r--r--parse.c5
-rw-r--r--typecheck.c12
5 files changed, 21 insertions, 16 deletions
diff --git a/ast.c b/ast.c
index 59ba8db2..dd227a58 100644
--- a/ast.c
+++ b/ast.c
@@ -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
diff --git a/ast.h b/ast.h
index 45a192ad..bd7213ea 100644
--- a/ast.h
+++ b/ast.h
@@ -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;
diff --git a/compile.c b/compile.c
index 5ebbf93e..39ddb34b 100644
--- a/compile.c
+++ b/compile.c
@@ -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",
diff --git a/parse.c b/parse.c
index 426f5cae..60707613 100644
--- a/parse.c
+++ b/parse.c
@@ -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: {