small code change for big improvements
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 6 Jan 2022 00:00:58 +0000 (18:00 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 6 Jan 2022 00:00:58 +0000 (18:00 -0600)
core/container/heap.onyx
core/container/set.onyx
src/checker.c
src/clone.c
src/entities.c
src/symres.c
src/utils.c

index ae8c388ddbcd06e58a096b39fd2ada482b88e8d5..05f0fc33d7449ccc9ad38fe93b68c02234b6e9d3 100644 (file)
@@ -15,12 +15,12 @@ make :: ($T: type_expr, cmp: (T, T) -> i32 = null_proc) -> Heap(T) {
     return h;
 }
 
-init :: (use heap: ^Heap($T), cmp: (T, T) -> i32 = null_proc) {
+init :: (use heap: ^Heap, cmp: (heap.T, heap.T) -> i32 = null_proc) {
     array.init(^data);
     compare = cmp;
 }
 
-insert :: (use heap: ^Heap($T), v: T) {
+insert :: (use heap: ^Heap, v: heap.T) {
     data << v;
     shift_up(heap, data.count - 1);
 }
index dbc5ad6e578311bb41799c2812722e777b90e62a..fcf4ac74a1063da673312f3f9119d43e3440f28d 100644 (file)
@@ -13,13 +13,13 @@ use package core.intrinsics.onyx { __zero_value }
     T == T;
 }
 
-Set :: struct (T: type_expr) where SetValue(T) {
+Set :: struct (Elem_Type: type_expr) where SetValue(Elem_Type) {
     allocator : Allocator;
 
     hashes  : [] i32;
-    entries : [..] Entry(T);
+    entries : [..] Entry(Elem_Type);
 
-    default_value: T;
+    default_value: Elem_Type;
 
     Entry :: struct (T: type_expr) {
         next  : i32;
@@ -56,7 +56,7 @@ free :: (use set: ^Set) {
     array.free(^entries);
 }
 
-insert :: (use set: ^Set($T), value: T) {
+insert :: (use set: ^Set, value: set.Elem_Type) {
     if hashes.data == null do init(set);
     lr := lookup(set, value);
 
@@ -70,17 +70,17 @@ insert :: (use set: ^Set($T), value: T) {
 
 #operator << macro (set: Set($T), value: T) do (package core.set).insert(^set, value);
 
-has :: (use set: ^Set($T), value: T) -> bool {
+has :: (use set: ^Set, value: set.Elem_Type) -> bool {
     lr := lookup(set, value);
     return lr.entry_index >= 0;
 }
 
-get :: (use set: ^Set($T), value: T) -> T {
+get :: (use set: ^Set, value: set.Elem_Type) -> set.Elem_Type {
     lr := lookup(set, value);
     return entries[lr.entry_index].value if lr.entry_index >= 0 else __zero_value(T);
 }
 
-remove :: (use set: ^Set($T), value: T) {
+remove :: (use set: ^Set, value: set.Elem_Type) {
     lr := lookup(set, value);
     if lr.entry_index < 0 do return;
 
@@ -118,7 +118,7 @@ iterator :: (set: ^Set($T)) -> Iterator(T) {
     context.set = set;
     context.position = 0;
 
-    next :: ($T: type_expr, use context: ^Context(T)) -> (T, bool) {
+    next :: (use context: ^Context($T)) -> (T, bool) {
         if position < set.entries.count {
             defer position += 1;
             return set.entries[position].value, true;
@@ -128,14 +128,10 @@ iterator :: (set: ^Set($T)) -> Iterator(T) {
         }
     }
 
-    close :: (context: rawptr) {
-        cfree(context);
-    }
-
     return .{
         data = context,
         next = #solidify next { T = T },
-        close = close,
+        close = cfree,
     };
 }
 
@@ -150,7 +146,7 @@ iterator :: (set: ^Set($T)) -> Iterator(T) {
         entry_prev  : i32 = -1;
     }
 
-    lookup :: (use set: ^Set($T), value: T) -> SetLookupResult {
+    lookup :: (use set: ^Set, value: set.Elem_Type) -> SetLookupResult {
         lr := SetLookupResult.{};
 
         hash_value: u32 = hash.to_u32(value); // You cannot have a set of this type without defining a to_u32 hash.
index fa05dac1e0637dcef7c69c63519d1adc7a2d9106..efcdf7623b7ebf9d145fd66bd176fb9c2fa9f320 100644 (file)
@@ -498,7 +498,7 @@ static CheckStatus check_resolve_callee(AstCall* call, AstTyped** effective_call
         arguments_remove_baked(&call->args);
         callee = new_callee;
 
-    } else if (callee->kind == Ast_Kind_Polymorphic_Proc) {
+    } else while (callee->kind == Ast_Kind_Polymorphic_Proc) {
         AstTyped* new_callee = (AstTyped *) polymorphic_proc_lookup((AstFunction *) callee, PPLM_By_Arguments, &call->args, call->token);
         if (new_callee == NULL) return Check_Error;
         if (new_callee == (AstTyped *) &node_that_signals_a_yield) {
index e1a17afafbbb0e8e3ee7af1758e04108b1a064e2..958a932c4ca5458e4ca392ec41f52c07268941ad 100644 (file)
@@ -10,6 +10,8 @@ static inline b32 should_clone(AstNode* node) {
 
     if (dont_copy_structs) {
         if (node->kind == Ast_Kind_Struct_Type) return 0;
+        if (node->kind == Ast_Kind_Function)    return 0;
+        if (node->kind == Ast_Kind_Polymorphic_Proc) return 0;
     }
 
     switch (node->kind) {
@@ -430,9 +432,9 @@ AstNode* ast_clone(bh_allocator a, void* n) {
                 dont_copy_structs = 1;
                 new_param.local = (AstLocal *) ast_clone(a, param->local);
                 new_param.local->flags &= ~Ast_Flag_Param_Symbol_Dirty;
+                new_param.default_value = (AstTyped *) ast_clone(a, param->default_value);
                 dont_copy_structs = 0;
 
-                new_param.default_value = (AstTyped *) ast_clone(a, param->default_value);
                 new_param.vararg_kind = param->vararg_kind;
                 new_param.is_used = param->is_used;
                 bh_arr_push(df->params, new_param);
@@ -557,9 +559,9 @@ AstFunction* clone_function_header(bh_allocator a, AstFunction* func) {
         dont_copy_structs = 1;
         new_param.local = (AstLocal *) ast_clone(a, param->local);
         new_param.local->flags &= ~Ast_Flag_Param_Symbol_Dirty;
+        new_param.default_value = (AstTyped *) ast_clone(a, param->default_value);
         dont_copy_structs = 0;
 
-        new_param.default_value = (AstTyped *) ast_clone(a, param->default_value);
         new_param.vararg_kind = param->vararg_kind;
         new_param.is_used = param->is_used;
         bh_arr_push(new_func->params, new_param);
index e8f39de047ed43d6d8cbb4888aea4716586835bb..db5dbcd8d69157ce6f68400c537410d4b701b540 100644 (file)
@@ -74,11 +74,14 @@ void entity_heap_init(EntityHeap* entities) {
     bh_arena_init(&entities->entity_arena, global_heap_allocator, 32 * 1024);
 }
 
+static u32 next_entity_id = 0;
+
 // Allocates the entity in the entity heap. Don't quite feel this is necessary...
 Entity* entity_heap_register(EntityHeap* entities, Entity e) {
     bh_allocator alloc = bh_arena_allocator(&entities->entity_arena);
     Entity* entity = bh_alloc_item(alloc, Entity);
     *entity = e;
+    entity->id = next_entity_id++;
     entity->macro_attempts = 0;
     entity->micro_attempts = 0;
     entity->entered_in_queue = 0;
index 675e826965a4332721841b0ee3a4a5364295bdf2..038e2660eb2bd5a4ebfc744b2348a39b3c1bd25d 100644 (file)
@@ -17,6 +17,13 @@ static Entity* waiting_on         = NULL;
     if (ss > Symres_Errors_Start) return ss;         \
     } while (0)
 
+#define SYMRES_INVISIBLE(kind, node, ...) do { \
+    (node)->flags |= Ast_Flag_Symbol_Invisible; \
+    SymresStatus ss = symres_ ## kind (__VA_ARGS__); \
+    (node)->flags &= ~Ast_Flag_Symbol_Invisible; \
+    if (ss > Symres_Errors_Start) return ss;         \
+    } while (0)
+
 typedef enum SymresStatus {
     Symres_Success,
     Symres_Complete,
@@ -209,10 +216,7 @@ static SymresStatus symres_type(AstType** type) {
 
         case Ast_Kind_Alias: {
             AstAlias* alias = (AstAlias *) *type;
-            alias->flags |= Ast_Flag_Symbol_Invisible;
-            SymresStatus ss = symres_type((AstType **) &alias->alias);
-            alias->flags &= ~Ast_Flag_Symbol_Invisible;
-            if (ss > Symres_Errors_Start) return ss;
+            SYMRES_INVISIBLE(type, alias, &alias->alias);
 
             break;
         }
@@ -455,10 +459,8 @@ static SymresStatus symres_expression(AstTyped** expr) {
         case Ast_Kind_Size_Of:      SYMRES(size_of, (AstSizeOf *)*expr); break;
         case Ast_Kind_Align_Of:     SYMRES(align_of, (AstAlignOf *)*expr); break;
         case Ast_Kind_Alias: {
-            (*expr)->flags |= Ast_Flag_Symbol_Invisible;
-            SymresStatus ss = symres_expression((AstTyped **) &((AstAlias *) *expr)->alias);
-            (*expr)->flags &= ~Ast_Flag_Symbol_Invisible;
-            if (ss > Symres_Errors_Start) return ss;
+            AstAlias *alias = (AstAlias *) *expr;
+            SYMRES_INVISIBLE(expression, alias, &alias->alias);
             break;
         }
 
@@ -942,9 +944,11 @@ SymresStatus symres_function_header(AstFunction* func) {
 
     bh_arr_each(AstParam, param, func->params) {
         symbol_introduce(curr_scope, param->local->token, (AstNode *) param->local);
+    }
 
+    bh_arr_each(AstParam, param, func->params) {
         if (param->local->type_node != NULL) {
-            SYMRES(type, &param->local->type_node);
+            SYMRES_INVISIBLE(type, param->local, &param->local->type_node);
         }
     }
 
@@ -1304,6 +1308,8 @@ static SymresStatus symres_process_directive(AstNode* directive) {
 }
 
 static SymresStatus symres_macro(AstMacro* macro) {
+    macro->flags |= Ast_Flag_Comptime;
+
     if (macro->body->kind == Ast_Kind_Function) {
         SYMRES(function_header, (AstFunction *) macro->body);
     }
@@ -1311,8 +1317,6 @@ static SymresStatus symres_macro(AstMacro* macro) {
         SYMRES(polyproc, (AstFunction *) macro->body);
     }
 
-    macro->flags |= Ast_Flag_Comptime;
-
     return Symres_Success;
 }
 
index 51436c6d375c5c34cd70d934d729af6196cb3292..87bff0fcdb7dd096a6b46bcf8c6d25a11c84b13b 100644 (file)
@@ -1015,6 +1015,8 @@ char *find_closest_symbol_in_scope(Scope *scope, char *sym, u32 *out_distance) {
 
     char* closest = NULL;
     fori (i, 0, shlen(scope->symbols)) {
+        if (scope->symbols[i].value->flags & Ast_Flag_Symbol_Invisible) continue;
+
         char *key = scope->symbols[i].key;
         u32 d = levenshtein_distance(key, sym); 
         if (d < *out_distance) {