make CommandList free its command pointers

This commit is contained in:
Luna 2020-08-18 20:40:06 -03:00
parent 4adf80e51a
commit f61e9b013f
4 changed files with 28 additions and 7 deletions

View File

@ -474,7 +474,29 @@ pub const Command = struct {
}); });
}; };
pub const CommandList = std.ArrayList(*Command); const CmdArrayList = std.ArrayList(*Command);
pub const CommandList = struct {
list: CmdArrayList,
const Self = @This();
pub fn init(allocator: *std.mem.Allocator) Self {
return .{
.list = CmdArrayList.init(allocator),
};
}
pub fn deinit(self: *Self) void {
for (self.list.items) |cmd_ptr| {
self.list.allocator.destroy(cmd_ptr);
}
self.list.deinit();
}
pub fn append(self: *Self, cmd: *Command) !void {
return try self.list.append(cmd);
}
};
/// A parser. /// A parser.
pub const Lang = struct { pub const Lang = struct {

View File

@ -65,7 +65,7 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
defer existing_cmds.deinit(); defer existing_cmds.deinit();
// copy the existing command list into the repl's command list // copy the existing command list into the repl's command list
for (existing_cmds.items) |existing_cmd| { for (existing_cmds.list.items) |existing_cmd| {
try cmds.append(existing_cmd); try cmds.append(existing_cmd);
} }
} else { } else {
@ -210,9 +210,8 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
}; };
// no command? ignore! // no command? ignore!
if (cmds_parsed.items.len == 0) continue; if (cmds_parsed.list.items.len == 0) continue;
current = cmds_parsed.list.items[0].*;
current = cmds_parsed.items[0].*;
// by cloning the parent runner, we can iteratively write // by cloning the parent runner, we can iteratively write
// whatever command we want and only commit the good results // whatever command we want and only commit the good results

View File

@ -31,7 +31,7 @@ fn printCommand(stream: anytype, cmd: *langs.Command, comptime tag: langs.Comman
} }
pub fn printList(list: langs.CommandList, stream: anytype) !void { pub fn printList(list: langs.CommandList, stream: anytype) !void {
for (list.items) |cmd| { for (list.list.items) |cmd| {
const command = @tagName(cmd.tag); const command = @tagName(cmd.tag);
try stream.print("{}", .{command}); try stream.print("{}", .{command});

View File

@ -312,7 +312,7 @@ pub const Runner = struct {
cmds: lang.CommandList, cmds: lang.CommandList,
debug_flag: bool, debug_flag: bool,
) !void { ) !void {
for (cmds.items) |cmd| { for (cmds.list.items) |cmd| {
cmd.print(); cmd.print();
try self.runCommand(cmd.*); try self.runCommand(cmd.*);
} }