diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 14:48:26 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 15:14:31 -0300 |
| commit | 7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb (patch) | |
| tree | a6add92cd4dcbbf4c31cd1ce989d99b190258353 /anim/scenes/sort.zig | |
| parent | ed46c51fe127377eb1fde8dee83da984e0f61860 (diff) | |
| download | computeranim-7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb.tar.gz computeranim-7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb.zip | |
canim: animations of recordings, as a debugger's whole context with a lens beside it
A package of its own (anim/), on sketchbook's anim module and pardes's
syntax. A scene opens a recording, takes a cursor into it, and declares
blocks that read the moment the cursor is at: the state column (registers
each followed as a pointer and inked by where it points, the stack
telescope, the backtrace), the program column (the whole source, the
disassembly in address order, and the scene's lens: a node-link list over
a hexdump of its heap, array cells, a call tree, a syscall crossing), and
ties between them. Motion is a step told as a sentence (the line fires,
its effects cascade a level at a time, the mark moves on last), timed by
what the step contains; walk, hurry and sprint pace a loop. Five scenes
from real recordings of fib, a linked list, a sort and syscalls.
First drafts, by the agent's own handover: the frame of the design's
second revision, the hexdump lens and the ties; no disassembly
annotations, no gallery, scenes not yet reviewed against the mocks.
Diffstat (limited to 'anim/scenes/sort.zig')
| -rw-r--r-- | anim/scenes/sort.zig | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/anim/scenes/sort.zig b/anim/scenes/sort.zig new file mode 100644 index 0000000..0f14269 --- /dev/null +++ b/anim/scenes/sort.zig @@ -0,0 +1,63 @@ +//! Insertion sort: neighbours compared, and swapped while the left is larger. +//! +//! How it moves (design/motion.md): it opens where `i` has reached 12, the +//! first value out of order; the compare is walked (its two cells framed: +//! the swap's anticipation), the caption about the swap stands two +//! seconds before it, and the swap is two flights on arcs, the second a +//! little behind the first, each cell struck as its value lands. The next +//! trips are a run, the long middle a sprint, and the last swap is walked. +const anim = @import("anim"); +const canim = @import("canim"); +const recordings = @import("recordings"); + +pub const background = canim.background; +pub const scene = anim.Live(frame, struct {}); + +fn frame(s: *anim.Frame) void { + const rec = canim.open(recordings.sort); + const t = canim.theme.time; + const sort = rec.call("sort", 0); + // The `while`, about to compare 31 with 12: the stop before the one that calls swap. + var start = sort.enter; + while (rec.next(.over, rec.next(.over, start)) < rec.call("swap", 0).enter) start = rec.next(.over, start); + var at = canim.cursor(s, rec, start); + // The array is the idea; `i` and `j` are memory, and strike a beat before their tags slide. + at.levels = .{ .cpu = false, .idea = .data, .follow = t.quick, .source_rows = 8 }; + // Its last compare: nothing larger is left of 20. + const last = rec.call("swap", 9); + const end = rec.next(.over, rec.next(.over, last.exit.?)); + at.journey(.over, end); + // The context is the instrumented program's: its registers are unmarked. + const split = canim.layout.split.beside; + const ctx = canim.context(s, at, .{ .split = split, .quiet = true }); + // The lens: the array as cells, and the two indices that walk it. + const cells = canim.array(s, .{ .cursor = at, .box = split.lens, .variable = "a", .indices = &.{ "i", "j" }, .focus = true }); + var says: canim.Narrator = .{}; + // A swap, start to finish, as one step of sort(): over the call. + const swap = struct { + fn step(c: canim.Cursor, frame_: *anim.Frame, r: *canim.Recording) void { + c.step(frame_, r.next(.over, c.moment(frame_)), canim.Array.swap, canim.Array.swap.total); + } + }.step; + + const joins = canim.ties(s, .{ .cursor = at }); + canim.establish(s, .{ ctx.clock, ctx.source, ctx.disasm, ctx.registers, ctx.stack, ctx.backtrace, cells, joins }); + says.line(s, "`i` walks right. Everything left of it is in order,\nand 12 is not yet in its place."); + // The compare: both cells framed. It is the swap's anticipation. + at.walk(s, .over, rec.next(.over, start)); + says.line(s, "While the one on the left is larger, they trade places,\nand `j` follows the smaller value down."); + // The caption of the step the scene is about stands a second longer. + s.wait(t.caption_lead); + // Arcs: the two values fly past each other, one over, one under. + swap(at, s, rec); + at.walk(s, .over, rec.next(.over, at.moment(s))); + + // Pacing: the same again, so at a run; then the long way 5 has to go, + // at a sprint; slowing for the last swap. + at.hurry(s, .over, rec.call("swap", 3).enter - 1, .{ .to = t.run, .settle = false }); + at.hurry(s, .over, last.enter - 1, .{ .pace = t.sprint, .from = t.run }); + swap(at, s, rec); + at.walk(s, .over, end); + says.line(s, "Nothing larger is left of 20. The array is sorted."); + s.wait(t.before_cut - t.caption_lead); +} |
