added: qualified import of top-level package
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 27 Mar 2023 03:07:45 +0000 (22:07 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 27 Mar 2023 03:07:45 +0000 (22:07 -0500)
compiler/include/astnodes.h
compiler/src/parser.c
compiler/src/symres.c

index fbc658bae881fe71c077ad1210e0d07aef360bd9..07c52d8a32352e3f06a60a922ac7e06e31337bd9 100644 (file)
@@ -1194,7 +1194,16 @@ struct AstImport {
     //
     //     use core {package, *}
     //
-    b32 also_import_package;
+    b32 import_package_itself;
+
+    // Set to be the symbol that the package will
+    // be imported as. If NULL, the last token of
+    // the package path is used. The following
+    // imports the core package as C.
+    // 
+    //     use core { C :: package }
+    //
+    OnyxToken *qualified_package_name;
 };
 
 
index 45779b356febc7b9b23058744bd08616fdb28ff4..948c2388059c3a3800f710c4a7c332f7a138ac94 100644 (file)
@@ -3619,14 +3619,23 @@ static void parse_import_statement(OnyxParser* parser, OnyxToken *token) {
     import_node->flags |= Ast_Flag_Comptime;
     import_node->token = token;
     import_node->imported_package = package_node;
-    import_node->also_import_package = 1;
+    import_node->import_package_itself = 1;
 
     if (consume_token_if_next(parser, '{')) {
         import_node->specified_imports = 1;
-        import_node->also_import_package = 0;
+        import_node->import_package_itself = 0;
 
-        if (consume_token_if_next(parser, Token_Type_Keyword_Package)) {
-            import_node->also_import_package = 1;
+        if (next_tokens_are(parser, 4, Token_Type_Symbol, ':', ':', Token_Type_Keyword_Package)) {
+            import_node->qualified_package_name = expect_token(parser, Token_Type_Symbol);
+            consume_tokens(parser, 3);
+
+            import_node->import_package_itself = 1;
+            if (parser->curr->type != '}')
+                expect_token(parser, ',');
+        }
+
+        else if (consume_token_if_next(parser, Token_Type_Keyword_Package)) {
+            import_node->import_package_itself = 1;
             if (parser->curr->type != '}')
                 expect_token(parser, ',');
         }
index b83792b57524415f13d9fac380271acc73ef4f3e..6a543a4639cf0247b3879f62cc50a41c48f9c7da 100644 (file)
@@ -1668,10 +1668,13 @@ static SymresStatus symres_import(AstImport* import) {
     AstPackage* package = import->imported_package;
     SYMRES(package, package);
 
-    if (import->also_import_package) {
+    if (import->import_package_itself) {
+        OnyxToken *name = bh_arr_last(package->path);
+        name = import->qualified_package_name ?: name;    // Had to find somewhere to use the Elvis operator in the codebase :)
+
         symbol_introduce(
                 current_entity->scope,
-                bh_arr_last(package->path),
+                name,
                 (AstNode *) package);
     }