From: Brendan Hansen Date: Fri, 24 Jul 2020 16:08:30 +0000 (-0500) Subject: Bug fixes in package system; package graph cycles are resolved X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=3457af325dc095e6b35e5d5c84e35a452545b786;p=onyx.git Bug fixes in package system; package graph cycles are resolved --- diff --git a/docs/plan b/docs/plan index 57afcee5..fd4b099f 100644 --- a/docs/plan +++ b/docs/plan @@ -115,7 +115,7 @@ HOW: - Foreign functions will rename in the code because it is turning out to be a nightmare to remove them. Lot's of refactoring... ugh - [ ] Package system + [X] Package system [ ] Enum types diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index d35b0546..f2ac24e1 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -377,7 +377,7 @@ typedef enum EntityType { typedef struct Entity { EntityType type; - Scope *scope; + Package *package; union { AstUsePackage *use_package; @@ -392,7 +392,9 @@ typedef struct Entity { struct Package { char *name; + Scope *scope; + Scope *include_scope; }; // NOTE: Simple data structure for storing what comes out of the parser diff --git a/include/onyxparser.h b/include/onyxparser.h index 7eeac8b0..de20a794 100644 --- a/include/onyxparser.h +++ b/include/onyxparser.h @@ -9,6 +9,7 @@ typedef struct NodeToProcess { Scope *scope; + Package *package; AstNode *node; } NodeToProcess; @@ -26,7 +27,7 @@ typedef struct OnyxParser { bh_allocator allocator; ProgramInfo *program; - Scope *package_scope; + Package *package; // NOTE: not used since all tokens are lexed before parsing starts OnyxTokenizer *tokenizer; diff --git a/include/onyxsempass.h b/include/onyxsempass.h index 3dce4427..f2863d65 100644 --- a/include/onyxsempass.h +++ b/include/onyxsempass.h @@ -16,6 +16,7 @@ typedef struct SemState { ProgramInfo* program; // NOTE: Used in symbol resolution phase + Package* curr_package; Scope* global_scope; Scope* curr_scope; AstFunction* curr_function; diff --git a/onyx b/onyx index 0c456e0e..c60887d9 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/ez.onyx b/progs/ez.onyx index e7d01677..40df5fc8 100644 --- a/progs/ez.onyx +++ b/progs/ez.onyx @@ -31,4 +31,5 @@ proc #export "main" { p.print(1234); print_f32(123.0f); -} \ No newline at end of file +} + diff --git a/src/onyx.c b/src/onyx.c index 9a1046cc..177d18b3 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -184,7 +184,7 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res AstNode* node = n->node; AstKind nkind = node->kind; - ent.scope = n->scope; + ent.package = n->package; switch (nkind) { case Ast_Kind_Function: { diff --git a/src/onyxparser.c b/src/onyxparser.c index 0867da82..fb6792e1 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -75,7 +75,8 @@ static OnyxToken* expect_token(OnyxParser* parser, TokenType token_type) { static void add_node_to_process(OnyxParser* parser, AstNode* node) { bh_arr_push(parser->results.nodes_to_process, ((NodeToProcess) { - .scope = parser->package_scope, + .package = parser->package, + .scope = parser->package->scope, .node = node, })); } @@ -1241,7 +1242,7 @@ ParseResults onyx_parse(OnyxParser *parser) { parser->allocator); token_toggle_end(symbol); - parser->package_scope = package->scope; + parser->package = package; } else { Package *package = program_info_package_lookup_or_create( @@ -1250,7 +1251,7 @@ ParseResults onyx_parse(OnyxParser *parser) { parser->program->global_scope, parser->allocator); - parser->package_scope = package->scope; + parser->package = package; } while (parser->curr->type != Token_Type_End_Stream) { @@ -1262,7 +1263,7 @@ ParseResults onyx_parse(OnyxParser *parser) { switch (curr_stmt->kind) { case Ast_Kind_Include_File: bh_arr_push(parser->results.files, (AstIncludeFile *) curr_stmt); break; case Ast_Kind_Binding: { - symbol_introduce(parser->package_scope, + symbol_introduce(parser->package->scope, ((AstBinding *) curr_stmt)->token, ((AstBinding *) curr_stmt)->node); break; diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 2ed0ae79..801da72e 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -62,7 +62,8 @@ static void symres_overloaded_function(AstOverloadedFunction* ofunc); static void symres_use_package(AstUsePackage* package); static void scope_enter(Scope* new_scope) { - new_scope->parent = semstate.curr_scope; + if (new_scope->parent == NULL) + new_scope->parent = semstate.curr_scope; semstate.curr_scope = new_scope; } @@ -368,8 +369,9 @@ static void symres_use_package(AstUsePackage* package) { if (package->alias != NULL) { AstPackage *pac_node = onyx_ast_node_new(semstate.node_allocator, sizeof(AstPackage), Ast_Kind_Package); pac_node->package = p; + pac_node->token = package->alias; - symbol_introduce(semstate.curr_scope, package->alias, (AstNode *) pac_node); + symbol_introduce(semstate.curr_package->include_scope, package->alias, (AstNode *) pac_node); } if (package->only != NULL) { @@ -382,12 +384,12 @@ static void symres_use_package(AstUsePackage* package) { "not found in package"); return; } - symbol_introduce(semstate.curr_scope, *tkn, thing); + symbol_introduce(semstate.curr_package->include_scope, *tkn, thing); } } if (package->alias == NULL && package->only == NULL) - scope_include(semstate.curr_scope, p->scope); + scope_include(semstate.curr_package->include_scope, p->scope); } void onyx_resolve_symbols() { @@ -402,7 +404,8 @@ void onyx_resolve_symbols() { } bh_arr_each(Entity, entity, semstate.program->entities) { - scope_enter(entity->scope); + scope_enter(entity->package->scope); + semstate.curr_package = entity->package; switch (entity->type) { case Entity_Type_Use_Package: symres_use_package(entity->use_package); break; diff --git a/src/onyxutils.c b/src/onyxutils.c index f5c27cd4..ec1233f5 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -91,7 +91,8 @@ Package* program_info_package_lookup_or_create(ProgramInfo* prog, char* package_ memcpy(pac_name, package_name, strlen(package_name) + 1); package->name = pac_name; - package->scope = scope_create(alloc, parent_scope); + package->include_scope = scope_create(alloc, parent_scope); + package->scope = scope_create(alloc, package->include_scope); bh_table_put(Package *, prog->packages, pac_name, package); @@ -153,7 +154,7 @@ AstNode* symbol_resolve(Scope* start_scope, OnyxToken* tkn) { } } - if (res == NULL ) { + if (res == NULL) { onyx_message_add(Msg_Type_Unknown_Symbol, tkn->pos, tkn->text);