summaryrefslogtreecommitdiff
path: root/src/tangle.zig
blob: 92a2200aa26835aca568f422f136a4e0dabf4b50 (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
// Copyright 2022 DistressNetwork° <uplink@distress.network>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

const std = @import("std");
const data = @import("data.zig");
const log = @import("log.zig").log;

const Allocator = std.mem.Allocator;

pub fn main() !u8 {
    const stdin = std.io.getStdIn();
    const stdout = std.io.getStdOut();

    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    var alloc = arena.allocator();
    defer arena.deinit();

    const input = stdin.reader().readAllAlloc(alloc, data.input_max) catch |err| switch (err) {
        error.StreamTooLong => {
            log(.err, "input too large (maximum {})", .{std.fmt.fmtIntSizeBin(data.input_max)});
            return 1;
        },
        else => |e| return e,
    };

    const lines = try data.split_lines(input, alloc);

    const sections = data.parse(lines, alloc) catch |err| switch (err) {
        error.UnexpectedStart, error.UnexpectedEnd => {
            return 1;
        },
        else => |e| return e,
    };

    const code = data.codegen(lines, sections, alloc) catch |err| switch (err) {
        error.DereferenceLimit, error.NotFound => {
            return 1;
        },
        else => |e| return e,
    };

    for (code) |line| {
        try stdout.writer().print("{s}\n", .{line});
    }

    return 0;
}