diff --git a/src/lang.zig b/src/lang.zig index 3d33909..80450f3 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -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 { diff --git a/src/main.zig b/src/main.zig index a53c1fb..119009e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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 diff --git a/src/printer.zig b/src/printer.zig index 69cc4b5..5ea5a7b 100644 --- a/src/printer.zig +++ b/src/printer.zig @@ -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}); diff --git a/src/runner.zig b/src/runner.zig index a9ea3e3..37fade3 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -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.*); }