From: Brendan Hansen Date: Mon, 3 Aug 2020 15:00:05 +0000 (-0500) Subject: Added '#private' directive for package limited scope X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=df2e789126ddf775e58d54ea61f518f9d8d0af6d;p=onyx.git Added '#private' directive for package limited scope --- diff --git a/docs/plan b/docs/plan index ebb99d2f..aadfdc33 100644 --- a/docs/plan +++ b/docs/plan @@ -134,7 +134,11 @@ HOW: [X] Deferred statements - [ ] Pointer math + [X] Pointer math + - Addition and subtraction + + [X] #private + - symbol is scoped to package and not brought in from a 'use package' statement [ ] Better checking for casts diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index ccfcb592..3ba79b67 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -131,6 +131,7 @@ typedef enum AstFlags { Ast_Flag_Foreign = BH_BIT(1), Ast_Flag_Const = BH_BIT(2), Ast_Flag_Comptime = BH_BIT(3), + Ast_Flag_Private_Package = BH_BIT(4), // Function flags Ast_Flag_Inline = BH_BIT(8), @@ -437,6 +438,7 @@ struct Package { Scope *scope; Scope *include_scope; + Scope *private_scope; }; // NOTE: Simple data structure for storing what comes out of the parser diff --git a/misc/onyx.sublime-syntax b/misc/onyx.sublime-syntax index 5159d580..67c95740 100644 --- a/misc/onyx.sublime-syntax +++ b/misc/onyx.sublime-syntax @@ -36,6 +36,9 @@ contexts: - match: '\b(-)?[0-9.]+\b' scope: constant.numeric.onyx + - match: '#[a-zA-Z]+' + scope: keyword.other.onyx + double_quoted_string: - meta_scope: string.quoted.double.onyx - match: '\\.' diff --git a/onyx b/onyx index 91353764..6252d1f4 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/alloc_test.onyx b/progs/alloc_test.onyx index 9b2c7cd4..ebb8b131 100644 --- a/progs/alloc_test.onyx +++ b/progs/alloc_test.onyx @@ -13,7 +13,7 @@ deferred_example :: proc -> i32 { walker := cast(^i64) arr; for _: 0, 8 { - print(*(walker + 2)); + print(*walker); walker += 1; } @@ -21,7 +21,7 @@ deferred_example :: proc -> i32 { } proc #export "main" { - asdf :: "staring asdfkjasd asdflkjasdflkajsdflk"; + asdf :: "staring asdfkjasd asdflkjasdflkajsdflya"; heap_init(); print(deferred_example()); diff --git a/src/onyxparser.c b/src/onyxparser.c index 7702fd91..519f3aa6 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1244,6 +1244,11 @@ static AstTyped* parse_top_level_expression(OnyxParser* parser) { // 'use' // :: static AstNode* parse_top_level_statement(OnyxParser* parser) { + b32 is_private = 0; + if (parse_possible_directive(parser, "private")) { + is_private = 1; + } + switch (parser->curr->type) { case Token_Type_Keyword_Use: { OnyxToken* use_token = expect_token(parser, Token_Type_Keyword_Use); @@ -1315,6 +1320,9 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) { AstTyped* node = parse_top_level_expression(parser); + if (is_private) + node->flags |= Ast_Flag_Private_Package; + if (node->kind == Ast_Kind_Function) { AstFunction* func = (AstFunction *) node; @@ -1360,6 +1368,9 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) { memres->token = symbol; memres->type_node = parse_type(parser); + if (is_private) + memres->flags |= Ast_Flag_Private_Package; + add_node_to_process(parser, (AstNode *) memres); AstBinding* binding = make_node(AstBinding, Ast_Kind_Binding); @@ -1451,9 +1462,15 @@ 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, - ((AstBinding *) curr_stmt)->token, - ((AstBinding *) curr_stmt)->node); + 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 { + 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 3015662b..275582a4 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -436,6 +436,7 @@ static void symres_use_package(AstUsePackage* package) { "not found in package"); return; } + symbol_introduce(semstate.curr_package->scope, (*alias)->alias, thing); } } @@ -521,7 +522,7 @@ void onyx_resolve_symbols() { } bh_arr_each(Entity, entity, semstate.program->entities) { - scope_enter(entity->package->scope); + scope_enter(entity->package->private_scope); semstate.curr_package = entity->package; switch (entity->type) { diff --git a/src/onyxutils.c b/src/onyxutils.c index 4c434fde..481e7eea 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -99,6 +99,7 @@ Package* program_info_package_lookup_or_create(ProgramInfo* prog, char* package_ package->name = pac_name; package->include_scope = scope_create(alloc, parent_scope); package->scope = scope_create(alloc, package->include_scope); + package->private_scope = scope_create(alloc, package->scope); bh_table_put(Package *, prog->packages, pac_name, package);