From: Brendan Hansen Date: Fri, 18 Sep 2020 00:44:55 +0000 (-0500) Subject: added #private_file; 'use package' is at file scope, not package scope X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c828683504e32ac5ebdaf9b6369d1609a5651cd1;p=onyx.git added #private_file; 'use package' is at file scope, not package scope --- diff --git a/CHANGELOG b/CHANGELOG index bde7b6c3..f895af03 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,10 +7,12 @@ Additions: * Added println to core library; print followed by a newline. * Added tests/ folder and runtests.sh which will compile and execute the programs in the folder and test against their expected output. +* #private_file for specifying symbols at the file scope Removals: Changes: +* BREAKING: 'use package' now places included symbols at the file scope, not package scope. * Switched to using TCC as the primary compiler, while maintaining support for GCC. * boolean literals are compile time known so they can be used at top level. diff --git a/core/i32map.onyx b/core/i32map.onyx index 05f6e566..e5551190 100644 --- a/core/i32map.onyx +++ b/core/i32map.onyx @@ -82,14 +82,14 @@ i32map_clear :: proc (use imap: ^I32Map($T)) { // Private symbols // -#private +#private_file I32MapLookupResult :: struct { hash_index : i32 = -1; entry_index : i32 = -1; entry_prev : i32 = -1; } -#private +#private_file i32map_lookup :: proc (use imap: ^I32Map($T), key: i32) -> I32MapLookupResult { lr := I32MapLookupResult.{}; diff --git a/core/ptrmap.onyx b/core/ptrmap.onyx index ba91a797..2cfc01b7 100644 --- a/core/ptrmap.onyx +++ b/core/ptrmap.onyx @@ -83,14 +83,14 @@ ptrmap_clear :: proc (use pmap: ^PtrMap) { // Private symbols // -#private +#private_file PtrMapLookupResult :: struct { hash_index : i32 = -1; entry_index : i32 = -1; entry_prev : i32 = -1; } -#private +#private_file ptrmap_lookup :: proc (use pmap: ^PtrMap, key: rawptr) -> PtrMapLookupResult { lr := PtrMapLookupResult.{}; diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 08067a3c..d86b9405 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -157,38 +157,39 @@ typedef enum AstFlags { Ast_Flag_Const = BH_BIT(3), Ast_Flag_Comptime = BH_BIT(4), Ast_Flag_Private_Package = BH_BIT(5), + Ast_Flag_Private_File = BH_BIT(6), // Global flags - Ast_Flag_Global_Stack_Top = BH_BIT(6), - Ast_Flag_Global_Stack_Base = BH_BIT(7), + Ast_Flag_Global_Stack_Top = BH_BIT(7), + Ast_Flag_Global_Stack_Base = BH_BIT(8), // Function flags - Ast_Flag_Inline = BH_BIT(8), - Ast_Flag_Intrinsic = BH_BIT(9), - Ast_Flag_Function_Used = BH_BIT(10), - Ast_Flag_No_Stack = BH_BIT(11), + Ast_Flag_Inline = BH_BIT(9), + Ast_Flag_Intrinsic = BH_BIT(10), + Ast_Flag_Function_Used = BH_BIT(11), + Ast_Flag_No_Stack = BH_BIT(12), // Expression flags - Ast_Flag_Expr_Ignored = BH_BIT(12), - Ast_Flag_Param_Use = BH_BIT(13), - Ast_Flag_Address_Taken = BH_BIT(14), + Ast_Flag_Expr_Ignored = BH_BIT(13), + Ast_Flag_Param_Use = BH_BIT(14), + Ast_Flag_Address_Taken = BH_BIT(15), // Type flags - Ast_Flag_Type_Is_Resolved = BH_BIT(15), + Ast_Flag_Type_Is_Resolved = BH_BIT(16), // Enum flags - Ast_Flag_Enum_Is_Flags = BH_BIT(16), + Ast_Flag_Enum_Is_Flags = BH_BIT(17), // Struct flags - Ast_Flag_Struct_Is_Union = BH_BIT(17), + Ast_Flag_Struct_Is_Union = BH_BIT(18), - Ast_Flag_No_Clone = BH_BIT(18), + Ast_Flag_No_Clone = BH_BIT(19), - Ast_Flag_Cannot_Take_Addr = BH_BIT(19), + Ast_Flag_Cannot_Take_Addr = BH_BIT(20), - Ast_Flag_Arg_Is_VarArg = BH_BIT(20), + Ast_Flag_Arg_Is_VarArg = BH_BIT(21), - Ast_Flag_Struct_Mem_Used = BH_BIT(21), + Ast_Flag_Struct_Mem_Used = BH_BIT(22), } AstFlags; typedef enum UnaryOp { @@ -679,6 +680,7 @@ typedef enum EntityType { typedef struct Entity { EntityType type; Package *package; + Scope *scope; union { AstUsePackage *use_package; @@ -698,10 +700,7 @@ typedef struct Entity { struct Package { char *name; - bh_arr(Package *) unqualified_uses; - Scope *scope; - Scope *include_scope; Scope *private_scope; }; diff --git a/include/onyxparser.h b/include/onyxparser.h index 2d5766a0..18b38ebf 100644 --- a/include/onyxparser.h +++ b/include/onyxparser.h @@ -33,6 +33,7 @@ typedef struct OnyxParser { ProgramInfo *program; Package *package; + Scope *file_scope; // NOTE: not used since all tokens are lexed before parsing starts OnyxTokenizer *tokenizer; diff --git a/onyx b/onyx index a18d32f4..9975660a 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyx.c b/src/onyx.c index 89bc81b8..5036c03c 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -226,6 +226,7 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res AstKind nkind = node->kind; ent.package = n->package; + ent.scope = n->scope; switch (nkind) { case Ast_Kind_Function: { diff --git a/src/onyxparser.c b/src/onyxparser.c index 63436cb7..b4e0ea94 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -110,7 +110,7 @@ static OnyxToken* soft_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) { .package = parser->package, - .scope = parser->package->scope, + .scope = parser->file_scope, .node = node, })); } @@ -1784,9 +1784,13 @@ static AstTyped* parse_top_level_expression(OnyxParser* parser) { // 'use' // :: static AstNode* parse_top_level_statement(OnyxParser* parser) { - b32 is_private = 0; + AstFlags private_kind = 0; if (parse_possible_directive(parser, "private")) { - is_private = 1; + private_kind = Ast_Flag_Private_Package; + } + + else if (parse_possible_directive(parser, "private_file")) { + private_kind = Ast_Flag_Private_File; } switch ((u16) parser->curr->type) { @@ -1853,8 +1857,7 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) { AstTyped* node = parse_top_level_expression(parser); - if (is_private) - node->flags |= Ast_Flag_Private_Package; + node->flags |= private_kind; if (node->kind == Ast_Kind_Function) { AstFunction* func = (AstFunction *) node; @@ -1908,8 +1911,7 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) { } } - if (is_private) - memres->flags |= Ast_Flag_Private_Package; + memres->flags |= private_kind; add_node_to_process(parser, (AstNode *) memres); @@ -2025,6 +2027,8 @@ ParseResults onyx_parse(OnyxParser *parser) { parser->package = package; } + parser->file_scope = scope_create(parser->allocator, parser->package->private_scope); + AstUsePackage* implicit_use_builtin = make_node(AstUsePackage, Ast_Kind_Use_Package); implicit_use_builtin->package = (AstPackage *) &builtin_package_node; add_node_to_process(parser, (AstNode *) implicit_use_builtin); @@ -2042,11 +2046,18 @@ ParseResults onyx_parse(OnyxParser *parser) { case Ast_Kind_Include_Folder: bh_arr_push(parser->results.includes, (AstInclude *) curr_stmt); break; + case Ast_Kind_Binding: { if (((AstBinding *) curr_stmt)->node->flags & Ast_Flag_Private_Package) { symbol_introduce(parser->package->private_scope, ((AstBinding *) curr_stmt)->token, ((AstBinding *) curr_stmt)->node); + + } else if (((AstBinding *) curr_stmt)->node->flags & Ast_Flag_Private_File) { + symbol_introduce(parser->file_scope, + ((AstBinding *) curr_stmt)->token, + ((AstBinding *) curr_stmt)->node); + } else { symbol_introduce(parser->package->scope, ((AstBinding *) curr_stmt)->token, diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 85a4bd6a..c28519cd 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -631,7 +631,7 @@ static void symres_use_package(AstUsePackage* package) { pac_node->package = p; pac_node->token = package->alias; - symbol_introduce(semstate.curr_package->include_scope, package->alias, (AstNode *) pac_node); + symbol_introduce(semstate.curr_scope, package->alias, (AstNode *) pac_node); } if (package->only != NULL) { @@ -643,24 +643,12 @@ static void symres_use_package(AstUsePackage* package) { return; } - symbol_introduce(semstate.curr_package->include_scope, (*alias)->alias, thing); + symbol_introduce(semstate.curr_scope, (*alias)->alias, thing); } } if (package->alias == NULL && package->only == NULL) { - b32 already_included = 0; - bh_arr_each(Package *, included_package, semstate.curr_package->unqualified_uses) { - if (*included_package == p) { - already_included = 1; - break; - } - } - - if (already_included) return; - - scope_include(semstate.curr_package->include_scope, p->scope); - - bh_arr_push(semstate.curr_package->unqualified_uses, p); + scope_include(semstate.curr_scope, p->scope); } } @@ -729,7 +717,7 @@ void onyx_resolve_symbols() { bh_arr_each(Entity, entity, semstate.program->entities) { if (entity->package) { - scope_enter(entity->package->private_scope); + scope_enter(entity->scope); semstate.curr_package = entity->package; } diff --git a/src/onyxutils.c b/src/onyxutils.c index f15c0930..b13db668 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -122,11 +122,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->include_scope = scope_create(alloc, parent_scope); - package->scope = scope_create(alloc, package->include_scope); + package->scope = scope_create(alloc, parent_scope); package->private_scope = scope_create(alloc, package->scope); - package->unqualified_uses = NULL; - bh_arr_new(global_heap_allocator, package->unqualified_uses, 4); bh_table_put(Package *, prog->packages, pac_name, package);