From 41c1fa06dafb16f0720fb6de35071da08937bffb Mon Sep 17 00:00:00 2001 From: Chadtech Date: Sat, 16 Jan 2021 14:55:04 -0500 Subject: [PATCH] Beginning to implement Dict size --- compiler/builtins/bitcode/src/dict.zig | 6 ++++-- compiler/builtins/bitcode/src/main.zig | 6 ++++++ compiler/builtins/docs/Dict.roc | 10 ++++++++-- compiler/module/src/symbol.rs | 1 + 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/compiler/builtins/bitcode/src/dict.zig b/compiler/builtins/bitcode/src/dict.zig index a7327a4310..adb2863959 100644 --- a/compiler/builtins/bitcode/src/dict.zig +++ b/compiler/builtins/bitcode/src/dict.zig @@ -2,9 +2,8 @@ const std = @import("std"); const testing = std.testing; const expectEqual = testing.expectEqual; - const RocDict = struct { - len: u32, + size: usize, pub fn init() RocDict { return RocDict{ @@ -17,6 +16,9 @@ const RocDict = struct { } }; +pub fn dictSize(dict: RocDict) usize { + return dict.size; +} test "RocDict.init() contains nothing" { const dict = RocDict.init(); diff --git a/compiler/builtins/bitcode/src/main.zig b/compiler/builtins/bitcode/src/main.zig index 1106c2a5f6..8719eb85dd 100644 --- a/compiler/builtins/bitcode/src/main.zig +++ b/compiler/builtins/bitcode/src/main.zig @@ -2,6 +2,12 @@ const builtin = @import("builtin"); const std = @import("std"); const testing = std.testing; +// Dict Module +const dict = @import("dict.zig"); +comptime { + exportDictFn(dict.size, "size"); +} + // Num Module const num = @import("num.zig"); comptime { diff --git a/compiler/builtins/docs/Dict.roc b/compiler/builtins/docs/Dict.roc index 36056e81e5..c3a78b00df 100644 --- a/compiler/builtins/docs/Dict.roc +++ b/compiler/builtins/docs/Dict.roc @@ -1,7 +1,9 @@ -interface Dict2 +interface Dict exposes [ isEmpty, map ] imports [] +size : Dict * * -> Nat + isEmpty : Dict * * -> Bool ## Convert each key and value in the #Dict to something new, by calling a conversion @@ -13,4 +15,8 @@ isEmpty : Dict * * -> Bool ## ## `map` functions like this are common in Roc, and they all work similarly. ## See for example #Result.map, #List.map, and #Set.map. -map : List before, (before -> after) -> List after +map : + Dict beforeKey beforeValue, + (\{ key: beforeKey, value: beforeValue } -> + { key: afterKey, value: afterValue } + ) -> Dict afterKey afterValue diff --git a/compiler/module/src/symbol.rs b/compiler/module/src/symbol.rs index efa7f13cc1..be66298b87 100644 --- a/compiler/module/src/symbol.rs +++ b/compiler/module/src/symbol.rs @@ -882,6 +882,7 @@ define_builtins! { 3 DICT_SINGLETON: "singleton" 4 DICT_GET: "get" 5 DICT_INSERT: "insert" + 6 DICT_SIZE: "size" } 7 SET: "Set" => { 0 SET_SET: "Set" imported // the Set.Set type alias