From: Brendan Hansen Date: Mon, 31 May 2021 03:31:05 +0000 (-0500) Subject: small code cleanup X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=930307f856d88b60520ccdfb2ce34b7e44acad0d;p=onyx.git small code cleanup --- diff --git a/bin/onyx b/bin/onyx index 6213c344..b1a21794 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/container/array.onyx b/core/container/array.onyx index ec767a07..4eb21681 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -215,35 +215,37 @@ to_slice :: (arr: ^[..] $T) -> [] T { ** Simple insertion sort ** cmp should return >0 if left > right */ -sort :: (arr: ^[..] $T, cmp: (T, T) -> i32) { - for i: 1 .. arr.count { - x := arr.data[i]; - j := i - 1; - - @ShortCircuitLogicalOps // This is written this way because '&&' does not short circuit right now. - while j >= 0 { - if cmp(arr.data[j], x) > 0 { - arr.data[j + 1] = arr.data[j]; - j -= 1; - } else { - break; +sort :: proc { + (arr: ^[..] $T, cmp: (T, T) -> i32) { + for i: 1 .. arr.count { + x := arr.data[i]; + j := i - 1; + + @ShortCircuitLogicalOps // This is written this way because '&&' does not short circuit right now. + while j >= 0 { + if cmp(arr.data[j], x) > 0 { + arr.data[j + 1] = arr.data[j]; + j -= 1; + } else { + break; + } } - } - - arr.data[j + 1] = x; - } -} -sort_ptr :: (arr: ^[..] $T, cmp: (^T, ^T) -> i32) { - for i: 1 .. arr.count { - j := i; + arr.data[j + 1] = x; + } + }, - while j > 0 { - if cmp(^arr.data[j - 1], ^arr.data[j]) > 0 { - arr.data[j], arr.data[j - 1] = arr.data[j - 1], arr.data[j]; - j -= 1; - } else { - break; + (arr: ^[..] $T, cmp: (^T, ^T) -> i32) { + for i: 1 .. arr.count { + j := i; + + while j > 0 { + if cmp(^arr.data[j - 1], ^arr.data[j]) > 0 { + arr.data[j], arr.data[j - 1] = arr.data[j - 1], arr.data[j]; + j -= 1; + } else { + break; + } } } } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 6cab4d82..7e4ab81e 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1724,26 +1724,6 @@ CheckStatus check_function_header(AstFunction* func) { func->type = type_build_function_type(context.ast_alloc, func); - /* - CLEANUP: These checks need to be ported to a process directive check. - if ((func->flags & Ast_Flag_Exported) != 0) { - if ((func->flags & Ast_Flag_Foreign) != 0) { - onyx_report_error(func->token->pos, "exporting a foreign function"); - return Check_Error; - } - - if ((func->flags & Ast_Flag_Intrinsic) != 0) { - onyx_report_error(func->token->pos, "exporting a intrinsic function"); - return Check_Error; - } - - if (func->exported_name == NULL) { - onyx_report_error(func->token->pos, "exporting function without a name"); - return Check_Error; - } - } - */ - return Check_Success; } diff --git a/src/onyxsymres.c b/src/onyxsymres.c index e02f608e..e6bbc20c 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -6,8 +6,6 @@ // Variables used during the symbol resolution phase. static Scope* curr_scope = NULL; -static Package* curr_package = NULL; -static AstFunction* curr_function = NULL; bh_arr(AstBlock *) block_stack = NULL; static b32 report_unresolved_symbols = 1; @@ -787,13 +785,6 @@ SymresStatus symres_function_header(AstFunction* func) { if (param->default_value != NULL) { SYMRES(expression, ¶m->default_value); if (onyx_has_errors()) return Symres_Error; - - // HACK: It shouldn't be necessary to do this twice, but right now - // if `null` is the default parameter and it hasn't been used anywhere in - // code yet, it doesn't resolve properly. So for now I am just checking symbols twice. - // -brendanfh 2020/12/24 - SYMRES(expression, ¶m->default_value); - if (onyx_has_errors()) return Symres_Error; } } @@ -866,7 +857,6 @@ SymresStatus symres_function(AstFunction* func) { } } - curr_function = func; SYMRES(block, func->body); scope_leave(); @@ -1061,6 +1051,24 @@ static SymresStatus symres_process_directive(AstNode* directive) { if (export->export->kind == Ast_Kind_Function) { AstFunction *func = (AstFunction *) export->export; func->exported_name = export->export_name; + + if ((func->flags & Ast_Flag_Exported) != 0) { + if ((func->flags & Ast_Flag_Foreign) != 0) { + onyx_report_error(export->token->pos, "exporting a foreign function"); + return Symres_Error; + } + + if ((func->flags & Ast_Flag_Intrinsic) != 0) { + onyx_report_error(export->token->pos, "exporting a intrinsic function"); + return Symres_Error; + } + + // NOTE: This should never happen + if (func->exported_name == NULL) { + onyx_report_error(export->token->pos, "exporting function without a name"); + return Symres_Error; + } + } } break; @@ -1073,8 +1081,6 @@ static SymresStatus symres_process_directive(AstNode* directive) { void symres_entity(Entity* ent) { if (block_stack == NULL) bh_arr_new(global_heap_allocator, block_stack, 16); - if (ent->package) curr_package = ent->package; - Scope* old_scope = NULL; if (ent->scope) { old_scope = curr_scope; @@ -1091,7 +1097,7 @@ void symres_entity(Entity* ent) { switch (ent->type) { case Entity_Type_Binding: { symbol_introduce(curr_scope, ent->binding->token, ent->binding->node); - package_reinsert_use_packages(curr_package); + package_reinsert_use_packages(ent->package); next_state = Entity_State_Finalized; break; } @@ -1139,5 +1145,4 @@ void symres_entity(Entity* ent) { if (ss == Symres_Yield_Micro) ent->micro_attempts++; if (ent->scope) curr_scope = old_scope; - curr_package = NULL; } diff --git a/tests/aoc-2020/day21.onyx b/tests/aoc-2020/day21.onyx index 5649e72a..118eb6ce 100644 --- a/tests/aoc-2020/day21.onyx +++ b/tests/aoc-2020/day21.onyx @@ -162,7 +162,7 @@ main :: (args: [] cstr) { } } - array.sort_ptr(^matched_ingredients, (i1: ^Ingredient, i2: ^Ingredient) -> i32 { + array.sort(^matched_ingredients, (i1: ^Ingredient, i2: ^Ingredient) -> i32 { return string.compare(i1.allergen, i2.allergen); });