Skip to content

Commit a54ecbe

Browse files
authored
Create utils.zig
1 parent 1e3feff commit a54ecbe

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

hacker-parser/utils.zig

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const std = @import("std");
2+
const parse = @import("parse.zig");
3+
pub const HACKER_DIR_SUFFIX = "/.hackeros/hacker-lang";
4+
pub fn deinitParseResult(res: *parse.ParseResult, allocator: std.mem.Allocator) void {
5+
{
6+
var it = res.deps.keyIterator();
7+
while (it.next()) |key| {
8+
allocator.free(key.*);
9+
}
10+
res.deps.deinit();
11+
}
12+
{
13+
var it = res.libs.keyIterator();
14+
while (it.next()) |key| {
15+
allocator.free(key.*);
16+
}
17+
res.libs.deinit();
18+
}
19+
{
20+
var it = res.vars_dict.iterator();
21+
while (it.next()) |entry| {
22+
allocator.free(entry.key_ptr.*);
23+
allocator.free(entry.value_ptr.*);
24+
}
25+
res.vars_dict.deinit();
26+
}
27+
{
28+
for (res.cmds.items) |item| {
29+
allocator.free(item);
30+
}
31+
res.cmds.deinit();
32+
}
33+
{
34+
for (res.includes.items) |item| {
35+
allocator.free(item);
36+
}
37+
res.includes.deinit();
38+
}
39+
{
40+
for (res.binaries.items) |item| {
41+
allocator.free(item);
42+
}
43+
res.binaries.deinit();
44+
}
45+
{
46+
for (res.plugins.items) |item| {
47+
allocator.free(item);
48+
}
49+
res.plugins.deinit();
50+
}
51+
{
52+
for (res.errors.items) |item| {
53+
allocator.free(item);
54+
}
55+
res.errors.deinit();
56+
}
57+
{
58+
var it = res.config_data.iterator();
59+
while (it.next()) |entry| {
60+
allocator.free(entry.key_ptr.*);
61+
allocator.free(entry.value_ptr.*);
62+
}
63+
res.config_data.deinit();
64+
}
65+
}
66+
pub fn mergeHashMaps(comptime V: type, dest: *std.StringHashMap(V), src: std.StringHashMap(V), allocator: std.mem.Allocator) !void {
67+
var it = src.iterator();
68+
while (it.next()) |entry| {
69+
try dest.put(try allocator.dupe(u8, entry.key_ptr.*), entry.value_ptr.*);
70+
}
71+
}
72+
pub fn mergeStringHashMaps(dest: *std.StringHashMap([]const u8), src: std.StringHashMap([]const u8), allocator: std.mem.Allocator) !void {
73+
var it = src.iterator();
74+
while (it.next()) |entry| {
75+
try dest.put(try allocator.dupe(u8, entry.key_ptr.*), try allocator.dupe(u8, entry.value_ptr.*));
76+
}
77+
}

0 commit comments

Comments
 (0)