harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit adcd7db262ffb1deed24044d882cbbb8028f62b0
parent e495ace2dbaff1913aa0b862ae0624227cb78abd
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 22 Jan 2021 10:36:11 -0500

check modules, insert symbols into subunit scope

Diffstat:
Minclude/ast.h | 8+++-----
Minclude/check.h | 2+-
Msrc/check.c | 44++++++++++++++++++++++++++++++--------------
Msrc/mod.c | 6+++---
4 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/include/ast.h b/include/ast.h @@ -18,11 +18,9 @@ enum ast_import_mode { struct ast_imports { struct location loc; enum ast_import_mode mode; - union { - struct identifier ident; - struct identifier *alias; - struct ast_imports *members; - }; + struct identifier ident; + struct identifier *alias; + struct ast_imports *members; struct ast_imports *next; }; diff --git a/include/check.h b/include/check.h @@ -74,7 +74,7 @@ struct unit { struct ast_expression; struct ast_unit; -void check(struct type_store *ts, +struct scope *check(struct type_store *ts, const struct ast_unit *aunit, struct unit *unit); diff --git a/src/check.c b/src/check.c @@ -1420,8 +1420,10 @@ scan_function(struct context *ctx, const struct ast_function_decl *decl) struct identifier ident = {0}; if (decl->symbol) { ident.name = strdup(decl->symbol); - } else { + } else if (!decl->ident.ns) { mkident(ctx, &ident, &decl->ident); + } else { + ident = decl->ident; } scope_insert(ctx->unit, O_DECL, &ident, &decl->ident, fntype, NULL); @@ -1527,7 +1529,30 @@ scan_declarations(struct context *ctx, const struct ast_decls *decls) trleave(TR_SCAN, NULL); } -void +static void +load_import(struct ast_imports *import, + struct type_store *ts, struct scope *scope) +{ + struct scope *mod = module_resolve(&import->ident, ts); + + switch (import->mode) { + case AST_IMPORT_IDENTIFIER: + for (struct scope_object *obj = mod->objects; + obj; obj = obj->next) { + scope_insert(scope, obj->otype, &obj->ident, + &obj->name, obj->type, obj->value); + } + break; + case AST_IMPORT_ALIAS: + assert(0); // TODO + case AST_IMPORT_MEMBERS: + assert(0); // TODO + } + + scope_free(mod); +} + +struct scope * check(struct type_store *ts, const struct ast_unit *aunit, struct unit *unit) { struct context ctx = {0}; @@ -1555,18 +1580,7 @@ check(struct type_store *ts, const struct ast_unit *aunit, struct unit *unit) for (struct ast_imports *imports = su->imports; imports; imports = imports->next) { - struct scope *mod; - switch (imports->mode) { - case AST_IMPORT_IDENTIFIER: - mod = module_resolve(&imports->ident, ts); - break; - case AST_IMPORT_ALIAS: - assert(0); // TODO - case AST_IMPORT_MEMBERS: - assert(0); // TODO - } - - (void)mod; // TODO: Populate subunit scope + load_import(imports, ts, ctx.scope); } scan_declarations(&ctx, &su->decls); @@ -1587,4 +1601,6 @@ check(struct type_store *ts, const struct ast_unit *aunit, struct unit *unit) trleave(TR_CHECK, NULL); scope = scope->next; } + + return ctx.unit; } diff --git a/src/mod.c b/src/mod.c @@ -39,7 +39,6 @@ open_typedefs(struct identifier *ident) const char *ipath = ident_to_path(ident); snprintf(path, sizeof(path), pathfmt, ipath, version); free(version); - return path; } @@ -54,7 +53,8 @@ module_resolve(struct identifier *ident, struct type_store *store) lex_init(&lexer, f, path); parse(&lexer, &aunit.subunits); lex_finish(&lexer); - (void)aunit; - assert(0); // TODO + // TODO: Free unused bits + struct unit u = {0}; + return check(store, &aunit, &u); }