summaryrefslogtreecommitdiff
path: root/doc/tangle.html
diff options
context:
space:
mode:
authoreudoxia <uplink@distress.network>2022-03-31 10:19:21 -0400
committereudoxia <uplink@distress.network>2022-03-31 10:19:21 -0400
commit27f4773b122a2758cfaf88537c9e1a8ad5df7e69 (patch)
treef6a31476fee7208d1ceb49186b243b106b09a5bd /doc/tangle.html
initial
Diffstat (limited to 'doc/tangle.html')
-rw-r--r--doc/tangle.html141
1 files changed, 141 insertions, 0 deletions
diff --git a/doc/tangle.html b/doc/tangle.html
new file mode 100644
index 0000000..1c26eae
--- /dev/null
+++ b/doc/tangle.html
@@ -0,0 +1,141 @@
+<!doctype html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
+<style>
+* {box-sizing: border-box;}
+html {
+--fs-sans: Helvetica, "Helvetica Neue", Arial, "San Francisco", sans;
+--fs-mono: jetbrains-mono-regular, Consolas, Menlo, monospace;
+font-size: 16px;
+}
+body {background-color: #111; color: #ddd; margin: 2rem calc(50% - 32rem); font-family: var(--fs-sans); font-size: 1rem; font-style: normal;}
+body * {line-height: 1.5; text-align: justify; text-justify: inter-word;}
+h1,h2 {font-family: var(--fs-sans); margin: 2rem 0 1rem; font-weight: bold; line-height: 1;}
+h1 {font-size: 3rem;}
+h2 {font-size: 2rem;}
+h3 {margin: 1rem; font-size: 1rem; font-weight: normal; font-style: italic;}
+code,code * {font-family: var(--fs-mono); hyphens: none; line-height: 1.25rem;}
+body>pre {background-color: #000; margin: 1rem; padding: 1rem; white-space: pre-wrap; border-radius: 0.25rem;}
+ul,ol {margin: 1rem 0; padding: 0 0 0 2rem;}
+ul ul,ol ol {margin: 0;}
+.ref {font-family: var(--fs-sans); font-style: italic;}
+@media screen and (max-width: 68rem) {body {margin: 2rem calc(50% - 24rem);}}
+@media screen and (max-width: 52rem) {body {margin: 2rem calc(50% - 16rem);}}
+@media screen and (max-width: 36rem) {body {margin: 2rem; hyphens: auto;} h3,body>pre {margin: 1rem 0}}
+</style>
+</head>
+
+<body>
+<h1 id="tangle.zig">tangle.zig</h1>
+
+<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><span class="ref" >(Imports)</span>
+
+pub fn main() !u8 {
+ <span class="ref" >(IO initialization)</span>
+
+ <span class="ref" >(Allocator initialization)</span>
+
+ <span class="ref" >(Read file from stdin)</span>
+
+ <span class="ref" >(Split into lines)</span>
+
+ <span class="ref" >(Parse lines into sections)</span>
+
+ <span class="ref" >(Generate code)</span>
+
+ <span class="ref" >(Write to stdout)</span>
+
+ 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>
+</body>
+</html> \ No newline at end of file