summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreudoxia <uplink@distress.network>2022-04-01 09:37:34 -0400
committereudoxia <uplink@distress.network>2022-04-01 09:37:34 -0400
commit3a5a61229a4fa0e7acba3f49fb4a28fa898672a3 (patch)
tree30236e050a098d80927438b0794cce7af5071ab0
parent5546f30e9bf6be1325ac004afab71bec921c2041 (diff)
software/literary: initial
-rwxr-xr-xsoftware/literary/data.html517
-rwxr-xr-xsoftware/literary/log.html100
-rwxr-xr-xsoftware/literary/tangle.html139
-rwxr-xr-xsoftware/literary/weave.html123
4 files changed, 879 insertions, 0 deletions
diff --git a/software/literary/data.html b/software/literary/data.html
new file mode 100755
index 0000000..ccf8c3a
--- /dev/null
+++ b/software/literary/data.html
@@ -0,0 +1,517 @@
+<!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>data.lp &mdash; DistressNetwork°</title>
+<meta http-equiv="last-modified" content="2022-03-31 23:28:56-04:00" />
+<style>.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-style: italic;}</style>
+</head>
+<body>
+<div class="contentlevel">
+<main>
+<div class="leading">data.lp</div>
+<hr>
+<h1 id="data.zig">data.zig</h1>
+
+<p>This file contains the various data processing-related constants and functions referenced by the tangling and weaving processes.</p>
+
+<h3 id="section">*:</h3>
+
+<pre><code><span class="lp-ref">(Imports)</span>
+
+<span class="lp-ref">(Processing limits)</span>
+
+<span class="lp-ref">(Formatting keywords)</span>
+
+<span class="lp-ref">(Configuration keywords)</span>
+
+<span class="lp-ref">(Data structure types)</span>
+
+<span class="lp-ref">(Error set)</span>
+
+<span class="lp-ref">(Line splitting function)</span>
+
+<span class="lp-ref">(Configuration searching function)</span>
+
+<span class="lp-ref">(Section searching function)</span>
+
+<span class="lp-ref">(Parsing functions)</span>
+
+<span class="lp-ref">(Code generation functions)</span>
+
+<span class="lp-ref">(Text generation function)</span>
+</code></pre>
+
+<h2 id="constants">Constants</h2>
+
+<p>We first import the standard library and the logging function from <code>log.zig</code>.</p>
+
+<h3 id="imports">Imports:</h3>
+
+<pre><code>const std = @import("std");
+const log = @import("log.zig").log;
+
+const Allocator = std.mem.Allocator;
+</code></pre>
+
+<p>We define the maximum input file size of 4GiB, and the code generation function&#8217;s maximum recursion depth of 250 nested calls.</p>
+
+<h3 id="processing-limits">Processing limits:</h3>
+
+<pre><code>pub const input_max = 0x1_0000_0000;
+pub const dereference_max = 250;
+</code></pre>
+
+<p>We then define the recognized formatting keywords. These consist of the following:</p>
+
+<ul>
+<li><code>@:</code>, which begins a new code section;</li>
+<li><code>@+</code>, which appends content to a previous code section;</li>
+<li><code>@.</code>, which terminates the definition of a code section;</li>
+<li><code>@=</code>, which creates a reference to another code section;</li>
+<li><code>*</code>, which is a reserved section name representing the root level of the source code.</li>
+</ul>
+
+<h3 id="formatting-keywords">Formatting keywords:</h3>
+
+<pre><code>pub const k_start = "@: ";
+pub const k_add = "@+ ";
+pub const k_end = "@.";
+pub const k_ref = "@= ";
+pub const k_root = "*";
+</code></pre>
+
+<p>We similarly define the recognized configuration keywords, consisting of:</p>
+
+<ul>
+<li><code>@start</code>, which defines the leading formatted code delimiter when beginning new sections;</li>
+<li><code>@add</code>, which defines the leading formatted code delimiter when appending to existing sections;</li>
+<li><code>@end</code>, which defines the trailing formatted code delimiter;</li>
+<li><code>@ref</code>, which defines the format for section references;</li>
+<li><code>@@</code>, which is the escape sequence representing the current section name;</li>
+<li><code>\n</code>, which is the escape sequence representing a newline.</li>
+</ul>
+
+<h3 id="configuration-keywords">Configuration keywords:</h3>
+
+<pre><code>pub const kc_start = "@start ";
+pub const kc_add = "@add ";
+pub const kc_end = "@end ";
+pub const kc_ref = "@ref ";
+pub const kc_esc = "@@";
+pub const kc_nl = "\\n";
+</code></pre>
+
+<p>We then define the data structure used for parsing the input into code sections, described as follows:</p>
+
+<ul>
+<li>The overall structure of the file is an array of <code>Section</code>s.</li>
+<li>A <code>Section</code> consists of the section name and an array of <code>Content</code> elements.</li>
+<li>A <code>Content</code> element may be either a range of literal lines of code or a reference to another section.</li>
+<li>A <code>LineRange</code> is a pair of integers indicating the starting and ending line numbers of the section.</li>
+</ul>
+
+<h3 id="data-structure-types">Data structure types:</h3>
+
+<pre><code>pub const Section = struct {
+ name: []const u8,
+ content: []const Content,
+};
+
+pub const CodeType = enum { literal, reference };
+pub const Content = union(CodeType) {
+ literal: LineRange,
+ reference: []const u8,
+};
+
+pub const LineRange = struct {
+ start: u32,
+ end: u32,
+};
+</code></pre>
+
+<p>We also define the set of errors which may be encountered by the various processing functions, consisting of:</p>
+
+<ul>
+<li>Unexpected section start commands,</li>
+<li>Unexpected section end commands, </li>
+<li>Recursive dereferencing exceeding the specified depth limit,</li>
+<li>References to nonexistent section names or configuration commands.</li>
+</ul>
+
+<h3 id="error-set">Error set:</h3>
+
+<pre><code>pub const Errors = error{
+ UnexpectedStart,
+ UnexpectedEnd,
+ DereferenceLimit,
+ NotFound,
+};
+</code></pre>
+
+<h2 id="preprocessing-searching">Preprocessing &#38; Searching</h2>
+
+<p>The line splitting function is defined, which operates on a buffer as follows.</p>
+
+<h3 id="line-splitting-function">Line splitting function:</h3>
+
+<pre><code>pub fn split_lines(file: []const u8, alloc: Allocator) ![][]const u8 {
+ var buffer = std.ArrayList([]const u8).init(alloc);
+ defer buffer.deinit();
+
+ <span class="lp-ref">(Split file at each newline)</span>
+
+ return buffer.toOwnedSlice();
+}
+</code></pre>
+
+<p>The function simply iteratively splits the file at each newline, and appends each resulting line to the buffer.</p>
+
+<h3 id="split-file-at-each-newline">Split file at each newline:</h3>
+
+<pre><code>var iterator = std.mem.split(u8, file, "\n");
+while (iterator.next()) |line| {
+ try buffer.append(line);
+}
+</code></pre>
+
+<p>In addition, the final empty line created by the trailing newline at the end of the file (inserted automatically by some text editors) is removed, if it exists. This may only be performed if the file is non-empty, to avoid out-of-bounds indexing.</p>
+
+<h3 id="split-file-at-each-newline-1">+ Split file at each newline:</h3>
+
+<pre><code>if ((buffer.items.len &#62; 0) and std.mem.eql(u8, buffer.items[buffer.items.len - 1], "")) {
+ _ = buffer.pop();
+}
+</code></pre>
+
+<p>We define the configuration command searching function, which returns a list containing the segments of the split format string. The function will return from within the for loop if the declaration is found, otherwise an error is reported.</p>
+
+<h3 id="configuration-searching-function">Configuration searching function:</h3>
+
+<pre><code>pub fn get_conf(lines: [][]const u8, key: []const u8, alloc: Allocator) ![][]const u8 {
+ for (lines) |line| {
+ if (std.mem.startsWith(u8, line, key)) {
+ return try fmt_conf(line, key, alloc);
+ }
+ }
+ log(.err, "config declaration '{s}' not found", .{std.mem.trimRight(u8, key, " \t")});
+ return error.NotFound;
+}
+
+<span class="lp-ref">(Auxiliary formatting function)</span>
+</code></pre>
+
+<p>If the declaration is found, its contained format string is split along instances of the section name escape sequence, and each substring has its instances of the newline escape sequence replaced with a literal newline.</p>
+
+<h3 id="auxiliary-formatting-function">Auxiliary formatting function:</h3>
+
+<pre><code>fn fmt_conf(line: []const u8, key: []const u8, alloc: Allocator) ![][]const u8 {
+ var buffer = std.ArrayList([]const u8).init(alloc);
+ defer buffer.deinit();
+
+ var iterator = std.mem.split(u8, line[(key.len)..], kc_esc);
+ while (iterator.next()) |str| {
+ try buffer.append(try std.mem.replaceOwned(u8, alloc, str, kc_nl, "\n"));
+ }
+
+ return buffer.toOwnedSlice();
+}
+</code></pre>
+
+<p>We define the code section searching function, which returns the index (into the section list) of the first section with a matching name, or returns an error if none exist.</p>
+
+<h3 id="section-searching-function">Section searching function:</h3>
+
+<pre><code>fn search(list: []Section, name: []const u8) !usize {
+ for (list) |section, index| {
+ if (std.mem.eql(u8, section.name, name)) return index;
+ }
+ log(.err, "section '{s}' not found", .{name});
+ return error.NotFound;
+}
+// TODO return last match instead?
+</code></pre>
+
+<h2 id="parsing">Parsing</h2>
+
+<p>We then define the parsing functions, consisting of the main <code>parse</code> function which builds the list of <code>Section</code>s, and its auxiliary <code>parse_code</code> subroutine which builds the contents of each <code>CodeSection</code>.</p>
+
+<h3 id="parsing-functions">Parsing functions:</h3>
+
+<pre><code>pub fn parse(lines: [][]const u8, alloc: Allocator) ![]Section {
+ var sections = std.ArrayList(Section).init(alloc);
+ defer sections.deinit();
+
+ <span class="lp-ref">(Main parsing routine)</span>
+
+ return sections.toOwnedSlice();
+}
+
+fn parse_code(lines: [][]const u8, index: u32, alloc: Allocator) !CodeReturn {
+ var content = std.ArrayList(Content).init(alloc);
+ defer content.deinit();
+
+ <span class="lp-ref">(Code parsing subroutine)</span>
+
+ return CodeReturn{ .content = content.toOwnedSlice(), .index = i + 1 };
+}
+</code></pre>
+
+<p>The latter function takes as arguments the list of lines and the allocator similarly to the main function, but it is also passed the index of the current line being processed, and returns the line at which the main function should resume parsing after the code section is parsed. It thus returns a struct consisting of the contents of the code section and the next line number index, as follows.</p>
+
+<h3 id="parsing-functions-1">+ Parsing functions:</h3>
+
+<pre><code>const CodeReturn = struct {
+ content: []const Content,
+ index: u32,
+};
+</code></pre>
+
+<p>The main parsing routine iterates over the list of lines, adding code sections where they occur, and otherwise ignoring text sections. If a section end command is encountered in the absence of a preceding starting command, an error is returned.</p>
+
+<h3 id="main-parsing-routine">Main parsing routine:</h3>
+
+<pre><code>var i: u32 = 0;
+while (i &#60; lines.len) {
+ const line = lines[i];
+ if (std.mem.startsWith(u8, line, k_start)) {
+ <span class="lp-ref">(Add new section)</span>
+ } else if (std.mem.startsWith(u8, line, k_add)) {
+ <span class="lp-ref">(Append to section)</span>
+ } else if (std.mem.eql(u8, line, k_end)) {
+ log(.err, "line {d}: unexpected section end", .{i + 1});
+ return error.UnexpectedEnd;
+ } else {
+ i += 1;
+ }
+}
+</code></pre>
+
+<p>To add a new section, the name (consisting of everything after the starting token) is first retrieved from the starting command. Then the code parsing subroutine is called, beginning at the line after the starting command, and it returns the resulting code section (<code>section.content</code>) and the next line at which to resume parsing (<code>section.index</code>). The code section is appended to the section list, and the parsing routine continues at the provided index.</p>
+
+<h3 id="add-new-section">Add new section:</h3>
+
+<pre><code>const name = line[(k_start.len)..];
+log(.debug, "({d}) starting section '{s}'", .{ i + 1, name });
+
+const section = try parse_code(lines, i + 1, alloc);
+try sections.append(.{ .name = name, .content = section.content });
+
+log(.debug, "({d}) ending section '{s}'", .{ section.index, name });
+i = section.index;
+</code></pre>
+
+<p>To append to an existing section, the section name and the code section contents to be appended are retrieved as above. The index of the section is located, along with its address within the section list. Next, the new contents of the section are created by concatenating the old contents with the newly parsed code section contents. The section list is then updated to point to the new section contents, and the parsing routine continues.</p>
+
+<h3 id="append-to-section">Append to section:</h3>
+
+<pre><code>const name = line[(k_add.len)..];
+log(.debug, "({d}) appending to section '{s}'", .{ i + 1, name });
+
+const section = try parse_code(lines, i + 1, alloc);
+const index = try search(sections.items, name);
+const old = &#38;sections.items[index];
+const new = try std.mem.concat(alloc, Content, &#38;[_][]const Content{ old.*.content, section.content });
+old.*.content = new;
+
+log(.debug, "({d}) ending section '{s}'", .{ section.index, name });
+i = section.index;
+</code></pre>
+
+<p>The code parsing subroutine iterates over the list of lines similarly to the main routine. If a starting or appending command is encountered (lacking a matching ending command), an error is raised. Reference commands may be preceded with any amount of whitespace. The loop exits upon encountering an ending command. Otherwise, the line is appended as a literal element.</p>
+
+<h3 id="code-parsing-subroutine">Code parsing subroutine:</h3>
+
+<pre><code>var i = index;
+while (i &#60; lines.len) {
+ const line = lines[i];
+ if (std.mem.startsWith(u8, line, k_start) or std.mem.startsWith(u8, line, k_add)) {
+ log(.err, "line {d}: unexpected section start", .{i + 1});
+ return error.UnexpectedStart;
+ } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
+ <span class="lp-ref">(Add reference)</span>
+ } else if (std.mem.eql(u8, line, k_end)) {
+ break;
+ } else {
+ <span class="lp-ref">(Add literal range)</span>
+ }
+}
+</code></pre>
+
+<p>To add a reference, the name of the referenced section is retrieved, consisting of the characters following the leading whitespace and the command token. The resulting string is appended to the section contents list, and the parser continues at the next line.</p>
+
+<h3 id="add-reference">Add reference:</h3>
+
+<pre><code>const ref_name = std.mem.trimLeft(u8, line, " \t")[(k_ref.len)..];
+try content.append(.{ .reference = ref_name });
+log(.debug, "({d}) \tappended reference '{s}'", .{ i + 1, ref_name });
+i += 1;
+</code></pre>
+
+<p>To add a literal range, the parser either updates the end index of the previous literal element, or creates a new literal element if the last element added is a reference. This action of switching on the previous section element must only occur if the section contents list is non-empty, in order to prevent out-of-bounds indexing. Otherwise, the parser unconditionally appends a new literal element to the list. After either case, parsing continues at the next line.</p>
+
+<h3 id="add-literal-range">Add literal range:</h3>
+
+<pre><code>if (content.items.len &#62; 0) {
+ switch (content.items[content.items.len - 1]) {
+ .literal =&#62; |*range| {
+ range.*.end = i;
+ },
+ .reference =&#62; {
+ try content.append(.{ .literal = .{ .start = i, .end = i } });
+ log(.debug, "({d}) \tappending literal", .{i + 1});
+ },
+ }
+} else {
+ try content.append(.{ .literal = .{ .start = i, .end = i } });
+ log(.debug, "({d}) \tappending literal", .{i + 1});
+}
+i += 1;
+</code></pre>
+
+<h2 id="code-generation">Code Generation</h2>
+
+<p>We define the source code generation procedure which is split into two functions, consisting of a wrapper function which begins code generation at (the index of) the top-level section, and the main procedure which iterates over the current section contents, recursively resolving section references and appending literal elements to the list of source code lines.</p>
+
+<h3 id="code-generation-functions">Code generation functions:</h3>
+
+<pre><code>pub fn codegen(lines: [][]const u8, list: []Section, alloc: Allocator) ![][]const u8 {
+ const root = try search(list, k_root);
+ return try codegen_main(lines, list, root, 0, alloc);
+}
+
+fn codegen_main(lines: [][]const u8, list: []Section, index: usize, depth: u8, alloc: Allocator) anyerror![][]const u8 {
+ var buffer = std.ArrayList([]const u8).init(alloc);
+ defer buffer.deinit();
+
+ const section = list[index];
+ log(.debug, "generating section '{s}'", .{section.name});
+ for (section.content) |content| switch (content) {
+ .literal =&#62; |range| {
+ <span class="lp-ref">(Append literal range)</span>
+ },
+ .reference =&#62; |name| {
+ <span class="lp-ref">(Resolve reference)</span>
+ },
+ };
+
+ log(.debug, "ending section '{s}'", .{section.name});
+ return buffer.toOwnedSlice();
+}
+</code></pre>
+
+<p>To append a literal range, the range of lines is simply appended to the buffer.</p>
+
+<h3 id="append-literal-range">Append literal range:</h3>
+
+<pre><code>log(.debug, "adding literal range {d}-{d}", .{ range.start + 1, range.end + 1 });
+try buffer.appendSlice(lines[(range.start)..(range.end + 1)]);
+</code></pre>
+
+<p>To resolve a section reference, the function must first check whether the current recursion depth has exceeded the configured limit, and return an error if this occurs. Otherwise, the index of the referenced section is retrieved, its contents are recursively parsed (with an incremented recursion depth), and the resulting source code lines are appended to the buffer.</p>
+
+<h3 id="resolve-reference">Resolve reference:</h3>
+
+<pre><code>if (depth &#62; dereference_max) {
+ log(.err, "section dereferencing recursion depth exceeded (max {d})", .{dereference_max});
+ return error.DereferenceLimit;
+}
+const ref = try search(list, name);
+const code = try codegen_main(lines, list, ref, depth + 1, alloc);
+try buffer.appendSlice(code);
+</code></pre>
+
+<h2 id="text-generation">Text Generation</h2>
+
+<p>Finally, we define the text generation function which iterates over the list of lines and produces the literate program text to be passed to an external document processor. In order to keep track of the name of the code section currently being formatted at any given point, the variable <code>current_name</code> is continually updated to contain the current name string. Configuration declarations are skipped, and lines which do not contain any formatting commands are appended as they are.</p>
+
+<h3 id="text-generation-function">Text generation function:</h3>
+
+<pre><code>pub fn textgen(lines: [][]const u8, alloc: Allocator) ![][]const u8 {
+ var buffer = std.ArrayList([]const u8).init(alloc);
+ defer buffer.deinit();
+
+ <span class="lp-ref">(Process configuration declarations)</span>
+
+ var current_name: []const u8 = undefined;
+ for (lines) |line| {
+ if ( std.mem.startsWith(u8, line, kc_start)
+ or std.mem.startsWith(u8, line, kc_add)
+ or std.mem.startsWith(u8, line, kc_end)
+ or std.mem.startsWith(u8, line, kc_ref)) {
+ continue;
+ } else if (std.mem.startsWith(u8, line, k_start)) {
+ <span class="lp-ref">(Format starting command)</span>
+ } else if (std.mem.startsWith(u8, line, k_add)) {
+ <span class="lp-ref">(Format appending command)</span>
+ } else if (std.mem.startsWith(u8, line, k_end)) {
+ <span class="lp-ref">(Format ending command)</span>
+ } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
+ <span class="lp-ref">(Format reference command)</span>
+ } else {
+ try buffer.append(line);
+ }
+ }
+
+ return buffer.toOwnedSlice();
+}
+</code></pre>
+
+<p>The formatting strings given by each configuration declaration are first retrieved. If the declaration of the format string for the section appending command is omitted, the format string for the section starting command is used in its place.</p>
+
+<h3 id="process-configuration-declarations">Process configuration declarations:</h3>
+
+<pre><code>const conf_start = try get_conf(lines, kc_start, alloc);
+const conf_add = get_conf(lines, kc_add, alloc) catch conf_start;
+const conf_end = try get_conf(lines, kc_end, alloc);
+const conf_ref = try get_conf(lines, kc_ref, alloc);
+</code></pre>
+
+<p>To process a section starting command, the current section name is updated, and the contents of the corresponding formatting command (that is, the segments of the split formatting string) are interspersed with copies of the current section name. The resulting string is then appended to the buffer.</p>
+
+<h3 id="format-starting-command">Format starting command:</h3>
+
+<pre><code>current_name = line[(k_start.len)..];
+try buffer.append(try std.mem.join(alloc, current_name, conf_start));
+</code></pre>
+
+<p>Processing a section appending command is performed similarly.</p>
+
+<h3 id="format-appending-command">Format appending command:</h3>
+
+<pre><code>current_name = line[(k_add.len)..];
+try buffer.append(try std.mem.join(alloc, current_name, conf_add));
+</code></pre>
+
+<p>Processing a section ending command, however, does not require updating the current section name.</p>
+
+<h3 id="format-ending-command">Format ending command:</h3>
+
+<pre><code>try buffer.append(try std.mem.join(alloc, current_name, conf_end));
+</code></pre>
+
+<p>To process a reference command, the index of the reference command keyword is first extracted. Then the formatted reference string is created, to which the reference command line&#8217;s leading whitespace is prepended (to preserve indentation). </p>
+
+<h3 id="format-reference-command">Format reference command:</h3>
+
+<pre><code>const start = std.mem.indexOf(u8, line, k_ref).?;
+const ref = try std.mem.join(alloc, line[(start + k_ref.len)..], conf_ref);
+try buffer.append(try std.mem.concat(alloc, u8, &#38;[_][]const u8{ line[0..start], ref }));
+</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>
diff --git a/software/literary/log.html b/software/literary/log.html
new file mode 100755
index 0000000..5a538c3
--- /dev/null
+++ b/software/literary/log.html
@@ -0,0 +1,100 @@
+<!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>log.lp &mdash; DistressNetwork°</title>
+<meta http-equiv="last-modified" content="2022-03-31 23:28:57-04:00" />
+<style>.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-style: italic;}</style>
+</head>
+<body>
+<div class="contentlevel">
+<main>
+<div class="leading">log.lp</div>
+<hr>
+<h1 id="log.zig">log.zig</h1>
+
+<p>This file contains a simple logging function. It is a modified version of the example logging function implementation provided in <code>std.log</code>.</p>
+
+<h3 id="section">*:</h3>
+
+<pre><code><span class="lp-ref">(Imports)</span>
+
+<span class="lp-ref">(Level setting)</span>
+
+<span class="lp-ref">(Logging function)</span>
+</code></pre>
+
+<p>We first import the standard library, and the <code>Level</code> type which is an enum representing the possible log levels.</p>
+
+<h3 id="imports">Imports:</h3>
+
+<pre><code>const std = @import("std");
+const Level = std.log.Level;
+</code></pre>
+
+<p>The logging function is structured such that only log messages equal to or above a certain severity threshold will be printed to the console. This threshold can then be globally modified during development. The threshold constant is defined below.</p>
+
+<h3 id="level-setting">Level setting:</h3>
+
+<pre><code>pub const log_level: Level = .warn;
+</code></pre>
+
+<p>We then define the logging function itself, which accepts a <code>Level</code> value and the format string and argument struct to be passed to the inner print function.</p>
+
+<h3 id="logging-function">Logging function:</h3>
+
+<pre><code>pub fn log(
+ comptime level: Level,
+ comptime format: []const u8,
+ args: anytype,
+) void {
+ <span class="lp-ref">(Compare with level threshold)</span>
+
+ <span class="lp-ref">(Define message string)</span>
+
+ <span class="lp-ref">(Print to console)</span>
+}
+</code></pre>
+
+<p>First the comparison against the severity threshold is made. (A lower integer value signifies a higher severity.) If the severity is lower than the threshold, the function immediately exits.</p>
+
+<h3 id="compare-with-level-threshold">Compare with level threshold:</h3>
+
+<pre><code>if (@enumToInt(level) &#62; @enumToInt(log_level)) return;
+</code></pre>
+
+<p>Next the message string is created. The unformatted content of this string is evaluated at compile-time, before being formatted by the print function at runtime. The &#8220;info&#8221; and &#8220;error&#8221; log levels use custom names, whereas all other levels use their default display names.</p>
+
+<h3 id="define-message-string">Define message string:</h3>
+
+<pre><code>const msg = "[" ++ switch (level) {
+ .info =&#62; "ok",
+ .err =&#62; "err",
+ else =&#62; level.asText(),
+} ++ "]\t" ++ format ++ "\n";
+</code></pre>
+
+<p>Finally, the message is printed to the console. If an error is returned by the <code>print()</code> call, the logging function silently exits.</p>
+
+<h3 id="print-to-console">Print to console:</h3>
+
+<pre><code>const stderr = std.io.getStdErr().writer();
+nosuspend stderr.print(msg, args) catch return;
+</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>
diff --git a/software/literary/tangle.html b/software/literary/tangle.html
new file mode 100755
index 0000000..b8269a3
--- /dev/null
+++ b/software/literary/tangle.html
@@ -0,0 +1,139 @@
+<!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>
+<meta http-equiv="last-modified" content="2022-03-31 23:28:57-04:00" />
+<style>.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-style: italic;}</style>
+</head>
+<body>
+<div class="contentlevel">
+<main>
+<div class="leading">tangle.lp</div>
+<hr>
+<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="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>
+
+<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>
diff --git a/software/literary/weave.html b/software/literary/weave.html
new file mode 100755
index 0000000..9bfee51
--- /dev/null
+++ b/software/literary/weave.html
@@ -0,0 +1,123 @@
+<!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>weave.lp &mdash; DistressNetwork°</title>
+<meta http-equiv="last-modified" content="2022-03-31 23:28:57-04:00" />
+<style>.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-style: italic;}</style>
+</head>
+<body>
+<div class="contentlevel">
+<main>
+<div class="leading">weave.lp</div>
+<hr>
+<h1 id="weave.zig">weave.zig</h1>
+
+<p>The structure of this file is quite similar to that of <code>tangle.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">(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">(Generate text)</span>
+
+ <span class="lp-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 text file is then generated. This entails searching for the configuration declarations, which may fail and thus return an error. Logging such errors is handled by the function itself, and thus the errors are handled here solely by exiting.</p>
+
+<h3 id="generate-text">Generate text:</h3>
+
+<pre><code>const text = data.textgen(lines, alloc) catch |err| switch (err) {
+ error.NotFound =&#62; {
+ return 1;
+ },
+ else =&#62; |e| return e,
+};
+</code></pre>
+
+<p>Finally, the lines of the text file are written to stdout, separated by newlines.</p>
+
+<h3 id="write-to-stdout">Write to stdout:</h3>
+
+<pre><code>for (text) |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>