summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 14:27:06 -0300
committerGabriel Schneider <[email protected]>2026-11-06 14:53:45 -0300
commit333cdea69956adda91f462980a18f5bac6548dda (patch)
tree641e3226c4ce0192bbdab3ad167248f120d9ab8b
parent31706a1d1c13c26c69f67f107b72179019cbd255 (diff)
downloadpardes-333cdea69956adda91f462980a18f5bac6548dda.tar.gz
pardes-333cdea69956adda91f462980a18f5bac6548dda.zip
kinds #27, life: addons/life/life.zig is a second shipped Zig addon, Conway's Life in a pane written clean-room against src/kinds/addon.zig and the header alone (steps ten generations a second while focused, space pauses, r reseeds, hjkl and x edit, the prefix the generation and alive count, the body the board, dump and load the board), with its script and golden under addon-test and its own zig tests; the module's timing test and the dogfood that found Kind.step's stale tick clock
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
-rw-r--r--addons/life/life.out321
-rw-r--r--addons/life/life.script24
-rw-r--r--addons/life/life.zig127
-rw-r--r--build.zig6
4 files changed, 476 insertions, 2 deletions
diff --git a/addons/life/life.out b/addons/life/life.out
new file mode 100644
index 00000000..07ed2a49
--- /dev/null
+++ b/addons/life/life.out
@@ -0,0 +1,321 @@
+> seed 7
+> size 40 36
+window 38x16
+tag gen 0 alive 130
+|.#.#.......##......#...#..#.....
+|#.#......#......#......#.#......
+|.#.......##..##.....#...#...#..#
+|.....#..##.....#.#.#......#....#
+|#...#..........####....#.#......
+|..#.#.#....#.......#......###...
+|........##..#............#.....#
+|...#......##........#.......#...
+|...#.#.....#.###...#.###....#.##
+|##....#...#...#..#..#...##..#..#
+|..#..#.........#.........##...#.
+|.#..##.......#..#..#....#.#.....
+|..#..#...#.##....#.#..........##
+|..#...#..###..#...........#..#.#
+|....#......##.##..#.#.....#.#.#.
+|..#..##.#.......##.....#...#....
+> tick 100
+tag gen 0 alive 130
+|.#.#.......##......#...#..#.....
+|#.#......#......#......#.#......
+|.#.......##..##.....#...#...#..#
+|.....#..##.....#.#.#......#....#
+|#...#..........####....#.#......
+|..#.#.#....#.......#......###...
+|........##..#............#.....#
+|...#......##........#.......#...
+|...#.#.....#.###...#.###....#.##
+|##....#...#...#..#..#...##..#..#
+|..#..#.........#.........##...#.
+|.#..##.......#..#..#....#.#.....
+|..#..#...#.##....#.#..........##
+|..#...#..###..#...........#..#.#
+|....#......##.##..#.#.....#.#.#.
+|..#..##.#.......##.....#...#....
+> tick 100
+tag gen 1 alive 170
+|.#.#............##....##..#.....
+|#.#......#.###.........#.#......
+|.#........#...###.......##.....#
+|........###....#.#.#....##.....#
+|...##..........#.#.#.....#......
+|...#.#..........###.....####....
+|...#.....#..#.............#.#...
+|....#....###.##.....###......###
+|..#.#......#####...#.####..##.##
+|###.###......#..#...########....
+|..#.###.......###.........##...#
+|.######.....#...#.#.......#...##
+|#######..#.###....#......#....##
+|...#.#...#....##..##.......#.#.#
+|...#..##.#.#######........#.###.
+|..####.......#.#####......##....
+> tick 100
+tag gen 2 alive 126
+|.#...........###......######....
+|#.#.......#####..#....##.##.....
+|.#......#...#####......#..#....#
+|#........##......#........#.....
+|...##....#.....#...#............
+|..##............###.....#..#....
+|...#.....#.###...#...#....#.###.
+|....#....#.....#....#..........#
+|..#.#......#...#...#.......##...
+|#.#...#.........#...#.........#.
+|.......#.....##.#....####.....##
+|.......#...##.#.#........###....
+|..........######..#.......#..#..
+|.#.....#.#........##......##....
+|......#.#.#.#.............#..##.
+|.....##......#...........##.##..
+> key x x
+tag gen 2 alive 126 paused
+|.#...........###......######....
+|#.#.......#####..#....##.##.....
+|.#......#...#####......#..#....#
+|#........##......#........#.....
+|...##....#.....#...#............
+|..##............###.....#..#....
+|...#.....#.###...#...#....#.###.
+|....#....#.....#....#..........#
+|..#.#......#...#...#.......##...
+|#.#...#.........#...#.........#.
+|.......#.....##.#....####.....##
+|.......#...##.#.#........###....
+|..........######..#.......#..#..
+|.#.....#.#........##......##....
+|......#.#.#.#.............#..##.
+> tick 300
+tag gen 2 alive 126 paused
+|.#...........###......######....
+|#.#.......#####..#....##.##.....
+|.#......#...#####......#..#....#
+|#........##......#........#.....
+|...##....#.....#...#............
+|..##............###.....#..#....
+|...#.....#.###...#...#....#.###.
+|....#....#.....#....#..........#
+|..#.#......#...#...#.......##...
+|#.#...#.........#...#.........#.
+|.......#.....##.#....####.....##
+|.......#...##.#.#........###....
+|..........######..#.......#..#..
+|.#.....#.#........##......##....
+|......#.#.#.#.............#..##.
+> key lljx
+tag gen 2 alive 125 paused
+|.#...........###......######....
+|#.........#####..#....##.##.....
+|.#......#...#####......#..#....#
+|#........##......#........#.....
+|...##....#.....#...#............
+|..##............###.....#..#....
+|...#.....#.###...#...#....#.###.
+|....#....#.....#....#..........#
+|..#.#......#...#...#.......##...
+|#.#...#.........#...#.........#.
+|.......#.....##.#....####.....##
+|.......#...##.#.#........###....
+|..........######..#.......#..#..
+|.#.....#.#........##......##....
+|......#.#.#.#.............#..##.
+> key k
+tag gen 2 alive 125 paused
+|.#...........###......######....
+|#.........#####..#....##.##.....
+|.#......#...#####......#..#....#
+|#........##......#........#.....
+|...##....#.....#...#............
+|..##............###.....#..#....
+|...#.....#.###...#...#....#.###.
+|....#....#.....#....#..........#
+|..#.#......#...#...#.......##...
+|#.#...#.........#...#.........#.
+|.......#.....##.#....####.....##
+|.......#...##.#.#........###....
+|..........######..#.......#..#..
+|.#.....#.#........##......##....
+|......#.#.#.#.............#..##.
+> body
+=.#...........###......######....
+=#.........#####..#....##.##.....
+=.#......#...#####......#..#....#
+=#........##......#........#.....
+=...##....#.....#...#............
+=..##............###.....#..#....
+=...#.....#.###...#...#....#.###.
+=....#....#.....#....#..........#
+=..#.#......#...#...#.......##...
+=#.#...#.........#...#.........#.
+=.......#.....##.#....####.....##
+=.......#...##.#.#........###....
+=..........######..#.......#..#..
+=.#.....#.#........##......##....
+=......#.#.#.#.............#..##.
+=.....##......#...........##.##..
+> dump
+dump 536 bytes
+tag gen 2 alive 125 paused
+|.#...........###......######....
+|#.........#####..#....##.##.....
+|.#......#...#####......#..#....#
+|#........##......#........#.....
+|...##....#.....#...#............
+|..##............###.....#..#....
+|...#.....#.###...#...#....#.###.
+|....#....#.....#....#..........#
+|..#.#......#...#...#.......##...
+|#.#...#.........#...#.........#.
+|.......#.....##.#....####.....##
+|.......#...##.#.#........###....
+|..........######..#.......#..#..
+|.#.....#.#........##......##....
+|......#.#.#.#.............#..##.
+|.....##......#...........##.##..
+> key r
+tag gen 0 alive 130 paused
+|.#.#.......##......#...#..#.....
+|#.#......#......#......#.#......
+|.#.......##..##.....#...#...#..#
+|.....#..##.....#.#.#......#....#
+|#...#..........####....#.#......
+|..#.#.#....#.......#......###...
+|........##..#............#.....#
+|...#......##........#.......#...
+|...#.#.....#.###...#.###....#.##
+|##....#...#...#..#..#...##..#..#
+|..#..#.........#.........##...#.
+|.#..##.......#..#..#....#.#.....
+|..#..#...#.##....#.#..........##
+|..#...#..###..#...........#..#.#
+|....#......##.##..#.#.....#.#.#.
+> key h l
+tag gen 0 alive 130
+|.#.#.......##......#...#..#.....
+|#.#......#......#......#.#......
+|.#.......##..##.....#...#...#..#
+|.....#..##.....#.#.#......#....#
+|#...#..........####....#.#......
+|..#.#.#....#.......#......###...
+|........##..#............#.....#
+|...#......##........#.......#...
+|...#.#.....#.###...#.###....#.##
+|##....#...#...#..#..#...##..#..#
+|..#..#.........#.........##...#.
+|.#..##.......#..#..#....#.#.....
+|..#..#...#.##....#.#..........##
+|..#...#..###..#...........#..#.#
+|....#......##.##..#.#.....#.#.#.
+|..#..##.#.......##.....#...#....
+> tick 100
+tag gen 4 alive 112
+|.#....#....#................#...
+|#.#............#......#....#....
+|.#.......##.....#......#.......#
+|####....#.#.....#......#........
+|.###....#........#..........#...
+|.#........#.....#.#........#..#.
+|..#.#.....#.##.#..##.......#..#.
+|..#.##.....#....#.#.#.......#.##
+|#.#.#.........#.##.#..#......#.#
+|..#...#.......#....#.....#...#..
+|.............##.....#......#...#
+|.......#.#.#.........#.....#.###
+|.......##.#...##...#...#......#.
+|......#...#...##.#.#......##..#.
+|.....#...#...#............#....#
+|.....#.##...#...............#.#.
+> tick 100
+tag gen 5 alive 132
+|.#....##...................###..
+|#.#.......#.....................
+|...#.....##....##.....##.......#
+|...#....#.#.....##..............
+|...#............##..............
+|.#.......#.#....#.##.......###..
+|.##.##....#.#..##.#........##.#.
+|#.#.##.....####.#...#.......#...
+|#.#.#...........##.##.......##.#
+|##.#..........#...###.......#..#
+|.............##.....#.....#..#.#
+|.......#.##..#.#....#.......##.#
+|......###.##..###.#.#.....###...
+|......###.#..#.##.#.......##..##
+|.....#.###...##...........#..###
+|.....#.##...#..............#.#..
+> blur
+tag gen 5 alive 132
+|.#....##...................###..
+|#.#.......#.....................
+|...#.....##....##.....##.......#
+|...#....#.#.....##..............
+|...#............##..............
+|.#.......#.#....#.##.......###..
+|.##.##....#.#..##.#........##.#.
+|#.#.##.....####.#...#.......#...
+|#.#.#...........##.##.......##.#
+|##.#..........#...###.......#..#
+|.............##.....#.....#..#.#
+|.......#.##..#.#....#.......##.#
+|......###.##..###.#.#.....###...
+|......###.#..#.##.#.......##..##
+|.....#.###...##...........#..###
+|.....#.##...#..............#.#..
+> tick 300
+tag gen 5 alive 132
+|.#....##...................###..
+|#.#.......#.....................
+|...#.....##....##.....##.......#
+|...#....#.#.....##..............
+|...#............##..............
+|.#.......#.#....#.##.......###..
+|.##.##....#.#..##.#........##.#.
+|#.#.##.....####.#...#.......#...
+|#.#.#...........##.##.......##.#
+|##.#..........#...###.......#..#
+|.............##.....#.....#..#.#
+|.......#.##..#.#....#.......##.#
+|......###.##..###.#.#.....###...
+|......###.#..#.##.#.......##..##
+|.....#.###...##...........#..###
+|.....#.##...#..............#.#..
+> focus
+tag gen 5 alive 132
+|.#....##...................###..
+|#.#.......#.....................
+|...#.....##....##.....##.......#
+|...#....#.#.....##..............
+|...#............##..............
+|.#.......#.#....#.##.......###..
+|.##.##....#.#..##.#........##.#.
+|#.#.##.....####.#...#.......#...
+|#.#.#...........##.##.......##.#
+|##.#..........#...###.......#..#
+|.............##.....#.....#..#.#
+|.......#.##..#.#....#.......##.#
+|......###.##..###.#.#.....###...
+|......###.#..#.##.#.......##..##
+|.....#.###...##...........#..###
+|.....#.##...#..............#.#..
+> tick 200
+tag gen 6 alive 123
+|.#....###..................#.#..
+|###......##.................#...
+|..##......##...###..............
+|..###.....#.....................
+|..#......##....#............#...
+|.#.##.....##......##.......#.#..
+|#.#.##....#...#.#.#.............
+|#.#........####...#.#.........#.
+|..#.##......#.#.##...#.....####.
+|.###.........###.##..#.....##...
+|.............#.#....##.....#.#.#
+|......##.#####..#...##....#..#..
+|...........###............#....#
+|.....#....####..#..#.....#.....#
+|.....#......####..........#..#.#
+|.....#...#...#............##....
diff --git a/addons/life/life.script b/addons/life/life.script
new file mode 100644
index 00000000..fba9cd62
--- /dev/null
+++ b/addons/life/life.script
@@ -0,0 +1,24 @@
+# pardes --addon-test life.so addons/life/life.script | diff addons/life/life.out -
+seed 7
+size 40 36
+tick 100
+tick 100
+tick 100
+# x, Space (pause), x: the cell under the cursor toggled twice
+key x x
+tick 300
+key lljx
+key k
+body
+dump
+key r
+# h, Space (resume; a line's ends are trimmed), l. The tick after a key is
+# handed the time of the last tick, from before the paused 300 ms, so the
+# next tick 100 catches up four generations.
+key h l
+tick 100
+tick 100
+blur
+tick 300
+focus
+tick 200
diff --git a/addons/life/life.zig b/addons/life/life.zig
new file mode 100644
index 00000000..5ea9c302
--- /dev/null
+++ b/addons/life/life.zig
@@ -0,0 +1,127 @@
+//! Conway's Game of Life in a pane: ten generations a second while the
+//! pane has the keyboard. Space pauses, r reseeds, hjkl move the cursor,
+//! x toggles the cell under it.
+const std = @import("std");
+const addon = @import("addon");
+
+export const pardes_addon = addon.ops(Life);
+
+const W = 32;
+const H = 16;
+const period = std.time.ns_per_s / 10;
+
+const Life = extern struct {
+ cells: [H][W]bool,
+ /// The pane's, kept: r after a restore reseeds as before it.
+ seed: u64,
+ gen: u32,
+ cx: u8,
+ cy: u8,
+ paused: bool,
+ next: addon.Deadline,
+
+ pub const head: addon.Head = .{ .name = "life", .version = 1, .words = &.{.{ .name = "Life", .opens = true, .doc = "a pane of Conway's Game of Life" }} };
+
+ pub fn open(s: *Life, _: ?[]const u8) void {
+ s.seed = addon.seed();
+ s.reseed();
+ }
+
+ pub fn tick(s: *Life, now: u64, flags: addon.Flags) u64 {
+ if (!flags.focused or s.paused) return s.next.stop();
+ // A record's deadline is of another clock's origin.
+ if (s.next.at > now + period) s.next.at = 0;
+ if (!s.next.due(now)) return s.next.every(now, period);
+ s.step();
+ // From the step's due time, not now: a tick comes on a frame, late.
+ s.next.at = if (now - s.next.at < period) s.next.at + period else now + period;
+ return s.next.at;
+ }
+
+ pub fn key(s: *Life, k: addon.Key) bool {
+ switch (k.code) {
+ addon.char(' ') => s.paused = !s.paused,
+ addon.char('r') => s.reseed(),
+ addon.char('h') => s.cx = (s.cx + W - 1) % W,
+ addon.char('l') => s.cx = (s.cx + 1) % W,
+ addon.char('k') => s.cy = (s.cy + H - 1) % H,
+ addon.char('j') => s.cy = (s.cy + 1) % H,
+ addon.char('x') => s.cells[s.cy][s.cx] = !s.cells[s.cy][s.cx],
+ else => return false,
+ }
+ return true;
+ }
+
+ pub fn paint(s: *Life, w: addon.Window, p: addon.Paint) void {
+ p.prefix("gen {d} alive {d}{s}", .{ s.gen, s.alive(), if (s.paused) " paused" else "" });
+ for (s.cells, 0..) |row, y| for (row, 0..) |on, x| {
+ const cursor = w.focused and x == s.cx and y == s.cy;
+ w.put(x, y, if (on) "#" else ".", .{ .attrs = .{ .reverse = cursor } });
+ };
+ }
+
+ pub fn body(s: *Life, out: *std.Io.Writer) !void {
+ for (s.cells) |row| {
+ for (row) |on| try out.writeByte(if (on) '#' else '.');
+ try out.writeByte('\n');
+ }
+ }
+
+ pub const dump = addon.dumpBytes;
+ pub const load = addon.loadBytes;
+
+ fn reseed(s: *Life) void {
+ var prng: std.Random.DefaultPrng = .init(s.seed);
+ const r = prng.random();
+ for (&s.cells) |*row| for (row) |*on| {
+ on.* = r.uintLessThan(u8, 4) == 0;
+ };
+ s.gen = 0;
+ }
+
+ fn alive(s: *const Life) u32 {
+ var n: u32 = 0;
+ for (s.cells) |row| for (row) |on| {
+ n += @intFromBool(on);
+ };
+ return n;
+ }
+
+ fn step(s: *Life) void {
+ var next: [H][W]bool = undefined;
+ for (0..H) |y| for (0..W) |x| {
+ var n: u8 = 0;
+ for ([_]usize{ H - 1, 0, 1 }) |dy| for ([_]usize{ W - 1, 0, 1 }) |dx| {
+ if (dy == 0 and dx == 0) continue;
+ n += @intFromBool(s.cells[(y + dy) % H][(x + dx) % W]);
+ };
+ next[y][x] = n == 3 or (n == 2 and s.cells[y][x]);
+ };
+ s.cells = next;
+ s.gen += 1;
+ }
+};
+
+test "a blinker blinks, wrapping at the edges" {
+ var s = std.mem.zeroes(Life);
+ for ([_]usize{ W - 1, 0, 1 }) |x| s.cells[0][x] = true;
+ s.step();
+ try std.testing.expectEqual(3, s.alive());
+ for ([_]usize{ H - 1, 0, 1 }) |y| try std.testing.expect(s.cells[y][0]);
+ s.step();
+ try std.testing.expect(s.cells[0][W - 1] and s.cells[0][1] and !s.cells[1][0]);
+ try std.testing.expectEqual(2, s.gen);
+}
+
+test "a record loads back into a fresh pane" {
+ var a = std.mem.zeroes(Life);
+ a.seed = 42;
+ a.reseed();
+ a.step();
+ var buf: [@sizeOf(Life)]u8 = undefined;
+ try std.testing.expectEqual(buf.len, Life.dump(&a, &buf));
+ var b = std.mem.zeroes(Life);
+ try std.testing.expect(Life.load(&b, &buf, Life.head.version));
+ try std.testing.expectEqualDeep(a.cells, b.cells);
+ try std.testing.expect(!Life.load(&b, &buf, 2));
+}
diff --git a/build.zig b/build.zig
index 86297c62..4808d8e8 100644
--- a/build.zig
+++ b/build.zig
@@ -1621,10 +1621,12 @@ pub fn build(b: *std.Build) void {
const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake, snake and pdf addons, against addons/*/*.out, and the Zig addons' own tests");
const snake_mod = b.createModule(.{ .root_source_file = b.path("addons/snake/snake.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "addon", .module = addon_mod }} });
const snake_lib = b.addLibrary(.{ .name = "snake", .linkage = .dynamic, .root_module = snake_mod });
- for ([_]*std.Build.Module{ snake_mod, image_mod }) |m| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
+ const life_mod = b.createModule(.{ .root_source_file = b.path("addons/life/life.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "addon", .module = addon_mod }} });
+ const life_lib = b.addLibrary(.{ .name = "life", .linkage = .dynamic, .root_module = life_mod });
+ for ([_]*std.Build.Module{ snake_mod, life_mod, image_mod }) |m| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
// ...and the pdf addon's, which mupdf-check runs too (its script
// opens docs/design.pdf, from the build root).
- const libs = [_]struct { ?*std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" }, .{ pdf_addon, "addons/pdf/pdf" } };
+ const libs = [_]struct { ?*std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" }, .{ life_lib, "addons/life/life" }, .{ pdf_addon, "addons/pdf/pdf" } };
for (libs) |t| if (t[0]) |lib| {
const run_addon_test = b.addRunArtifact(exe);
run_addon_test.setCwd(b.path("."));