summaryrefslogtreecommitdiff
path: root/doc/online/tangle.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/online/tangle.html')
-rwxr-xr-xdoc/online/tangle.html144
1 files changed, 144 insertions, 0 deletions
diff --git a/doc/online/tangle.html b/doc/online/tangle.html
new file mode 100755
index 0000000..02e5905
--- /dev/null
+++ b/doc/online/tangle.html
@@ -0,0 +1,144 @@
+<!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">
+<title>tangle.lp &mdash; DistressNetwork°</title>
+<style>h3,.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-size: 1rem; font-weight: normal; font-style: italic;} h3 {margin: 1rem;}</style>
+</head>
+<body>
+<div class="contentlevel">
+<main>
+<div class="leading">tangle.lp</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><span class="lp-ref">(License)</span>
+
+<span class="lp-ref">(Imports)</span>
+
+pub fn main() !u8 {
+ <span class="lp-ref">(IO initialization)</span>
+
+ <span class="lp-ref">(Allocator initialization)</span>
+
+ <span class="lp-ref">(Read file from stdin)</span>
+
+ <span class="lp-ref">(Split into lines)</span>
+
+ <span class="lp-ref">(Parse lines into sections)</span>
+
+ <span class="lp-ref">(Generate code)</span>
+
+ <span class="lp-ref">(Write to stdout)</span>
+
+ return 0;
+}
+</code></pre>
+
+<h3 id="license">License:</h3>
+
+<pre><code>// Copyright 2022 DistressNetwork° &#60;uplink@distress.network&#62;
+// 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/.
+</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>