Bug fixes in package system; package graph cycles are resolved
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 24 Jul 2020 16:08:30 +0000 (11:08 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 24 Jul 2020 16:08:30 +0000 (11:08 -0500)
docs/plan
include/onyxastnodes.h
include/onyxparser.h
include/onyxsempass.h
onyx
progs/ez.onyx
src/onyx.c
src/onyxparser.c
src/onyxsymres.c
src/onyxutils.c

index 57afcee5360206d64200c3b204f9ce061e1c6293..fd4b099f22f621ddfe506beea97a78eb2b58431b 100644 (file)
--- 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
 
index d35b05461080e15559393e416d95ebbc1810ab87..f2ac24e1820854b4767183914f199da0eae93a46 100644 (file)
@@ -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
index 7eeac8b03a5143b373cb59fc75b06fdb14e5325a..de20a794e07dffd43839586377d696d4ebae0b4f 100644 (file)
@@ -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;
index 3dce4427e7633024b7f306c29cd57fbc544aebd7..f2863d657d7aeda5c7f487a7212a118628ce3d74 100644 (file)
@@ -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 0c456e0e69af43e9faf543b9e2890a03e216d548..c60887d932277951aff5e9f3da8d50586f2ce6ce 100755 (executable)
Binary files a/onyx and b/onyx differ
index e7d016778787241c72a401c998894115ebd833a8..40df5fc8017ef7d4bc8b3098610264c1ee66255c 100644 (file)
@@ -31,4 +31,5 @@ proc #export "main" {
     p.print(1234);
 
     print_f32(123.0f);
-}
\ No newline at end of file
+}
+
index 9a1046ccee23b0f0091000d7e4bab052a6f90471..177d18b3c27a2bf1783e22c0c82096363cae527b 100644 (file)
@@ -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: {
index 0867da82eafae01e0f1da125a160417c783dfe07..fb6792e13ed44e6cbc55917d1885e1c1cf621344 100644 (file)
@@ -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;
index 2ed0ae792dee6a177df233bdbc61e5e20a272c3f..801da72e7ee2cca8805699d83e894d93d73f8afa 100644 (file)
@@ -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;
index f5c27cd4a15545f662e4a46c2d8958b208324753..ec1233f59965200e5362f0fe3fdee7f33ec0a43d 100644 (file)
@@ -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);