aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/stub.zig
blob: d6e7fc891c5df351108caf7eb4e6bea6c696c446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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);
    }
};