changed: `Map` no longer stores a default value
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Jun 2023 19:31:20 +0000 (14:31 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 22 Jun 2023 19:31:20 +0000 (14:31 -0500)
15 files changed:
compiler/src/symres.c
core/container/map.onyx
core/conv/format.onyx
tests/aoc-2020/day14.onyx
tests/aoc-2020/day15.onyx
tests/aoc-2020/day17.onyx
tests/aoc-2020/day20.onyx
tests/aoc-2020/day21.onyx
tests/aoc-2020/day22.onyx
tests/aoc-2020/day24.onyx
tests/aoc-2020/day25.onyx
tests/aoc-2020/day7.onyx
tests/aoc-2020/day8.onyx
tests/baked_parameters.onyx
tests/i32map.onyx

index 4f63cb50831b3e7bfc42a239eddfe9b1591f0643..781df2b51db40d9c62f7d9e64d474bcafbd59443 100644 (file)
@@ -489,8 +489,14 @@ static SymresStatus symres_pipe(AstBinaryOp** pipe) {
 //     foo->member_function(...)
 static SymresStatus symres_method_call(AstBinaryOp** mcall) {
     // :EliminatingSymres
+    
+    // We have to check this no matter what, because if we return to symbol resolution
+    // the left hand side could be something different. In particular this was a problem
+    // when expanding `some_map["value"]->unwrap()`, as the left hand side expands to a
+    // macro.
+    SYMRES(expression, &(*mcall)->left);
+
     if (((*mcall)->flags & Ast_Flag_Has_Been_Symres) == 0) {
-        SYMRES(expression, &(*mcall)->left);
         if ((*mcall)->left == NULL) return Symres_Error;
 
         if ((*mcall)->right->kind != Ast_Kind_Call) {
index aa3b6d41ab0569f6dba92d20d043d44b8ce6752b..594aa3e53cf6c31a132b5da87fcac5cecc670b1a 100644 (file)
@@ -22,9 +22,6 @@ Map :: struct (Key_Type: type_expr, Value_Type: type_expr) where ValidKey(Key_Ty
     hashes  : [] i32;
     entries : [..] Entry(Key_Type, Value_Type);
 
-    // The value provided by `map.get`, if nothing was found.
-    default_value : Value_Type;
-
     Entry :: struct (K: type_expr, V: type_expr) {
         next  : i32;
         hash  : u32;
@@ -75,18 +72,17 @@ __make_overload :: macro (x: &Map($K, $V), allocator := context.allocator) =>
 #doc """
    Creates and initializes a new map using the types provided.
 """
-make :: macro ($Key: type_expr, $Value: type_expr, default := Value.{}) -> Map(Key, Value) {
+make :: macro ($Key: type_expr, $Value: type_expr) -> Map(Key, Value) {
     map : Map(Key, Value);
-    #this_package.init(&map, default = default);
+    #this_package.init(&map);
     return map;
 }
 
 #doc "Initializes a map."
-init :: (use map: &Map($K, $V), default := V.{}) {
+init :: (use map: &Map($K, $V)) {
     __initialize(map);
 
     allocator = context.allocator;
-    default_value = default;
 
     hashes = builtin.make([] u32, 8, allocator=allocator);
     array.fill(hashes, -1);
@@ -142,7 +138,7 @@ get :: (use map: &Map, key: map.Key_Type) -> map.Value_Type {
     lr := lookup(map, key);
     if lr.entry_index >= 0 do return entries[lr.entry_index].value;
 
-    return default_value;
+    return .{};
 }
 
 #doc """
index ce9aa44805c8745c21c61ebe083f51c54972b9fc..1f0eafd66b0765fe3c4b46f4fafd9042920aaa8c 100644 (file)
@@ -18,8 +18,8 @@ use runtime
     registers them to be used in format_any and parse_any.
 """
 custom_formatters_initialized :: #init () {
-    map.init(&custom_formatters, default=null_proc);
-    map.init(&custom_parsers,    default=null_proc);
+    map.init(&custom_formatters);
+    map.init(&custom_parsers);
 
     #if Enable_Custom_Formatters {
         use runtime.info {*};
index 7f7095a05c58281c557f8b224583c9347097138d..34b4118f4e89dfd3bbb842632325cd24b258746f 100644 (file)
@@ -77,7 +77,7 @@ main :: (args: [] cstr) {
 
        file := contents;
 
-       mem := map.make(u64, u64, default=0);
+       mem := map.make(u64, u64);
        defer map.free(&mem);
 
        mask : Bitmask;
index 875b58f11257646059c8a921f62640b23fae8f51..6fa54b7f7c1e140df739007091218b531728529c 100644 (file)
@@ -13,7 +13,7 @@ main :: (args: [] cstr) {
     // The current implementation of Map is rather slow at a large scale.
     // Any changes to the implementation of Map should be tested on this
     // file to validate if they 1) work and 2) are faster.
-    nums := map.make(u32, spoken_times, .{});
+    nums := map.make(u32, spoken_times);
     defer map.free(&nums);
 
     turn := 1;
index 370c0a0c288fa20918ad4dfd3db0ede37b7ef525..ccf9b0d1da8d94cb426e50e3cb3ca956558f9219 100644 (file)
@@ -55,7 +55,7 @@ main :: (args: [] cstr) {
 
     file := contents;
 
-    cubes := map.make(CubePos, CubeState, .{});
+    cubes := map.make(CubePos, CubeState);
     defer map.free(&cubes);
 
     z := 0;
index 80c9b5b3d7a29e61c4f0637aaf3380a709aa130d..99c983cc5c23883881b8e235882ceb730238c483 100644 (file)
@@ -245,7 +245,7 @@ main :: (args: [] cstr) {
        tiles := array.make(Tile);
        defer array.free(&tiles);
 
-    tile_map := map.make(u32, &Tile, null);
+    tile_map := map.make(u32, &Tile);
     defer map.free(&tile_map);
 
        tile_data := cast(&TileData) calloc(200 * sizeof TileData);
index 995c530c463ea9c9c96a8d9cefea386bd04d5319..6a6ef029e5be70939d574651422e645583366eb4 100644 (file)
@@ -35,8 +35,8 @@ main :: (args: [] cstr) {
 
        file := contents;
 
-    map.init(&ingredient_map, .{});
-    map.init(&allergen_map, .{});
+    map.init(&ingredient_map);
+    map.init(&allergen_map);
     defer {
         map.free(&ingredient_map);
         map.free(&allergen_map);
index f5bd8ec86a256cb20afae578653619b4a50a0fed..9a95b8115bf97d77cc11a80af5d8f53c5aee349b 100644 (file)
@@ -59,7 +59,7 @@ encode_hands :: (alloc: Allocator, p1: &[..] u32, p2: &[..] u32) -> str {
 }
 
 recursive_combat :: (player1: &[..] u32, player2: &[..] u32) -> u32 {
-    hand_seen := map.make(str, bool, false);
+    hand_seen := map.make(str, bool);
     defer map.free(&hand_seen);
 
     while player1.count > 0 && player2.count > 0 {
index 089b85ca7acc2720091078e78d5dc24f3373b099..c77000305eaf06406eb416eab729c65ea9594b3e 100644 (file)
@@ -31,7 +31,7 @@ main :: (args: [] cstr) {
     file_stream := io.buffer_stream_make(contents);
        file := io.reader_make(&file_stream);
 
-       grid := map.make(Vec2, Cell, .{}); // `true` is black
+       grid := map.make(Vec2, Cell); // `true` is black
        defer map.free(&grid);
 
        while !io.reader_empty(&file) {
index 207b4a3018df0ea49bd52601144052c9253053c7..2da20af980a54725fcca402250e21d0d33ae97b8 100644 (file)
@@ -21,7 +21,7 @@ power_mod :: (base: u32, exp: u32, mod: u32) -> u32 {
 dlp_bsgs :: (n: u32, a: u32, b: u32) -> u32 {
     m := cast(u32) math.ceil(math.sqrt(cast(f64) n));
 
-    t := map.make(u32, u32, default=0);
+    t := map.make(u32, u32);
     defer map.free(&t);
 
     tmp: u64 = 1;
index ec14a31e59d272718d00cc4879a5218335a1d62e..cc03c7c9c6e95b53b35a4cb8c6c85d9c386faf40 100644 (file)
@@ -23,7 +23,7 @@ BagContainment :: struct {
 
 bg_init :: (use graph: &BagGraph) {
     array.init(&nodes, 16);
-    map.init(&node_map, null);
+    map.init(&node_map);
 }
 
 bg_free :: (use graph: &BagGraph) {
index 24d54f3c9b3b8ba19ce5228ec63075c6922b4c00..c883c126ba99e8366e2e03f9cf1a46eea396ce5b 100644 (file)
@@ -13,7 +13,7 @@ Instruction :: struct {
 
 // Returns if the program successfully exited.
 get_acc_value :: (instrs: [..] Instruction, ret_acc: &i32) -> bool {
-    already_been := map.make(i32, bool, false);
+    already_been := map.make(i32, bool);
     defer map.free(&already_been);
 
     ip   := 0;
index 8a21aa2f409ffcd7f84a19fcbe5c2b8a8f6df639..d7b7bfc05faa56b48178df0cd2416b729c0e737f 100644 (file)
@@ -31,7 +31,7 @@ main :: (args: [] cstr) {
 
 
     // This is so much cleaner than the alternative.
-    ages := map.make(str, u32, default=0);
+    ages := map.make(str, u32);
     defer delete(&ages);
 
     map.put(&ages, "Dwight", 32);
index bc452fbef84a27675539c245d60017d3ae839a9a..1c2cb03efaa38f86ad2b992f8518ec39fe014f84 100644 (file)
@@ -6,7 +6,7 @@ use core {*}
 
 main :: (args: [] cstr) {
     imap : Map(i32, str);
-    map.init(&imap, "");
+    map.init(&imap);
     defer {
         print("Freeing map\n");
         map.free(&imap);