small code cleanup
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 31 May 2021 03:31:05 +0000 (22:31 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 31 May 2021 03:31:05 +0000 (22:31 -0500)
bin/onyx
core/container/array.onyx
src/onyxchecker.c
src/onyxsymres.c
tests/aoc-2020/day21.onyx

index 6213c3448ea4a21a441cf240e193183d160314bd..b1a21794a5d9904d15aaab88e570ade524a0fc65 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index ec767a07543b496ea80d00fa185d4a22d9611d53..4eb216810cd2b93372514b14c0b9800e3aac7ff4 100644 (file)
@@ -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;
+                }
             }
         }
     }
index 6cab4d82b192f5b026b0564f5542c056aed4bf04..7e4ab81e03d542afa3a5e35d2624e2e553655ceb 100644 (file)
@@ -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;
 }
 
index e02f608e98e07fc0dba6366e43b786cfb55bd8f4..e6bbc20c69201c79b197f52f39e9b49a251db042 100644 (file)
@@ -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, &param->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, &param->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;
 }
index 5649e72ad12e4a63c672d68f66380e57e07066cf..118eb6ce6775bafd81e1e045376dff8ad5a06a95 100644 (file)
@@ -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);
     });