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.
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();
// 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);
}
} else {
@ -210,9 +210,8 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
};
// no command? ignore!
if (cmds_parsed.items.len == 0) continue;
current = cmds_parsed.items[0].*;
if (cmds_parsed.list.items.len == 0) continue;
current = cmds_parsed.list.items[0].*;
// by cloning the parent runner, we can iteratively write
// 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 {
for (list.items) |cmd| {
for (list.list.items) |cmd| {
const command = @tagName(cmd.tag);
try stream.print("{}", .{command});

View file

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