summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 6559046ce170d64e8fef8a0db84a24d2fc5f0328 (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
const std = @import("std");
const zisp = @import("zisp");

const File = std.Io.File;

const gc = zisp.gc;
const io = zisp.io;
const value = zisp.value;

pub fn main() !u8 {
    try zisp.init();

    var stdin_buffer: [4096]u8 = undefined;
    var stdin_reader = File.stdin().reader(io.mainIo(), &stdin_buffer);
    const reader = &stdin_reader.interface;

    var stdout_buffer: [4096]u8 = undefined;
    var stdout_writer = File.stdout().writer(io.mainIo(), &stdout_buffer);
    const writer = &stdout_writer.interface;

    var p = try io.Parser.init();
    defer p.deinit();
    while (true) {
        const datum = p.run(reader) catch {
            const format = "Parse error: {s}, pos: {d}, unread_char: {s}\n";
            const err = p.err_msg;
            const pos = stdin_reader.logicalPos();
            const unread: [4]u8 =
                if (p.unread_char) |c|
                    "0x".* ++ std.fmt.hex(c)
                else
                    "none".*;
            std.debug.print(format, .{ err, pos, unread });
            return 1;
        };
        if (datum.eq(value.eof)) {
            return 0;
        }
        try writer.writeAll("0x");
        try writer.printInt(datum.bits, 16, .lower, .{});
        try writer.writeAll(": ");
        try io.print(writer, datum);
        try writer.writeAll("\n");
        try writer.flush();

        // To test list array free / reuse:
        if (datum.isList()) try value.list.free(p.alloc, p.list_pool, datum);
    }
}