diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 07:57:28 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 610ffe373f0f5dd5ccb59bf575f5479ad1ecdbaf (patch) | |
| tree | a18106eee98a132c16af929c5262496f98305e7d /examples/live/src/main.zig | |
| parent | 1533644884c5cd2b072f78e3a42d5c4fa52f793a (diff) | |
| download | sketchbook-610ffe373f0f5dd5ccb59bf575f5479ad1ecdbaf.tar.gz sketchbook-610ffe373f0f5dd5ccb59bf575f5479ad1ecdbaf.zip | |
examples/live: hear this, the expression a node walk leaves the selection on
After Alt-o, Alt-i, Alt-n or Alt-p (helix's node walks, run in the
editor on the module's parse) the primary range is read back and the
text compiled again with it as a probe: Lang.compile takes the bytes of
a selection and compiles the smallest expression holding them into a
third tape, `Program.probe`. The compiler learns each expression's bytes
as it parses (`spanned`: a number, a name used, a call, parentheses, a
unary minus, each binary operation, a whole line), keeping the smallest
that holds the probe: no table of spans. A name used is the line that
defines it, so a walk onto `kick` hears kick.
State keeps the probe's tape (`probe`, empty when there is none). While
it is set, `draw` hears it as `out` (through the crossfade, as any
change) and sees it as the picture, grey unless it is a colour. Any
other key empties it, so Escape or an edit goes back to the program. It
is in State, so a replay probes where the session did.
Test: Lang.zig, a probe on an argument, a number, a cursor on one of its
digits, a name used, a line's name, a selection Alt-o grew to, and
between two lines (none); test/live.script, Alt-o twice from the 2 of
220 (`220`, then `tau * 220`), then Escape. Lang.compile takes the probe
as a second argument; the other calls pass null. zig build test: 85 of
85 steps, 8 of 8 tests.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/live/src/main.zig')
| -rw-r--r-- | examples/live/src/main.zig | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index d6bc8d6..cc11dbe 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -44,6 +44,10 @@ pub const State = struct { /// The last program that parsed. out: Lang.Tape = .{}, color: Lang.Tape = .{}, + /// Hear this: after a node walk (Alt-o, Alt-i, Alt-n, Alt-p), the + /// expression the primary range is on, heard and seen (grey) instead of + /// out and color until the next key; empty otherwise. + probe: Lang.Tape = .{}, /// What is wrong with the text, if it does not parse (`len` 0: nothing). problem: Lang.Problem = .{}, /// Seconds, from recorded ticks: a replay draws the same frames. @@ -127,11 +131,23 @@ pub fn update(s: *State, in: *const sk.Input) void { var fba: std.heap.FixedBufferAllocator = .init(&scratch); // ponytail: a key the text has no room for is dropped, as part 1's // buffer dropped it; the editor leaves the value as it was. - _ = s.ed.key(fba.allocator(), &syn, k); + const outcome = s.ed.key(fba.allocator(), &syn, k); + s.probe = .{}; + if (k.alt and outcome == .done and (k.cp == 'o' or k.cp == 'i' or k.cp == 'n' or k.cp == 'p')) probe: { + fba.reset(); + const ranges = fba.allocator().create([pe.max_selections]pe.Selection) catch break :probe; + const got = s.ed.ranges(fba.allocator(), ranges) catch break :probe; + const r = ranges[got.primary]; + var text: [Editor.capacity.text]u8 = undefined; + switch (Lang.compile(s.ed.read(&text), .{ @intCast(@min(r.anchor, r.head)), @intCast(@max(r.anchor, r.head)) })) { + .program => |p| s.probe = p.probe, + .problem => {}, + } + } } if (s.ed.revision == revision) return; var text: [Editor.capacity.text]u8 = undefined; - switch (Lang.compile(s.ed.read(&text))) { + switch (Lang.compile(s.ed.read(&text), null)) { .program => |p| { s.out = p.out; s.color = p.color; @@ -204,7 +220,8 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { } // A tape past 64 instructions is drawn at half resolution: at 1280x720 // 128 instructions took 2.7 ms of the GPU, at half 0.85 (the probe). - const half: u32 = if (s.color.len > 64) 2 else 1; + const seen = if (s.probe.len > 0) &s.probe else &s.color; + const half: u32 = if (seen.len > 64) 2 else 1; const size: [2]u32 = .{ width / half, height / half }; if (!std.meta.eql(ss.size, size)) { if (ss.picture != .none) f.host.freeImage(ss.picture); @@ -218,8 +235,8 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { .height = size[1], .pipelines = &.{picture_pipeline}, .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, Lang.Picture{ - .tape = std.mem.bytesToValue([Lang.max_tape][4]u32, &s.color.code), - .len = s.color.len, + .tape = std.mem.bytesToValue([Lang.max_tape][4]u32, &seen.code), + .len = seen.len, .time = s.time, .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, }) }}, @@ -255,10 +272,11 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { } const want: u32 = @min(in.lead -| queued, max_block); if (want > 0) { - const new = std.mem.sliceAsBytes(s.out.slice()); + const heard = if (s.probe.len > 0) &s.probe else &s.out; + const new = std.mem.sliceAsBytes(heard.slice()); const changed = in.jumped or !std.mem.eql(u8, new, std.mem.sliceAsBytes(ss.heard.slice())); const tapes = f.scratch.create([2]Lang.Tape.Code) catch return; - tapes.* = .{ s.out.code, ss.heard.code }; + tapes.* = .{ heard.code, ss.heard.code }; f.upload(ss.tapes, 0, std.mem.asBytes(tapes)); f.compute(.{ .label = "synth", @@ -266,7 +284,7 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { .readonly = &.{ss.tapes}, .readwrite = &.{ss.block}, .uniforms = gpu.uniform(f.scratch, Lang.Synth{ - .len = s.out.len, + .len = heard.len, .old_len = ss.heard.len, .count = want, .fade = if (changed and s.crossfade) want else 0, @@ -279,7 +297,7 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { f.audioBuffer(stream, ss.block, .{ .count = want }); f.download(ss.block, .{ .size = want * @sizeOf(f32), .key = stream }); ss.clock += @as(f64, @floatFromInt(want)) / rate; - ss.heard = s.out; + ss.heard = heard.*; } ss.scope.draw(f, .{ .oscilloscope = .{ .rect = .{ .x = 16, .y = height - 104, .w = 480, .h = 88 }, .samples = 1024 }, |
