From: Brendan Hansen Date: Mon, 27 Mar 2023 03:07:45 +0000 (-0500) Subject: added: qualified import of top-level package X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9a21c1d6340e56496fa9155e440c3b07b2a9590d;p=onyx.git added: qualified import of top-level package --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index fbc658ba..07c52d8a 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -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; }; diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 45779b35..948c2388 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -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, ','); } diff --git a/compiler/src/symres.c b/compiler/src/symres.c index b83792b5..6a543a46 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -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); }