summaryrefslogtreecommitdiff
path: root/software/literary/tangle.html
blob: 9d853fe7a48589151dc58096e58fde74aff8d59f (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/fonts/fonts.css">
<link rel="stylesheet" href="/css/main.css">
<link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<script src="/js/bg.js"></script>
<title></title>
<meta name="description" content="" />
<meta http-equiv="last-modified" content="" />
</head>
<body>
<header>
<div class="bomber"></div>
<hr>
<div class="identcontainer">
<div class="ident"><time datetime=""></time>––––<a href="https://distress.network">DistressNetwork°</a></div>
<img class="headerlogo" src="/media/distressnetwork-b.svg" alt="">
</div>
</header>
<div class="contentlevel">
<main>
<div class="leading"></div>
<hr>
<p>The structure of this file is quite similar to that of <code>weave.zig</code>, only differing in terms of which functions are used to transform the input data.</p>

<h3 id="section">*:</h3>

<pre><code>@= Imports =@

pub fn main() !u8 {
    @= IO initialization =@

    @= Allocator initialization =@

    @= Read file from stdin =@

    @= Split into lines =@

    @= Parse lines into sections =@

    @= Generate code =@

    @= Write to stdout =@

    return 0;
}
</code></pre>

<p>First we import the other files containing the core functions.</p>

<h3 id="imports">Imports:</h3>

<pre><code>const std = @import("std");
const data = @import("data.zig");
const log = @import("log.zig").log;

const Allocator = std.mem.Allocator;
</code></pre>

<p>Within the main procedure, we first initialize the stdin and stdout interfaces.</p>

<h3 id="io-initialization">IO initialization:</h3>

<pre><code>const stdin = std.io.getStdIn();
const stdout = std.io.getStdOut();
</code></pre>

<p>We then initialize the allocator, deferring its deinitialization to the end of the process. Since the overall memory usage pattern is one in which all resources may be freed at once, the arena allocator is the most appropriate choice for this program.</p>

<h3 id="allocator-initialization">Allocator initialization:</h3>

<pre><code>var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var alloc = arena.allocator();
defer arena.deinit();
</code></pre>

<p>The input file is then read from stdin. In the case of input exceeding the maximum permitted file size, the program may report the error and exit normally. All other errors which may be returned are memory allocation failures and should thus yield control to the panic handler.</p>

<h3 id="read-file-from-stdin">Read file from stdin:</h3>

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

<p>We then pass the input into the line splitting function, creating an array of strings.</p>

<h3 id="split-into-lines">Split into lines:</h3>

<pre><code>const lines = try data.split_lines(input, alloc);
</code></pre>

<p>The lines are then passed into the parsing function, which may return a parsing error. Logging such errors is handled by the function itself, and thus the errors are handled here solely by exiting.</p>

<h3 id="parse-lines-into-sections">Parse lines into sections:</h3>

<pre><code>const sections = data.parse(lines, alloc) catch |err| switch (err) {
    error.UnexpectedStart,
    error.UnexpectedEnd =&#62; {
        return 1;
    },
    else =&#62; |e| return e,
};
</code></pre>

<p>The code file is then generated. This entails resolving references to section names, which may return an error, handled by exiting as above.</p>

<h3 id="generate-code">Generate code:</h3>

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

<p>Finally, the lines of the code file are written to stdout, separated by newlines.</p>

<h3 id="write-to-stdout">Write to stdout:</h3>

<pre><code>for (code) |line| {
    try stdout.writer().print("{s}\n", .{line});
}
</code></pre>
</main>
<nav>
</nav>
</div>
<footer>
<p><a href="/info">About.</a> <a href="mailto:uplink@distress.network">Contact.</a> <a href="/cw.html">Content Warning.</a> <a href="https://git.distress.network">Git.</a> <a href="/meta/sitemap">Sitemap.</a> <a href="https://creativecommons.org/licenses/by-sa/4.0">CC BY-SA 4.0.</a></p>
<img src="/media/distressnetwork-w.svg" alt="">
</footer>
</body>
</html>