harec

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

commit c3d4f4e42068ee9a74856d83c2f89c2062029701
parent 5967b6be19f1e8569957e5181889f36f2e057700
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  1 Apr 2021 12:19:47 -0400

Permit some sub-units to have no declarations

Diffstat:
Minclude/ast.h | 2+-
Msrc/check.c | 9+++++++--
Msrc/parse.c | 16++++++++++------
3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/include/ast.h b/include/ast.h @@ -407,7 +407,7 @@ struct ast_decls { struct ast_subunit { struct ast_imports *imports; - struct ast_decls decls; + struct ast_decls *decls; struct ast_subunit *next; }; diff --git a/src/check.c b/src/check.c @@ -3477,7 +3477,7 @@ check_internal(struct type_store *ts, struct unresolveds *new = xcalloc(sizeof(struct unresolveds), 1); - new->unresolved = &su->decls; + new->unresolved = su->decls; new->next = cur; cur = new; @@ -3547,11 +3547,16 @@ check_internal(struct type_store *ts, su; su = su->next) { ctx.scope = scope->scope; trenter(TR_CHECK, "scope %p", ctx.scope); - next_decl = check_declarations(&ctx, &su->decls, next_decl); + next_decl = check_declarations(&ctx, su->decls, next_decl); trleave(TR_CHECK, NULL); scope = scope->next; } + if (!unit->declarations) { + fprintf(stderr, "Error: module contains no declarations\n"); + abort(); + } + return ctx.unit; } diff --git a/src/parse.c b/src/parse.c @@ -2562,24 +2562,28 @@ parse_decl(struct lexer *lexer, struct ast_decl *decl) } static void -parse_decls(struct lexer *lexer, struct ast_decls *decls) +parse_decls(struct lexer *lexer, struct ast_decls **decls) { trenter(TR_PARSE, "decls"); struct token tok = {0}; - struct ast_decls **next = &decls; + struct ast_decls **next = decls; while (tok.token != T_EOF) { + struct ast_decls *decl = *next = + xcalloc(1, sizeof(struct ast_decls)); switch (lex(lexer, &tok)) { case T_EXPORT: - (*next)->decl.exported = true; + decl->decl.exported = true; trace(TR_PARSE, "export"); break; default: unlex(lexer, &tok); break; } - parse_decl(lexer, &(*next)->decl); - next = &(*next)->next; - *next = xcalloc(1, sizeof(struct ast_decls)); + if (tok.token == T_EOF) { + break; + } + parse_decl(lexer, &decl->decl); + next = &decl->next; want(lexer, T_SEMICOLON, NULL); if (lex(lexer, &tok) != T_EOF) { unlex(lexer, &tok);