From bbd2e67cb80f0444f89772023268884870feb01b Mon Sep 17 00:00:00 2001 From: Karchnu Date: Tue, 15 Dec 2020 17:54:21 +0100 Subject: [PATCH] Common structures: WIP. --- src/common-structures.zig | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/common-structures.zig diff --git a/src/common-structures.zig b/src/common-structures.zig new file mode 100644 index 0000000..abdbf02 --- /dev/null +++ b/src/common-structures.zig @@ -0,0 +1,51 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; +const hashString = std.hash_map.hashString; +const eqlString = std.hash_map.eqlString; +const HashMap = std.HashMap; +const AutoHashMap = std.AutoHashMap; +const ArrayList = std.ArrayList; + + +const PropertyHashMap = AutoHashMap([] const u8, PropertyValue); +const NodeList = ArrayList(Node); +const Definitions = AutoHashMap([] const u8, Node); + + +pub const Node = struct { + id: ?[] const u8, + type_name: [] const u8, + properties: PropertyHashMap, + children: NodeList, +}; + +pub const PropertyValue = union { + string: [] const u8, // String. + integer: u64, // Num (integer). + float: f64, // Num (float). + reference: *u8, // Reference to another property (property binding). +}; + +pub const Root = struct { + definitions: Definitions, + children: NodeList, +}; + +test "simple test about structures" { + const allocator = std.heap.page_allocator; + var value = PropertyValue { .integer = 10 }; + + var properties = PropertyHashMap.init(allocator); + defer properties.deinit(); + + try properties.put("hello", value); + + std.debug.print("\n", .{}); + var it = properties.iterator(); + while(it.next()) |kv| { + std.debug.print("key: {} => value: {}\n", .{kv.key, properties.get(kv.key)}); + } +} + +