'builtin' package is implicitly included; multiple unqualified uses are eliminated
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 2 Sep 2020 14:14:55 +0000 (09:14 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 2 Sep 2020 14:14:55 +0000 (09:14 -0500)
17 files changed:
core/file.onyx
core/js/webgl.onyx
core/ptrmap.onyx
core/std/js.onyx
core/std/wasi.onyx
core/sys/js.onyx
core/sys/wasi.onyx
core/wasi.onyx
docs/plan
include/onyxastnodes.h
onyx
progs/poly_test.onyx
progs/wasi_test.onyx
src/onyxbuiltins.c
src/onyxparser.c
src/onyxsymres.c
src/onyxutils.c

index 83ca71885f0477708a2546484785e0fdc758615b..348e3e03762f7656e6fee6651364b0f8a303fb18 100644 (file)
@@ -3,7 +3,7 @@ package core_file
 // Many of these functions will be improved when
 // multiple return values are implemented.
 
-use package builtin
+
 use package core
 use package wasi
 
index 738f8b124127a7a66ccc338381e74d3d664a8f45..d738253c6d6c8e1d57a3c9195b81e8f9bf6769b8 100644 (file)
@@ -1,7 +1,7 @@
 package gl
 // To be used with the corresponding gl.js
 
-use package builtin
+
 
 // ClearBufferMask 
 DEPTH_BUFFER_BIT               :: 0x00000100
index e828771b4e438c89f38edb45b52b5030de31efa9..8951cd00b05bd83fca71a14453e26af72f30665f 100644 (file)
@@ -10,7 +10,7 @@ PtrMapEntry :: struct {
     key   : rawptr;
     value : rawptr;
 
-    next  : i32;    
+    next  : i32;
 }
 
 ptrmap_init :: proc (use pmap: ^PtrMap, hash_count: i32 = 16) {
@@ -95,7 +95,7 @@ PtrMapLookupResult :: struct {
 ptrmap_lookup :: proc (use pmap: ^PtrMap, key: rawptr) -> PtrMapLookupResult {
     lr := PtrMapLookupResult.{};
 
-    hash := cast(u32) 0xcbf29ce4 ^ cast(u32) key;
+    hash := cast(u32) 0xcbf29ce7 ^ cast(u32) key;
 
     lr.hash_index = hash % hashes.count;
     lr.entry_index = hashes[lr.hash_index];
index 69b0473bf91287b015f71ae2554aacf59dbf2375..9df405cc8ed3bc88187365e59f5189a21b01e6fe 100644 (file)
@@ -1,6 +1,6 @@
 package core
 
-use package builtin
+
 
 #include_file "core/alloc"
 #include_file "core/array"
index 0c8d9495150997c01825bbab249fa6771a90bc99..b9f6e40d527bf02dc797dd7b104aa1451e8c63ec 100644 (file)
@@ -1,6 +1,6 @@
 package core
 
-use package builtin
+
 
 #include_file "core/alloc"
 #include_file "core/array"
index 69d471499f06588ffbaef76c20a45d370164d2df..5f9be60bd322510212344fbb9828639bfc31e9f5 100644 (file)
@@ -1,6 +1,6 @@
 package system
 
-use package builtin
+
 use package core
 use package main as main
 
index 828aefb76cbd85c118358ad95f84df4648502f41..eda86edb5a8641ade546a923fabcbe8072e23eda 100644 (file)
@@ -2,7 +2,7 @@ package system
 
 #include_file "core/wasi"
 
-use package builtin
+
 use package wasi
 use package core
 use package main as main
index 088192e61782e3526346a89d43ce6448e156d9ee..307007a340dc3605f8bdae10f7af0e14d33aa444 100644 (file)
@@ -1,6 +1,6 @@
 package wasi
 
-use package builtin
+
 use package core {
     memory_init,
     stdio_init,
index 4e947f7444d0453eb7701943989b844d5f38f832..60b058bfb482a246144ede4f1cf5cc9b16c5b7f3 100644 (file)
--- a/docs/plan
+++ b/docs/plan
@@ -226,7 +226,9 @@ HOW:
         [X] #file and #line directives
             - string and u32 respectively that represent the current file and line number where the directive is
 
-        [ ] ** Put type info in data section so it is runtime accessible
+        [ ] data structure based iteration
+
+        [ ] Put type info in data section so it is runtime accessible
             - type name
             - size
             - alignment
index 84d721f4f102ec3207d008ed82bc81a5db850e1e..34537780b9ba47f827efe402200f1c1ac6e570ba 100644 (file)
@@ -542,6 +542,8 @@ typedef struct Entity {
 struct Package {
     char *name;
 
+    bh_arr(Package *) unqualified_uses;
+
     Scope *scope;
     Scope *include_scope;
     Scope *private_scope;
@@ -575,6 +577,7 @@ extern AstBasicType basic_type_f32;
 extern AstBasicType basic_type_f64;
 extern AstBasicType basic_type_rawptr;
 
+extern AstNode   builtin_package_node;
 extern AstNumLit builtin_heap_start;
 extern AstGlobal builtin_stack_top;
 extern AstType  *builtin_string_type;
diff --git a/onyx b/onyx
index 03ec709f4d09b4175d948a951e143a5b86d003e4..a21c505676db1d50d980aa85b2ca603188a64485 100755 (executable)
Binary files a/onyx and b/onyx differ
index 335d0052c4f507876b3e03c5675cb3c79ac958a3..69f6548875cb68bdc150ae63d574d54a8ccc897e 100644 (file)
@@ -2,7 +2,6 @@ package main
 
 #include_file "core/std/wasi"
 
-use package builtin
 use package core
 
 print_arr_details :: proc (arr: ^[..] $T) {
@@ -75,7 +74,7 @@ main :: proc (args: [] cstring) {
     print_arr_details(^s.b);
 
     for i: 0, 100 {
-        array_push(^s.a, 5 * i);
+        array_push(^s.a, (5 * i) % 21);
         array_push(^s.b, 3l * cast(i64) i);
     }
 
@@ -94,7 +93,7 @@ main :: proc (args: [] cstring) {
     print("\n\n");
 
     map : PtrMap;
-    ptrmap_init(^map);
+    ptrmap_init(^map, 50);
     defer ptrmap_free(^map);
 
     for i: 0, 100 do ptrmap_put(^map, ^s.a[i], ^s.b[i]);
index 45a4cb3132a3071f3509f3a683002a204fb6a5d6..92798e390c578487437dc47c90680ea056621419 100644 (file)
@@ -10,7 +10,7 @@ package main
 #include_file "string"
 #include_file "file"
 
-use package builtin
+
 
 // NOTE: Didn't realize this would work so easily
 use package core { string_builder_append as sba }
index d1cd9261b82eb7f3ad9fb0ecbdaafa5fd9e67186..b1f4e487def45bf5a8f5a95e15d83d2a66e724c3 100644 (file)
@@ -17,6 +17,9 @@ AstBasicType basic_type_f32    = { Ast_Kind_Basic_Type, 0, NULL, "f32"   , &basi
 AstBasicType basic_type_f64    = { Ast_Kind_Basic_Type, 0, NULL, "f64"   , &basic_types[Basic_Kind_F64]   };
 AstBasicType basic_type_rawptr = { Ast_Kind_Basic_Type, 0, NULL, "rawptr", &basic_types[Basic_Kind_Rawptr] };
 
+static OnyxToken builtin_package_token = { Token_Type_Symbol, 7, "builtin ", { 0 } };
+AstNode   builtin_package_node  = { Ast_Kind_Symbol, Ast_Flag_No_Clone, &builtin_package_token, NULL };
+
 static OnyxToken builtin_heap_start_token = { Token_Type_Symbol, 12, "__heap_start ", { 0 } };
 static OnyxToken builtin_stack_top_token  = { Token_Type_Symbol, 11, "__stack_top ",  { 0 } };
 AstNumLit builtin_heap_start  = { Ast_Kind_NumLit, Ast_Flag_Const, &builtin_heap_start_token, NULL, (AstType *) &basic_type_rawptr, NULL, 0 };
@@ -45,6 +48,9 @@ const BuiltinSymbol builtin_symbols[] = {
 };
 
 void initialize_builtins(bh_allocator a, ProgramInfo* prog) {
+    // HACK
+    builtin_package_token.text = bh_strdup(global_heap_allocator, builtin_package_token.text);
+
     BuiltinSymbol* bsym = (BuiltinSymbol *) &builtin_symbols[0];
     while (bsym->sym != NULL) {
         if (bsym->package == NULL)
index 1f12e8516dd8bb5e6e6d78d714079fc8e0ea1bea..3c511d705c28ae4b790a43060d37eea0842b7a35 100644 (file)
@@ -1959,6 +1959,10 @@ ParseResults onyx_parse(OnyxParser *parser) {
         parser->package = package;
     }
 
+    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);
+
     while (parser->curr->type != Token_Type_End_Stream) {
         if (parser->hit_unexpected_token) return parser->results;
 
index 23ba952f34be48e8855268b5b1dc65b0a49b1f5a..ece7115fc82e38492e81b8292ab0a64ddda964ef 100644 (file)
@@ -503,7 +503,13 @@ void symres_function(AstFunction* func) {
             param->local->type = param->default_value->type;
         }
 
-        if (param->local->type == NULL) break;
+        if (param->local->type == NULL) {
+            onyx_report_error(param->local->token->pos,
+                    "Unable to resolve type for parameter, '%b'.\n",
+                    param->local->token->text,
+                    param->local->token->length);
+            return;
+        }
 
         symbol_introduce(semstate.curr_scope, param->local->token, (AstNode *) param->local);
 
@@ -591,8 +597,21 @@ static void symres_use_package(AstUsePackage* package) {
         }
     }
 
-    if (package->alias == NULL && package->only == NULL)
+    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);
+    }
 }
 
 static void symres_enum(AstEnumType* enum_node) {
index a5eec07cf0dc27c0c6d13ab9ad6832cf0eb9f954..9c057fd25f72322de4d9ea924be8bcba01677751 100644 (file)
@@ -122,6 +122,8 @@ Package* program_info_package_lookup_or_create(ProgramInfo* prog, char* package_
         package->include_scope = scope_create(alloc, parent_scope);
         package->scope = scope_create(alloc, package->include_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);