diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-07 10:08:25 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-07 10:08:25 -0500 |
commit | e12ddf6125a465d95c043b070838849cd9689eb7 (patch) | |
tree | a437f324b3a2ec5da79680714a359b76b4316581 | |
parent | b22a2b562bdb2f3ca4a6ae304f9231d48f441f9d (diff) |
more importing
-rw-r--r-- | package.c | 4 | ||||
-rw-r--r-- | types.c | 25 |
2 files changed, 26 insertions, 3 deletions
@@ -605,3 +605,7 @@ static bool exptr_finish(Exporter *ex) { return true; } + +static void import_pkg(Package *p, FILE *f) { + idents_create(&p->idents); +} @@ -873,11 +873,30 @@ static bool types_expr(Typer *tr, Expression *e) { err_print(name_expr->where, "Package name has negative length (" I64_FMT ")!", name_str.n); return false; } - char *name_cstr = err_malloc((size_t)name_str.n + 1); - memcpy(name_cstr, name_str.data, (size_t)name_str.n); + size_t name_str_len = (size_t)name_str.n; + char *name_cstr = err_malloc(name_str_len + 1); + memcpy(name_cstr, name_str.data, name_str_len); name_cstr[name_str.n] = '\0'; - Identifier name_ident = ident_insert(tr->idents, &name_cstr); + /* advance copy so we can free the original */ + char *copy = name_cstr; + Identifier name_ident = ident_insert(tr->idents, ©); + free(name_cstr); e->pkg.name_ident = name_ident; + if (!name_ident->pkg) { + char *filename = err_malloc(name_str_len + 5); + Package *pkg = name_ident->pkg = allocr_calloc(tr->allocr, 1, sizeof *pkg); + memcpy(filename, name_str.data, name_str_len); + strcpy(filename + name_str.n, ".top"); + /* TODO: library paths */ + FILE *fp = fopen(filename, "rb"); + if (!fp) { + err_print(e->where, "Could not open package: %s (does this file exist?)", filename); + free(filename); + return false; + } + import_pkg(pkg, fp); + fclose(fp); + } } break; case EXPR_EACH: { EachExpr *ea = e->each; |