From: Brendan Hansen Date: Thu, 6 Jan 2022 00:00:58 +0000 (-0600) Subject: small code change for big improvements X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a9c3c1615a1f90ece4fc29fd1c952f89835eb84e;p=onyx.git small code change for big improvements --- diff --git a/core/container/heap.onyx b/core/container/heap.onyx index ae8c388d..05f0fc33 100644 --- a/core/container/heap.onyx +++ b/core/container/heap.onyx @@ -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); } diff --git a/core/container/set.onyx b/core/container/set.onyx index dbc5ad6e..fcf4ac74 100644 --- a/core/container/set.onyx +++ b/core/container/set.onyx @@ -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. diff --git a/src/checker.c b/src/checker.c index fa05dac1..efcdf762 100644 --- a/src/checker.c +++ b/src/checker.c @@ -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) { diff --git a/src/clone.c b/src/clone.c index e1a17afa..958a932c 100644 --- a/src/clone.c +++ b/src/clone.c @@ -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); diff --git a/src/entities.c b/src/entities.c index e8f39de0..db5dbcd8 100644 --- a/src/entities.c +++ b/src/entities.c @@ -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; diff --git a/src/symres.c b/src/symres.c index 675e8269..038e2660 100644 --- a/src/symres.c +++ b/src/symres.c @@ -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, ¶m->local->type_node); + SYMRES_INVISIBLE(type, param->local, ¶m->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; } diff --git a/src/utils.c b/src/utils.c index 51436c6d..87bff0fc 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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) {