aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.zig
blob: 51a542ef6e74951ed1a13cedc9d0b4c661b02c45 (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 field = @import("field.zig");
const io = @import("io.zig");
const pointer = @import("pointer.zig");

pub const Interpreter = struct {
    allocator: std.mem.Allocator,
    field: *field.Field,
    pointer: *pointer.Pointer,

    pub fn deinit(self: *Interpreter) void {
        self.pointer.deinit();
        self.field.deinit();
        self.allocator.destroy(self);
    }
    pub inline fn getField(self: *Interpreter) *field.Field {
        return self.field;
    }
    pub inline fn getPointer(self: *Interpreter) *pointer.Pointer {
        return self.pointer;
    }
    pub fn init(allocator: std.mem.Allocator, fileReader: anytype, timestamp: ?i64, args: []const []const u8, env: []const [*:0]const u8) !*Interpreter {
        var i = try allocator.create(Interpreter);
        errdefer allocator.destroy(i);
        i.allocator = allocator;
        i.field = try field.Field.init_from_reader(allocator, fileReader);
        errdefer i.field.deinit();
        i.pointer = try pointer.Pointer.init(allocator, i.field, timestamp, args, env);
        errdefer i.pointer.deinit();
        return i;
    }
    pub fn run(self: *Interpreter, ioContext: anytype) !i64 {
        while (true) {
            if (try self.pointer.exec(ioContext)) |ret| {
                if (ret.code) |code| {
                    return code;
                } else {
                    return 0;
                }
            }
        }
    }
    pub fn step(self: *Interpreter, ioContext: anytype) !?i64 {
        if (try self.pointer.exec(ioContext)) |ret| {
            if (ret.code) |code| {
                return code;
            } else {
                return 0;
            }
        }
        return null;
    }
};

test "all" {
    std.testing.refAllDecls(@This());
}