lib/pluck/src/toplevel/shared.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const pluck = @import("../root.zig");
3 const Allocator = std.mem.Allocator;
4
5 const pexpr = pluck.pexpr;
6 const Definition = pexpr.Definition;
7 const Definitions = pexpr.Definitions;
8 const TypeRegistry = pexpr.TypeRegistry;
9
10 pub fn cloneDefinitionsShallow(allocator: Allocator, source: *const Definitions) !Definitions {
11 var defs = std.StringHashMap(Definition).init(allocator);
12 errdefer defs.deinit();
13
14 var iter = source.defs.iterator();
15 while (iter.next()) |entry| {
16 try defs.put(entry.key_ptr.*, entry.value_ptr.*);
17 }
18
19 return Definitions{
20 .allocator = allocator,
21 .defs = defs,
22 };
23 }
24
25 pub fn cloneTypesShallow(allocator: Allocator, source: *const TypeRegistry) !TypeRegistry {
26 var types = TypeRegistry.init(allocator);
27 errdefer {
28 types.type_of_constructor.deinit();
29 types.constructors_of_type.deinit();
30 types.args_of_constructor.deinit();
31 }
32
33 var iter = source.type_of_constructor.iterator();
34 while (iter.next()) |entry| {
35 try types.type_of_constructor.put(entry.key_ptr.*, entry.value_ptr.*);
36 }
37 var iter2 = source.constructors_of_type.iterator();
38 while (iter2.next()) |entry| {
39 try types.constructors_of_type.put(entry.key_ptr.*, entry.value_ptr.*);
40 }
41 var iter3 = source.args_of_constructor.iterator();
42 while (iter3.next()) |entry| {
43 try types.args_of_constructor.put(entry.key_ptr.*, entry.value_ptr.*);
44 }
45
46 return types;
47 }