aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: b85aa6f958e93996d61f63e149cd1a6516f34b4e (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
const std = @import("std");
const interpreter = @import("interpreter.zig");
const io = @import("io.zig");

pub fn main() anyerror!void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(!gpa.deinit());
    var args = try std.process.argsAlloc(gpa.allocator());
    defer std.process.argsFree(gpa.allocator(), args);
    if (args.len < 2) {
        std.debug.print("Usage: {s} <b98_file_to_run>\n", .{args[0]});
        std.os.exit(1);
    }

    var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{});
    defer file.close();

    var i = try interpreter.Interpreter.init(gpa.allocator(), file.reader(), args);
    defer i.deinit();

    var ioContext = try io.context(gpa.allocator(), std.io.getStdIn().reader(), std.io.getStdOut().writer());
    defer ioContext.deinit();
    std.os.exit(@intCast(u8, try i.run(ioContext)));
}

test "all" {
    std.testing.refAllDecls(@This());
}
test "sanity" {
    var file = try std.fs.cwd().openFile("mycology/sanity.bf", .{});
    defer file.close();
    var stdin = std.io.fixedBufferStream("");
    var stdout = std.ArrayList(u8).init(std.testing.allocator);
    defer stdout.deinit();
    const expected = "0123456789";
    const args = [_][]const u8{"sanity"};
    var i = try interpreter.Interpreter.init(std.testing.allocator, file.reader(), args[0..]);
    defer i.deinit();
    var ioContext = try io.context(std.testing.allocator, stdin.reader(), stdout.writer());
    defer ioContext.deinit();
    try std.testing.expectEqual(try i.run(ioContext), 0);
    try std.testing.expectEqual(std.mem.eql(u8, stdout.items, expected), true);
}