summaryrefslogtreecommitdiff
path: root/src/host/fields.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 01:52:03 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit3e5ad32c529c4c566989ef8201159100a4a3e32c (patch)
treedac282e335b4c09fb7c5753af9020114e54e815f /src/host/fields.zig
parentcf347f91ee838759dc44ed34ec6ff320186e90ce (diff)
downloadsketchbook-3e5ad32c529c4c566989ef8201159100a4a3e32c.tar.gz
sketchbook-3e5ad32c529c4c566989ef8201159100a4a3e32c.zip
The State as files: state/ in the 9P tree, a directory per struct and array
state/ serves the running sketch's State nested: a directory per struct and per array, a file per value, state/tracker/songs/3/bpm. A read is the value as get shows it, and a newline; a write is the console's set with the file's path (control.zig builds `set tracker.songs[3].bpm 90` and runs it through runLine, so it stops a replay, logs, marks a jump and notes the line like any set). Numbers, bools and enums are written; optionals, unions and the like read as `<N bytes>` and refuse writes. An array of scalars (a vector's lanes, talk.text) is one file of its values, blank-separated; an array of arrays is a directory of such rows (grid/1 for grid[1] of a [2][3]i16). So a file is one value or one row, the run of values one set names, and stays the size of a row however large the array; and paths keep the State's own shape. An array of structs is a directory of indices: drat-meout's patterns, 8x16x16x4 of {pitch, velocity}, are state/tracker/songs/<n>/patterns/<n>/<n>/<n>/ pitch, reached by walking, listed a dimension at a time. state_files.zig does the walking from a (Layout, bytes) alone: lookup, readdir, read, the set path and a README line per file, so the save's newest State and a sketch's defaults can be served the same way. A node is the layout's first leaf under it, its depth and the element's offset, with bit 62 set (bit 63 stays the sketch's). A node a reload made stale is refused, "StateLayoutChangedWalkAgain". fields.setRow sets a row from its words, all or none, and the console's set takes it: `set talk.text 104 105 0`. The engine keeps names up to 64 bytes (from 28), for long field names. Tests: test-control (now also "the State as files" and "a row of scalars"): 40/40 steps, 9/9 tests. Live, headless, the replay fixture over plan9port 9p: ls state, read and write dots/buffer/3/kind and the position vector, a bad value refused as InvalidValue. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host/fields.zig')
-rw-r--r--src/host/fields.zig37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/host/fields.zig b/src/host/fields.zig
index 2e70414..fc1a1e7 100644
--- a/src/host/fields.zig
+++ b/src/host/fields.zig
@@ -123,6 +123,25 @@ pub fn set(leaf: Leaf, memory: []u8, text: []const u8) !void {
}
}
+/// Sets every value of the row of scalars `path` names (an array of
+/// numbers, a vector, `grid[1]` of a `grid: [3][2]u16`), in memory order,
+/// from `text`'s words, one each; nothing when one is not a value.
+pub fn setRow(layout: Layout, memory: []u8, path: []const u8, text: []const u8) !void {
+ for (layout.fields) |leaf| {
+ if (leaf.whole() or !std.mem.endsWith(u8, leaf.path, "[]")) continue;
+ const at = abi.layout.match(leaf.path[0 .. leaf.path.len - 2], leaf.dims[0 .. leaf.dims.len - 1], path) orelse continue;
+ const d = leaf.dims[leaf.dims.len - 1];
+ var scratch: [16]u8 = undefined;
+ var words = std.mem.tokenizeAny(u8, text, " \t\n");
+ for (0..d.count) |_| try set(leaf.at(0), &scratch, words.next() orelse return error.TooFewValues);
+ if (words.next() != null) return error.TooManyValues;
+ words.reset();
+ for (0..d.count) |i| try set(leaf.at(leaf.offset + at + i * d.stride), memory, words.next().?);
+ return;
+ }
+ return error.UnsupportedField;
+}
+
/// True when the field's formatted value equals `text` (enum names compare
/// with or without the leading dot).
pub fn matches(leaf: Leaf, memory: []const u8, text: []const u8) bool {
@@ -206,6 +225,24 @@ test "fields format, parse and compare by kind" {
try std.testing.expectError(error.InvalidValue, set(layout.find("x").?, bytes, "banana"));
}
+test "a row of scalars is set from its words, all or none" {
+ const S = struct { grid: [3][2]i16 = @splat(@splat(0)), pos: @Vector(2, f32) = .{ 0, 0 } };
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const layout = try Layout.copy(arena.allocator(), abi.layout.of(S, .state));
+ var s: S = .{};
+ const bytes = std.mem.asBytes(&s);
+ try setRow(layout, bytes, "grid[1]", "4 -5");
+ try std.testing.expectEqual([2]i16{ 4, -5 }, s.grid[1]);
+ try setRow(layout, bytes, "pos", "1.5\n2");
+ try std.testing.expectEqual(@as(f32, 2), s.pos[1]);
+ try std.testing.expectError(error.TooFewValues, setRow(layout, bytes, "grid[2]", "7"));
+ try std.testing.expectError(error.TooManyValues, setRow(layout, bytes, "grid[2]", "7 8 9"));
+ try std.testing.expectError(error.InvalidValue, setRow(layout, bytes, "grid[2]", "7 x"));
+ try std.testing.expectEqual([2]i16{ 0, 0 }, s.grid[2]);
+ try std.testing.expectError(error.UnsupportedField, setRow(layout, bytes, "grid", "1 2"));
+}
+
test "indexed paths reach array elements and vector lanes" {
const S = struct { pos: @Vector(2, f32) = .{ 250, 3 }, boids: struct { buffer: [4]struct { pos: [2]f32 = .{ 0, 0 }, mood: enum(u8) { calm = 1, wild = 2 } = .calm } = @splat(.{}), len: u32 = 0 } = .{} };
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);