From: Brendan Hansen Date: Thu, 22 Jun 2023 19:31:20 +0000 (-0500) Subject: changed: `Map` no longer stores a default value X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c99fb4364a3a30477feb6c656644a6061a1d2adc;p=onyx.git changed: `Map` no longer stores a default value --- diff --git a/compiler/src/symres.c b/compiler/src/symres.c index 4f63cb50..781df2b5 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -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) { diff --git a/core/container/map.onyx b/core/container/map.onyx index aa3b6d41..594aa3e5 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -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 """ diff --git a/core/conv/format.onyx b/core/conv/format.onyx index ce9aa448..1f0eafd6 100644 --- a/core/conv/format.onyx +++ b/core/conv/format.onyx @@ -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 {*}; diff --git a/tests/aoc-2020/day14.onyx b/tests/aoc-2020/day14.onyx index 7f7095a0..34b4118f 100644 --- a/tests/aoc-2020/day14.onyx +++ b/tests/aoc-2020/day14.onyx @@ -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; diff --git a/tests/aoc-2020/day15.onyx b/tests/aoc-2020/day15.onyx index 875b58f1..6fa54b7f 100644 --- a/tests/aoc-2020/day15.onyx +++ b/tests/aoc-2020/day15.onyx @@ -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; diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index 370c0a0c..ccf9b0d1 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -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; diff --git a/tests/aoc-2020/day20.onyx b/tests/aoc-2020/day20.onyx index 80c9b5b3..99c983cc 100644 --- a/tests/aoc-2020/day20.onyx +++ b/tests/aoc-2020/day20.onyx @@ -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); diff --git a/tests/aoc-2020/day21.onyx b/tests/aoc-2020/day21.onyx index 995c530c..6a6ef029 100644 --- a/tests/aoc-2020/day21.onyx +++ b/tests/aoc-2020/day21.onyx @@ -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); diff --git a/tests/aoc-2020/day22.onyx b/tests/aoc-2020/day22.onyx index f5bd8ec8..9a95b811 100644 --- a/tests/aoc-2020/day22.onyx +++ b/tests/aoc-2020/day22.onyx @@ -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 { diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 089b85ca..c7700030 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -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) { diff --git a/tests/aoc-2020/day25.onyx b/tests/aoc-2020/day25.onyx index 207b4a30..2da20af9 100644 --- a/tests/aoc-2020/day25.onyx +++ b/tests/aoc-2020/day25.onyx @@ -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; diff --git a/tests/aoc-2020/day7.onyx b/tests/aoc-2020/day7.onyx index ec14a31e..cc03c7c9 100644 --- a/tests/aoc-2020/day7.onyx +++ b/tests/aoc-2020/day7.onyx @@ -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) { diff --git a/tests/aoc-2020/day8.onyx b/tests/aoc-2020/day8.onyx index 24d54f3c..c883c126 100644 --- a/tests/aoc-2020/day8.onyx +++ b/tests/aoc-2020/day8.onyx @@ -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; diff --git a/tests/baked_parameters.onyx b/tests/baked_parameters.onyx index 8a21aa2f..d7b7bfc0 100644 --- a/tests/baked_parameters.onyx +++ b/tests/baked_parameters.onyx @@ -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); diff --git a/tests/i32map.onyx b/tests/i32map.onyx index bc452fbe..1c2cb03e 100644 --- a/tests/i32map.onyx +++ b/tests/i32map.onyx @@ -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);