diff options
author | jjanzen <jjanzen@jjanzen.ca> | 2025-01-22 14:22:12 -0600 |
---|---|---|
committer | jjanzen <jjanzen@jjanzen.ca> | 2025-01-22 14:22:12 -0600 |
commit | 14c2530817f20986b51f611ef637d09f43aac23c (patch) | |
tree | cc948750173fd7c0aba570f462b65ad1fe405121 /src/frontend/stub.zig | |
parent | 8ecefa4b3fd56fc87f822f3dbefa1ce1ef6601d8 (diff) |
fix memory leak and implement stub front end
Diffstat (limited to 'src/frontend/stub.zig')
-rw-r--r-- | src/frontend/stub.zig | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/frontend/stub.zig b/src/frontend/stub.zig new file mode 100644 index 0000000..d6e7fc8 --- /dev/null +++ b/src/frontend/stub.zig @@ -0,0 +1,57 @@ +const std = @import("std"); +const Action = @import("../actions.zig").Action; +const IOInterface = @import("io_interface.zig").IOInterface; + +pub const ActionStack = struct { + stream: std.ArrayList(Action), + + pub fn init(allocator: std.mem.Allocator) ActionStack { + return ActionStack{ + .stream = std.ArrayList(Action).init(allocator), + }; + } + + pub fn deinit(self: *ActionStack) void { + self.stream.deinit(); + } + + pub fn scheduleAction(self: *ActionStack, action: Action) !void { + try self.stream.append(action); + } + + pub fn next(self: *ActionStack) ?Action { + return self.stream.popOrNull(); + } +}; + +pub const IO = struct { + allocator: std.mem.Allocator, + as: ActionStack, + out_as: ActionStack, + + pub fn displayMessage(self: *IO, str: []const u8) anyerror!void { + _ = self; + std.log.info("{s}\n", .{str}); + } + + pub fn waitForTick(self: *IO) anyerror!Action { + const a = self.as.next() orelse return error.NoAvailableAction; + + try self.out_as.scheduleAction(a); + + return a; + } + + pub fn init(allocator: std.mem.Allocator, as: ActionStack) !IOInterface { + var io = try allocator.create(IO); + io.allocator = allocator; + io.as = as; + io.out_as = ActionStack.init(allocator); + return IOInterface.init(io); + } + + pub fn deinit(self: *IO) void { + self.out_as.deinit(); + self.allocator.destroy(self); + } +}; |