added #private_file; 'use package' is at file scope, not package scope
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 18 Sep 2020 00:44:55 +0000 (19:44 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 18 Sep 2020 00:44:55 +0000 (19:44 -0500)
CHANGELOG
core/i32map.onyx
core/ptrmap.onyx
include/onyxastnodes.h
include/onyxparser.h
onyx
src/onyx.c
src/onyxparser.c
src/onyxsymres.c
src/onyxutils.c

index bde7b6c33a6e1b0ebc3e69360ceaafeaaa600af1..f895af03b32f1c103826925f7d0b5f69933c6bbb 100644 (file)
--- 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.
 
index 05f6e5662aa5ac553e667e6aa444fd93aac49c99..e55511904888a66bb751a34ab6c0eb90c44558b0 100644 (file)
@@ -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.{};
 
index ba91a797a01c60d7c157404a530722667ee14da7..2cfc01b7c48cb2dca98538b3bcb2887af4b0fdce 100644 (file)
@@ -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.{};
 
index 08067a3c6a1b023e958a788a302ebc49b8a77a75..d86b940555901fe9ed2f39ee39df467800c66d11 100644 (file)
@@ -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;
 };
 
index 2d5766a0a220b4d1b6ca6adc99ac1d22f6163907..18b38ebfd70b79d42ceeaab8d1c7f1d91c37f54a 100644 (file)
@@ -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 a18d32f484a6e13aa1b36e618dbc8800afab8535..9975660a7ff32b15b7aa1feba2eb5878776fc296 100755 (executable)
Binary files a/onyx and b/onyx differ
index 89bc81b81303d942b93168dfc011ad2c1ee5129a..5036c03c8d56c1e8d2000daee94afbd755cbdf1e 100644 (file)
@@ -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: {
index 63436cb7807dba1795c31467ebcfa2ace1b71f63..b4e0ea9463e076687c6b50f7b4c34720106c2378 100644 (file)
@@ -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' <string>
 // <symbol> :: <expr>
 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,
index 85a4bd6a0a33877a3a81ea15450ab4de299d4fd3..c28519cd22961533e652173dfdd2e8700020771f 100644 (file)
@@ -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;
         }
 
index f15c09300703913e72a05ce466437961205161d9..b13db6683fab5c9f973f8f69caeb076e1118c3da 100644 (file)
@@ -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);