summaryrefslogtreecommitdiff
path: root/src/parser.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2024-08-07 16:56:22 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2024-08-07 16:56:22 -0500
commitb83e1ee47fd6e8a9bfa539f5f486401c6fbed342 (patch)
treec5858adcad36db3a5b5afda438ccd5f55b471b08 /src/parser.zig
parent068ff09a96ad3cdea31d788c053ea3d544713e39 (diff)
downloadcalico-b83e1ee47fd6e8a9bfa539f5f486401c6fbed342.tar.gz
Fixed memory leaks
Diffstat (limited to 'src/parser.zig')
-rw-r--r--src/parser.zig30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/parser.zig b/src/parser.zig
index 8cd539f..08f098c 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -113,16 +113,30 @@ pub const Parser = struct {
};
}
+ fn dinitHelper(self: *Parser, node: NodeStmt) !void {
+ switch (node.kind) {
+ .block => |blk| {
+ const children = try node.children(self.allocator);
+ defer self.allocator.free(children);
+ for (children) |child| try self.dinitHelper(child.Stmt);
+ self.allocator.free(blk);
+ node.symtable.deinit();
+ },
+ .function => |fun| {
+ const children = try node.children(self.allocator);
+ defer self.allocator.free(children);
+ for (children) |child| try self.dinitHelper(child.Stmt);
+ self.allocator.destroy(fun.block);
+ },
+ else => {},
+ }
+ }
+
pub fn deinit(self: *Parser) void {
for (self.nodes.items) |node| {
- switch (node.kind) {
- .block => |blk| self.allocator.free(blk),
- .function => |fun| {
- self.allocator.free(fun.block.kind.block);
- self.allocator.destroy(fun.block);
- },
- else => {},
- }
+ self.dinitHelper(node) catch |err| {
+ if (err == error.OutOfMemory) {}
+ };
}
self.nodes.deinit();
}