From 1637cf5efa338fee7797ba83f264dcab5bde4800 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:50:47 -0300 Subject: examples/live: the interpreter of a tape, and its probe Demo 1's route: a program becomes a flat tape of at most 128 instructions (src/Lang.zig: the ops, the instruction, the tape and the two uniform blocks that carry it; build.zig writes Lang.Op out as ops.glsl, so the op numbers have one definition), and ONE interpreter, shaders/vm.glsl, runs it per pixel (picture.frag) and per sample (synth.comp, crossing from the tape it replaces over the first `fade` samples). Instruction i writes register i; the tape is pushed as uniforms. The probe measures it before anything else is built: `zig build probe` plays made-up tapes (every instruction reaching the last, a noise in ten) under --profile-gpu. RTX 3050 (driver 620.70.06), Debug host, means of 601 frames, three runs agreeing within 0.07 ms: picture, empty tape 720p 0.14-0.23 1080p 0.19-0.28 (a pass's cost) picture, 60 720p 1.38-1.45 1080p 2.89-2.96 picture, 128 720p 2.71-2.81 1080p 5.86-5.95 picture, 128 at 640x360 0.85-0.94 The budget, under 2 ms at 1280x720: 60 instructions meet it, 128 do not. The cheap fixes, on 128 at 720p: the tape in a storage buffer 2.97-3.03 (worse); the registers as an array of 16 (kept in registers, indexing turned into selects) 5.82-5.93 (twice as slow); the 128-array with only 16 slots used 2.11-2.34 (cache footprint: a register allocator would buy about a sixth); half resolution 0.85-0.94. So the sketch will draw a tape longer than 64 instructions at half resolution, scaled up. The variants are kept outside the tree. The synth's numbers taken here were of a kernel that wrote nothing (its uniform block was past SDL's 4 KiB): the sound's change measures it again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 43 +++++++++++ examples/live/build.zig.zon | 10 +++ examples/live/shaders/fullscreen.vert | 6 ++ examples/live/shaders/picture.frag | 13 ++++ examples/live/shaders/synth.comp | 26 +++++++ examples/live/shaders/vm.glsl | 92 ++++++++++++++++++++++++ examples/live/src/Lang.zig | 95 +++++++++++++++++++++++++ examples/live/src/probe.zig | 130 ++++++++++++++++++++++++++++++++++ examples/live/test/probe.script | 4 ++ 9 files changed, 419 insertions(+) create mode 100644 examples/live/build.zig create mode 100644 examples/live/build.zig.zon create mode 100644 examples/live/shaders/fullscreen.vert create mode 100644 examples/live/shaders/picture.frag create mode 100644 examples/live/shaders/synth.comp create mode 100644 examples/live/shaders/vm.glsl create mode 100644 examples/live/src/Lang.zig create mode 100644 examples/live/src/probe.zig create mode 100644 examples/live/test/probe.script diff --git a/examples/live/build.zig b/examples/live/build.zig new file mode 100644 index 0000000..cc93612 --- /dev/null +++ b/examples/live/build.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const sketchbook = @import("sketchbook"); +const Lang = @import("src/Lang.zig"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const sb = sketchbook.init(b, .{ .target = target, .optimize = optimize }); + + // The interpreter's op numbers, from Lang.Op: one definition. + const ops = b.addWriteFiles(); + _ = ops.add("ops.glsl", comptime blk: { + var glsl: []const u8 = "// Generated from Lang.Op by build.zig; do not edit.\n"; + for (std.enums.values(Lang.Op)) |op| glsl = glsl ++ std.fmt.comptimePrint("const uint OP_{t} = {d}u;\n", .{ op, @intFromEnum(op) }); + break :blk glsl; + }); + const lang = b.createModule(.{ .root_source_file = b.path("src/Lang.zig") }); + const shaders = sb.addShaders(.{ + .layouts = .{ .module = lang, .types = &.{ "Picture", "Synth" } }, + .include_dirs = &.{ops.getDirectory()}, + .sources = &.{ + b.path("shaders/fullscreen.vert"), + b.path("shaders/picture.frag"), + b.path("shaders/synth.comp"), + }, + }); + const imports: []const std.Build.Module.Import = &.{ + .{ .name = "Lang", .module = lang }, + .{ .name = "shaders", .module = shaders.module }, + }; + + // The interpreter's GPU time over made-up tapes (`--profile-gpu`). + const probe = sb.app(.{ .name = "live-probe", .root_source_file = b.path("src/probe.zig"), .imports = imports }); + const run_probe = b.addRunArtifact(probe.addExecutable()); + run_probe.addArgs(&.{ "--headless", "--fresh", "--profile-gpu", "--script" }); + run_probe.addFileArg(b.path("test/probe.script")); + run_probe.addArg("--state"); + _ = run_probe.addOutputFileArg("probe.xitdb"); + run_probe.setCwd(b.path(".")); + run_probe.stdio = .inherit; + run_probe.has_side_effects = true; + b.step("probe", "Time the interpreter on the GPU over made-up tapes").dependOn(&run_probe.step); +} diff --git a/examples/live/build.zig.zon b/examples/live/build.zig.zon new file mode 100644 index 0000000..27bd831 --- /dev/null +++ b/examples/live/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = .live, + .version = "0.0.1", + .dependencies = .{ + .sketchbook = .{ .path = "../.." }, + }, + .minimum_zig_version = "0.17.0", + .paths = .{""}, + .fingerprint = 0x530f2caf5c8f14ab, +} diff --git a/examples/live/shaders/fullscreen.vert b/examples/live/shaders/fullscreen.vert new file mode 100644 index 0000000..f45ee09 --- /dev/null +++ b/examples/live/shaders/fullscreen.vert @@ -0,0 +1,6 @@ +#version 450 +// One triangle covering the target. +void main() { + vec2 p = vec2(float((gl_VertexIndex << 1) & 2), float(gl_VertexIndex & 2)); + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/examples/live/shaders/picture.frag b/examples/live/shaders/picture.frag new file mode 100644 index 0000000..ec1293e --- /dev/null +++ b/examples/live/shaders/picture.frag @@ -0,0 +1,13 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The color tape at every pixel; x and y run 0..1, y up. +#include "Picture.glsl" +#define TAPE(i) u.tape[i] +#include "vm.glsl" + +layout(location = 0) out vec4 out_color; + +void main() { + vec2 p = vec2(gl_FragCoord.x, u.size.y - gl_FragCoord.y) / u.size; + out_color = vec4(clamp(vm(0u, u.len, u.time, p.x, p.y), 0.0, 1.0), 1.0); +} diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp new file mode 100644 index 0000000..9a066a6 --- /dev/null +++ b/examples/live/shaders/synth.comp @@ -0,0 +1,26 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// One audio block, one thread per sample: the out tape at each sample's +// time, crossing over the first `fade` samples from the tape it replaces, +// so an edit does not click. x and y are 0. +layout(local_size_x = 64) in; + +#include "Synth.glsl" +#define TAPE(i) u.tape[i] +#include "vm.glsl" + +layout(set = 1, binding = 0) buffer Block { float block[]; }; + +// A sample the device can take: no NaN or infinity, -1..1. +float heard(float v) { + return isnan(v) || isinf(v) ? 0.0 : clamp(v, -1.0, 1.0); +} + +void main() { + uint n = gl_GlobalInvocationID.x; + if (n >= u.count) return; + float t = u.start + float(n) / u.rate; + float v = heard(vm(0u, u.len, t, 0.0, 0.0).x); + if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, t, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); + block[n] = v; +} diff --git a/examples/live/shaders/vm.glsl b/examples/live/shaders/vm.glsl new file mode 100644 index 0000000..185d23a --- /dev/null +++ b/examples/live/shaders/vm.glsl @@ -0,0 +1,92 @@ +// The interpreter of Lang.zig's tapes, included by picture.frag (a pixel) +// and synth.comp (a sample), which define TAPE(i), instruction i as a uvec4 +// (op, a, b, c). Instruction i writes register r[i] from earlier registers +// a, b and c; a constant's a is its value's bits, seq's b and c a first +// register and a count. Every thread runs the same instructions, so the +// switch never diverges. The registers are a local array (in the GPU's +// local memory): an array of 16 kept in registers measured twice as slow, +// its indexing turned into selects (the probe, in the package's README). +#include "ops.glsl" + +const uint MAX_TAPE = 128u; + +// An integer hash (no tables), to -1..1. +float lattice(vec3 p) { + uvec3 q = uvec3(ivec3(p)); + uint h = (q.x * 0x8da6b343u) ^ (q.y * 0xd8163841u) ^ (q.z * 0xcb1ab31fu); + h ^= h >> 16; + h *= 0x7feb352du; + h ^= h >> 15; + h *= 0x846ca68bu; + h ^= h >> 16; + return float(h) * (2.0 / 4294967295.0) - 1.0; +} + +// Value noise, smooth, -1..1. +float noise(vec3 p) { + vec3 i = floor(p); + vec3 f = p - i; + vec3 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0); + return mix( + mix(mix(lattice(i), lattice(i + vec3(1, 0, 0)), w.x), mix(lattice(i + vec3(0, 1, 0)), lattice(i + vec3(1, 1, 0)), w.x), w.y), + mix(mix(lattice(i + vec3(0, 0, 1)), lattice(i + vec3(1, 0, 1)), w.x), mix(lattice(i + vec3(0, 1, 1)), lattice(i + vec3(1, 1, 1)), w.x), w.y), + w.z); +} + +vec3 hsv(float h, float s, float v) { + vec3 k = clamp(abs(fract(h + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0) - 1.0, 0.0, 1.0); + return v * mix(vec3(1.0), k, s); +} + +// The tape at TAPE(base) .. TAPE(base + len - 1) at one point: a colour if +// it ends in one, else its last register, grey. +vec3 vm(uint base, uint len, float t, float x, float y) { + float r[MAX_TAPE]; + vec3 color = vec3(0.0); + for (uint i = 0u; i < len; i++) { + uvec4 k = TAPE(base + i); + float a = r[k.y & 127u]; + float b = r[k.z & 127u]; + float c = r[k.w & 127u]; + float v = 0.0; + switch (k.x) { + case OP_constant: v = uintBitsToFloat(k.y); break; + case OP_t: v = t; break; + case OP_x: v = x; break; + case OP_y: v = y; break; + case OP_add: v = a + b; break; + case OP_sub: v = a - b; break; + case OP_mul: v = a * b; break; + case OP_div: v = a / b; break; + case OP_neg: v = -a; break; + case OP_copy: v = a; break; + case OP_seq: v = r[(k.z + uint(mod(floor(a), float(k.w)))) & 127u]; break; + case OP_sin: v = sin(a); break; + case OP_cos: v = cos(a); break; + case OP_exp: v = exp(a); break; + case OP_log: v = log(a); break; + case OP_pow: v = pow(a, b); break; + case OP_sqrt: v = sqrt(a); break; + case OP_abs: v = abs(a); break; + case OP_floor: v = floor(a); break; + case OP_fract: v = fract(a); break; + case OP_min: v = min(a, b); break; + case OP_max: v = max(a, b); break; + case OP_clamp: v = clamp(a, b, c); break; + case OP_mix: v = mix(a, b, c); break; + case OP_step: v = step(a, b); break; + case OP_smoothstep: v = smoothstep(a, b, c); break; + case OP_atan2: v = atan(a, b); break; + case OP_noise: v = noise(vec3(a, b, c)); break; + case OP_saw: v = 2.0 * fract(a + 0.5) - 1.0; break; + case OP_sqr: v = fract(a) < 0.5 ? 1.0 : -1.0; break; + case OP_tri: v = 4.0 * abs(fract(a - 0.25) - 0.5) - 1.0; break; + case OP_hsv: color = hsv(a, b, c); break; + case OP_rgb: color = vec3(a, b, c); break; + } + r[i] = v; + } + if (len == 0u) return vec3(0.0); + uint last = TAPE(base + len - 1u).x; + return last == OP_hsv || last == OP_rgb ? color : vec3(r[len - 1u]); +} diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig new file mode 100644 index 0000000..644ff61 --- /dev/null +++ b/examples/live/src/Lang.zig @@ -0,0 +1,95 @@ +//! The language: lines of `name = expression` over `t`, `x` and `y`, +//! compiled into two tapes, `out` (heard) and `color` (seen), that one +//! interpreter (shaders/vm.glsl) runs per sample and per pixel. +//! +//! A tape is at most `max_tape` instructions; instruction i writes register +//! i from earlier ones. Its GPU form is the uniform blocks at the end of +//! this file, and build.zig writes `Op` out as ops.glsl for the shaders: +//! this file is the one definition of both. +const std = @import("std"); + +pub const max_tape = 128; + +/// By how many registers an instruction reads: none, one, two, three. +pub const Op = enum(u32) { + /// `a` is the value's bits. + constant, + t, + x, + y, + neg, + sin, + cos, + exp, + log, + sqrt, + abs, + floor, + fract, + saw, + sqr, + tri, + /// Register `a`, so that seq's values sit side by side. + copy, + /// Register `b + floor(a) mod c`: `b` and `c` are numbers, not + /// registers it reads, and registers b.. hold copies. + seq, + add, + sub, + mul, + div, + pow, + min, + max, + step, + atan2, + clamp, + mix, + smoothstep, + noise, + /// A colour, only as the color tape's last instruction. + hsv, + rgb, +}; + +pub const Instruction = extern struct { + op: Op = .constant, + a: u32 = 0, + b: u32 = 0, + c: u32 = 0, +}; + +pub const Tape = struct { + len: u32 = 0, + code: [max_tape]Instruction = @splat(.{}), +}; + +/// picture.frag's uniforms (SDL_GPU's fragment set). +pub const Picture = extern struct { + tape: [max_tape][4]u32, + len: u32, + /// Seconds. + time: f32, + /// The target, pixels. + size: [2]f32, + + pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } }; +}; + +/// synth.comp's uniforms (SDL_GPU's compute set): the tape, and the one +/// it replaces, which the first `fade` samples cross from. +pub const Synth = extern struct { + /// The new tape, then the old one. + tape: [2 * max_tape][4]u32, + len: u32, + old_len: u32, + /// Samples in the block. + count: u32, + fade: u32, + /// The block's first sample, seconds. + start: f32, + rate: f32, + pad: [2]f32 = .{ 0, 0 }, + + pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; +}; diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig new file mode 100644 index 0000000..663bed3 --- /dev/null +++ b/examples/live/src/probe.zig @@ -0,0 +1,130 @@ +//! The probe: the interpreter alone over made-up tapes of 60 and 128 +//! instructions, each pass at 1280x720 and 1920x1080 (and 128 at half of +//! 1280x720, as the sketch draws a long tape), and the synth kernel over +//! blocks of 256 and 1024 samples. `zig build probe` plays +//! test/probe.script with `--profile-gpu`, which prints each label's time +//! at exit; the empty tape's is the cost of a pass doing nothing. +const std = @import("std"); +const sk = @import("sketch"); +const gpu = sk.abi.gpu; +const Lang = @import("Lang"); +const shaders = @import("shaders"); + +pub const name = "live-probe"; +pub const width = 640; +pub const height = 360; + +pub const Session = struct { + images: [3]sk.Image = .{ .none, .none, .none }, + block: sk.Buffer = .none, + time: f32 = 0, +}; + +const sizes = [3][2]u32{ .{ 1280, 720 }, .{ 1920, 1080 }, .{ 640, 360 } }; + +const Picture = struct { label: []const u8, len: u32, size: u2 }; +const pictures = [_]Picture{ + .{ .label = "picture 0 720p", .len = 0, .size = 0 }, + .{ .label = "picture 60 720p", .len = 60, .size = 0 }, + .{ .label = "picture 128 720p", .len = 128, .size = 0 }, + .{ .label = "picture 0 1080p", .len = 0, .size = 1 }, + .{ .label = "picture 60 1080p", .len = 60, .size = 1 }, + .{ .label = "picture 128 1080p", .len = 128, .size = 1 }, + .{ .label = "picture 128 360p", .len = 128, .size = 2 }, +}; + +const Block = struct { label: []const u8, len: u32, count: u32, fade: bool = false }; +const blocks = [_]Block{ + .{ .label = "synth 60 x256", .len = 60, .count = 256 }, + .{ .label = "synth 128 x256", .len = 128, .count = 256 }, + .{ .label = "synth 60 x1024", .len = 60, .count = 1024 }, + .{ .label = "synth 128 x1024", .len = 128, .count = 1024 }, + .{ .label = "synth 128 x1024 fading", .len = 128, .count = 1024, .fade = true }, +}; + +/// `len` instructions, every one reaching the last: x, y, t and a +/// constant, then a cycle of ops over the three before, a noise every +/// tenth, and hsv. +fn madeUp(comptime len: u32) [Lang.max_tape][4]u32 { + @setEvalBranchQuota(10_000); + var code: [Lang.max_tape][4]u32 = @splat(.{ 0, 0, 0, 0 }); + const cycle = [_]Lang.Op{ .mul, .add, .sin, .fract, .sub, .smoothstep, .abs, .mix, .cos }; + for (0..len) |i| { + const op: Lang.Op = switch (i) { + 0 => .x, + 1 => .y, + 2 => .t, + 3 => .constant, + else => if (i == len - 1) .hsv else if (i % 10 == 9) .noise else cycle[i % cycle.len], + }; + const a: u32 = if (op == .constant) @bitCast(@as(f32, 6)) else @intCast(i -| 1); + code[i] = .{ @intFromEnum(op), a, @intCast(i -| 2), @intCast(i -| 3) }; + } + return code; +} + +const tapes = .{ madeUp(0), madeUp(60), madeUp(128) }; + +fn tape(len: u32) [Lang.max_tape][4]u32 { + return switch (len) { + 0 => tapes[0], + 60 => tapes[1], + else => tapes[2], + }; +} + +const pipeline: gpu.Pipeline = .{ + .vertex = .{ .code = shaders.fullscreen_vert }, + .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 }, + .stride = 0, + .attributes = &.{}, + .blend = .none, +}; +const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readwrite_buffers = 1, .uniform_buffers = 1 }; + +pub fn draw(ss: *Session, f: *sk.Frame) void { + f.clear(0x000000); + if (!f.host.gpu()) return f.text(8, 8, 2, "no GPU here", 0xffffff); + if (ss.block == .none) { + for (&ss.images, sizes) |*image, size| image.* = f.host.newImage(size[0], size[1]); + ss.block = f.host.newBuffer(1024 * @sizeOf(f32)); + } + ss.time += @floatCast(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s); + for (pictures) |p| { + const size = sizes[p.size]; + f.gpu(.{ + .label = p.label, + .image = ss.images[p.size], + .width = size[0], + .height = size[1], + .pipelines = &.{pipeline}, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, Lang.Picture{ + .tape = tape(p.len), + .len = p.len, + .time = ss.time, + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + }) }}, + }); + } + for (blocks) |k| { + var both: [2 * Lang.max_tape][4]u32 = undefined; + both[0..Lang.max_tape].* = tape(k.len); + both[Lang.max_tape..].* = tape(k.len); + f.compute(.{ + .label = k.label, + .kernel = kernel, + .readwrite = &.{ss.block}, + .uniforms = gpu.uniform(f.scratch, Lang.Synth{ + .tape = both, + .len = k.len, + .old_len = k.len, + .count = k.count, + .fade = if (k.fade) k.count else 0, + .start = ss.time, + .rate = 48000, + }), + .groups = .{ (k.count + 63) / 64, 1, 1 }, + }); + } + f.image(ss.images[0], .{ .x = 0, .y = 0, .w = f.in.width, .h = f.in.height }, .{}); +} diff --git a/examples/live/test/probe.script b/examples/live/test/probe.script new file mode 100644 index 0000000..fc2e162 --- /dev/null +++ b/examples/live/test/probe.script @@ -0,0 +1,4 @@ +# The probe (src/probe.zig): frames enough for steady means, and what the +# 60-instruction tape draws, to see that the interpreter runs. +frames 600 +screenshot .zig-cache/live/probe.png -- cgit v1.3 From e7679f416daeee551e81a592c7fe06b8d8f7b705 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:00:47 -0300 Subject: examples/live: the language, text in, a tape and a problem out Lang.compile reads lines of `name = expression` over t, x and y (+ - * /, unary minus, parentheses, numbers, # comments) and returns either a Program, the out and color tapes, or a Problem: a byte of the text and words. The builtins are one table, `builtins` (call, op, arity, value, doc): the survey's sin, cos, exp, log, pow, sqrt, abs, floor, fract, min, max, clamp, mix, step, smoothstep, atan2, noise, saw, sqr, tri, seq, hsv, rgb, tau and pi, and the inputs t, x, y. A name is defined once, before it is used; builtins cannot be renamed; hsv and rgb are colours, so only the whole color line can be one. Each tape is emitted from its line's root, depth first, each term once, so it holds only what the line reaches; t, x and y are a term each however often named, and a negative number is a number. seq's values go in registers side by side: as emitted when they are fresh numbers, else copied. noise's missing arguments are 0. Nothing is allocated: the compiler is a struct on the stack, 512 terms, 128 names, 32 levels of nesting, 17 arguments (seq's 16 values), 128 instructions a tape, about 28 KiB in all; past any of them is a problem in words ("the out line needs more than 128 instructions", at the line). vm.glsl's smoothstep is written out: GLSL leaves lo > hi undefined, and the survey's ring is smoothstep(0.03, 0, ...). Tests: a tone instruction by instruction; the survey's 20-line program (out 54 instructions, color 46, neither holding the other's terms, every operand an earlier register); every builtin; precedence, comments and numbers; 24 problems, each with its byte and words; each capacity, met and passed. zig build test: 6 of 6. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 5 +- examples/live/shaders/vm.glsl | 3 +- examples/live/src/Lang.zig | 617 +++++++++++++++++++++++++++++++++++++++++- examples/live/src/probe.zig | 2 +- 4 files changed, 617 insertions(+), 10 deletions(-) diff --git a/examples/live/build.zig b/examples/live/build.zig index cc93612..2ad85fb 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void { const ops = b.addWriteFiles(); _ = ops.add("ops.glsl", comptime blk: { var glsl: []const u8 = "// Generated from Lang.Op by build.zig; do not edit.\n"; - for (std.enums.values(Lang.Op)) |op| glsl = glsl ++ std.fmt.comptimePrint("const uint OP_{t} = {d}u;\n", .{ op, @intFromEnum(op) }); + for (std.enums.values(Lang.Op)) |op| glsl = glsl ++ std.fmt.comptimePrint("const uint OP_{t} = {d}u;\n", .{ op, @backingInt(op) }); break :blk glsl; }); const lang = b.createModule(.{ .root_source_file = b.path("src/Lang.zig") }); @@ -29,6 +29,9 @@ pub fn build(b: *std.Build) void { .{ .name = "shaders", .module = shaders.module }, }; + const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); + b.step("test", "Run the language's tests").dependOn(&b.addRunArtifact(lang_test).step); + // The interpreter's GPU time over made-up tapes (`--profile-gpu`). const probe = sb.app(.{ .name = "live-probe", .root_source_file = b.path("src/probe.zig"), .imports = imports }); const run_probe = b.addRunArtifact(probe.addExecutable()); diff --git a/examples/live/shaders/vm.glsl b/examples/live/shaders/vm.glsl index 185d23a..06319ec 100644 --- a/examples/live/shaders/vm.glsl +++ b/examples/live/shaders/vm.glsl @@ -75,7 +75,8 @@ vec3 vm(uint base, uint len, float t, float x, float y) { case OP_clamp: v = clamp(a, b, c); break; case OP_mix: v = mix(a, b, c); break; case OP_step: v = step(a, b); break; - case OP_smoothstep: v = smoothstep(a, b, c); break; + // GLSL leaves smoothstep undefined for lo > hi; this one is not. + case OP_smoothstep: v = clamp((c - a) / (b - a), 0.0, 1.0); v = v * v * (3.0 - 2.0 * v); break; case OP_atan2: v = atan(a, b); break; case OP_noise: v = noise(vec3(a, b, c)); break; case OP_saw: v = 2.0 * fract(a + 0.5) - 1.0; break; diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 644ff61..a6db8b9 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -1,14 +1,35 @@ //! The language: lines of `name = expression` over `t`, `x` and `y`, -//! compiled into two tapes, `out` (heard) and `color` (seen), that one -//! interpreter (shaders/vm.glsl) runs per sample and per pixel. +//! compiled into two tapes, `out` (heard, -1..1) and `color` (seen), that +//! one interpreter (shaders/vm.glsl) runs per sample and per pixel. Names +//! are shared by both lines; a name is defined once, before it is used, so +//! a program is a graph without cycles and every program ends. //! -//! A tape is at most `max_tape` instructions; instruction i writes register -//! i from earlier ones. Its GPU form is the uniform blocks at the end of -//! this file, and build.zig writes `Op` out as ops.glsl for the shaders: -//! this file is the one definition of both. +//! beat = t * 2 +//! kick = exp(-7 * fract(beat)) +//! out = sin(tau * 48 * t) * kick # heard +//! color = hsv(x, 0.5, y * kick) # seen +//! +//! `+ - * /`, unary `-` and parentheses; numbers like 0.5, 120, 1e-3; +//! `#` to the end of the line is a comment; the builtins are `builtins`. +//! +//! A tape is at most `max_tape` instructions, holding only what its line +//! reaches; instruction i writes register i from earlier ones. Its GPU +//! form is the uniform blocks at the end of this file, and build.zig +//! writes `Op` out as ops.glsl for the shaders: this file is the one +//! definition of both. `compile` allocates nothing: its work is a few +//! fixed arrays on the stack (`max_terms`, `max_names`, `max_depth`), and a +//! program past any of them is a problem like any other, in words. const std = @import("std"); pub const max_tape = 128; +/// Numbers, names used and operations in the whole text. +pub const max_terms = 512; +/// Lines that define a name. +pub const max_names = 128; +/// Parentheses and unary minuses inside each other. +pub const max_depth = 32; +/// seq's index and values. +pub const max_args = 17; /// By how many registers an instruction reads: none, one, two, three. pub const Op = enum(u32) { @@ -32,7 +53,7 @@ pub const Op = enum(u32) { /// Register `a`, so that seq's values sit side by side. copy, /// Register `b + floor(a) mod c`: `b` and `c` are numbers, not - /// registers it reads, and registers b.. hold copies. + /// registers it reads, and registers b.. hold the values. seq, add, sub, @@ -52,6 +73,59 @@ pub const Op = enum(u32) { rgb, }; +pub const Builtin = struct { + /// As it is called, for the manual. + call: []const u8, + op: Op, + /// Arguments; 0 for an input or a constant, used without parentheses. + min: u8 = 0, + max: u8 = 0, + value: f32 = 0, + doc: []const u8, + + pub fn name(b: Builtin) []const u8 { + return b.call[0 .. std.mem.indexOfScalar(u8, b.call, '(') orelse b.call.len]; + } +}; + +/// The builtins, the one table: a name is added here and in vm.glsl's +/// switch, and only for a program that needs it. +pub const builtins = [_]Builtin{ + .{ .call = "t", .op = .t, .doc = "seconds" }, + .{ .call = "x", .op = .x, .doc = "0 at the left to 1 at the right; 0 in sound" }, + .{ .call = "y", .op = .y, .doc = "0 at the bottom to 1 at the top; 0 in sound" }, + .{ .call = "tau", .op = .constant, .value = std.math.tau, .doc = "a turn in radians, 6.283..." }, + .{ .call = "pi", .op = .constant, .value = std.math.pi, .doc = "half a turn, 3.141..." }, + .{ .call = "sin(a)", .op = .sin, .min = 1, .max = 1, .doc = "sine, of radians" }, + .{ .call = "cos(a)", .op = .cos, .min = 1, .max = 1, .doc = "cosine, of radians" }, + .{ .call = "exp(a)", .op = .exp, .min = 1, .max = 1, .doc = "e to the a" }, + .{ .call = "log(a)", .op = .log, .min = 1, .max = 1, .doc = "natural logarithm" }, + .{ .call = "pow(a, b)", .op = .pow, .min = 2, .max = 2, .doc = "a to the b (a not negative)" }, + .{ .call = "sqrt(a)", .op = .sqrt, .min = 1, .max = 1, .doc = "square root" }, + .{ .call = "abs(a)", .op = .abs, .min = 1, .max = 1, .doc = "a without its sign" }, + .{ .call = "floor(a)", .op = .floor, .min = 1, .max = 1, .doc = "the whole number at or below a" }, + .{ .call = "fract(a)", .op = .fract, .min = 1, .max = 1, .doc = "a - floor(a): 0..1, again every 1" }, + .{ .call = "min(a, b)", .op = .min, .min = 2, .max = 2, .doc = "the smaller" }, + .{ .call = "max(a, b)", .op = .max, .min = 2, .max = 2, .doc = "the larger" }, + .{ .call = "clamp(v, lo, hi)", .op = .clamp, .min = 3, .max = 3, .doc = "v kept between lo and hi" }, + .{ .call = "mix(a, b, w)", .op = .mix, .min = 3, .max = 3, .doc = "a + (b - a) * w" }, + .{ .call = "step(edge, v)", .op = .step, .min = 2, .max = 2, .doc = "0 below edge, 1 from it on" }, + .{ .call = "smoothstep(lo, hi, v)", .op = .smoothstep, .min = 3, .max = 3, .doc = "0 to 1, smoothly, as v goes from lo to hi (either way round)" }, + .{ .call = "atan2(y, x)", .op = .atan2, .min = 2, .max = 2, .doc = "the angle of the point (x, y), radians" }, + .{ .call = "noise(x, y, z)", .op = .noise, .min = 1, .max = 3, .doc = "smooth noise, -1..1, a new value about every 1; y and z are 0 when left out" }, + .{ .call = "saw(p)", .op = .saw, .min = 1, .max = 1, .doc = "a sawtooth, -1..1, of period 1 (cycles, not radians), rising through 0 at 0" }, + .{ .call = "sqr(p)", .op = .sqr, .min = 1, .max = 1, .doc = "a square wave, 1 then -1, of period 1" }, + .{ .call = "tri(p)", .op = .tri, .min = 1, .max = 1, .doc = "a triangle wave, -1..1, of period 1, rising through 0 at 0" }, + .{ .call = "seq(i, v0, v1, ...)", .op = .seq, .min = 2, .max = max_args, .doc = "the value at floor(i), round the list: a step pattern (16 values at most)" }, + .{ .call = "hsv(h, s, v)", .op = .hsv, .min = 3, .max = 3, .doc = "a colour: hue round the wheel 0..1, saturation, value; only as the whole color line" }, + .{ .call = "rgb(r, g, b)", .op = .rgb, .min = 3, .max = 3, .doc = "a colour, red, green and blue 0..1; only as the whole color line" }, +}; + +fn builtin(name: []const u8) ?Builtin { + for (builtins) |b| if (std.mem.eql(u8, b.name(), name)) return b; + return null; +} + pub const Instruction = extern struct { op: Op = .constant, a: u32 = 0, @@ -62,6 +136,328 @@ pub const Instruction = extern struct { pub const Tape = struct { len: u32 = 0, code: [max_tape]Instruction = @splat(.{}), + + pub fn slice(tape: *const Tape) []const Instruction { + return tape.code[0..@min(tape.len, max_tape)]; + } +}; + +/// What is wrong with a program, and where: a byte of its text. +pub const Problem = struct { + at: u32 = 0, + len: u32 = 0, + words: [96]u8 = @splat(0), + + pub fn text(p: *const Problem) []const u8 { + return p.words[0..@min(p.len, p.words.len)]; + } +}; + +pub const Program = struct { out: Tape = .{}, color: Tape = .{} }; + +pub const Result = union(enum) { program: Program, problem: Problem }; + +pub fn compile(text: []const u8) Result { + var c: Compiler = .{ .text = text }; + var program: Program = .{}; + c.lines() catch return .{ .problem = c.problem }; + c.tape(&program.out, "out") catch return .{ .problem = c.problem }; + c.tape(&program.color, "color") catch return .{ .problem = c.problem }; + return .{ .program = program }; +} + +const none = std.math.maxInt(u16); + +const Term = struct { + op: Op, + value: f32 = 0, + /// Where it was written. + at: u32, + len: u8 = 0, + args: [max_args]u16 = undefined, +}; + +const Name = struct { text: []const u8, term: u16, at: u32 }; + +const Token = struct { + kind: enum { name, number, symbol, newline, end }, + at: u32, + end: u32, + symbol: u8 = 0, + + fn is(tok: Token, symbol: u8) bool { + return tok.kind == .symbol and tok.symbol == symbol; + } +}; + +const Compiler = struct { + text: []const u8, + pos: u32 = 0, + terms: [max_terms]Term = undefined, + term_count: u16 = 0, + names: [max_names]Name = undefined, + name_count: u16 = 0, + /// The term of t, x and y, made once. + inputs: [3]u16 = .{ none, none, none }, + depth: u8 = 0, + /// The line's hsv or rgb, if it called one. + colour: u16 = none, + problem: Problem = .{}, + + fn fail(c: *Compiler, at: u32, comptime fmt: []const u8, args: anytype) error{Problem} { + c.problem = .{ .at = at }; + var w: std.Io.Writer = .fixed(&c.problem.words); + w.print(fmt, args) catch {}; + c.problem.len = @intCast(w.end); + return error.Problem; + } + + fn span(c: *const Compiler, tok: Token) []const u8 { + return c.text[tok.at..tok.end]; + } + + fn next(c: *Compiler) Token { + const s = c.text; + var i = c.pos; + while (i < s.len and (s[i] == ' ' or s[i] == '\t' or s[i] == '\r')) i += 1; + if (i < s.len and s[i] == '#') while (i < s.len and s[i] != '\n') : (i += 1) {}; + const at = i; + defer c.pos = i; + if (i == s.len) return .{ .kind = .end, .at = at, .end = at }; + switch (s[i]) { + '\n' => { + i += 1; + return .{ .kind = .newline, .at = at, .end = i }; + }, + 'a'...'z', 'A'...'Z', '_' => { + while (i < s.len and (std.ascii.isAlphanumeric(s[i]) or s[i] == '_')) i += 1; + return .{ .kind = .name, .at = at, .end = i }; + }, + '0'...'9', '.' => { + while (i < s.len and (std.ascii.isDigit(s[i]) or s[i] == '.' or s[i] == 'e' or + ((s[i] == '-' or s[i] == '+') and s[i - 1] == 'e'))) i += 1; + return .{ .kind = .number, .at = at, .end = i }; + }, + else => { + i += 1; + return .{ .kind = .symbol, .at = at, .end = i, .symbol = s[at] }; + }, + } + } + + fn peek(c: *Compiler) Token { + const pos = c.pos; + defer c.pos = pos; + return c.next(); + } + + fn describe(c: *const Compiler, tok: Token) []const u8 { + return switch (tok.kind) { + .newline, .end => "the end of the line", + else => c.span(tok), + }; + } + + fn term(c: *Compiler, t: Term) !u16 { + if (c.term_count == max_terms) return c.fail(t.at, "the program is longer than {d} terms", .{max_terms}); + c.terms[c.term_count] = t; + c.term_count += 1; + return c.term_count - 1; + } + + fn call(c: *Compiler, op: Op, at: u32, args: []const u16) !u16 { + var t: Term = .{ .op = op, .at = at, .len = @intCast(args.len) }; + @memcpy(t.args[0..args.len], args); + return c.term(t); + } + + fn lookup(c: *const Compiler, name: []const u8) ?Name { + for (c.names[0..c.name_count]) |n| if (std.mem.eql(u8, n.text, name)) return n; + return null; + } + + fn lines(c: *Compiler) !void { + while (true) { + const first = c.next(); + switch (first.kind) { + .end => return, + .newline => continue, + .name => {}, + else => return c.fail(first.at, "a line is a name, =, and an expression", .{}), + } + const name = c.span(first); + const eq = c.next(); + if (!eq.is('=')) return c.fail(eq.at, "expected = after {s}, found {s}", .{ name, c.describe(eq) }); + if (builtin(name) != null) return c.fail(first.at, "{s} is a builtin: call yours something else", .{name}); + if (c.lookup(name) != null) return c.fail(first.at, "{s} is defined twice: a name is given once", .{name}); + c.colour = none; + const root = try c.expr(); + const after = c.next(); + if (after.kind != .newline and after.kind != .end) return c.fail(after.at, "expected the end of the line, found {s}", .{c.describe(after)}); + if (c.colour != none and (c.colour != root or !std.mem.eql(u8, name, "color"))) + return c.fail(c.terms[c.colour].at, "a colour (hsv, rgb) can only be the whole color line", .{}); + if (c.name_count == max_names) return c.fail(first.at, "more than {d} names", .{max_names}); + c.names[c.name_count] = .{ .text = name, .term = root, .at = first.at }; + c.name_count += 1; + } + } + + fn enter(c: *Compiler, at: u32) !void { + if (c.depth == max_depth) return c.fail(at, "nested more than {d} deep", .{max_depth}); + c.depth += 1; + } + + fn expr(c: *Compiler) error{Problem}!u16 { + try c.enter(c.peek().at); + defer c.depth -= 1; + var left = try c.product(); + while (true) { + const tok = c.peek(); + const op: Op = if (tok.is('+')) .add else if (tok.is('-')) .sub else return left; + _ = c.next(); + left = try c.call(op, tok.at, &.{ left, try c.product() }); + } + } + + fn product(c: *Compiler) !u16 { + var left = try c.unary(); + while (true) { + const tok = c.peek(); + const op: Op = if (tok.is('*')) .mul else if (tok.is('/')) .div else return left; + _ = c.next(); + left = try c.call(op, tok.at, &.{ left, try c.unary() }); + } + } + + fn unary(c: *Compiler) error{Problem}!u16 { + const tok = c.peek(); + if (!tok.is('-')) return c.primary(); + _ = c.next(); + try c.enter(tok.at); + defer c.depth -= 1; + const a = try c.unary(); + // A negative number is a number (each literal is a term of its own). + if (c.terms[a].op == .constant) { + c.terms[a].value = -c.terms[a].value; + return a; + } + return c.call(.neg, tok.at, &.{a}); + } + + fn primary(c: *Compiler) !u16 { + const tok = c.next(); + switch (tok.kind) { + .number => { + const value = std.fmt.parseFloat(f32, c.span(tok)) catch return c.fail(tok.at, "{s} is not a number", .{c.span(tok)}); + return c.term(.{ .op = .constant, .value = value, .at = tok.at }); + }, + .name => { + const name = c.span(tok); + if (c.peek().is('(')) return c.callOf(tok); + if (c.lookup(name)) |n| { + if (c.terms[n.term].op == .hsv or c.terms[n.term].op == .rgb) return c.fail(tok.at, "{s} is a colour: only the color line can be one", .{name}); + return n.term; + } + const b = builtin(name) orelse return c.fail(tok.at, "{s} is not defined above", .{name}); + if (b.max > 0) return c.fail(tok.at, "{s} takes arguments: {s}", .{ name, b.call }); + const input: ?usize = switch (b.op) { + .t => 0, + .x => 1, + .y => 2, + else => null, + }; + const i = input orelse return c.term(.{ .op = b.op, .value = b.value, .at = tok.at }); + if (c.inputs[i] == none) c.inputs[i] = try c.term(.{ .op = b.op, .at = tok.at }); + return c.inputs[i]; + }, + .newline, .end => return c.fail(tok.at, "expected an expression before the end of the line", .{}), + .symbol => { + if (!tok.is('(')) { + if (c.text[tok.at] < 0x20 or c.text[tok.at] > 0x7e) return c.fail(tok.at, "byte 0x{x:0>2} is not part of the language", .{c.text[tok.at]}); + return c.fail(tok.at, "expected an expression, found {s}", .{c.span(tok)}); + } + const inner = try c.expr(); + const close = c.next(); + if (!close.is(')')) return c.fail(close.at, "expected ) to close the ( at byte {d}, found {s}", .{ tok.at, c.describe(close) }); + return inner; + }, + } + } + + /// `name(args)`, `name` being `tok`. + fn callOf(c: *Compiler, tok: Token) !u16 { + const name = c.span(tok); + _ = c.next(); + const b = builtin(name) orelse { + if (c.lookup(name) != null) return c.fail(tok.at, "{s} is a name, not a builtin: it takes no ( )", .{name}); + return c.fail(tok.at, "{s} is not a builtin", .{name}); + }; + if (b.max == 0) return c.fail(tok.at, "{s} takes no arguments", .{name}); + var args: [max_args]u16 = undefined; + var n: u8 = 0; + if (c.peek().is(')')) { + _ = c.next(); + } else while (true) { + const at = c.peek().at; + if (n == b.max) return c.fail(at, "{s} takes at most {d} arguments: {s}", .{ name, b.max, b.call }); + args[n] = try c.expr(); + n += 1; + const sep = c.next(); + if (sep.is(')')) break; + if (!sep.is(',')) return c.fail(sep.at, "expected , or ) in {s}(...), found {s}", .{ name, c.describe(sep) }); + } + if (n < b.min) return c.fail(tok.at, "{s} takes {d} arguments, not {d}: {s}", .{ name, b.min, n, b.call }); + if (b.op == .noise) while (n < 3) : (n += 1) { + args[n] = try c.term(.{ .op = .constant, .at = tok.at }); + }; + const t = try c.call(b.op, tok.at, args[0..n]); + if (b.op == .hsv or b.op == .rgb) c.colour = t; + return t; + } + + /// The tape of the line named `name` (empty without one). + fn tape(c: *Compiler, into: *Tape, name: []const u8) !void { + into.* = .{}; + const line = c.lookup(name) orelse return; + var registers: [max_terms]u16 = @splat(none); + _ = c.emit(into, line.term, ®isters) catch + return c.fail(line.at, "the {s} line needs more than {d} instructions", .{ name, max_tape }); + } + + fn push(into: *Tape, i: Instruction) !u32 { + if (into.len == max_tape) return error.Full; + into.code[into.len] = i; + into.len += 1; + return into.len - 1; + } + + fn emit(c: *const Compiler, into: *Tape, at: u16, registers: *[max_terms]u16) error{Full}!u32 { + if (registers[at] != none) return registers[at]; + const t = c.terms[at]; + var r: [max_args]u32 = @splat(0); + for (t.args[0..t.len], 0..) |arg, i| r[i] = try c.emit(into, arg, registers); + const i: Instruction = switch (t.op) { + .constant => .{ .op = .constant, .a = @bitCast(t.value) }, + .seq => seq: { + // The values in registers side by side: as they are when + // each was emitted just now, else copied. + const values = r[1..t.len]; + var first = values[0]; + for (values, 0..) |v, k| { + if (v != values[0] + k) { + first = @intCast(into.len); + for (values) |each| _ = try push(into, .{ .op = .copy, .a = each }); + break; + } + } + break :seq .{ .op = .seq, .a = r[0], .b = first, .c = @intCast(values.len) }; + }, + else => .{ .op = t.op, .a = r[0], .b = r[1], .c = r[2] }, + }; + const register = try push(into, i); + registers[at] = @intCast(register); + return register; + } }; /// picture.frag's uniforms (SDL_GPU's fragment set). @@ -93,3 +489,210 @@ pub const Synth = extern struct { pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; }; + +// --------------------------------------------------------------------------- +// Tests. + +const testing = std.testing; + +fn compiled(text: []const u8) !Program { + return switch (compile(text)) { + .program => |p| p, + .problem => |p| { + std.debug.print("unexpected problem at {d}: {s}\n", .{ p.at, p.text() }); + return error.TestUnexpectedResult; + }, + }; +} + +fn ops(tape: Tape) [max_tape]Op { + var out: [max_tape]Op = @splat(.constant); + for (tape.slice(), 0..) |i, k| out[k] = i.op; + return out; +} + +fn expectOps(tape: Tape, want: []const Op) !void { + try testing.expectEqualSlices(Op, want, ops(tape)[0..tape.len]); +} + +/// The survey's program (reports/demo1.md, ยง1 A). +const survey = + \\# the beat, shared by sound and picture + \\bpm = 120 + \\beat = t * bpm / 60 + \\kick = exp(-7 * fract(beat)) + \\bar = beat / 4 + \\# sound: out is what you hear, -1..1 + \\root = seq(bar, 55, 55, 65.4, 49) + \\bass = saw(root * t) * (1 - kick) * 0.25 + \\thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5 + \\hat = noise(t * 9000) * exp(-30 * fract(beat * 2 + 0.5)) * 0.08 + \\out = bass + thump + hat + \\# picture: color is what you see; x and y run 0..1 + \\dx = x - 0.5 + \\dy = y - 0.5 + \\r = sqrt(dx * dx + dy * dy) + \\ring = smoothstep(0.03, 0, abs(r - 0.2 - 0.1 * kick)) + \\glow = 0.15 * noise(x * 6, y * 6, t * 0.5) + \\color = hsv(fract(bar / 4 + r), 0.6, ring + glow) + \\ +; + +test "a tone, instruction by instruction" { + const p = try compiled("out = sin(tau * 220 * t) * 0.3"); + try expectOps(p.out, &.{ .constant, .constant, .mul, .t, .mul, .sin, .constant, .mul }); + const code = p.out.slice(); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, std.math.tau))), code[0].a); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, 220))), code[1].a); + try testing.expectEqual(Instruction{ .op = .mul, .a = 0, .b = 1 }, code[2]); + try testing.expectEqual(Instruction{ .op = .mul, .a = 2, .b = 3 }, code[4]); + try testing.expectEqual(Instruction{ .op = .sin, .a = 4 }, code[5]); + try testing.expectEqual(Instruction{ .op = .mul, .a = 5, .b = 6 }, code[7]); + try testing.expectEqual(@as(u32, 0), p.color.len); +} + +test "the survey's program: each tape holds only what its line reaches" { + const p = try compiled(survey); + const heard = ops(p.out)[0..p.out.len]; + const seen = ops(p.color)[0..p.color.len]; + // The sound never reads x or y, nor is coloured; the picture has no + // bass line or hat. + for (heard) |op| try testing.expect(op != .x and op != .y and op != .hsv); + for (seen) |op| try testing.expect(op != .saw and op != .seq and op != .sin); + try testing.expectEqual(Op.add, heard[heard.len - 1]); + try testing.expectEqual(Op.hsv, seen[seen.len - 1]); + // t is one instruction however often it is named; seq's four numbers + // were emitted side by side, so there are no copies. + try testing.expectEqual(@as(usize, 1), std.mem.count(Op, heard, &.{.t})); + try testing.expectEqual(@as(usize, 0), std.mem.count(Op, heard, &.{.copy})); + try testing.expectEqual(@as(u32, 54), p.out.len); + try testing.expectEqual(@as(u32, 46), p.color.len); + // Every operand is an earlier register. + for ([_]Tape{ p.out, p.color }) |tape| for (tape.slice(), 0..) |i, k| { + if (@backingInt(i.op) < @backingInt(Op.neg)) continue; + try testing.expect(i.a < k); + if (i.op == .seq) { + try testing.expect(i.b + i.c <= k); + } else if (@backingInt(i.op) >= @backingInt(Op.add)) try testing.expect(i.b < k); + }; +} + +test "every builtin" { + for (builtins) |b| { + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try w.print("{s} = {s}", .{ if (b.op == .hsv or b.op == .rgb) "color" else "out", b.name() }); + if (b.max > 0) { + try w.writeAll("(t"); + for (1..b.min) |_| try w.writeAll(", t * 2"); + try w.writeAll(")"); + } + const p = try compiled(w.buffered()); + const tape = if (b.op == .hsv or b.op == .rgb) p.color else p.out; + const last = tape.slice()[tape.len - 1]; + try testing.expectEqual(b.op, last.op); + if (b.op == .constant) try testing.expectEqual(@as(u32, @bitCast(b.value)), last.a); + } + // noise's missing arguments are 0; seq copies values that were not + // emitted side by side. + const p = try compiled("a = t * 3\nout = noise(t) + seq(t, 1, a, 2)"); + try expectOps(p.out, &.{ .t, .constant, .constant, .noise, .constant, .constant, .mul, .constant, .copy, .copy, .copy, .seq, .add }); + try testing.expectEqual(Instruction{ .op = .seq, .a = 0, .b = 8, .c = 3 }, p.out.slice()[11]); + try testing.expectEqual(Instruction{ .op = .copy, .a = 6 }, p.out.slice()[9]); +} + +test "precedence, unary minus, numbers, comments, blank lines" { + const p = try compiled("\n # nothing\n a = 1 + 2 * -t / 4 - (5 - -6) # trailing\n\nout = a + 1e-3\n"); + // 1 + ((2 * -t) / 4) - (5 - -6) + try expectOps(p.out, &.{ .constant, .constant, .t, .neg, .mul, .constant, .div, .add, .constant, .constant, .sub, .sub, .constant, .add }); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, -6))), p.out.slice()[9].a); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, 1e-3))), p.out.slice()[12].a); +} + +test "every problem, with its byte" { + const cases = [_]struct { text: []const u8, at: u32, words: []const u8 }{ + .{ .text = "= 1", .at = 0, .words = "a line is a name, =, and an expression" }, + .{ .text = "out 1", .at = 4, .words = "expected = after out, found 1" }, + .{ .text = "out", .at = 3, .words = "expected = after out, found the end of the line" }, + .{ .text = "sin = 1", .at = 0, .words = "sin is a builtin: call yours something else" }, + .{ .text = "a = 1\na = 2", .at = 6, .words = "a is defined twice: a name is given once" }, + .{ .text = "out = 1 2", .at = 8, .words = "expected the end of the line, found 2" }, + .{ .text = "out = rgb(1, 1, 1)", .at = 6, .words = "a colour (hsv, rgb) can only be the whole color line" }, + .{ .text = "color = hsv(1, 1, 1) * 2", .at = 8, .words = "a colour (hsv, rgb) can only be the whole color line" }, + .{ .text = "color = hsv(1, 1, 1)\nout = color", .at = 27, .words = "color is a colour: only the color line can be one" }, + .{ .text = "out = 1.2.3", .at = 6, .words = "1.2.3 is not a number" }, + .{ .text = "out = kick\nkick = 1", .at = 6, .words = "kick is not defined above" }, + .{ .text = "out = sin", .at = 6, .words = "sin takes arguments: sin(a)" }, + .{ .text = "out = 1 +", .at = 9, .words = "expected an expression before the end of the line" }, + .{ .text = "out = * 2", .at = 6, .words = "expected an expression, found *" }, + .{ .text = "out = 1 $ 2", .at = 8, .words = "expected the end of the line, found $" }, + .{ .text = "out = \x01", .at = 6, .words = "byte 0x01 is not part of the language" }, + .{ .text = "out = (1 + 2", .at = 12, .words = "expected ) to close the ( at byte 6, found the end of the line" }, + .{ .text = "a = 1\nout = a(2)", .at = 12, .words = "a is a name, not a builtin: it takes no ( )" }, + .{ .text = "out = foo(2)", .at = 6, .words = "foo is not a builtin" }, + .{ .text = "out = t(2)", .at = 6, .words = "t takes no arguments" }, + .{ .text = "out = sin(1, 2)", .at = 13, .words = "sin takes at most 1 arguments: sin(a)" }, + .{ .text = "out = sin(1 2)", .at = 12, .words = "expected , or ) in sin(...), found 2" }, + .{ .text = "out = mix(1, 2)", .at = 6, .words = "mix takes 3 arguments, not 2: mix(a, b, w)" }, + .{ .text = "out = sin()", .at = 6, .words = "sin takes 1 arguments, not 0: sin(a)" }, + }; + for (cases) |case| switch (compile(case.text)) { + .program => { + std.debug.print("{s}: no problem\n", .{case.text}); + return error.TestUnexpectedResult; + }, + .problem => |p| { + testing.expectEqualStrings(case.words, p.text()) catch |e| { + std.debug.print("in {s}\n", .{case.text}); + return e; + }; + try testing.expectEqual(case.at, p.at); + }, + }; +} + +test "past the capacities: problems in words" { + var buf: [8192]u8 = undefined; + // A tape of 128 fits; one more instruction does not. + var w: std.Io.Writer = .fixed(&buf); + try w.writeAll("out = t"); + for (0..63) |_| try w.writeAll(" + 1"); + const fits = try compiled(w.buffered()); + try testing.expectEqual(@as(u32, 127), fits.out.len); + try w.writeAll(" + 1"); + switch (compile(w.buffered())) { + .program => return error.TestUnexpectedResult, + .problem => |p| { + try testing.expectEqualStrings("the out line needs more than 128 instructions", p.text()); + try testing.expectEqual(@as(u32, 0), p.at); + }, + } + // More terms than the compiler keeps, though no tape reaches them. + w = .fixed(&buf); + for (0..300) |_| try w.writeAll("1+"); + try w.writeAll("1"); + const long = w.buffered(); + var text: [8192]u8 = undefined; + const unused = try std.fmt.bufPrint(&text, "a = {s}\n", .{long}); + switch (compile(unused)) { + .program => return error.TestUnexpectedResult, + .problem => |p| try testing.expectEqualStrings("the program is longer than 512 terms", p.text()), + } + // Names. + w = .fixed(&buf); + for (0..max_names + 1) |i| try w.print("n{d} = {d}\n", .{ i, i }); + switch (compile(w.buffered())) { + .program => return error.TestUnexpectedResult, + .problem => |p| try testing.expectEqualStrings("more than 128 names", p.text()), + } + // Depth. + w = .fixed(&buf); + try w.writeAll("out = "); + for (0..40) |_| try w.writeAll("("); + try w.writeAll("1"); + for (0..40) |_| try w.writeAll(")"); + switch (compile(w.buffered())) { + .program => return error.TestUnexpectedResult, + .problem => |p| try testing.expectEqualStrings("nested more than 32 deep", p.text()), + } +} diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig index 663bed3..5f6e8ee 100644 --- a/examples/live/src/probe.zig +++ b/examples/live/src/probe.zig @@ -58,7 +58,7 @@ fn madeUp(comptime len: u32) [Lang.max_tape][4]u32 { else => if (i == len - 1) .hsv else if (i % 10 == 9) .noise else cycle[i % cycle.len], }; const a: u32 = if (op == .constant) @bitCast(@as(f32, 6)) else @intCast(i -| 1); - code[i] = .{ @intFromEnum(op), a, @intCast(i -| 2), @intCast(i -| 3) }; + code[i] = .{ @backingInt(op), a, @intCast(i -| 2), @intCast(i -| 3) }; } return code; } -- cgit v1.3 From c300a059850115a87a34456dfd60ac97c4a09890 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:03:15 -0300 Subject: examples/live: the picture, typed over itself The sketch: State holds the text (a plain 4 KiB buffer, typed characters appended, backspace deleting the last, return a newline: marked ponytail, the place pardes's editor takes in part 2), the last good out and color tapes, the problem and the time, summed from recorded ticks. `update` compiles the text at every keystroke; a program that parses replaces the tapes and clears the problem, one that does not sets the problem and changes nothing else, so the last good picture stays. `draw` runs picture.frag over the color tape into an image the size of the canvas, 1280x720, or half of it when the tape is longer than 64 instructions (the probe: 128 took 2.7 ms at full size, 0.85 at half), and scales it up. The text is drawn over it on a dark band with the library's text calls (the code face at 1.25, a column a fixed advance), the caret at the end, the problem as a red mark under its byte and its words under the text, and live/recording/replay/paused with the time in a corner. Test: test/live.script, the start of the scripted proof: a tone typed (its eight instructions asserted), a gradient (hsv over x and y, four), an edit (y to x: three, the picture different), a broken line (the problem at byte 52, the tape and the picture the same) and its mend. zig build test: 56 of 56 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 4 +- examples/live/src/main.zig | 163 +++++++++++++++++++++++++++++++++++++++++ examples/live/test/live.script | 37 ++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 examples/live/src/main.zig create mode 100644 examples/live/test/live.script diff --git a/examples/live/build.zig b/examples/live/build.zig index 2ad85fb..2f76306 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -29,8 +29,10 @@ pub fn build(b: *std.Build) void { .{ .name = "shaders", .module = shaders.module }, }; + const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports }); + const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); - b.step("test", "Run the language's tests").dependOn(&b.addRunArtifact(lang_test).step); + steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); // The interpreter's GPU time over made-up tapes (`--profile-gpu`). const probe = sb.app(.{ .name = "live-probe", .root_source_file = b.path("src/probe.zig"), .imports = imports }); diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig new file mode 100644 index 0000000..02a9c6d --- /dev/null +++ b/examples/live/src/main.zig @@ -0,0 +1,163 @@ +//! live: a program typed over its own picture. Each line is `name = +//! expression` over t, x and y (src/Lang.zig); the `color` line is what is +//! seen. At every keystroke that leaves a program that parses, `update` +//! compiles it into a tape kept in State, which one interpreter on the GPU +//! (shaders/vm.glsl) runs per pixel. While a line is broken the last good +//! tape keeps playing and the problem is marked at its byte. +//! +//! State is the text, the tapes, the problem and the time, all changed by +//! `update` from recorded keys and ticks, so a replay or a seek brings back +//! the text, the tape and the picture of that moment. Session holds the +//! GPU objects. +const std = @import("std"); +const sk = @import("sketch"); +const gpu = sk.abi.gpu; +const Lang = @import("Lang"); +const shaders = @import("shaders"); + +pub const name = "live"; +pub const width = 1280; +pub const height = 720; + +pub const State = struct { + // ponytail: a plain buffer, typed characters appended and backspace + // deleting the last; part 2 puts pardes's editor here. + text: [4096]u8 = @splat(0), + len: u32 = 0, + /// The last program that parsed. + out: Lang.Tape = .{}, + color: 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. + time: f32 = 0, +}; + +pub const Session = struct { + picture: sk.Image = .none, + size: [2]u32 = .{ 0, 0 }, +}; + +pub fn update(s: *State, in: *const sk.Input) void { + var edited = false; + for (in.events) |e| switch (e.kind) { + .tick => s.time += e.x, + .text => { + var utf8: [4]u8 = undefined; + const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; + if (s.len + n > s.text.len) continue; + @memcpy(s.text[s.len..][0..n], utf8[0..n]); + s.len += n; + edited = true; + }, + .key_down => switch (e.key()) { + .backspace => if (s.len > 0) { + s.len -= 1; + while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1; + edited = true; + }, + .@"return", .keypad_enter => if (s.len < s.text.len) { + s.text[s.len] = '\n'; + s.len += 1; + edited = true; + }, + else => {}, + }, + else => {}, + }; + if (!edited) return; + switch (Lang.compile(s.text[0..s.len])) { + .program => |p| { + s.out = p.out; + s.color = p.color; + s.problem = .{}; + }, + .problem => |p| s.problem = p, + } +} + +const picture_pipeline: gpu.Pipeline = .{ + .vertex = .{ .code = shaders.fullscreen_vert }, + .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 }, + .stride = 0, + .attributes = &.{}, + .blend = .none, +}; + +pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + f.clear(0x000000); + if (!f.host.gpu()) { + f.text(16, 16, 2, "live needs a GPU: the host has none.", 0xff8080); + return; + } + // 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 size: [2]u32 = .{ width / half, height / half }; + if (!std.meta.eql(ss.size, size)) { + if (ss.picture != .none) f.host.freeImage(ss.picture); + ss.picture = f.host.newImage(size[0], size[1]); + ss.size = size; + } + f.gpu(.{ + .label = "picture", + .image = ss.picture, + .width = size[0], + .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, + .time = s.time, + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + }) }}, + }); + f.image(ss.picture, .{ .x = 0, .y = 0, .w = width, .h = height }, .{}); + code(s, f); +} + +const scale = 1.25; +const margin = 16; +const ink = 0xe8e8e8; +const red = 0xff5a50; + +/// The text over the picture on a dark band, the caret at its end, and the +/// problem: a red mark under its byte and its words under the text. +// ponytail: one face at one size, so a column is a fixed advance; part 2 +// draws the editor's text with sketchbook's new text instead. +fn code(s: *const State, f: *sk.Frame) void { + const text = s.text[0..s.len]; + const cell = f.host.textSizeIn(.code, scale, "M"); + const lines = std.mem.count(u8, text, "\n") + 1; + var widest: f32 = 0; + var it = std.mem.splitScalar(u8, text, '\n'); + while (it.next()) |line| widest = @max(widest, f.host.textSizeIn(.code, scale, line)[0]); + const problem = s.problem.len > 0; + if (problem) widest = @max(widest, f.host.textSizeIn(.code, scale, s.problem.text())[0]); + const band_h = @as(f32, @floatFromInt(lines + @intFromBool(problem))) * cell[1] + 2 * margin; + f.fill(.{ .x = 0, .y = 0, .w = @max(widest, 24 * cell[0]) + 2 * margin + cell[0], .h = band_h }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 }); + + var y: f32 = margin; + var start: usize = 0; + it = std.mem.splitScalar(u8, text, '\n'); + while (it.next()) |line| : (y += cell[1]) { + f.textIn(.code, margin, y, scale, line, ink); + const end = start + line.len; + if (problem and s.problem.at >= start and s.problem.at <= end) { + const x = margin + f.host.textSizeIn(.code, scale, text[start..s.problem.at])[0]; + f.fill(.{ .x = x, .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red); + } + // The caret, after the last byte. + if (end == text.len) f.fill(.{ .x = margin + f.host.textSizeIn(.code, scale, line)[0], .y = y, .w = 2, .h = cell[1] }, ink); + start = end + 1; + } + if (problem) f.textIn(.code, margin, y, scale, s.problem.text(), red); + + const mode = switch (f.in.mode) { + .live => "live", + .recording => "recording", + .playing => "replay", + .paused => "paused", + }; + f.print(width - 200, height - 24, 1, ink, "{s} {d:.1} s", .{ mode, s.time }); +} diff --git a/examples/live/test/live.script b/examples/live/test/live.script new file mode 100644 index 0000000..386ab33 --- /dev/null +++ b/examples/live/test/live.script @@ -0,0 +1,37 @@ +# A program typed, its tapes after each line, a broken line and its mend. +# Screenshots land in .zig-cache/live/ for review. +frames 2 +type out = sin(tau * 220 * t) * 0.3 +expect problem.len 0 +expect out.len 8 +expect out.code[5].op sin +expect out.code[7].op mul +key return +type color = hsv(x, 0.5, y) +expect color.len 4 +expect color.code[0].op x +expect color.code[3].op hsv +frames 2 +screenshot .zig-cache/live/gradient.png +# An edit: y becomes x, and the picture changes. +key backspace +key backspace +type x) +expect color.len 3 +expect color.code[2].op hsv +expect color.code[2].c 0 +frames 2 +screenshot .zig-cache/live/edited.png +expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 380 +# Broken: the tape and the picture stay, the problem is set at its byte. +key backspace +expect problem.at 52 +expect color.len 3 +expect color.code[2].c 0 +frames 2 +screenshot .zig-cache/live/broken.png +expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 380 +# Mended. +type ) +expect problem.len 0 +expect color.len 3 -- cgit v1.3 From a4d1e3131499c17bd6f73e65154aa0c2b31c62b3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:05:00 -0300 Subject: examples/live: the sound, the out tape on the GPU per sample `pub const audio_pull = 256`; each frame `draw` runs synth.comp over as many samples as keep the host's lead queued (`in.lead -| queued`, 8192 at most) and streams the block with f.audioBuffer, as spheres does. Time is a sample clock in Session: set from State's time at every jump (a seek, a step, a replay's start; the queue is dropped then, so the jump is heard at once), and free-running otherwise, so a paused replay keeps sounding its moment. When the out tape differs from the one the last block played, the block crosses from the old tape to the new over its length (`set crossfade false` turns that off, to hear what it saves). The blocks are read back for the scope (an oscilloscope and a spectrum along the bottom) and, after the console's `capture`, appended to the sketch's capture.f32 beside its save. The status line shows the lead. The tapes reach synth.comp in a storage buffer, uploaded per block: two tapes are 4 KiB, and SDL_GPU's Vulkan backend binds a uniform block 4 KiB at a time (MAX_UBO_SECTION_SIZE), so a 4128-byte Synth block read its count as 0 past the cut and the kernel wrote nothing, silently: the probe's first synth numbers timed that. Measured again (RTX 3050, Debug host, two runs): every synth dispatch 0.08-0.14 ms, 60 or 128 instructions, 256 or 1024 samples, fading or not, the empty pass's cost alone; the 4 KiB upload 0.07-0.08. Measured on SDL's dummy driver (nothing audible) from captures: out = 0.3 * sin(t * tau * 220): peak 0.3000, RMS 0.2121, fundamental 220.000 Hz; edited to 330 in one keystroke, 330.000 Hz. The largest sample-to-sample step across the edit: 0.0131 with the crossfade (a 330 Hz sine's own steepest step is 0.0130), 0.3725 without; into the first sound, 0.0011 and 0.2967. The survey's program: peak 0.50, RMS 0.16, its bass at 55.0 Hz. Test: live.script also expects the stream to receive the GPU's blocks, and compares screenshots above the scope. zig build test: 68 of 68 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 1 + examples/live/shaders/synth.comp | 4 +- examples/live/src/Lang.zig | 12 +++-- examples/live/src/main.zig | 108 +++++++++++++++++++++++++++++++++++---- examples/live/src/probe.zig | 12 +++-- examples/live/test/live.script | 10 ++-- 6 files changed, 124 insertions(+), 23 deletions(-) diff --git a/examples/live/build.zig b/examples/live/build.zig index 2f76306..9d0983d 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -27,6 +27,7 @@ pub fn build(b: *std.Build) void { const imports: []const std.Build.Module.Import = &.{ .{ .name = "Lang", .module = lang }, .{ .name = "shaders", .module = shaders.module }, + .{ .name = "scope", .module = sb.modules.scope }, }; const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports }); diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp index 9a066a6..8ae48bb 100644 --- a/examples/live/shaders/synth.comp +++ b/examples/live/shaders/synth.comp @@ -6,7 +6,9 @@ layout(local_size_x = 64) in; #include "Synth.glsl" -#define TAPE(i) u.tape[i] +// The new tape, then the old one. +layout(set = 0, binding = 0) readonly buffer Tapes { uvec4 tape[]; }; +#define TAPE(i) tape[i] #include "vm.glsl" layout(set = 1, binding = 0) buffer Block { float block[]; }; diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index a6db8b9..4f3c3bf 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -135,7 +135,9 @@ pub const Instruction = extern struct { pub const Tape = struct { len: u32 = 0, - code: [max_tape]Instruction = @splat(.{}), + code: Code = @splat(.{}), + + pub const Code = [max_tape]Instruction; pub fn slice(tape: *const Tape) []const Instruction { return tape.code[0..@min(tape.len, max_tape)]; @@ -472,11 +474,11 @@ pub const Picture = extern struct { pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } }; }; -/// synth.comp's uniforms (SDL_GPU's compute set): the tape, and the one -/// it replaces, which the first `fade` samples cross from. +/// synth.comp's uniforms (SDL_GPU's compute set). Its tapes, the new one +/// and the one it replaces, which the first `fade` samples cross from, are +/// a storage buffer: SDL_GPU's Vulkan backend binds a uniform block 4 KiB +/// at a time, and two tapes are 4 KiB alone. pub const Synth = extern struct { - /// The new tape, then the old one. - tape: [2 * max_tape][4]u32, len: u32, old_len: u32, /// Samples in the block. diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 02a9c6d..13f89be 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -1,23 +1,35 @@ -//! live: a program typed over its own picture. Each line is `name = -//! expression` over t, x and y (src/Lang.zig); the `color` line is what is -//! seen. At every keystroke that leaves a program that parses, `update` -//! compiles it into a tape kept in State, which one interpreter on the GPU -//! (shaders/vm.glsl) runs per pixel. While a line is broken the last good -//! tape keeps playing and the problem is marked at its byte. +//! live: a program typed over its own picture and heard. Each line is +//! `name = expression` over t, x and y (src/Lang.zig); the `out` line is +//! what is heard, the `color` line what is seen. At every keystroke that +//! leaves a program that parses, `update` compiles it into two tapes kept +//! in State, which one interpreter on the GPU (shaders/vm.glsl) runs per +//! pixel and per sample. While a line is broken the last good tapes keep +//! playing and the problem is marked at its byte. //! //! State is the text, the tapes, the problem and the time, all changed by //! `update` from recorded keys and ticks, so a replay or a seek brings back -//! the text, the tape and the picture of that moment. Session holds the -//! GPU objects. +//! the text, the tape and the picture of that moment. The sound runs on a +//! sample clock in Session, set from State's time at every jump (a seek, +//! a replay's start) and free-running otherwise, so a paused replay keeps +//! sounding its moment. Session holds it, the GPU objects and the scope. const std = @import("std"); const sk = @import("sketch"); const gpu = sk.abi.gpu; const Lang = @import("Lang"); const shaders = @import("shaders"); +const scope = @import("scope"); pub const name = "live"; pub const width = 1280; pub const height = 720; +/// A change is heard about 6.5 ms after `draw`, not 19-27 (README, sound). +pub const audio_pull = 256; +pub const commands = .{"capture"}; + +/// The sound's stream, and its readback's key. +const stream = 1; +/// The most samples synthesised in one frame. +const max_block = 8192; pub const State = struct { // ponytail: a plain buffer, typed characters appended and backspace @@ -31,13 +43,35 @@ pub const State = struct { problem: Lang.Problem = .{}, /// Seconds, from recorded ticks: a replay draws the same frames. time: f32 = 0, + /// A new out tape crosses from the old one over a block; `set + /// crossfade false` hears (and measures) the click it saves. + crossfade: bool = true, }; pub const Session = struct { picture: sk.Image = .none, size: [2]u32 = .{ 0, 0 }, + block: sk.Buffer = .none, + /// The out tape and the one it replaces, for synth.comp. + tapes: sk.Buffer = .none, + /// Seconds: where the next block starts. + clock: f64 = 0, + /// The tape the last block played. + heard: Lang.Tape = .{}, + scope: scope.Scope = .{}, + /// `capture` appends what is heard to `.capture.f32`. + capturing: bool = false, }; +/// `capture` starts or stops appending what is heard to the sketch's file +/// `capture.f32` (mono f32 at the device's rate, beside its save). +pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool { + if (!std.mem.eql(u8, std.mem.trim(u8, line, " \t"), "capture")) return false; + ss.capturing = !ss.capturing; + c.log(if (ss.capturing) "capturing what is heard to capture.f32" else "capture stopped"); + return true; +} + pub fn update(s: *State, in: *const sk.Input) void { var edited = false; for (in.events) |e| switch (e.kind) { @@ -76,6 +110,7 @@ pub fn update(s: *State, in: *const sk.Input) void { } } +const synth: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 }; const picture_pipeline: gpu.Pipeline = .{ .vertex = .{ .code = shaders.fullscreen_vert }, .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 }, @@ -113,9 +148,63 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { }) }}, }); f.image(ss.picture, .{ .x = 0, .y = 0, .w = width, .h = height }, .{}); + sound(s, ss, f); code(s, f); } +/// The out tape over the sample clock, as much as keeps the host's lead +/// queued, streamed from the GPU's block; and read back for the scope. +fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { + const in = f.in; + for (in.downloads) |d| if (d.key == stream) { + const samples = f.scratch.alloc(f32, d.bytes.len / 4) catch continue; + @memcpy(std.mem.sliceAsBytes(samples), d.bytes[0 .. samples.len * 4]); + ss.scope.push(samples); + if (ss.capturing) _ = f.host.appendFile("capture.f32", d.bytes); + }; + if (ss.block == .none) { + ss.block = f.host.newBuffer(max_block * @sizeOf(f32)); + ss.tapes = f.host.newBuffer(2 * @sizeOf(Lang.Tape.Code)); + } + // A jump is heard at once: what was queued from before it is dropped. + if (in.jumped) { + ss.clock = s.time; + f.audio(stream, &.{}, .{ .clear = true }); + } + const rate: f64 = @floatFromInt(in.sample_rate); + const want: u32 = @min(in.lead -| in.stream(stream).queued, max_block); + if (want > 0) { + const new = std.mem.sliceAsBytes(s.out.slice()); + const changed = !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 }; + f.upload(ss.tapes, 0, std.mem.asBytes(tapes)); + f.compute(.{ + .label = "synth", + .kernel = synth, + .readonly = &.{ss.tapes}, + .readwrite = &.{ss.block}, + .uniforms = gpu.uniform(f.scratch, Lang.Synth{ + .len = s.out.len, + .old_len = ss.heard.len, + .count = want, + .fade = if (changed and s.crossfade) want else 0, + .start = @floatCast(ss.clock), + .rate = @floatCast(rate), + }), + .groups = .{ (want + 63) / 64, 1, 1 }, + }); + 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.scope.draw(f, .{ + .oscilloscope = .{ .rect = .{ .x = 16, .y = height - 104, .w = 480, .h = 88 }, .samples = 1024 }, + .spectrum = .{ .x = 512, .y = height - 104, .w = 480, .h = 88 }, + }); +} + const scale = 1.25; const margin = 16; const ink = 0xe8e8e8; @@ -159,5 +248,6 @@ fn code(s: *const State, f: *sk.Frame) void { .playing => "replay", .paused => "paused", }; - f.print(width - 200, height - 24, 1, ink, "{s} {d:.1} s", .{ mode, s.time }); + const lead = @as(f32, @floatFromInt(f.in.lead)) / @as(f32, @floatFromInt(f.in.sample_rate)) * 1000; + f.print(width - 260, 12, 1, ink, "{s} {d:.1} s lead {d:.1} ms", .{ mode, s.time, lead }); } diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig index 5f6e8ee..1e2970c 100644 --- a/examples/live/src/probe.zig +++ b/examples/live/src/probe.zig @@ -17,6 +17,7 @@ pub const height = 360; pub const Session = struct { images: [3]sk.Image = .{ .none, .none, .none }, block: sk.Buffer = .none, + tapes: sk.Buffer = .none, time: f32 = 0, }; @@ -80,7 +81,7 @@ const pipeline: gpu.Pipeline = .{ .attributes = &.{}, .blend = .none, }; -const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readwrite_buffers = 1, .uniform_buffers = 1 }; +const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 }; pub fn draw(ss: *Session, f: *sk.Frame) void { f.clear(0x000000); @@ -88,6 +89,7 @@ pub fn draw(ss: *Session, f: *sk.Frame) void { if (ss.block == .none) { for (&ss.images, sizes) |*image, size| image.* = f.host.newImage(size[0], size[1]); ss.block = f.host.newBuffer(1024 * @sizeOf(f32)); + ss.tapes = f.host.newBuffer(2 * Lang.max_tape * 16); } ss.time += @floatCast(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s); for (pictures) |p| { @@ -107,15 +109,15 @@ pub fn draw(ss: *Session, f: *sk.Frame) void { }); } for (blocks) |k| { - var both: [2 * Lang.max_tape][4]u32 = undefined; - both[0..Lang.max_tape].* = tape(k.len); - both[Lang.max_tape..].* = tape(k.len); + const both = f.scratch.create([2][Lang.max_tape][4]u32) catch return; + both.* = .{ tape(k.len), tape(k.len) }; + f.upload(ss.tapes, 0, std.mem.asBytes(both)); f.compute(.{ .label = k.label, .kernel = kernel, + .readonly = &.{ss.tapes}, .readwrite = &.{ss.block}, .uniforms = gpu.uniform(f.scratch, Lang.Synth{ - .tape = both, .len = k.len, .old_len = k.len, .count = k.count, diff --git a/examples/live/test/live.script b/examples/live/test/live.script index 386ab33..22085bf 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -1,11 +1,15 @@ # A program typed, its tapes after each line, a broken line and its mend. -# Screenshots land in .zig-cache/live/ for review. +# Screenshots land in .zig-cache/live/ for review; they are compared over +# the picture alone (640,300 600x300: not the text, the scope or the time). frames 2 type out = sin(tau * 220 * t) * 0.3 expect problem.len 0 expect out.len 8 expect out.code[5].op sin expect out.code[7].op mul +# The GPU's blocks reach the speakers' stream (SDL's dummy driver here). +frames 10 +expect-audio 1 key return type color = hsv(x, 0.5, y) expect color.len 4 @@ -22,7 +26,7 @@ expect color.code[2].op hsv expect color.code[2].c 0 frames 2 screenshot .zig-cache/live/edited.png -expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 380 +expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 300 # Broken: the tape and the picture stay, the problem is set at its byte. key backspace expect problem.at 52 @@ -30,7 +34,7 @@ expect color.len 3 expect color.code[2].c 0 frames 2 screenshot .zig-cache/live/broken.png -expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 380 +expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 300 # Mended. type ) expect problem.len 0 -- cgit v1.3 From 8dc369f343fda8f54756376728f4690386c20a69 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:10:20 -0300 Subject: examples/live: the scripted proof, a session played back and sought test/live.script now ends with the run's own session replayed: `play session 0` from its start, then `seek` to the two moments marked as they happened (the gradient just typed, and the end after an edit, a broken line and its mend). At each, the State equals the mark leaf by leaf (`expect-snapshot`: the text, both tapes, the problem, the time), and at the first the screenshot equals the live one over the text and the picture (0,0 1000x600, without the scope and the status line). `stop` then goes on live from that moment: the typed program, before its edit. What a script could not do: a `record` clip ends at the first mark (Console.zig's `save` stops it), so the clip cannot be marked while it is made: the session is replayed instead. A mark is State before its frame's tick, so its time is the printed time less a frame, and `seek MS` lands on the last frame at or before MS: the script seeks 5 ms past each mark's time (272 and 488). The sound at a jump: `want` counted the queue the clear is about to drop, so a seek starved the device for a frame; now it counts from empty. And the block after a jump crosses from the old tape at where the device was (the clock less what was queued) to the tape at the new time (Synth.old_start). Not measured: a capture records every block made, including the tail the jump drops, so it cannot show what was heard across a seek. zig build test: 68 of 68 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/shaders/synth.comp | 10 +++++----- examples/live/src/Lang.zig | 6 ++++-- examples/live/src/main.zig | 13 +++++++++---- examples/live/src/probe.zig | 1 + examples/live/test/live.script | 22 +++++++++++++++++++++- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp index 8ae48bb..0e7ab02 100644 --- a/examples/live/shaders/synth.comp +++ b/examples/live/shaders/synth.comp @@ -1,8 +1,9 @@ #version 450 #extension GL_GOOGLE_include_directive : require // One audio block, one thread per sample: the out tape at each sample's -// time, crossing over the first `fade` samples from the tape it replaces, -// so an edit does not click. x and y are 0. +// time, crossing over the first `fade` samples from the tape it replaces +// (at its own time, which differs after a seek), so an edit or a seek +// does not click. x and y are 0. layout(local_size_x = 64) in; #include "Synth.glsl" @@ -21,8 +22,7 @@ float heard(float v) { void main() { uint n = gl_GlobalInvocationID.x; if (n >= u.count) return; - float t = u.start + float(n) / u.rate; - float v = heard(vm(0u, u.len, t, 0.0, 0.0).x); - if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, t, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); + float v = heard(vm(0u, u.len, u.start + float(n) / u.rate, 0.0, 0.0).x); + if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, u.old_start + float(n) / u.rate, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); block[n] = v; } diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 4f3c3bf..484de54 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -484,10 +484,12 @@ pub const Synth = extern struct { /// Samples in the block. count: u32, fade: u32, - /// The block's first sample, seconds. + /// The block's first sample, seconds; the old tape's, which differs + /// after a jump (a seek: the old tape where the device was). start: f32, + old_start: f32, rate: f32, - pad: [2]f32 = .{ 0, 0 }, + pad: f32 = 0, pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; }; diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 13f89be..965152a 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -166,16 +166,20 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { ss.block = f.host.newBuffer(max_block * @sizeOf(f32)); ss.tapes = f.host.newBuffer(2 * @sizeOf(Lang.Tape.Code)); } - // A jump is heard at once: what was queued from before it is dropped. + // A jump is heard at once: what was queued from before it is dropped, + // and the block crosses from where the device was to the new time. + const rate: f64 = @floatFromInt(in.sample_rate); + var queued = in.stream(stream).queued; + const was = ss.clock - @as(f64, @floatFromInt(queued)) / rate; if (in.jumped) { ss.clock = s.time; + queued = 0; f.audio(stream, &.{}, .{ .clear = true }); } - const rate: f64 = @floatFromInt(in.sample_rate); - const want: u32 = @min(in.lead -| in.stream(stream).queued, max_block); + const want: u32 = @min(in.lead -| queued, max_block); if (want > 0) { const new = std.mem.sliceAsBytes(s.out.slice()); - const changed = !std.mem.eql(u8, new, std.mem.sliceAsBytes(ss.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 }; f.upload(ss.tapes, 0, std.mem.asBytes(tapes)); @@ -190,6 +194,7 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { .count = want, .fade = if (changed and s.crossfade) want else 0, .start = @floatCast(ss.clock), + .old_start = @floatCast(if (in.jumped) was else ss.clock), .rate = @floatCast(rate), }), .groups = .{ (want + 63) / 64, 1, 1 }, diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig index 1e2970c..4f816b2 100644 --- a/examples/live/src/probe.zig +++ b/examples/live/src/probe.zig @@ -123,6 +123,7 @@ pub fn draw(ss: *Session, f: *sk.Frame) void { .count = k.count, .fade = if (k.fade) k.count else 0, .start = ss.time, + .old_start = ss.time, .rate = 48000, }), .groups = .{ (k.count + 63) / 64, 1, 1 }, diff --git a/examples/live/test/live.script b/examples/live/test/live.script index 22085bf..695d16a 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -1,4 +1,6 @@ -# A program typed, its tapes after each line, a broken line and its mend. +# A program typed, its tapes after each line, a broken line and its mend; +# then this run's session played back, and sought to two moments marked as +# they happened. (A mark ends a clip, `record`, so the session is replayed.) # Screenshots land in .zig-cache/live/ for review; they are compared over # the picture alone (640,300 600x300: not the text, the scope or the time). frames 2 @@ -16,6 +18,7 @@ expect color.len 4 expect color.code[0].op x expect color.code[3].op hsv frames 2 +console save typed screenshot .zig-cache/live/gradient.png # An edit: y becomes x, and the picture changes. key backspace @@ -39,3 +42,20 @@ expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 6 type ) expect problem.len 0 expect color.len 3 +console save end +# Played back from the start, then sought: each moment's text, tapes and +# time as they were live, leaf by leaf (a mark is taken before its frame's +# tick: 0.2667 s and 0.4833 s; a seek lands on the last frame at or before). +console play session 0 +frames 100 +console seek 488 +expect-snapshot end +console seek 272 +expect-snapshot typed +expect color.len 4 +screenshot .zig-cache/live/sought.png +expect-same-screenshot .zig-cache/live/gradient.png .zig-cache/live/sought.png 0 0 0 1000 600 +# Live again from there: the typed program, before its edit. +console stop +expect len 53 +expect color.code[3].op hsv -- cgit v1.3 From c009f708e631700fb1d65ccba2c8229c10054528 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:13:50 -0300 Subject: examples/live: the README, the language on one page examples/live/README.md: what the sketch is, how to run it (keys, the console's replay verbs, capture, crossfade, the test and probe steps), the language (its grammar and every builtin a line, from Lang.builtins), the capacities, what it cannot say (no state between samples or frames: no filters, feedback or reverb; the price of computing each sample alone on the GPU and of exact replays) and how it works, with the probe's numbers. Its example program parses (out 22 instructions, color 38). The sketchbook README lists the example. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- README.md | 1 + examples/live/README.md | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 examples/live/README.md diff --git a/README.md b/README.md index 733cc3d..a6358d8 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ The standalone example packages live in [`examples/`](examples/): - [`vecanim`](examples/vecanim/): vector animation scenes. Select one with `-Dscene=scenes/live.zig`. - [`micscope`](examples/micscope/): records the microphone or opens sound files, plays them either way, and shows waveform, spectrogram and spectrum; a tuner. - [`spheres`](examples/spheres/): the three-body problem on the GPU, each body a voice synthesised by a compute shader. +- [`live`](examples/live/): a tiny language for sound and picture, typed over its own picture: every line that parses is compiled to a tape one GPU interpreter runs per pixel and per sample. - [`talk.zig`](examples/talk.zig): a talk with a slider in it, a sketch that runs its own `anim.Deck` (`zig build test-talk` plays it). - [`manual.zig`](examples/manual.zig): Figure 2-1 of the Intel SDM comes alive, then ADD's opcode table is written row by row and its `80 /0 ib` turns into SUB's `80 /5 ib`: PDF pages as vector shapes and parts of them (`zig build manual -Dmanual=PDF`; see Animation, PDF pages and parts). diff --git a/examples/live/README.md b/examples/live/README.md new file mode 100644 index 0000000..3c0c885 --- /dev/null +++ b/examples/live/README.md @@ -0,0 +1,89 @@ +# live + +A program typed over its own picture, and heard. Each line names an +expression; the `out` line is what you hear (-1..1), the `color` line what +you see. At every keystroke that leaves a program that parses, the +program is compiled into two tapes of at most 128 instructions, and one +interpreter on the GPU runs them per sample and per pixel. While a line +is broken, the last good program keeps playing and the problem is marked +under its byte. + +``` +bpm = 120 +beat = t * bpm / 60 +kick = exp(-7 * fract(beat)) +thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5 +out = thump +r = sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) +color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.03, 0, abs(r - 0.2 - 0.1 * kick))) +``` + +The picture pulses with the kick because both lines say `kick`. + +## Running it + +`zig build run` (with `zig build lib --watch` beside it for hot reload). +Type; Backspace deletes the last character, Return starts a line. The +console (backtick) records, plays and seeks as for any sketch: `play +session 0`, `pause`, `seek 5000`, `step -60`, `stop`; a seek brings back +the text, the tapes and the picture of that moment, and the sound with +them. `capture` appends what is heard to `capture.f32` beside the save +(mono f32 at the device's rate) until `capture` again; `set crossfade +false` lets an edit click. `zig build test` runs the language's tests and +test/live.script headless; `zig build probe` times the interpreter on the +GPU (`--profile-gpu`). + +## The language + +A line is `name = expression`, a name defined once and before it is used, +so a program has no loops or recursion and always ends. Expressions: +numbers (`0.5`, `120`, `1e-3`), names, `+ - * /`, unary `-`, parentheses, +calls; `#` starts a comment. The builtins (src/Lang.zig's one table): + +| | | +| --- | --- | +| `t` | seconds | +| `x`, `y` | 0..1, left to right and bottom to top; 0 in sound | +| `tau`, `pi` | a turn and half a turn, in radians | +| `sin(a)`, `cos(a)` | of radians | +| `exp(a)`, `log(a)`, `pow(a, b)`, `sqrt(a)` | e to the a, its inverse, a to the b (a not negative), square root | +| `abs(a)`, `floor(a)`, `fract(a)` | without its sign, the whole number at or below, a - floor(a) | +| `min(a, b)`, `max(a, b)`, `clamp(v, lo, hi)` | the smaller, the larger, v kept between | +| `mix(a, b, w)` | a + (b - a) * w | +| `step(edge, v)` | 0 below edge, 1 from it on | +| `smoothstep(lo, hi, v)` | 0 to 1 smoothly as v goes from lo to hi, either way round | +| `atan2(y, x)` | the angle of the point (x, y) | +| `noise(x, y, z)` | smooth noise, -1..1, a new value about every 1; y and z 0 when left out | +| `saw(p)`, `sqr(p)`, `tri(p)` | waves of period 1 (cycles, not radians), -1..1; saw and tri rise through 0 at 0, sqr is 1 then -1 | +| `seq(i, v0, v1, ...)` | the value at floor(i), round the list: a step pattern, 16 values at most | +| `hsv(h, s, v)`, `rgb(r, g, b)` | a colour (hue 0..1 round the wheel); only as the whole `color` line | + +Capacities: 128 instructions a tape (a line reaches only what it uses), +512 terms and 128 names in the text, nesting 32 deep, 4 KiB of text. Past +any of them is a problem like a syntax error, in words, at its byte. + +## What it cannot say + +No state between samples or between frames: every sample is a function of +its time alone, every pixel of x, y and t. So no filters, feedback, delay +lines or reverb, and no picture that remembers the last frame. That is the +price of computing every sample alone on the GPU, and what makes a replayed +moment sound and look as it did. An echo can be said by evaluating again +at an earlier time (`t - 0.25` in a copy of the line). No user functions, +no vectors but the final colour, and `t` is a 32-bit float: past ten +minutes or so a high pitch begins to roughen. + +## How it works + +src/Lang.zig compiles the text in `update` (no allocation: fixed arrays on +the stack) into tapes of `{op, a, b, c}`, instruction i writing register +i; State keeps the text, both tapes, the problem and the time. The tapes +go to the GPU as data: the color tape as picture.frag's uniforms, the out +tape (with the one it replaces, for the crossfade) in a storage buffer for +synth.comp, both including shaders/vm.glsl, a loop over the tape with a +switch per op. No shader is compiled after the first frame, so typing +never stalls one. A color tape over 64 instructions is drawn at half +resolution: on an RTX 3050, 128 instructions took 2.7 ms at 1280x720 and +0.85 at half; 60 took 1.4 at full. The sound is `pub const audio_pull = +256` and a block a frame, as much as keeps the host's lead queued: a +change is heard a pull or so after the frame that made it. -- cgit v1.3 From ed185f305731309cbddb981166678a552f2fda84 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:42:48 -0300 Subject: examples/live: pardes's editor in State, typed with helix's keys The plain buffer of part 1 gives way to pardes's text pane as a library (`pardes-editor`, src/lib_editor.zig there): `Editor(.{ .text = 4096, .undo = 16 KiB, .registers = 1024, .selections = 64 })`, a value of plain data, 39,264 bytes of a State of 43,480. The package takes pardes by path (`../../../02-pardes-code`, the checkout beside sketchbook's) with `.editor = true` and its smallest tree-sitter set; sketchbook is spelled `../../../sketchbook` as pardes spells it, since a package is told by its path's spelling and sketchbook's build.zig cannot be two modules. `update` turns events into keys: printables from `text` events only, specials (Escape, Return, Backspace, Tab, arrows, Home, End, pages, Delete) and ctrl or alt chords (helix's `A-I` an upper-case letter) from `key_down`, a chord's letter dropped if it also comes as text. Each call into the editor thaws the value into a static 128 KiB buffer reset for the call (`update` may not allocate from the host; a key at 4 KiB of text took 29 KiB of it). The text is compiled when the editor's `revision` moves. Measured (a scratch bench through `update`, the survey's program, 646 bytes, a key typed into its middle, 400 times): a typed key, edit and compile, 59.5 us best and 78.8 mean in Debug, 7.4 and 7.8 in ReleaseFast; a motion 6.4 and 0.3. The package's cold `zig build lib` went from 3.1 s to 6.6 s, its first build under `--watch -fincremental` from 4.0 to 5.9 s, and an edit of main.zig rebuilt under it from 0.062-0.067 s to 0.067-0.092 s. Test: test/live.script types through helix now (`i`, the text, Escape, `gl`, `h`, `rx` for the edit, `l` `d` to break the line, `u` to mend it) and still replays to equal text, tapes and screenshots, its seeks moved to the marks' new times. zig build test: 81 of 81 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 11 +++- examples/live/build.zig.zon | 8 ++- examples/live/src/main.zig | 122 ++++++++++++++++++++++++++++++----------- examples/live/test/live.script | 39 ++++++++----- 4 files changed, 132 insertions(+), 48 deletions(-) diff --git a/examples/live/build.zig b/examples/live/build.zig index 9d0983d..ccfd32d 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -30,7 +30,16 @@ pub fn build(b: *std.Build) void { .{ .name = "scope", .module = sb.modules.scope }, }; - const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports }); + // The editor: helix's keys over a value kept in State. Its tree-sitter + // set is the smallest there is (Zig's grammar), as the language's own + // grammar is handed in at run time. + const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .zig, .editor = true }); + const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = &.{ + .{ .name = "Lang", .module = lang }, + .{ .name = "shaders", .module = shaders.module }, + .{ .name = "scope", .module = sb.modules.scope }, + .{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }, + } }); const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); diff --git a/examples/live/build.zig.zon b/examples/live/build.zig.zon index 27bd831..d5d265a 100644 --- a/examples/live/build.zig.zon +++ b/examples/live/build.zig.zon @@ -2,7 +2,13 @@ .name = .live, .version = "0.0.1", .dependencies = .{ - .sketchbook = .{ .path = "../.." }, + // `../..` spelled as pardes spells it (its `../sketchbook`): a + // package is told by its path's spelling, and one file cannot be + // in two. + .sketchbook = .{ .path = "../../../sketchbook" }, + // pardes's editor as a library (`pardes-editor`), from the checkout + // beside sketchbook's. + .pardes = .{ .path = "../../../02-pardes-code" }, }, .minimum_zig_version = "0.17.0", .paths = .{""}, diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 965152a..e62522f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -18,6 +18,7 @@ const gpu = sk.abi.gpu; const Lang = @import("Lang"); const shaders = @import("shaders"); const scope = @import("scope"); +const pe = @import("pardes-editor"); pub const name = "live"; pub const width = 1280; @@ -31,11 +32,15 @@ const stream = 1; /// The most samples synthesised in one frame. const max_block = 8192; +/// The text: pardes's editor, helix's keys over a value of plain data +/// (src/lib_editor.zig in pardes), its capacities sized to the language's +/// 4 KiB of text. The undo ring keeps whole texts, the oldest dropped: four +/// at the most text, hundreds of a short program. 39,264 bytes in all, of a +/// State of 43,480. +pub const Editor = pe.Editor(.{ .text = 4096, .undo = 16 * 1024, .registers = 1024, .selections = 64 }); + pub const State = struct { - // ponytail: a plain buffer, typed characters appended and backspace - // deleting the last; part 2 puts pardes's editor here. - text: [4096]u8 = @splat(0), - len: u32 = 0, + ed: Editor = .{}, /// The last program that parsed. out: Lang.Tape = .{}, color: Lang.Tape = .{}, @@ -72,35 +77,45 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool { return true; } +/// What the editor thaws its value into for each call, reset after it: +/// `update` may not allocate from the host, and a fixed buffer keeps a +/// call deterministic and bounded. A key at the most text took 29 KiB of it. +var scratch: [128 * 1024]u8 = undefined; + pub fn update(s: *State, in: *const sk.Input) void { - var edited = false; - for (in.events) |e| switch (e.kind) { - .tick => s.time += e.x, - .text => { - var utf8: [4]u8 = undefined; - const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; - if (s.len + n > s.text.len) continue; - @memcpy(s.text[s.len..][0..n], utf8[0..n]); - s.len += n; - edited = true; - }, - .key_down => switch (e.key()) { - .backspace => if (s.len > 0) { - s.len -= 1; - while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1; - edited = true; + const revision = s.ed.revision; + // a chord's key_down may come with its letter as text too: not typed + var chord = false; + for (in.events) |e| { + var utf8: [4]u8 = undefined; + const k: pe.Key = switch (e.kind) { + .tick => { + s.time += e.x; + continue; }, - .@"return", .keypad_enter => if (s.len < s.text.len) { - s.text[s.len] = '\n'; - s.len += 1; - edited = true; + .text => text: { + const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; + if (chord) { + chord = false; + continue; + } + break :text .{ .cp = @intCast(@min(e.value, 0x10ffff)), .text = utf8[0..n] }; }, - else => {}, - }, - else => {}, - }; - if (!edited) return; - switch (Lang.compile(s.text[0..s.len])) { + .key_down => chord: { + const k = key(e) orelse continue; + chord = (k.ctrl or k.alt) and k.cp >= ' ' and k.cp < 0x7f; + break :chord k; + }, + else => continue, + }; + 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(), null, k); + } + if (s.ed.revision == revision) return; + var text: [Editor.capacity.text]u8 = undefined; + switch (Lang.compile(s.ed.read(&text))) { .program => |p| { s.out = p.out; s.color = p.color; @@ -110,6 +125,51 @@ pub fn update(s: *State, in: *const sk.Input) void { } } +/// A key_down as the editor takes it: the specials, and chords (ctrl or alt +/// with a letter, digit or sign), helix's `A-I` an upper-case letter with +/// alt. A printable without a chord comes as `text`, so it is not one here. +fn key(e: sk.Event) ?pe.Key { + const mods = e.value; + const ctrl = mods & sk.Key.Mod.ctrl != 0; + const alt = mods & sk.Key.Mod.alt != 0; + const shift = mods & sk.Key.Mod.shift != 0; + const special: u21 = switch (e.key()) { + .escape => pe.Key.escape, + .@"return", .keypad_enter, .return2 => pe.Key.enter, + .backspace => pe.Key.backspace, + .tab => pe.Key.tab, + .delete => pe.Key.delete, + .left => pe.Key.left, + .right => pe.Key.right, + .up => pe.Key.up, + .down => pe.Key.down, + .home => pe.Key.home, + .end => pe.Key.end, + .page_up => pe.Key.page_up, + .page_down => pe.Key.page_down, + else => 0, + }; + if (special != 0) return .{ .cp = special, .ctrl = ctrl, .alt = alt, .shift = shift }; + if (!ctrl and !alt) return null; + const scan = @backingInt(e.key()); + const cp: u21 = switch (e.key()) { + .a, .b, .c, .d, .e, .f, .g, .h, .i, .j, .k, .l, .m, .n, .o, .p, .q, .r, .s, .t, .u, .v, .w, .x, .y, .z => @as(u21, if (shift) 'A' else 'a') + scan - @backingInt(sk.Key.a), + .@"1", .@"2", .@"3", .@"4", .@"5", .@"6", .@"7", .@"8", .@"9" => '1' + @as(u21, scan - @backingInt(sk.Key.@"1")), + .@"0" => '0', + .space => ' ', + .minus => '-', + .equals => '=', + .left_bracket => '[', + .right_bracket => ']', + .semicolon => ';', + .comma => ',', + .period => '.', + .slash => '/', + else => return null, + }; + return .{ .cp = cp, .ctrl = ctrl, .alt = alt }; +} + const synth: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 }; const picture_pipeline: gpu.Pipeline = .{ .vertex = .{ .code = shaders.fullscreen_vert }, @@ -220,7 +280,7 @@ const red = 0xff5a50; // ponytail: one face at one size, so a column is a fixed advance; part 2 // draws the editor's text with sketchbook's new text instead. fn code(s: *const State, f: *sk.Frame) void { - const text = s.text[0..s.len]; + const text = s.ed.bytes(f.scratch) catch return; const cell = f.host.textSizeIn(.code, scale, "M"); const lines = std.mem.count(u8, text, "\n") + 1; var widest: f32 = 0; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index 695d16a..ecf3d77 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -1,9 +1,12 @@ -# A program typed, its tapes after each line, a broken line and its mend; -# then this run's session played back, and sought to two moments marked as -# they happened. (A mark ends a clip, `record`, so the session is replayed.) +# A program typed through the editor (helix's keys: `i`, text, Escape, +# motions), its tapes after each line, a broken line and its mend (an +# undo); then this run's session played back, and sought to two moments +# marked as they happened. (A mark ends a clip, `record`, so the session is +# replayed.) # Screenshots land in .zig-cache/live/ for review; they are compared over # the picture alone (640,300 600x300: not the text, the scope or the time). frames 2 +type i type out = sin(tau * 220 * t) * 0.3 expect problem.len 0 expect out.len 8 @@ -17,45 +20,51 @@ type color = hsv(x, 0.5, y) expect color.len 4 expect color.code[0].op x expect color.code[3].op hsv +key escape +expect ed.cursor.mode normal frames 2 console save typed screenshot .zig-cache/live/gradient.png -# An edit: y becomes x, and the picture changes. -key backspace -key backspace -type x) +# An edit: y becomes x (to the line's end, a character left, replaced), +# and the picture changes. +type gl +type h +type rx expect color.len 3 expect color.code[2].op hsv expect color.code[2].c 0 frames 2 screenshot .zig-cache/live/edited.png expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 300 -# Broken: the tape and the picture stay, the problem is set at its byte. -key backspace +# Broken: the ) deleted; the tape and the picture stay, the problem is set +# at its byte. +type l +type d expect problem.at 52 expect color.len 3 expect color.code[2].c 0 frames 2 screenshot .zig-cache/live/broken.png expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 300 -# Mended. -type ) +# Mended: undone. +type u expect problem.len 0 expect color.len 3 console save end # Played back from the start, then sought: each moment's text, tapes and # time as they were live, leaf by leaf (a mark is taken before its frame's -# tick: 0.2667 s and 0.4833 s; a seek lands on the last frame at or before). +# tick: 0.3 s and 0.5333 s; a seek lands on the last frame at or before). console play session 0 frames 100 -console seek 488 +console seek 538 expect-snapshot end -console seek 272 +console seek 305 expect-snapshot typed expect color.len 4 screenshot .zig-cache/live/sought.png expect-same-screenshot .zig-cache/live/gradient.png .zig-cache/live/sought.png 0 0 0 1000 600 # Live again from there: the typed program, before its edit. console stop -expect len 53 +expect ed.front 53 +expect ed.back 0 expect color.code[3].op hsv -- cgit v1.3 From fad609ddb13cbc0f33150fc3a9dfc25051641cd8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:48:52 -0300 Subject: examples/live: the editor's text drawn, its ranges, caret and mode One function, `code`, draws what the editor holds over the picture on a dark band: its lines, every range under its bytes (the primary stronger than the rest), a caret at each range's head that says the mode (a block in normal and select mode, a bar in insert mode, the primary's brighter), and the mode's name (NOR, INS, SEL) in the corner before the replay's state and the time; the problem as before, a red mark under its byte and its words under the text. One face at one size, so a byte of this ASCII language is a column of a fixed advance: marked ponytail as the place sketchbook's paragraph call (spans, any byte's place) takes. zig build test: 81 of 81 steps, 6 of 6 tests (test/live.script's screenshots compare the picture, and the sought moment's code band with the live one's). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/src/main.zig | 62 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index e62522f..ca4d0be 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -274,36 +274,56 @@ const scale = 1.25; const margin = 16; const ink = 0xe8e8e8; const red = 0xff5a50; +const selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 170 }; +const also_selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 90 }; -/// The text over the picture on a dark band, the caret at its end, and the -/// problem: a red mark under its byte and its words under the text. -// ponytail: one face at one size, so a column is a fixed advance; part 2 -// draws the editor's text with sketchbook's new text instead. +/// The editor's text over the picture on a dark band: its lines, every +/// range (the primary stronger), a caret at each range's head that says +/// the mode (a block in normal and select, a bar in insert), the mode's +/// name in the corner; the problem as a red mark under its byte and its +/// words under the text. +// ponytail: one face at one size, a column a fixed advance and a byte of +// this ASCII language a column; sketchbook's paragraph call (spans, any +// byte's place) takes the place of the measuring and the textIn here. fn code(s: *const State, f: *sk.Frame) void { const text = s.ed.bytes(f.scratch) catch return; + const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return; + const got = s.ed.ranges(f.scratch, ranges) catch return; const cell = f.host.textSizeIn(.code, scale, "M"); - const lines = std.mem.count(u8, text, "\n") + 1; - var widest: f32 = 0; - var it = std.mem.splitScalar(u8, text, '\n'); - while (it.next()) |line| widest = @max(widest, f.host.textSizeIn(.code, scale, line)[0]); const problem = s.problem.len > 0; - if (problem) widest = @max(widest, f.host.textSizeIn(.code, scale, s.problem.text())[0]); - const band_h = @as(f32, @floatFromInt(lines + @intFromBool(problem))) * cell[1] + 2 * margin; - f.fill(.{ .x = 0, .y = 0, .w = @max(widest, 24 * cell[0]) + 2 * margin + cell[0], .h = band_h }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 }); + var lines: usize = 1; + var widest: usize = 24; + var it = std.mem.splitScalar(u8, text, '\n'); + while (it.next()) |line| : (lines += 1) widest = @max(widest, line.len + 1); + if (problem) widest = @max(widest, s.problem.len); + f.fill(.{ .x = 0, .y = 0, .w = @as(f32, @floatFromInt(widest)) * cell[0] + 2 * margin, .h = @as(f32, @floatFromInt(lines - 1 + @intFromBool(problem))) * cell[1] + 2 * margin }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 }); var y: f32 = margin; var start: usize = 0; it = std.mem.splitScalar(u8, text, '\n'); while (it.next()) |line| : (y += cell[1]) { + // a line's bytes and its newline, start..end + const end = start + line.len + 1; + for (ranges[0..got.n], 0..) |r, i| { + const from = @max(@min(r.anchor, r.head), start); + const to = @min(@max(r.anchor, r.head), end); + if (from < to) f.fill(.{ .x = margin + @as(f32, @floatFromInt(from - start)) * cell[0], .y = y, .w = @as(f32, @floatFromInt(to - from)) * cell[0], .h = cell[1] }, if (i == got.primary) selected else also_selected); + } f.textIn(.code, margin, y, scale, line, ink); - const end = start + line.len; - if (problem and s.problem.at >= start and s.problem.at <= end) { - const x = margin + f.host.textSizeIn(.code, scale, text[start..s.problem.at])[0]; - f.fill(.{ .x = x, .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red); + for (ranges[0..got.n], 0..) |r, i| { + // the head's character: the one before it in a range that + // grows forward + const at = if (r.head > r.anchor) r.head - 1 else r.head; + if (at < start or at >= end) continue; + const x = margin + @as(f32, @floatFromInt(at - start)) * cell[0]; + const caret: sk.Color = .{ .r = 0xe8, .g = 0xe8, .b = 0xe8, .a = if (i == got.primary) 200 else 110 }; + if (s.ed.mode() == .insert) { + f.fill(.{ .x = x - 1, .y = y, .w = 2, .h = cell[1] }, caret); + } else f.fill(.{ .x = x, .y = y, .w = cell[0], .h = cell[1] }, caret.alpha(caret.a / 2)); } - // The caret, after the last byte. - if (end == text.len) f.fill(.{ .x = margin + f.host.textSizeIn(.code, scale, line)[0], .y = y, .w = 2, .h = cell[1] }, ink); - start = end + 1; + if (problem and s.problem.at >= start and s.problem.at < end) + f.fill(.{ .x = margin + @as(f32, @floatFromInt(s.problem.at - start)) * cell[0], .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red); + start = end; } if (problem) f.textIn(.code, margin, y, scale, s.problem.text(), red); @@ -314,5 +334,9 @@ fn code(s: *const State, f: *sk.Frame) void { .paused => "paused", }; const lead = @as(f32, @floatFromInt(f.in.lead)) / @as(f32, @floatFromInt(f.in.sample_rate)) * 1000; - f.print(width - 260, 12, 1, ink, "{s} {d:.1} s lead {d:.1} ms", .{ mode, s.time, lead }); + f.print(width - 300, 12, 1, ink, "{s} {s} {d:.1} s lead {d:.1} ms", .{ switch (s.ed.mode()) { + .normal => "NOR", + .insert => "INS", + .select => "SEL", + }, mode, s.time, lead }); } -- cgit v1.3 From f37732448a2a374281c9610d05a10315e97dcbbf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:51:24 -0300 Subject: examples/live: the language's grammar, its colours and a new line's indent grammar/grammar.js is the language as src/Lang.zig parses it, for tree-sitter (24 lines): lines of `name = expression` parted by newlines, `+ -` below `* /` below unary `-`, calls, parentheses, numbers (Lang's digits, point and `e`), names, `#` comments as extras. `tree-sitter generate` (tree-sitter 0.26.9; ABI 14, the CLI's without a tree-sitter.json) wrote grammar/src/parser.c (918 lines) and its tree_sitter/ headers, committed: the build compiles them into the sketch as a static library and never runs the generator. The `Language` is handed to the editor at run time (`tree_sitter_live`, queries/highlights.scm and queries/indents.scm embedded). The parse is a module's `Syntax`, kept between calls so Alt-i goes back the way Alt-o came, handed to every key, forgotten at a jump (in `draw`, `in.jumped`) and started again by a reload (the tree a reload leaves is leaked: marked ponytail). `code` colours each run of bytes by `Syntax.colors` in pardes's own theme (orchard, the first): a defined name as a keyword, a called builtin as a string, numbers and comments as theirs, the rest in the theme's ink. indents.scm gives a new line no indent outside an open call or parenthesis: pardes's plain indent is a level more after `)`, so every line of this language that ends in a call pushed the next one in. The library now reads a handed-in language's indent query (pardes, its own change). Test: test/grammar.zig parses with the grammar and compiles with Lang.zig 22 texts: the survey's program and two others both take; eleven broken lines both refuse (an unclosed or extra parenthesis, a missing operand or `=`, two numbers, `1.2.3`, a stray `$`); eight both parse but only Lang.zig refuses, as only it knows names, builtins, arities and where a colour may stand (a name used before it is defined or defined twice, a builtin redefined or called with too few arguments, an unknown call, a colour outside the color line, a name called). Lang.zig's survey is public for it. zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 11 +- examples/live/grammar/grammar.js | 24 + examples/live/grammar/src/parser.c | 918 +++++++++++++++++++++++++ examples/live/grammar/src/tree_sitter/alloc.h | 54 ++ examples/live/grammar/src/tree_sitter/array.h | 330 +++++++++ examples/live/grammar/src/tree_sitter/parser.h | 286 ++++++++ examples/live/queries/highlights.scm | 6 + examples/live/queries/indents.scm | 5 + examples/live/src/Lang.zig | 5 +- examples/live/src/main.zig | 37 +- examples/live/test/grammar.zig | 54 ++ 11 files changed, 1724 insertions(+), 6 deletions(-) create mode 100644 examples/live/grammar/grammar.js create mode 100644 examples/live/grammar/src/parser.c create mode 100644 examples/live/grammar/src/tree_sitter/alloc.h create mode 100644 examples/live/grammar/src/tree_sitter/array.h create mode 100644 examples/live/grammar/src/tree_sitter/parser.h create mode 100644 examples/live/queries/highlights.scm create mode 100644 examples/live/queries/indents.scm create mode 100644 examples/live/test/grammar.zig diff --git a/examples/live/build.zig b/examples/live/build.zig index ccfd32d..a07fe7d 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -40,7 +40,16 @@ pub fn build(b: *std.Build) void { .{ .name = "scope", .module = sb.modules.scope }, .{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }, } }); - const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")} }); + // The language's tree-sitter grammar: grammar/grammar.js and the + // parser.c generated from it, committed (the build never runs the + // generator); its colours and a new line's indent, queries/. + const grammar = b.addLibrary(.{ .name = "tree-sitter-live", .linkage = .static, .root_module = b.createModule(.{ .target = target, .optimize = .ReleaseFast, .link_libc = true, .pic = true }) }); + grammar.root_module.addCSourceFile(.{ .file = b.path("grammar/src/parser.c") }); + grammar.root_module.addIncludePath(b.path("grammar/src")); + app.module.linkLibrary(grammar); + app.module.addAnonymousImport("highlights.scm", .{ .root_source_file = b.path("queries/highlights.scm") }); + app.module.addAnonymousImport("indents.scm", .{ .root_source_file = b.path("queries/indents.scm") }); + const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")}, .tests = &.{b.path("test/grammar.zig")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); diff --git a/examples/live/grammar/grammar.js b/examples/live/grammar/grammar.js new file mode 100644 index 0000000..31f50b1 --- /dev/null +++ b/examples/live/grammar/grammar.js @@ -0,0 +1,24 @@ +// The language as src/Lang.zig parses it, for tree-sitter: lines of +// `name = expression`, `+ -` below `* /` below unary `-`, calls, numbers, +// names, `#` comments. `tree-sitter generate` here writes src/parser.c, +// which is committed: the build compiles it and never runs the generator. +module.exports = grammar({ + name: 'live', + extras: $ => [/[ \t\r]/, $.comment], + word: $ => $.name, + rules: { + source_file: $ => seq(optional($.definition), repeat(seq('\n', optional($.definition)))), + definition: $ => seq(field('name', $.name), '=', field('value', $._expression)), + _expression: $ => choice($.binary, $.unary, $.call, $.parenthesized, $.number, $.name), + binary: $ => choice( + prec.left(1, seq(field('left', $._expression), field('operator', choice('+', '-')), field('right', $._expression))), + prec.left(2, seq(field('left', $._expression), field('operator', choice('*', '/')), field('right', $._expression))), + ), + unary: $ => prec(3, seq('-', field('operand', $._expression))), + call: $ => seq(field('function', $.name), '(', optional(seq($._expression, repeat(seq(',', $._expression)))), ')'), + parenthesized: $ => seq('(', $._expression, ')'), + number: $ => /(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?/, + name: $ => /[a-zA-Z_][a-zA-Z0-9_]*/, + comment: $ => /#[^\n]*/, + }, +}); diff --git a/examples/live/grammar/src/parser.c b/examples/live/grammar/src/parser.c new file mode 100644 index 0000000..e694827 --- /dev/null +++ b/examples/live/grammar/src/parser.c @@ -0,0 +1,918 @@ +/* Automatically @generated by tree-sitter */ + +#include "tree_sitter/parser.h" + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +#define LANGUAGE_VERSION 14 +#define STATE_COUNT 31 +#define LARGE_STATE_COUNT 2 +#define SYMBOL_COUNT 22 +#define ALIAS_COUNT 0 +#define TOKEN_COUNT 13 +#define EXTERNAL_TOKEN_COUNT 0 +#define FIELD_COUNT 7 +#define MAX_ALIAS_SEQUENCE_LENGTH 5 +#define MAX_RESERVED_WORD_SET_SIZE 0 +#define PRODUCTION_ID_COUNT 5 +#define SUPERTYPE_COUNT 0 + +enum ts_symbol_identifiers { + sym_name = 1, + anon_sym_LF = 2, + anon_sym_EQ = 3, + anon_sym_PLUS = 4, + anon_sym_DASH = 5, + anon_sym_STAR = 6, + anon_sym_SLASH = 7, + anon_sym_LPAREN = 8, + anon_sym_COMMA = 9, + anon_sym_RPAREN = 10, + sym_number = 11, + sym_comment = 12, + sym_source_file = 13, + sym_definition = 14, + sym__expression = 15, + sym_binary = 16, + sym_unary = 17, + sym_call = 18, + sym_parenthesized = 19, + aux_sym_source_file_repeat1 = 20, + aux_sym_call_repeat1 = 21, +}; + +static const char * const ts_symbol_names[] = { + [ts_builtin_sym_end] = "end", + [sym_name] = "name", + [anon_sym_LF] = "\n", + [anon_sym_EQ] = "=", + [anon_sym_PLUS] = "+", + [anon_sym_DASH] = "-", + [anon_sym_STAR] = "*", + [anon_sym_SLASH] = "/", + [anon_sym_LPAREN] = "(", + [anon_sym_COMMA] = ",", + [anon_sym_RPAREN] = ")", + [sym_number] = "number", + [sym_comment] = "comment", + [sym_source_file] = "source_file", + [sym_definition] = "definition", + [sym__expression] = "_expression", + [sym_binary] = "binary", + [sym_unary] = "unary", + [sym_call] = "call", + [sym_parenthesized] = "parenthesized", + [aux_sym_source_file_repeat1] = "source_file_repeat1", + [aux_sym_call_repeat1] = "call_repeat1", +}; + +static const TSSymbol ts_symbol_map[] = { + [ts_builtin_sym_end] = ts_builtin_sym_end, + [sym_name] = sym_name, + [anon_sym_LF] = anon_sym_LF, + [anon_sym_EQ] = anon_sym_EQ, + [anon_sym_PLUS] = anon_sym_PLUS, + [anon_sym_DASH] = anon_sym_DASH, + [anon_sym_STAR] = anon_sym_STAR, + [anon_sym_SLASH] = anon_sym_SLASH, + [anon_sym_LPAREN] = anon_sym_LPAREN, + [anon_sym_COMMA] = anon_sym_COMMA, + [anon_sym_RPAREN] = anon_sym_RPAREN, + [sym_number] = sym_number, + [sym_comment] = sym_comment, + [sym_source_file] = sym_source_file, + [sym_definition] = sym_definition, + [sym__expression] = sym__expression, + [sym_binary] = sym_binary, + [sym_unary] = sym_unary, + [sym_call] = sym_call, + [sym_parenthesized] = sym_parenthesized, + [aux_sym_source_file_repeat1] = aux_sym_source_file_repeat1, + [aux_sym_call_repeat1] = aux_sym_call_repeat1, +}; + +static const TSSymbolMetadata ts_symbol_metadata[] = { + [ts_builtin_sym_end] = { + .visible = false, + .named = true, + }, + [sym_name] = { + .visible = true, + .named = true, + }, + [anon_sym_LF] = { + .visible = true, + .named = false, + }, + [anon_sym_EQ] = { + .visible = true, + .named = false, + }, + [anon_sym_PLUS] = { + .visible = true, + .named = false, + }, + [anon_sym_DASH] = { + .visible = true, + .named = false, + }, + [anon_sym_STAR] = { + .visible = true, + .named = false, + }, + [anon_sym_SLASH] = { + .visible = true, + .named = false, + }, + [anon_sym_LPAREN] = { + .visible = true, + .named = false, + }, + [anon_sym_COMMA] = { + .visible = true, + .named = false, + }, + [anon_sym_RPAREN] = { + .visible = true, + .named = false, + }, + [sym_number] = { + .visible = true, + .named = true, + }, + [sym_comment] = { + .visible = true, + .named = true, + }, + [sym_source_file] = { + .visible = true, + .named = true, + }, + [sym_definition] = { + .visible = true, + .named = true, + }, + [sym__expression] = { + .visible = false, + .named = true, + }, + [sym_binary] = { + .visible = true, + .named = true, + }, + [sym_unary] = { + .visible = true, + .named = true, + }, + [sym_call] = { + .visible = true, + .named = true, + }, + [sym_parenthesized] = { + .visible = true, + .named = true, + }, + [aux_sym_source_file_repeat1] = { + .visible = false, + .named = false, + }, + [aux_sym_call_repeat1] = { + .visible = false, + .named = false, + }, +}; + +enum ts_field_identifiers { + field_function = 1, + field_left = 2, + field_name = 3, + field_operand = 4, + field_operator = 5, + field_right = 6, + field_value = 7, +}; + +static const char * const ts_field_names[] = { + [0] = NULL, + [field_function] = "function", + [field_left] = "left", + [field_name] = "name", + [field_operand] = "operand", + [field_operator] = "operator", + [field_right] = "right", + [field_value] = "value", +}; + +static const TSMapSlice ts_field_map_slices[PRODUCTION_ID_COUNT] = { + [1] = {.index = 0, .length = 2}, + [2] = {.index = 2, .length = 1}, + [3] = {.index = 3, .length = 1}, + [4] = {.index = 4, .length = 3}, +}; + +static const TSFieldMapEntry ts_field_map_entries[] = { + [0] = + {field_name, 0}, + {field_value, 2}, + [2] = + {field_operand, 1}, + [3] = + {field_function, 0}, + [4] = + {field_left, 0}, + {field_operator, 1}, + {field_right, 2}, +}; + +static const TSSymbol ts_alias_sequences[PRODUCTION_ID_COUNT][MAX_ALIAS_SEQUENCE_LENGTH] = { + [0] = {0}, +}; + +static const uint16_t ts_non_terminal_alias_map[] = { + 0, +}; + +static const TSStateId ts_primary_state_ids[STATE_COUNT] = { + [0] = 0, + [1] = 1, + [2] = 2, + [3] = 3, + [4] = 4, + [5] = 5, + [6] = 6, + [7] = 7, + [8] = 8, + [9] = 9, + [10] = 10, + [11] = 11, + [12] = 12, + [13] = 13, + [14] = 14, + [15] = 15, + [16] = 16, + [17] = 17, + [18] = 18, + [19] = 19, + [20] = 20, + [21] = 21, + [22] = 22, + [23] = 23, + [24] = 24, + [25] = 25, + [26] = 26, + [27] = 27, + [28] = 28, + [29] = 29, + [30] = 30, +}; + +static bool ts_lex(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + if (eof) ADVANCE(4); + ADVANCE_MAP( + '\n', 5, + '#', 18, + '(', 11, + ')', 13, + '*', 9, + '+', 7, + ',', 12, + '-', 8, + '.', 2, + '/', 10, + '=', 6, + ); + if (lookahead == '\t' || + lookahead == '\r' || + lookahead == ' ') SKIP(0); + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(14); + if (('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(17); + END_STATE(); + case 1: + if (lookahead == '+' || + lookahead == '-') ADVANCE(3); + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(16); + END_STATE(); + case 2: + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(15); + END_STATE(); + case 3: + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(16); + END_STATE(); + case 4: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + case 5: + ACCEPT_TOKEN(anon_sym_LF); + END_STATE(); + case 6: + ACCEPT_TOKEN(anon_sym_EQ); + END_STATE(); + case 7: + ACCEPT_TOKEN(anon_sym_PLUS); + END_STATE(); + case 8: + ACCEPT_TOKEN(anon_sym_DASH); + END_STATE(); + case 9: + ACCEPT_TOKEN(anon_sym_STAR); + END_STATE(); + case 10: + ACCEPT_TOKEN(anon_sym_SLASH); + END_STATE(); + case 11: + ACCEPT_TOKEN(anon_sym_LPAREN); + END_STATE(); + case 12: + ACCEPT_TOKEN(anon_sym_COMMA); + END_STATE(); + case 13: + ACCEPT_TOKEN(anon_sym_RPAREN); + END_STATE(); + case 14: + ACCEPT_TOKEN(sym_number); + if (lookahead == '.') ADVANCE(15); + if (lookahead == 'e') ADVANCE(1); + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(14); + END_STATE(); + case 15: + ACCEPT_TOKEN(sym_number); + if (lookahead == 'e') ADVANCE(1); + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(15); + END_STATE(); + case 16: + ACCEPT_TOKEN(sym_number); + if (('0' <= lookahead && lookahead <= '9')) ADVANCE(16); + END_STATE(); + case 17: + ACCEPT_TOKEN(sym_name); + if (('0' <= lookahead && lookahead <= '9') || + ('A' <= lookahead && lookahead <= 'Z') || + lookahead == '_' || + ('a' <= lookahead && lookahead <= 'z')) ADVANCE(17); + END_STATE(); + case 18: + ACCEPT_TOKEN(sym_comment); + if (lookahead != 0 && + lookahead != '\n') ADVANCE(18); + END_STATE(); + default: + return false; + } +} + +static bool ts_lex_keywords(TSLexer *lexer, TSStateId state) { + START_LEXER(); + eof = lexer->eof(lexer); + switch (state) { + case 0: + ACCEPT_TOKEN(ts_builtin_sym_end); + END_STATE(); + default: + return false; + } +} + +static const TSLexMode ts_lex_modes[STATE_COUNT] = { + [0] = {.lex_state = 0}, + [1] = {.lex_state = 0}, + [2] = {.lex_state = 0}, + [3] = {.lex_state = 0}, + [4] = {.lex_state = 0}, + [5] = {.lex_state = 0}, + [6] = {.lex_state = 0}, + [7] = {.lex_state = 0}, + [8] = {.lex_state = 0}, + [9] = {.lex_state = 0}, + [10] = {.lex_state = 0}, + [11] = {.lex_state = 0}, + [12] = {.lex_state = 0}, + [13] = {.lex_state = 0}, + [14] = {.lex_state = 0}, + [15] = {.lex_state = 0}, + [16] = {.lex_state = 0}, + [17] = {.lex_state = 0}, + [18] = {.lex_state = 0}, + [19] = {.lex_state = 0}, + [20] = {.lex_state = 0}, + [21] = {.lex_state = 0}, + [22] = {.lex_state = 0}, + [23] = {.lex_state = 0}, + [24] = {.lex_state = 0}, + [25] = {.lex_state = 0}, + [26] = {.lex_state = 0}, + [27] = {.lex_state = 0}, + [28] = {.lex_state = 0}, + [29] = {.lex_state = 0}, + [30] = {.lex_state = 0}, +}; + +static const uint16_t ts_parse_table[LARGE_STATE_COUNT][SYMBOL_COUNT] = { + [STATE(0)] = { + [ts_builtin_sym_end] = ACTIONS(1), + [sym_name] = ACTIONS(1), + [anon_sym_LF] = ACTIONS(1), + [anon_sym_EQ] = ACTIONS(1), + [anon_sym_PLUS] = ACTIONS(1), + [anon_sym_DASH] = ACTIONS(1), + [anon_sym_STAR] = ACTIONS(1), + [anon_sym_SLASH] = ACTIONS(1), + [anon_sym_LPAREN] = ACTIONS(1), + [anon_sym_COMMA] = ACTIONS(1), + [anon_sym_RPAREN] = ACTIONS(1), + [sym_number] = ACTIONS(1), + [sym_comment] = ACTIONS(3), + }, + [STATE(1)] = { + [sym_source_file] = STATE(30), + [sym_definition] = STATE(22), + [aux_sym_source_file_repeat1] = STATE(23), + [ts_builtin_sym_end] = ACTIONS(5), + [sym_name] = ACTIONS(7), + [anon_sym_LF] = ACTIONS(9), + [sym_comment] = ACTIONS(3), + }, +}; + +static const uint16_t ts_small_parse_table[] = { + [0] = 7, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(17), 1, + anon_sym_RPAREN, + ACTIONS(19), 1, + sym_number, + STATE(17), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [26] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(21), 1, + sym_number, + STATE(10), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [49] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(23), 1, + sym_number, + STATE(18), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [72] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(25), 1, + sym_number, + STATE(20), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [95] = 3, + ACTIONS(3), 1, + sym_comment, + ACTIONS(29), 1, + anon_sym_LPAREN, + ACTIONS(27), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [112] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(31), 1, + sym_number, + STATE(13), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [135] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(33), 1, + sym_number, + STATE(19), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [158] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(11), 1, + sym_name, + ACTIONS(13), 1, + anon_sym_DASH, + ACTIONS(15), 1, + anon_sym_LPAREN, + ACTIONS(35), 1, + sym_number, + STATE(14), 5, + sym__expression, + sym_binary, + sym_unary, + sym_call, + sym_parenthesized, + [181] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(37), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [195] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(39), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [209] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(41), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [223] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(43), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [237] = 3, + ACTIONS(3), 1, + sym_comment, + ACTIONS(45), 2, + anon_sym_STAR, + anon_sym_SLASH, + ACTIONS(43), 6, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [253] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(47), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [267] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(49), 8, + ts_builtin_sym_end, + anon_sym_LF, + anon_sym_PLUS, + anon_sym_DASH, + anon_sym_STAR, + anon_sym_SLASH, + anon_sym_COMMA, + anon_sym_RPAREN, + [281] = 6, + ACTIONS(3), 1, + sym_comment, + ACTIONS(53), 1, + anon_sym_COMMA, + ACTIONS(55), 1, + anon_sym_RPAREN, + STATE(26), 1, + aux_sym_call_repeat1, + ACTIONS(45), 2, + anon_sym_STAR, + anon_sym_SLASH, + ACTIONS(51), 2, + anon_sym_PLUS, + anon_sym_DASH, + [302] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(45), 2, + anon_sym_STAR, + anon_sym_SLASH, + ACTIONS(51), 2, + anon_sym_PLUS, + anon_sym_DASH, + ACTIONS(57), 2, + ts_builtin_sym_end, + anon_sym_LF, + [318] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(45), 2, + anon_sym_STAR, + anon_sym_SLASH, + ACTIONS(51), 2, + anon_sym_PLUS, + anon_sym_DASH, + ACTIONS(59), 2, + anon_sym_COMMA, + anon_sym_RPAREN, + [334] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(61), 1, + anon_sym_RPAREN, + ACTIONS(45), 2, + anon_sym_STAR, + anon_sym_SLASH, + ACTIONS(51), 2, + anon_sym_PLUS, + anon_sym_DASH, + [349] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(7), 1, + sym_name, + STATE(28), 1, + sym_definition, + ACTIONS(63), 2, + ts_builtin_sym_end, + anon_sym_LF, + [363] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(9), 1, + anon_sym_LF, + ACTIONS(65), 1, + ts_builtin_sym_end, + STATE(24), 1, + aux_sym_source_file_repeat1, + [376] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(9), 1, + anon_sym_LF, + ACTIONS(65), 1, + ts_builtin_sym_end, + STATE(25), 1, + aux_sym_source_file_repeat1, + [389] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(9), 1, + anon_sym_LF, + ACTIONS(67), 1, + ts_builtin_sym_end, + STATE(25), 1, + aux_sym_source_file_repeat1, + [402] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(69), 1, + ts_builtin_sym_end, + ACTIONS(71), 1, + anon_sym_LF, + STATE(25), 1, + aux_sym_source_file_repeat1, + [415] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(53), 1, + anon_sym_COMMA, + ACTIONS(74), 1, + anon_sym_RPAREN, + STATE(27), 1, + aux_sym_call_repeat1, + [428] = 4, + ACTIONS(3), 1, + sym_comment, + ACTIONS(59), 1, + anon_sym_RPAREN, + ACTIONS(76), 1, + anon_sym_COMMA, + STATE(27), 1, + aux_sym_call_repeat1, + [441] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(69), 2, + ts_builtin_sym_end, + anon_sym_LF, + [449] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(79), 1, + anon_sym_EQ, + [456] = 2, + ACTIONS(3), 1, + sym_comment, + ACTIONS(81), 1, + ts_builtin_sym_end, +}; + +static const uint32_t ts_small_parse_table_map[] = { + [SMALL_STATE(2)] = 0, + [SMALL_STATE(3)] = 26, + [SMALL_STATE(4)] = 49, + [SMALL_STATE(5)] = 72, + [SMALL_STATE(6)] = 95, + [SMALL_STATE(7)] = 112, + [SMALL_STATE(8)] = 135, + [SMALL_STATE(9)] = 158, + [SMALL_STATE(10)] = 181, + [SMALL_STATE(11)] = 195, + [SMALL_STATE(12)] = 209, + [SMALL_STATE(13)] = 223, + [SMALL_STATE(14)] = 237, + [SMALL_STATE(15)] = 253, + [SMALL_STATE(16)] = 267, + [SMALL_STATE(17)] = 281, + [SMALL_STATE(18)] = 302, + [SMALL_STATE(19)] = 318, + [SMALL_STATE(20)] = 334, + [SMALL_STATE(21)] = 349, + [SMALL_STATE(22)] = 363, + [SMALL_STATE(23)] = 376, + [SMALL_STATE(24)] = 389, + [SMALL_STATE(25)] = 402, + [SMALL_STATE(26)] = 415, + [SMALL_STATE(27)] = 428, + [SMALL_STATE(28)] = 441, + [SMALL_STATE(29)] = 449, + [SMALL_STATE(30)] = 456, +}; + +static const TSParseActionEntry ts_parse_actions[] = { + [0] = {.entry = {.count = 0, .reusable = false}}, + [1] = {.entry = {.count = 1, .reusable = false}}, RECOVER(), + [3] = {.entry = {.count = 1, .reusable = true}}, SHIFT_EXTRA(), + [5] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 0, 0, 0), + [7] = {.entry = {.count = 1, .reusable = true}}, SHIFT(29), + [9] = {.entry = {.count = 1, .reusable = true}}, SHIFT(21), + [11] = {.entry = {.count = 1, .reusable = true}}, SHIFT(6), + [13] = {.entry = {.count = 1, .reusable = true}}, SHIFT(3), + [15] = {.entry = {.count = 1, .reusable = true}}, SHIFT(5), + [17] = {.entry = {.count = 1, .reusable = true}}, SHIFT(12), + [19] = {.entry = {.count = 1, .reusable = true}}, SHIFT(17), + [21] = {.entry = {.count = 1, .reusable = true}}, SHIFT(10), + [23] = {.entry = {.count = 1, .reusable = true}}, SHIFT(18), + [25] = {.entry = {.count = 1, .reusable = true}}, SHIFT(20), + [27] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym__expression, 1, 0, 0), + [29] = {.entry = {.count = 1, .reusable = true}}, SHIFT(2), + [31] = {.entry = {.count = 1, .reusable = true}}, SHIFT(13), + [33] = {.entry = {.count = 1, .reusable = true}}, SHIFT(19), + [35] = {.entry = {.count = 1, .reusable = true}}, SHIFT(14), + [37] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_unary, 2, 0, 2), + [39] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_parenthesized, 3, 0, 0), + [41] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_call, 3, 0, 3), + [43] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_binary, 3, 0, 4), + [45] = {.entry = {.count = 1, .reusable = true}}, SHIFT(7), + [47] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_call, 4, 0, 3), + [49] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_call, 5, 0, 3), + [51] = {.entry = {.count = 1, .reusable = true}}, SHIFT(9), + [53] = {.entry = {.count = 1, .reusable = true}}, SHIFT(8), + [55] = {.entry = {.count = 1, .reusable = true}}, SHIFT(15), + [57] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_definition, 3, 0, 1), + [59] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_call_repeat1, 2, 0, 0), + [61] = {.entry = {.count = 1, .reusable = true}}, SHIFT(11), + [63] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 1, 0, 0), + [65] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 1, 0, 0), + [67] = {.entry = {.count = 1, .reusable = true}}, REDUCE(sym_source_file, 2, 0, 0), + [69] = {.entry = {.count = 1, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2, 0, 0), + [71] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_source_file_repeat1, 2, 0, 0), SHIFT_REPEAT(21), + [74] = {.entry = {.count = 1, .reusable = true}}, SHIFT(16), + [76] = {.entry = {.count = 2, .reusable = true}}, REDUCE(aux_sym_call_repeat1, 2, 0, 0), SHIFT_REPEAT(8), + [79] = {.entry = {.count = 1, .reusable = true}}, SHIFT(4), + [81] = {.entry = {.count = 1, .reusable = true}}, ACCEPT_INPUT(), +}; + +#ifdef __cplusplus +extern "C" { +#endif +#ifdef TREE_SITTER_HIDE_SYMBOLS +#define TS_PUBLIC +#elif defined(_WIN32) +#define TS_PUBLIC __declspec(dllexport) +#else +#define TS_PUBLIC __attribute__((visibility("default"))) +#endif + +TS_PUBLIC const TSLanguage *tree_sitter_live(void) { + static const TSLanguage language = { + .abi_version = LANGUAGE_VERSION, + .symbol_count = SYMBOL_COUNT, + .alias_count = ALIAS_COUNT, + .token_count = TOKEN_COUNT, + .external_token_count = EXTERNAL_TOKEN_COUNT, + .state_count = STATE_COUNT, + .large_state_count = LARGE_STATE_COUNT, + .production_id_count = PRODUCTION_ID_COUNT, + .field_count = FIELD_COUNT, + .max_alias_sequence_length = MAX_ALIAS_SEQUENCE_LENGTH, + .parse_table = &ts_parse_table[0][0], + .small_parse_table = ts_small_parse_table, + .small_parse_table_map = ts_small_parse_table_map, + .parse_actions = ts_parse_actions, + .symbol_names = ts_symbol_names, + .field_names = ts_field_names, + .field_map_slices = ts_field_map_slices, + .field_map_entries = ts_field_map_entries, + .symbol_metadata = ts_symbol_metadata, + .public_symbol_map = ts_symbol_map, + .alias_map = ts_non_terminal_alias_map, + .alias_sequences = &ts_alias_sequences[0][0], + .lex_modes = (const void*)ts_lex_modes, + .lex_fn = ts_lex, + .keyword_lex_fn = ts_lex_keywords, + .keyword_capture_token = sym_name, + .primary_state_ids = ts_primary_state_ids, + }; + return &language; +} +#ifdef __cplusplus +} +#endif diff --git a/examples/live/grammar/src/tree_sitter/alloc.h b/examples/live/grammar/src/tree_sitter/alloc.h new file mode 100644 index 0000000..1abdd12 --- /dev/null +++ b/examples/live/grammar/src/tree_sitter/alloc.h @@ -0,0 +1,54 @@ +#ifndef TREE_SITTER_ALLOC_H_ +#define TREE_SITTER_ALLOC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +// Allow clients to override allocation functions +#ifdef TREE_SITTER_REUSE_ALLOCATOR + +extern void *(*ts_current_malloc)(size_t size); +extern void *(*ts_current_calloc)(size_t count, size_t size); +extern void *(*ts_current_realloc)(void *ptr, size_t size); +extern void (*ts_current_free)(void *ptr); + +#ifndef ts_malloc +#define ts_malloc ts_current_malloc +#endif +#ifndef ts_calloc +#define ts_calloc ts_current_calloc +#endif +#ifndef ts_realloc +#define ts_realloc ts_current_realloc +#endif +#ifndef ts_free +#define ts_free ts_current_free +#endif + +#else + +#ifndef ts_malloc +#define ts_malloc malloc +#endif +#ifndef ts_calloc +#define ts_calloc calloc +#endif +#ifndef ts_realloc +#define ts_realloc realloc +#endif +#ifndef ts_free +#define ts_free free +#endif + +#endif + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ALLOC_H_ diff --git a/examples/live/grammar/src/tree_sitter/array.h b/examples/live/grammar/src/tree_sitter/array.h new file mode 100644 index 0000000..56fc8cd --- /dev/null +++ b/examples/live/grammar/src/tree_sitter/array.h @@ -0,0 +1,330 @@ +#ifndef TREE_SITTER_ARRAY_H_ +#define TREE_SITTER_ARRAY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "./alloc.h" + +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4101) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" +#endif + +#define Array(T) \ + struct { \ + T *contents; \ + uint32_t size; \ + uint32_t capacity; \ + } + +/// Initialize an array. +#define array_init(self) \ + ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) + +/// Create an empty array. +#define array_new() \ + { NULL, 0, 0 } + +/// Get a pointer to the element at a given `index` in the array. +#define array_get(self, _index) \ + (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) + +/// Get a pointer to the first element in the array. +#define array_front(self) array_get(self, 0) + +/// Get a pointer to the last element in the array. +#define array_back(self) array_get(self, (self)->size - 1) + +/// Clear the array, setting its size to zero. Note that this does not free any +/// memory allocated for the array's contents. +#define array_clear(self) ((self)->size = 0) + +/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is +/// less than the array's current capacity, this function has no effect. +#define array_reserve(self, new_capacity) \ + ((self)->contents = _array__reserve( \ + (void *)(self)->contents, &(self)->capacity, \ + array_elem_size(self), new_capacity) \ + ) + +/// Free any memory allocated for this array. Note that this does not free any +/// memory allocated for the array's contents. +#define array_delete(self) \ + do { \ + if ((self)->contents) ts_free((self)->contents); \ + (self)->contents = NULL; \ + (self)->size = 0; \ + (self)->capacity = 0; \ + } while (0) + +/// Push a new `element` onto the end of the array. +#define array_push(self, element) \ + do { \ + (self)->contents = _array__grow( \ + (void *)(self)->contents, (self)->size, &(self)->capacity, \ + 1, array_elem_size(self) \ + ); \ + (self)->contents[(self)->size++] = (element); \ + } while(0) + +/// Increase the array's size by `count` elements. +/// New elements are zero-initialized. +#define array_grow_by(self, count) \ + do { \ + if ((count) == 0) break; \ + (self)->contents = _array__grow( \ + (self)->contents, (self)->size, &(self)->capacity, \ + count, array_elem_size(self) \ + ); \ + memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ + (self)->size += (count); \ + } while (0) + +/// Append all elements from one array to the end of another. +#define array_push_all(self, other) \ + array_extend((self), (other)->size, (other)->contents) + +/// Append `count` elements to the end of the array, reading their values from the +/// `contents` pointer. +#define array_extend(self, count, other_contents) \ + (self)->contents = _array__splice( \ + (void*)(self)->contents, &(self)->size, &(self)->capacity, \ + array_elem_size(self), (self)->size, 0, count, other_contents \ + ) + +/// Remove `old_count` elements from the array starting at the given `index`. At +/// the same index, insert `new_count` new elements, reading their values from the +/// `new_contents` pointer. +#define array_splice(self, _index, old_count, new_count, new_contents) \ + (self)->contents = _array__splice( \ + (void *)(self)->contents, &(self)->size, &(self)->capacity, \ + array_elem_size(self), _index, old_count, new_count, new_contents \ + ) + +/// Insert one `element` into the array at the given `index`. +#define array_insert(self, _index, element) \ + (self)->contents = _array__splice( \ + (void *)(self)->contents, &(self)->size, &(self)->capacity, \ + array_elem_size(self), _index, 0, 1, &(element) \ + ) + +/// Remove one element from the array at the given `index`. +#define array_erase(self, _index) \ + _array__erase((void *)(self)->contents, &(self)->size, array_elem_size(self), _index) + +/// Pop the last element off the array, returning the element by value. +#define array_pop(self) ((self)->contents[--(self)->size]) + +/// Assign the contents of one array to another, reallocating if necessary. +#define array_assign(self, other) \ + (self)->contents = _array__assign( \ + (void *)(self)->contents, &(self)->size, &(self)->capacity, \ + (const void *)(other)->contents, (other)->size, array_elem_size(self) \ + ) + +/// Swap one array with another +#define array_swap(self, other) \ + do { \ + void *_array_swap_tmp = (void *)(self)->contents; \ + (self)->contents = (other)->contents; \ + (other)->contents = _array_swap_tmp; \ + _array__swap(&(self)->size, &(self)->capacity, \ + &(other)->size, &(other)->capacity); \ + } while (0) + +/// Get the size of the array contents +#define array_elem_size(self) (sizeof *(self)->contents) + +/// Search a sorted array for a given `needle` value, using the given `compare` +/// callback to determine the order. +/// +/// If an existing element is found to be equal to `needle`, then the `index` +/// out-parameter is set to the existing value's index, and the `exists` +/// out-parameter is set to true. Otherwise, `index` is set to an index where +/// `needle` should be inserted in order to preserve the sorting, and `exists` +/// is set to false. +#define array_search_sorted_with(self, compare, needle, _index, _exists) \ + _array__search_sorted(self, 0, compare, , needle, _index, _exists) + +/// Search a sorted array for a given `needle` value, using integer comparisons +/// of a given struct field (specified with a leading dot) to determine the order. +/// +/// See also `array_search_sorted_with`. +#define array_search_sorted_by(self, field, needle, _index, _exists) \ + _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) + +/// Insert a given `value` into a sorted array, using the given `compare` +/// callback to determine the order. +#define array_insert_sorted_with(self, compare, value) \ + do { \ + unsigned _index, _exists; \ + array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ + if (!_exists) array_insert(self, _index, value); \ + } while (0) + +/// Insert a given `value` into a sorted array, using integer comparisons of +/// a given struct field (specified with a leading dot) to determine the order. +/// +/// See also `array_search_sorted_by`. +#define array_insert_sorted_by(self, field, value) \ + do { \ + unsigned _index, _exists; \ + array_search_sorted_by(self, field, (value) field, &_index, &_exists); \ + if (!_exists) array_insert(self, _index, value); \ + } while (0) + +// Private + +// Pointers to individual `Array` fields (rather than the entire `Array` itself) +// are passed to the various `_array__*` functions below to address strict aliasing +// violations that arises when the _entire_ `Array` struct is passed as `Array(void)*`. +// +// The `Array` type itself was not altered as a solution in order to avoid breakage +// with existing consumers (in particular, parsers with external scanners). + +/// This is not what you're looking for, see `array_erase`. +static inline void _array__erase(void* self_contents, uint32_t *size, + size_t element_size, uint32_t index) { + assert(index < *size); + char *contents = (char *)self_contents; + memmove(contents + index * element_size, contents + (index + 1) * element_size, + (*size - index - 1) * element_size); + (*size)--; +} + +/// This is not what you're looking for, see `array_reserve`. +static inline void *_array__reserve(void *contents, uint32_t *capacity, + size_t element_size, uint32_t new_capacity) { + void *new_contents = contents; + if (new_capacity > *capacity) { + if (contents) { + new_contents = ts_realloc(contents, new_capacity * element_size); + } else { + new_contents = ts_malloc(new_capacity * element_size); + } + *capacity = new_capacity; + } + return new_contents; +} + +/// This is not what you're looking for, see `array_assign`. +static inline void *_array__assign(void* self_contents, uint32_t *self_size, uint32_t *self_capacity, + const void *other_contents, uint32_t other_size, size_t element_size) { + void *new_contents = _array__reserve(self_contents, self_capacity, element_size, other_size); + *self_size = other_size; + memcpy(new_contents, other_contents, *self_size * element_size); + return new_contents; +} + +/// This is not what you're looking for, see `array_swap`. +static inline void _array__swap(uint32_t *self_size, uint32_t *self_capacity, + uint32_t *other_size, uint32_t *other_capacity) { + uint32_t tmp_size = *self_size; + uint32_t tmp_capacity = *self_capacity; + *self_size = *other_size; + *self_capacity = *other_capacity; + *other_size = tmp_size; + *other_capacity = tmp_capacity; +} + +/// This is not what you're looking for, see `array_push` or `array_grow_by`. +static inline void *_array__grow(void *contents, uint32_t size, uint32_t *capacity, + uint32_t count, size_t element_size) { + void *new_contents = contents; + uint32_t new_size = size + count; + if (new_size > *capacity) { + uint32_t new_capacity = *capacity * 2; + if (new_capacity < 8) new_capacity = 8; + if (new_capacity < new_size) new_capacity = new_size; + new_contents = _array__reserve(contents, capacity, element_size, new_capacity); + } + return new_contents; +} + +/// This is not what you're looking for, see `array_splice`. +static inline void *_array__splice(void *self_contents, uint32_t *size, uint32_t *capacity, + size_t element_size, + uint32_t index, uint32_t old_count, + uint32_t new_count, const void *elements) { + uint32_t new_size = *size + new_count - old_count; + uint32_t old_end = index + old_count; + uint32_t new_end = index + new_count; + assert(old_end <= *size); + + void *new_contents = _array__reserve(self_contents, capacity, element_size, new_size); + + char *contents = (char *)new_contents; + if (*size > old_end) { + memmove( + contents + new_end * element_size, + contents + old_end * element_size, + (*size - old_end) * element_size + ); + } + if (new_count > 0) { + if (elements) { + memcpy( + (contents + index * element_size), + elements, + new_count * element_size + ); + } else { + memset( + (contents + index * element_size), + 0, + new_count * element_size + ); + } + } + *size += new_count - old_count; + + return new_contents; +} + +/// A binary search routine, based on Rust's `std::slice::binary_search_by`. +/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`. +#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ + do { \ + *(_index) = start; \ + *(_exists) = false; \ + uint32_t size = (self)->size - *(_index); \ + if (size == 0) break; \ + int comparison; \ + while (size > 1) { \ + uint32_t half_size = size / 2; \ + uint32_t mid_index = *(_index) + half_size; \ + comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ + if (comparison <= 0) *(_index) = mid_index; \ + size -= half_size; \ + } \ + comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \ + if (comparison == 0) *(_exists) = true; \ + else if (comparison < 0) *(_index) += 1; \ + } while (0) + +/// Helper macro for the `_sorted_by` routines below. This takes the left (existing) +/// parameter by reference in order to work with the generic sorting function above. +#define _compare_int(a, b) ((int)*(a) - (int)(b)) + +#ifdef _MSC_VER +#pragma warning(pop) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_ARRAY_H_ diff --git a/examples/live/grammar/src/tree_sitter/parser.h b/examples/live/grammar/src/tree_sitter/parser.h new file mode 100644 index 0000000..858107d --- /dev/null +++ b/examples/live/grammar/src/tree_sitter/parser.h @@ -0,0 +1,286 @@ +#ifndef TREE_SITTER_PARSER_H_ +#define TREE_SITTER_PARSER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define ts_builtin_sym_error ((TSSymbol)-1) +#define ts_builtin_sym_end 0 +#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 + +#ifndef TREE_SITTER_API_H_ +typedef uint16_t TSStateId; +typedef uint16_t TSSymbol; +typedef uint16_t TSFieldId; +typedef struct TSLanguage TSLanguage; +typedef struct TSLanguageMetadata { + uint8_t major_version; + uint8_t minor_version; + uint8_t patch_version; +} TSLanguageMetadata; +#endif + +typedef struct { + TSFieldId field_id; + uint8_t child_index; + bool inherited; +} TSFieldMapEntry; + +// Used to index the field and supertype maps. +typedef struct { + uint16_t index; + uint16_t length; +} TSMapSlice; + +typedef struct { + bool visible; + bool named; + bool supertype; +} TSSymbolMetadata; + +typedef struct TSLexer TSLexer; + +struct TSLexer { + int32_t lookahead; + TSSymbol result_symbol; + void (*advance)(TSLexer *, bool); + void (*mark_end)(TSLexer *); + uint32_t (*get_column)(TSLexer *); + bool (*is_at_included_range_start)(const TSLexer *); + bool (*eof)(const TSLexer *); + void (*log)(const TSLexer *, const char *, ...); +}; + +typedef enum { + TSParseActionTypeShift, + TSParseActionTypeReduce, + TSParseActionTypeAccept, + TSParseActionTypeRecover, +} TSParseActionType; + +typedef union { + struct { + uint8_t type; + TSStateId state; + bool extra; + bool repetition; + } shift; + struct { + uint8_t type; + uint8_t child_count; + TSSymbol symbol; + int16_t dynamic_precedence; + uint16_t production_id; + } reduce; + uint8_t type; +} TSParseAction; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; +} TSLexMode; + +typedef struct { + uint16_t lex_state; + uint16_t external_lex_state; + uint16_t reserved_word_set_id; +} TSLexerMode; + +typedef union { + TSParseAction action; + struct { + uint8_t count; + bool reusable; + } entry; +} TSParseActionEntry; + +typedef struct { + int32_t start; + int32_t end; +} TSCharacterRange; + +struct TSLanguage { + uint32_t abi_version; + uint32_t symbol_count; + uint32_t alias_count; + uint32_t token_count; + uint32_t external_token_count; + uint32_t state_count; + uint32_t large_state_count; + uint32_t production_id_count; + uint32_t field_count; + uint16_t max_alias_sequence_length; + const uint16_t *parse_table; + const uint16_t *small_parse_table; + const uint32_t *small_parse_table_map; + const TSParseActionEntry *parse_actions; + const char * const *symbol_names; + const char * const *field_names; + const TSMapSlice *field_map_slices; + const TSFieldMapEntry *field_map_entries; + const TSSymbolMetadata *symbol_metadata; + const TSSymbol *public_symbol_map; + const uint16_t *alias_map; + const TSSymbol *alias_sequences; + const TSLexerMode *lex_modes; + bool (*lex_fn)(TSLexer *, TSStateId); + bool (*keyword_lex_fn)(TSLexer *, TSStateId); + TSSymbol keyword_capture_token; + struct { + const bool *states; + const TSSymbol *symbol_map; + void *(*create)(void); + void (*destroy)(void *); + bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); + unsigned (*serialize)(void *, char *); + void (*deserialize)(void *, const char *, unsigned); + } external_scanner; + const TSStateId *primary_state_ids; + const char *name; + const TSSymbol *reserved_words; + uint16_t max_reserved_word_set_size; + uint32_t supertype_count; + const TSSymbol *supertype_symbols; + const TSMapSlice *supertype_map_slices; + const TSSymbol *supertype_map_entries; + TSLanguageMetadata metadata; +}; + +static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) { + uint32_t index = 0; + uint32_t size = len - index; + while (size > 1) { + uint32_t half_size = size / 2; + uint32_t mid_index = index + half_size; + const TSCharacterRange *range = &ranges[mid_index]; + if (lookahead >= range->start && lookahead <= range->end) { + return true; + } else if (lookahead > range->end) { + index = mid_index; + } + size -= half_size; + } + const TSCharacterRange *range = &ranges[index]; + return (lookahead >= range->start && lookahead <= range->end); +} + +/* + * Lexer Macros + */ + +#ifdef _MSC_VER +#define UNUSED __pragma(warning(suppress : 4101)) +#else +#define UNUSED __attribute__((unused)) +#endif + +#define START_LEXER() \ + bool result = false; \ + bool skip = false; \ + UNUSED \ + bool eof = false; \ + int32_t lookahead; \ + goto start; \ + next_state: \ + lexer->advance(lexer, skip); \ + start: \ + skip = false; \ + lookahead = lexer->lookahead; + +#define ADVANCE(state_value) \ + { \ + state = state_value; \ + goto next_state; \ + } + +#define ADVANCE_MAP(...) \ + { \ + static const uint16_t map[] = { __VA_ARGS__ }; \ + for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \ + if (map[i] == lookahead) { \ + state = map[i + 1]; \ + goto next_state; \ + } \ + } \ + } + +#define SKIP(state_value) \ + { \ + skip = true; \ + state = state_value; \ + goto next_state; \ + } + +#define ACCEPT_TOKEN(symbol_value) \ + result = true; \ + lexer->result_symbol = symbol_value; \ + lexer->mark_end(lexer); + +#define END_STATE() return result; + +/* + * Parse Table Macros + */ + +#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) + +#define STATE(id) id + +#define ACTIONS(id) id + +#define SHIFT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = (state_value) \ + } \ + }} + +#define SHIFT_REPEAT(state_value) \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .state = (state_value), \ + .repetition = true \ + } \ + }} + +#define SHIFT_EXTRA() \ + {{ \ + .shift = { \ + .type = TSParseActionTypeShift, \ + .extra = true \ + } \ + }} + +#define REDUCE(symbol_name, children, precedence, prod_id) \ + {{ \ + .reduce = { \ + .type = TSParseActionTypeReduce, \ + .symbol = symbol_name, \ + .child_count = children, \ + .dynamic_precedence = precedence, \ + .production_id = prod_id \ + }, \ + }} + +#define RECOVER() \ + {{ \ + .type = TSParseActionTypeRecover \ + }} + +#define ACCEPT_INPUT() \ + {{ \ + .type = TSParseActionTypeAccept \ + }} + +#ifdef __cplusplus +} +#endif + +#endif // TREE_SITTER_PARSER_H_ diff --git a/examples/live/queries/highlights.scm b/examples/live/queries/highlights.scm new file mode 100644 index 0000000..f02ec48 --- /dev/null +++ b/examples/live/queries/highlights.scm @@ -0,0 +1,6 @@ +; The language's colours, in pardes's four classes (its themes' inks): +; the name a line defines, a builtin called, numbers, comments. +(definition name: (name) @keyword) +(call function: (name) @string) +(number) @number +(comment) @comment diff --git a/examples/live/queries/indents.scm b/examples/live/queries/indents.scm new file mode 100644 index 0000000..9ddd784 --- /dev/null +++ b/examples/live/queries/indents.scm @@ -0,0 +1,5 @@ +; A new line's indent (helix's indent queries): a level inside a call or +; parentheses left open, none else, so a line ending in `)` does not push +; the next one in. +[(call) (parenthesized)] @indent +")" @outdent diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 484de54..cc6280f 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -519,8 +519,9 @@ fn expectOps(tape: Tape, want: []const Op) !void { try testing.expectEqualSlices(Op, want, ops(tape)[0..tape.len]); } -/// The survey's program (reports/demo1.md, ยง1 A). -const survey = +/// The survey's program (reports/demo1.md, ยง1 A); main.zig's test of the +/// grammar parses it too. +pub const survey = \\# the beat, shared by sound and picture \\bpm = 120 \\beat = t * bpm / 60 diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index ca4d0be..d6bc8d6 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -82,8 +82,24 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool { /// call deterministic and bounded. A key at the most text took 29 KiB of it. var scratch: [128 * 1024]u8 = undefined; +/// The language's grammar (grammar/, its parser.c compiled in by build.zig), +/// handed to the editor at run time. +pub extern fn tree_sitter_live() *const pe.syntax.ts.Language; +pub fn language() pe.Language { + return .{ .name = "live", .ext = ".live", .ts = tree_sitter_live(), .highlights = @embedFile("highlights.scm"), .indents = @embedFile("indents.scm") }; +} + +/// The text's parse, for the editor's node walks (Alt-o, Alt-i, ...): +/// derived, so not in State, and kept between calls, as Alt-i goes back the +/// way Alt-o came only through its history; forgotten at a jump (`draw`). +/// A reload starts it again, with the code its grammar is in. +// ponytail: the tree a reload leaves behind is leaked (tree-sitter's +// malloc, a few KiB a reload). +var syn: pe.Syntax = .{}; + pub fn update(s: *State, in: *const sk.Input) void { const revision = s.ed.revision; + syn.lang = language(); // a chord's key_down may come with its letter as text too: not typed var chord = false; for (in.events) |e| { @@ -111,7 +127,7 @@ 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(), null, k); + _ = s.ed.key(fba.allocator(), &syn, k); } if (s.ed.revision == revision) return; var text: [Editor.capacity.text]u8 = undefined; @@ -180,6 +196,7 @@ const picture_pipeline: gpu.Pipeline = .{ }; pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + if (f.in.jumped) syn.forget(); f.clear(0x000000); if (!f.host.gpu()) { f.text(16, 16, 2, "live needs a GPU: the host has none.", 0xff8080); @@ -276,8 +293,11 @@ const ink = 0xe8e8e8; const red = 0xff5a50; const selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 170 }; const also_selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 90 }; +/// pardes's own theme: its text's ink and a class's. +const theme = &pe.syntax.themes[0]; -/// The editor's text over the picture on a dark band: its lines, every +/// The editor's text over the picture on a dark band: its lines coloured +/// by the grammar in pardes's theme, every /// range (the primary stronger), a caret at each range's head that says /// the mode (a block in normal and select, a bar in insert), the mode's /// name in the corner; the problem as a red mark under its byte and its @@ -289,6 +309,8 @@ fn code(s: *const State, f: *sk.Frame) void { const text = s.ed.bytes(f.scratch) catch return; const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return; const got = s.ed.ranges(f.scratch, ranges) catch return; + // a class a byte, from the grammar's highlights (none if it fails) + const colors = pe.Syntax.colors(f.scratch, &s.ed, language(), 0, text.len) catch &.{}; const cell = f.host.textSizeIn(.code, scale, "M"); const problem = s.problem.len > 0; var lines: usize = 1; @@ -309,7 +331,16 @@ fn code(s: *const State, f: *sk.Frame) void { const to = @min(@max(r.anchor, r.head), end); if (from < to) f.fill(.{ .x = margin + @as(f32, @floatFromInt(from - start)) * cell[0], .y = y, .w = @as(f32, @floatFromInt(to - from)) * cell[0], .h = cell[1] }, if (i == got.primary) selected else also_selected); } - f.textIn(.code, margin, y, scale, line, ink); + // a run of bytes of one class at a time, in the theme's inks + var a: usize = 0; + while (a < line.len) { + const class = if (start + a < colors.len) colors[start + a] else .none; + var b = a + 1; + while (b < line.len and (if (start + b < colors.len) colors[start + b] else .none) == class) b += 1; + const c = pe.syntax.synInk(theme, class) orelse pe.syntax.ink(theme); + f.textIn(.code, margin + @as(f32, @floatFromInt(a)) * cell[0], y, scale, line[a..b], sk.Color{ .r = c[0], .g = c[1], .b = c[2] }); + a = b; + } for (ranges[0..got.n], 0..) |r, i| { // the head's character: the one before it in a range that // grows forward diff --git a/examples/live/test/grammar.zig b/examples/live/test/grammar.zig new file mode 100644 index 0000000..497c05d --- /dev/null +++ b/examples/live/test/grammar.zig @@ -0,0 +1,54 @@ +//! The language's grammar (grammar/grammar.js, its parser.c compiled into +//! the sketch) against src/Lang.zig, which compiles the text: they must +//! agree on what is well formed, or what is coloured and walked would not +//! be the program that plays. +const std = @import("std"); +const app = @import("app"); +const Lang = @import("Lang"); +const pe = @import("pardes-editor"); + +test "the grammar and Lang.zig agree: both take a program, both refuse a broken line; what only Lang.zig refuses is its meaning" { + const parser = pe.syntax.ts.Parser.create(); + defer parser.destroy(); + try parser.setLanguage(app.tree_sitter_live()); + const Case = struct { text: []const u8, grammar: bool, lang: bool }; + for ([_]Case{ + .{ .text = Lang.survey, .grammar = true, .lang = true }, + .{ .text = "out = sin(tau * 220 * t) * 0.3\ncolor = hsv(x, 0.5, y)", .grammar = true, .lang = true }, + .{ .text = "\n # nothing\n a = 1 + 2 * -t / 4 - (5 - -6) # trailing\n\nout = a + 1e-3\n", .grammar = true, .lang = true }, + // broken lines: both refuse + .{ .text = "out = sin(tau * 220 * t * 0.3", .grammar = false, .lang = false }, + .{ .text = "out = 1 +", .grammar = false, .lang = false }, + .{ .text = "= 1", .grammar = false, .lang = false }, + .{ .text = "out 1", .grammar = false, .lang = false }, + .{ .text = "out", .grammar = false, .lang = false }, + .{ .text = "out = * 2", .grammar = false, .lang = false }, + .{ .text = "out = 1 2", .grammar = false, .lang = false }, + .{ .text = "out = sin(1 2)", .grammar = false, .lang = false }, + .{ .text = "out = 1.2.3", .grammar = false, .lang = false }, + .{ .text = "out = 1 $ 2", .grammar = false, .lang = false }, + .{ .text = "out = (1 + 2))", .grammar = false, .lang = false }, + // well formed, but meaning nothing: only Lang.zig knows its names, + // builtins and arities, and the problem it shows is the one drawn + .{ .text = "out = kick\nkick = 1", .grammar = true, .lang = false }, + .{ .text = "a = 1\na = 2", .grammar = true, .lang = false }, + .{ .text = "sin = 1", .grammar = true, .lang = false }, + .{ .text = "out = sin", .grammar = true, .lang = false }, + .{ .text = "out = foo(2)", .grammar = true, .lang = false }, + .{ .text = "out = mix(1, 2)", .grammar = true, .lang = false }, + .{ .text = "out = rgb(1, 1, 1)", .grammar = true, .lang = false }, + .{ .text = "a = 1\nout = a(2)", .grammar = true, .lang = false }, + }) |case| { + const tree = parser.parseString(case.text, null) orelse return error.NoTree; + defer tree.destroy(); + const lang = Lang.compile(case.text) == .program; + std.testing.expectEqual(case.grammar, !tree.rootNode().hasError()) catch |e| { + std.debug.print("the grammar on {s}\n", .{case.text}); + return e; + }; + std.testing.expectEqual(case.lang, lang) catch |e| { + std.debug.print("Lang.zig on {s}\n", .{case.text}); + return e; + }; + } +} -- cgit v1.3 From 1533644884c5cd2b072f78e3a42d5c4fa52f793a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:57:28 -0300 Subject: script: a key carries the modifiers `hold` has down, as a window's does A window's key event carries the modifiers held as it happened in its `value` (Host.zig, `modifiers(event.key.mod)`), and a sketch reads chords from there (examples/live: Alt-o, helix's node walk). A script's `key` (and a 9P ctl's `input key`) left `value` 0, so `hold left_alt` then `key o` was a plain o to the sketch: the chord could not be scripted. `key` now carries what `hold` has down, as `click` and the other pointer verbs already did in their `flags`. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/script.zig | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/host/script.zig b/src/host/script.zig index e3976e0..817462b 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -107,8 +107,9 @@ pub fn input(host: *Host, verb: Verb, words: *std.mem.TokenIterator(u8, .any), n .cancel => try host.route(.{ .kind = .cancel, .time = now }), .key => { const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = .key_down, .time = now, .code = @backingInt(key) }); - try host.route(.{ .kind = .key_up, .time = now, .code = @backingInt(key) }); + // with the modifiers `hold` has down, as a window's key carries them + try host.route(.{ .kind = .key_down, .time = now, .code = @backingInt(key), .value = mods(host) }); + try host.route(.{ .kind = .key_up, .time = now, .code = @backingInt(key), .value = mods(host) }); }, .hold, .release => { const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; @@ -285,14 +286,19 @@ fn step(host: *Host, arena: std.mem.Allocator, verb: Verb, rest: []const u8, wor if (verbs.get(verb).frame) try advance(host, now); } -/// The modifiers `hold` has down, for a pointer event's `flags`. +/// The modifiers `hold` has down, as `Key.Mod` bits: a key event's `value`. +fn mods(host: *const Host) u32 { + var m: u32 = 0; + if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) m |= abi.Key.Mod.shift; + if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) m |= abi.Key.Mod.ctrl; + if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) m |= abi.Key.Mod.alt; + if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) m |= abi.Key.Mod.gui; + return m; +} + +/// The same, for a pointer event's `flags`. fn held(host: *const Host) u8 { - var mods: u32 = 0; - if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) mods |= abi.Key.Mod.shift; - if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) mods |= abi.Key.Mod.ctrl; - if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) mods |= abi.Key.Mod.alt; - if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) mods |= abi.Key.Mod.gui; - return abi.Event.withModifiers(mods); + return abi.Event.withModifiers(mods(host)); } /// "VALUE" or "VALUE ~TOLERANCE". -- cgit v1.3 From 610ffe373f0f5dd5ccb59bf575f5479ad1ecdbaf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:57:28 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/src/Lang.zig | 115 +++++++++++++++++++++++++++++------------ examples/live/src/main.zig | 36 +++++++++---- examples/live/test/grammar.zig | 2 +- examples/live/test/live.script | 13 +++++ 4 files changed, 124 insertions(+), 42 deletions(-) diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index cc6280f..6ad1f40 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -155,16 +155,31 @@ pub const Problem = struct { } }; -pub const Program = struct { out: Tape = .{}, color: Tape = .{} }; +/// `probe` is what is heard and seen instead of the program while a +/// selection is on an expression (main.zig): empty when none is asked for, +/// none holds it, or it is longer than a tape. +pub const Program = struct { out: Tape = .{}, color: Tape = .{}, probe: Tape = .{} }; pub const Result = union(enum) { program: Program, problem: Problem }; -pub fn compile(text: []const u8) Result { - var c: Compiler = .{ .text = text }; +/// `probe`: bytes from..to of the text, a selection; the smallest +/// expression holding them is compiled as `Program.probe`. +pub fn compile(text: []const u8, probe: ?[2]u32) Result { + var c: Compiler = .{ .text = text, .probe = probe }; var program: Program = .{}; c.lines() catch return .{ .problem = c.problem }; - c.tape(&program.out, "out") catch return .{ .problem = c.problem }; - c.tape(&program.color, "color") catch return .{ .problem = c.problem }; + for ([_]struct { *Tape, []const u8 }{ .{ &program.out, "out" }, .{ &program.color, "color" } }) |line| { + const name = c.lookup(line[1]) orelse continue; + var registers: [max_terms]u16 = @splat(none); + _ = c.emit(line[0], name.term, ®isters) catch + return .{ .problem = (c.fail(name.at, "the {s} line needs more than {d} instructions", .{ line[1], max_tape }) catch c.problem) }; + } + if (c.heard != none) { + var registers: [max_terms]u16 = @splat(none); + _ = c.emit(&program.probe, c.heard, ®isters) catch { + program.probe = .{}; + }; + } return .{ .program = program }; } @@ -205,6 +220,21 @@ const Compiler = struct { /// The line's hsv or rgb, if it called one. colour: u16 = none, problem: Problem = .{}, + /// The bytes probed, and the smallest expression found holding them. + probe: ?[2]u32 = null, + heard: u16 = none, + heard_len: u32 = std.math.maxInt(u32), + + /// The expression from byte `from` to where the parse is, its term `t`: + /// the probe's, if it is the smallest yet that holds the probed bytes. + fn spanned(c: *Compiler, from: u32, t: u16) u16 { + const p = c.probe orelse return t; + if (from <= p[0] and p[1] <= c.pos and c.pos - from < c.heard_len) { + c.heard = t; + c.heard_len = c.pos - from; + } + return t; + } fn fail(c: *Compiler, at: u32, comptime fmt: []const u8, args: anytype) error{Problem} { c.problem = .{ .at = at }; @@ -293,7 +323,7 @@ const Compiler = struct { if (builtin(name) != null) return c.fail(first.at, "{s} is a builtin: call yours something else", .{name}); if (c.lookup(name) != null) return c.fail(first.at, "{s} is defined twice: a name is given once", .{name}); c.colour = none; - const root = try c.expr(); + const root = c.spanned(first.at, try c.expr()); const after = c.next(); if (after.kind != .newline and after.kind != .end) return c.fail(after.at, "expected the end of the line, found {s}", .{c.describe(after)}); if (c.colour != none and (c.colour != root or !std.mem.eql(u8, name, "color"))) @@ -310,24 +340,26 @@ const Compiler = struct { } fn expr(c: *Compiler) error{Problem}!u16 { - try c.enter(c.peek().at); + const from = c.peek().at; + try c.enter(from); defer c.depth -= 1; var left = try c.product(); while (true) { const tok = c.peek(); const op: Op = if (tok.is('+')) .add else if (tok.is('-')) .sub else return left; _ = c.next(); - left = try c.call(op, tok.at, &.{ left, try c.product() }); + left = c.spanned(from, try c.call(op, tok.at, &.{ left, try c.product() })); } } fn product(c: *Compiler) !u16 { + const from = c.peek().at; var left = try c.unary(); while (true) { const tok = c.peek(); const op: Op = if (tok.is('*')) .mul else if (tok.is('/')) .div else return left; _ = c.next(); - left = try c.call(op, tok.at, &.{ left, try c.unary() }); + left = c.spanned(from, try c.call(op, tok.at, &.{ left, try c.unary() })); } } @@ -341,9 +373,9 @@ const Compiler = struct { // A negative number is a number (each literal is a term of its own). if (c.terms[a].op == .constant) { c.terms[a].value = -c.terms[a].value; - return a; + return c.spanned(tok.at, a); } - return c.call(.neg, tok.at, &.{a}); + return c.spanned(tok.at, try c.call(.neg, tok.at, &.{a})); } fn primary(c: *Compiler) !u16 { @@ -351,14 +383,14 @@ const Compiler = struct { switch (tok.kind) { .number => { const value = std.fmt.parseFloat(f32, c.span(tok)) catch return c.fail(tok.at, "{s} is not a number", .{c.span(tok)}); - return c.term(.{ .op = .constant, .value = value, .at = tok.at }); + return c.spanned(tok.at, try c.term(.{ .op = .constant, .value = value, .at = tok.at })); }, .name => { const name = c.span(tok); - if (c.peek().is('(')) return c.callOf(tok); + if (c.peek().is('(')) return c.spanned(tok.at, try c.callOf(tok)); if (c.lookup(name)) |n| { if (c.terms[n.term].op == .hsv or c.terms[n.term].op == .rgb) return c.fail(tok.at, "{s} is a colour: only the color line can be one", .{name}); - return n.term; + return c.spanned(tok.at, n.term); } const b = builtin(name) orelse return c.fail(tok.at, "{s} is not defined above", .{name}); if (b.max > 0) return c.fail(tok.at, "{s} takes arguments: {s}", .{ name, b.call }); @@ -368,9 +400,9 @@ const Compiler = struct { .y => 2, else => null, }; - const i = input orelse return c.term(.{ .op = b.op, .value = b.value, .at = tok.at }); + const i = input orelse return c.spanned(tok.at, try c.term(.{ .op = b.op, .value = b.value, .at = tok.at })); if (c.inputs[i] == none) c.inputs[i] = try c.term(.{ .op = b.op, .at = tok.at }); - return c.inputs[i]; + return c.spanned(tok.at, c.inputs[i]); }, .newline, .end => return c.fail(tok.at, "expected an expression before the end of the line", .{}), .symbol => { @@ -381,7 +413,7 @@ const Compiler = struct { const inner = try c.expr(); const close = c.next(); if (!close.is(')')) return c.fail(close.at, "expected ) to close the ( at byte {d}, found {s}", .{ tok.at, c.describe(close) }); - return inner; + return c.spanned(tok.at, inner); }, } } @@ -417,15 +449,6 @@ const Compiler = struct { return t; } - /// The tape of the line named `name` (empty without one). - fn tape(c: *Compiler, into: *Tape, name: []const u8) !void { - into.* = .{}; - const line = c.lookup(name) orelse return; - var registers: [max_terms]u16 = @splat(none); - _ = c.emit(into, line.term, ®isters) catch - return c.fail(line.at, "the {s} line needs more than {d} instructions", .{ name, max_tape }); - } - fn push(into: *Tape, i: Instruction) !u32 { if (into.len == max_tape) return error.Full; into.code[into.len] = i; @@ -500,7 +523,7 @@ pub const Synth = extern struct { const testing = std.testing; fn compiled(text: []const u8) !Program { - return switch (compile(text)) { + return switch (compile(text, null)) { .program => |p| p, .problem => |p| { std.debug.print("unexpected problem at {d}: {s}\n", .{ p.at, p.text() }); @@ -641,7 +664,7 @@ test "every problem, with its byte" { .{ .text = "out = mix(1, 2)", .at = 6, .words = "mix takes 3 arguments, not 2: mix(a, b, w)" }, .{ .text = "out = sin()", .at = 6, .words = "sin takes 1 arguments, not 0: sin(a)" }, }; - for (cases) |case| switch (compile(case.text)) { + for (cases) |case| switch (compile(case.text, null)) { .program => { std.debug.print("{s}: no problem\n", .{case.text}); return error.TestUnexpectedResult; @@ -665,7 +688,7 @@ test "past the capacities: problems in words" { const fits = try compiled(w.buffered()); try testing.expectEqual(@as(u32, 127), fits.out.len); try w.writeAll(" + 1"); - switch (compile(w.buffered())) { + switch (compile(w.buffered(), null)) { .program => return error.TestUnexpectedResult, .problem => |p| { try testing.expectEqualStrings("the out line needs more than 128 instructions", p.text()); @@ -679,14 +702,14 @@ test "past the capacities: problems in words" { const long = w.buffered(); var text: [8192]u8 = undefined; const unused = try std.fmt.bufPrint(&text, "a = {s}\n", .{long}); - switch (compile(unused)) { + switch (compile(unused, null)) { .program => return error.TestUnexpectedResult, .problem => |p| try testing.expectEqualStrings("the program is longer than 512 terms", p.text()), } // Names. w = .fixed(&buf); for (0..max_names + 1) |i| try w.print("n{d} = {d}\n", .{ i, i }); - switch (compile(w.buffered())) { + switch (compile(w.buffered(), null)) { .program => return error.TestUnexpectedResult, .problem => |p| try testing.expectEqualStrings("more than 128 names", p.text()), } @@ -696,8 +719,36 @@ test "past the capacities: problems in words" { for (0..40) |_| try w.writeAll("("); try w.writeAll("1"); for (0..40) |_| try w.writeAll(")"); - switch (compile(w.buffered())) { + switch (compile(w.buffered(), null)) { .program => return error.TestUnexpectedResult, .problem => |p| try testing.expectEqualStrings("nested more than 32 deep", p.text()), } } + +test "a probe: the smallest expression holding the bytes, its own tape" { + const text = "kick = exp(-7 * fract(t * 2))\nout = sin(tau * 220 * t) * kick"; + for ([_]struct { [2]u32, []const Op }{ + // `tau * 220 * t`, a selection Alt-o grew to + .{ .{ 40, 53 }, &.{ .constant, .constant, .mul, .t, .mul } }, + // `220` alone, and a cursor on its `2` + .{ .{ 46, 49 }, &.{.constant} }, + .{ .{ 46, 47 }, &.{.constant} }, + // `kick` where it is used: the line that defines it + .{ .{ 57, 61 }, &.{ .constant, .t, .constant, .mul, .fract, .mul, .exp } }, + // an argument + .{ .{ 22, 27 }, &.{ .t, .constant, .mul } }, + // the out line from its name, and the whole of it + .{ .{ 30, 33 }, &.{ .constant, .constant, .mul, .t, .mul, .sin, .constant, .constant, .mul, .fract, .mul, .exp, .mul } }, + }) |case| { + const p = try switch (compile(text, case[0])) { + .program => |p| p, + .problem => error.TestUnexpectedResult, + }; + expectOps(p.probe, case[1]) catch |e| { + std.debug.print("probe {any}\n", .{case[0]}); + return e; + }; + } + // nothing holds the newline between the lines: no probe + try testing.expectEqual(@as(u32, 0), compile(text, .{ 29, 30 }).program.probe.len); +} 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 }, diff --git a/examples/live/test/grammar.zig b/examples/live/test/grammar.zig index 497c05d..2fa982f 100644 --- a/examples/live/test/grammar.zig +++ b/examples/live/test/grammar.zig @@ -41,7 +41,7 @@ test "the grammar and Lang.zig agree: both take a program, both refuse a broken }) |case| { const tree = parser.parseString(case.text, null) orelse return error.NoTree; defer tree.destroy(); - const lang = Lang.compile(case.text) == .program; + const lang = Lang.compile(case.text, null) == .program; std.testing.expectEqual(case.grammar, !tree.rootNode().hasError()) catch |e| { std.debug.print("the grammar on {s}\n", .{case.text}); return e; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index ecf3d77..a06ed97 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -68,3 +68,16 @@ console stop expect ed.front 53 expect ed.back 0 expect color.code[3].op hsv +# Hear this: Alt-o from the 2 of 220 to the number, then to `tau * 220`, +# which alone is heard and seen until the next key. +type gg +type 16l +hold left_alt +key o +expect probe.len 1 +key o +release left_alt +expect probe.len 3 +expect probe.code[2].op mul +key escape +expect probe.len 0 -- cgit v1.3 From 4350e6840940da0b7af800faf5a1455a1de1908e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:58:27 -0300 Subject: examples/live: drag a number, the program following as it moves The left button pressed on a number token (the grammar's `number` node at the byte under the pointer, found with the module's parse) starts a drag kept in State: the literal's bytes, the value, its precision. Each pointer move rewrites the literal through the editor's `replace`: a step for every 4 pixels right of where it was pressed (left, down), the step from the literal's own precision (`220` by ones, `0.25` by hundredths), printed with as many decimals. The first rewrite is a step to undo and the rest join it, so one `u` takes the whole drag back. The text changes, so the program is compiled and heard as it moves. A literal with an exponent is not dragged (marked ponytail). `update` finds the byte under the pointer as `code` lays the text out: a character is 10 by 22 pixels, measured once (the code face at 1.25, natively: ten M are 100) and now a constant both use, where `code` asked the host each frame. Test: test/live.script presses 220, drags 40 pixels right (230), back to 12 left of the press (217), lets go and undoes the drag in one step (220). zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/src/main.zig | 71 ++++++++++++++++++++++++++++++++++++++++-- examples/live/test/live.script | 13 ++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index cc11dbe..d7a514f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -48,6 +48,8 @@ pub const State = struct { /// expression the primary range is on, heard and seen (grey) instead of /// out and color until the next key; empty otherwise. probe: Lang.Tape = .{}, + /// A number the pointer was pressed on and drags. + drag: Drag = .{}, /// 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. @@ -57,6 +59,21 @@ pub const State = struct { crossfade: bool = true, }; +/// Drag a number: its literal's bytes, at..at + len, rewritten as the +/// pointer moves, a step for every 4 pixels to the right (left, down) from +/// where it was pressed: its own precision, `220` by ones, `0.25` by +/// hundredths. The drag is one step to undo. +pub const Drag = struct { + active: bool = false, + /// Rewritten once: the next frames join its step to undo. + moved: bool = false, + at: u32 = 0, + len: u32 = 0, + x: f32 = 0, + value: f64 = 0, + decimals: u8 = 0, +}; + pub const Session = struct { picture: sk.Image = .none, size: [2]u32 = .{ 0, 0 }, @@ -107,12 +124,59 @@ pub fn update(s: *State, in: *const sk.Input) void { // a chord's key_down may come with its letter as text too: not typed var chord = false; for (in.events) |e| { + var fba: std.heap.FixedBufferAllocator = .init(&scratch); var utf8: [4]u8 = undefined; const k: pe.Key = switch (e.kind) { .tick => { s.time += e.x; continue; }, + .mouse_down => { + // the number token under the pointer, by the grammar's parse + if (e.code != 1 or e.x < margin or e.y < margin) continue; + var text: [Editor.capacity.text]u8 = undefined; + const t = s.ed.read(&text); + const row: usize = @intFromFloat((e.y - margin) / cell[1]); + const col: usize = @intFromFloat((e.x - margin) / cell[0]); + var start: usize = 0; + for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len) + 1; + if (start > t.len) continue; + const end = std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len; + if (start + col >= end) continue; + const tree = syn.parse(fba.allocator(), &s.ed, language()) orelse continue; + const n = tree.rootNode().descendantForByteRange(@intCast(start + col), @intCast(start + col + 1)) orelse continue; + if (!std.mem.eql(u8, n.kind(), "number")) continue; + const literal = t[n.startByte()..n.endByte()]; + // ponytail: a number with an exponent is not dragged + if (std.mem.indexOfScalar(u8, literal, 'e') != null) continue; + const point = std.mem.indexOfScalar(u8, literal, '.'); + s.drag = .{ + .active = true, + .at = n.startByte(), + .len = @intCast(literal.len), + .x = e.x, + .value = std.fmt.parseFloat(f64, literal) catch continue, + .decimals = @intCast(if (point) |p| literal.len - p - 1 else 0), + }; + continue; + }, + .mouse_move => { + if (!s.drag.active) continue; + const d = &s.drag; + const steps = @round((e.x - d.x) / 4); + var buf: [32]u8 = undefined; + const num = std.fmt.bufPrint(&buf, "{[0]d:.[1]}", .{ d.value + steps * std.math.pow(f64, 10, -@as(f64, @floatFromInt(d.decimals))), d.decimals }) catch continue; + var text: [Editor.capacity.text]u8 = undefined; + if (std.mem.eql(u8, s.ed.read(&text)[d.at..][0..d.len], num)) continue; + if (s.ed.replace(fba.allocator(), d.at, d.at + d.len, num, d.moved) != .done) continue; + d.len = @intCast(num.len); + d.moved = true; + continue; + }, + .mouse_up, .cancel => { + s.drag.active = false; + continue; + }, .text => text: { const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; if (chord) { @@ -128,7 +192,6 @@ pub fn update(s: *State, in: *const sk.Input) void { }, else => continue, }; - 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. const outcome = s.ed.key(fba.allocator(), &syn, k); @@ -306,6 +369,11 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { } const scale = 1.25; +/// A character of the code face at `scale`, wide and high, in pixels: +/// measured (`f.host.textSizeIn(.code, 1.25, "M")` natively, and ten of +/// them are 100), so `update` finds the byte under the pointer as `code` +/// lays it out. +const cell: [2]f32 = .{ 10, 22 }; const margin = 16; const ink = 0xe8e8e8; const red = 0xff5a50; @@ -329,7 +397,6 @@ fn code(s: *const State, f: *sk.Frame) void { const got = s.ed.ranges(f.scratch, ranges) catch return; // a class a byte, from the grammar's highlights (none if it fails) const colors = pe.Syntax.colors(f.scratch, &s.ed, language(), 0, text.len) catch &.{}; - const cell = f.host.textSizeIn(.code, scale, "M"); const problem = s.problem.len > 0; var lines: usize = 1; var widest: usize = 24; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index a06ed97..bedcf83 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -81,3 +81,16 @@ expect probe.len 3 expect probe.code[2].op mul key escape expect probe.len 0 +# Drag a number: 220 (line 1, columns 16..18; a column 10 pixels from 16, +# a line 22 from 16) pressed and dragged 40 pixels right is 230, then 12 +# left of where it was pressed is 217; the program follows; the drag is one +# step to undo. +down 181 27 +move 201 27 +move 221 27 +expect out.code[1].a 1130758144 +move 169 27 +expect out.code[1].a 1129906176 +up 169 27 +type u +expect out.code[1].a 1130102784 -- cgit v1.3 From fb237529724ae1c01b15f99e3b25bdce7c77cf82 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:59:59 -0300 Subject: examples/live: square units, x and y from the centre in half the height picture.frag gives the tape x and y in half the target's height, 0 at its centre and y up (`y` -1 to 1, `x` -1.78 to 1.78 on 16:9), so a circle is round on any window, half resolution included. Lang.builtins and the README's table say so; the README's program draws its ring about the centre (`r = sqrt(x * x + y * y)`, the ring's radius and width doubled into the new unit). Test: test/live.script replaces the text (`%`, `c`) with a disc of radius 0.5, `step(x * x + y * y, 0.25)`, and reads its edge 180 pixels from the centre across and up: white at 170, black at 190, each way. zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/README.md | 6 +++--- examples/live/shaders/picture.frag | 5 +++-- examples/live/src/Lang.zig | 4 ++-- examples/live/test/live.script | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/live/README.md b/examples/live/README.md index 3c0c885..e69b583 100644 --- a/examples/live/README.md +++ b/examples/live/README.md @@ -14,8 +14,8 @@ beat = t * bpm / 60 kick = exp(-7 * fract(beat)) thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5 out = thump -r = sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) -color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.03, 0, abs(r - 0.2 - 0.1 * kick))) +r = sqrt(x * x + y * y) +color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.06, 0, abs(r - 0.4 - 0.2 * kick))) ``` The picture pulses with the kick because both lines say `kick`. @@ -43,7 +43,7 @@ calls; `#` starts a comment. The builtins (src/Lang.zig's one table): | | | | --- | --- | | `t` | seconds | -| `x`, `y` | 0..1, left to right and bottom to top; 0 in sound | +| `x`, `y` | across and up from the centre, in half the window's height (`y` -1 to 1, `x` -1.78 to 1.78 on 16:9: a circle is round); 0 in sound | | `tau`, `pi` | a turn and half a turn, in radians | | `sin(a)`, `cos(a)` | of radians | | `exp(a)`, `log(a)`, `pow(a, b)`, `sqrt(a)` | e to the a, its inverse, a to the b (a not negative), square root | diff --git a/examples/live/shaders/picture.frag b/examples/live/shaders/picture.frag index ec1293e..4621b31 100644 --- a/examples/live/shaders/picture.frag +++ b/examples/live/shaders/picture.frag @@ -1,6 +1,7 @@ #version 450 #extension GL_GOOGLE_include_directive : require -// The color tape at every pixel; x and y run 0..1, y up. +// The color tape at every pixel; x and y in half the target's height, 0 at +// its centre, y up: a circle is round on any window. #include "Picture.glsl" #define TAPE(i) u.tape[i] #include "vm.glsl" @@ -8,6 +9,6 @@ layout(location = 0) out vec4 out_color; void main() { - vec2 p = vec2(gl_FragCoord.x, u.size.y - gl_FragCoord.y) / u.size; + vec2 p = vec2(gl_FragCoord.x - 0.5 * u.size.x, 0.5 * u.size.y - gl_FragCoord.y) / (0.5 * u.size.y); out_color = vec4(clamp(vm(0u, u.len, u.time, p.x, p.y), 0.0, 1.0), 1.0); } diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 6ad1f40..4ce504e 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -92,8 +92,8 @@ pub const Builtin = struct { /// switch, and only for a program that needs it. pub const builtins = [_]Builtin{ .{ .call = "t", .op = .t, .doc = "seconds" }, - .{ .call = "x", .op = .x, .doc = "0 at the left to 1 at the right; 0 in sound" }, - .{ .call = "y", .op = .y, .doc = "0 at the bottom to 1 at the top; 0 in sound" }, + .{ .call = "x", .op = .x, .doc = "across, in half the window's height: 0 at the centre, -1.78 to 1.78 on 16:9; 0 in sound" }, + .{ .call = "y", .op = .y, .doc = "up, in half the window's height: -1 at the bottom, 0 at the centre, 1 at the top; 0 in sound" }, .{ .call = "tau", .op = .constant, .value = std.math.tau, .doc = "a turn in radians, 6.283..." }, .{ .call = "pi", .op = .constant, .value = std.math.pi, .doc = "half a turn, 3.141..." }, .{ .call = "sin(a)", .op = .sin, .min = 1, .max = 1, .doc = "sine, of radians" }, diff --git a/examples/live/test/live.script b/examples/live/test/live.script index bedcf83..1ddcec2 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -94,3 +94,18 @@ expect out.code[1].a 1129906176 up 169 27 type u expect out.code[1].a 1130102784 +# Square units: a disc of radius 0.5 (half the window's height is 1) is +# 180 pixels across each way from the centre, round on 16:9. +type % +type c +type color = step(x * x + y * y, 0.25) +key escape +expect color.len 7 +frames 2 +screenshot .zig-cache/live/disc.png +expect-pixel .zig-cache/live/disc.png 810 360 255 255 255 255 2 +expect-pixel .zig-cache/live/disc.png 640 190 255 255 255 255 2 +expect-pixel .zig-cache/live/disc.png 470 360 255 255 255 255 2 +expect-pixel .zig-cache/live/disc.png 830 360 0 0 0 255 2 +expect-pixel .zig-cache/live/disc.png 640 170 0 0 0 255 2 +expect-pixel .zig-cache/live/disc.png 640 550 0 0 0 255 2 -- cgit v1.3 From f0c4ef1a771f6b13cbd86cccdb56accfd39bbd69 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 08:01:22 -0300 Subject: gpu: a uniform block over 4096 bytes fails to compile SDL_GPU's Vulkan backend binds 4 KiB of a uniform block (its descriptors' range, MAX_UBO_SECTION_SIZE in SDL_gpu_vulkan.c) and a larger block was cut off without a word, the rest of it read as nothing: examples/live met it with its two tapes in one block. glsl.assertStd140, which `gpu.uniform` and the GLSL layouts of a `.uniform` type both run, now refuses a block past `max_uniform_block` (4096) at compile time, the error naming the type, its size and the storage buffer to use instead. The README says so where it says std140 is checked. Test: a 4096-byte block passes (glsl.zig); a 4112-byte one fails to compile with the message, checked by hand (there is no harness for compile errors). zig build test: 186 of 186 steps, 148 of 148 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- README.md | 4 +++- src/glsl.zig | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a6358d8..ebf49a0 100644 --- a/README.md +++ b/README.md @@ -433,7 +433,9 @@ just a new pipeline after a reload. No sketch touches a graphics API. The shaders are GLSL; the build turns them into SPIR-V for native and WGSL for the web, and each build imports the form its host reads (see Build API). `abi.gpu.pipeline(Vertex, ...)` derives the vertex layout from a struct, and `abi.gpu.uniform` checks std140 at -compile time. A pipeline without attributes makes its own vertices from +compile time, and the size: a uniform block is 4096 bytes at most, all +SDL_GPU's Vulkan backend binds of one (a larger one was cut off without a +word), so more goes in a storage buffer. A pipeline without attributes makes its own vertices from `gl_VertexIndex` (a full-screen triangle, for shader-toy style views). Compute is data too. `host.newBuffer(size)` makes a GPU storage buffer, diff --git a/src/glsl.zig b/src/glsl.zig index a1cd2d6..974f566 100644 --- a/src/glsl.zig +++ b/src/glsl.zig @@ -98,12 +98,19 @@ pub fn generate(comptime root: type, comptime paths: []const []const u8, io: std } } +/// The most bytes a uniform block holds: what SDL_GPU's Vulkan backend +/// binds of one (its descriptors' range, MAX_UBO_SECTION_SIZE); a larger +/// block was cut off without a word (examples/live's two tapes met it). +pub const max_uniform_block = 4096; + /// Fails to compile unless `U` lays out exactly as a std140 uniform block /// with the same members in GLSL: scalars at 4 bytes, `vec2` at 8, -/// `vec3`/`vec4` at 16, arrays and nested structs at 16 with 16-byte strides. +/// `vec3`/`vec4` at 16, arrays and nested structs at 16 with 16-byte strides; +/// and fits `max_uniform_block`. pub fn assertStd140(comptime U: type) void { comptime { if (@typeInfo(U).@"struct".layout != .@"extern") @compileError(@typeName(U) ++ " must be extern to be a uniform block"); + if (@sizeOf(U) > max_uniform_block) @compileError(std.fmt.comptimePrint("{s} is {d} bytes: a uniform block is {d} at most (SDL_GPU's Vulkan backend binds 4 KiB of one, MAX_UBO_SECTION_SIZE, and the rest reads as nothing); put the rest in a storage buffer", .{ @typeName(U), @sizeOf(U), max_uniform_block })); const s = @typeInfo(U).@"struct"; for (s.field_names, s.field_types) |name, F| { const want = std140Align(F); @@ -136,6 +143,10 @@ fn std140Align(comptime T: type) usize { }; } +test "a uniform block of 4096 bytes is one (4112 fails to compile, as a lone check showed)" { + comptime assertStd140(extern struct { a: [256][4]f32 }); +} + test "uniform blocks and vertex inputs from structs" { const Root = struct { pub const Paint = extern struct { -- cgit v1.3 From df1d47d4fddd4165bdf511f4056d8075562a663e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 03:50:16 -0300 Subject: Add pardes's presentation on SDL_GPU Moved from pardes's src/gui/gui.zig (step S3 of moving its renderer here): src/gpu/Present.zig, the device, the window's claim or a software presenter through SDL_Renderer (a transparent window, which the GPU API will not claim, or a claim refused), the present mode and frames in flight, detach and attach for a window closed and opened again, the frame's target (the swapchain's image, or a capture texture in test mode and for the soft presenter), the soft present (the frame read back, put in a streaming texture and flipped) and a captured frame's read back. The window stays the embedder's and is passed in where it is used. What pardes did inside these with its own state comes back instead: a soft present's phase times (Timings), each presented frame's pixels through a frame tap, a captured frame's mapped bytes and times (readFrame, then releaseFrame) and their byte order (bgr). Present.zig imports the embedder's SDL translation as `c` and Cells.zig as `gpu_cells`. --- src/gpu/Present.zig | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) create mode 100644 src/gpu/Present.zig diff --git a/src/gpu/Present.zig b/src/gpu/Present.zig new file mode 100644 index 0000000..a6a2dbf --- /dev/null +++ b/src/gpu/Present.zig @@ -0,0 +1,343 @@ +//! pardes's presentation on SDL_GPU: the device, the window's claim (or a +//! software presenter where the GPU API will not take the window), the +//! present mode, the frame's target (the swapchain's image, or a capture +//! texture), the read back of a captured frame and the soft present, moved +//! from pardes's gui.zig as they were. The window is the embedder's, passed +//! in where it is used; `c` is the embedder's own SDL translation, so the +//! embedder's SDL is the one linked. +const std = @import("std"); +const c = @import("c"); +const Cells = @import("gpu_cells"); + +// pardes's scope, so its log reads as it did before the move. +const log = std.log.scoped(.gui); + +const Present = @This(); + +device: *c.SDL_GPUDevice, +swapchain_format: c.SDL_GPUTextureFormat, + +/// Test mode: every frame goes to the capture texture, read back, never +/// presented. +capture: bool = false, +capture_tex: ?*c.SDL_GPUTexture = null, +capture_tex_w: u32 = 0, +capture_tex_h: u32 = 0, +capture_xfer: ?*c.SDL_GPUTransferBuffer = null, +capture_xfer_size: u32 = 0, + +soft_present: bool = false, +surface_opacity: bool = false, +soft_renderer: ?*c.SDL_Renderer = null, +soft_texture: ?*c.SDL_Texture = null, +/// What a window is attached with again (attach). +present_mode: c.SDL_GPUPresentMode = c.SDL_GPU_PRESENTMODE_VSYNC, +soft_tex_w: u32 = 0, +soft_tex_h: u32 = 0, + +/// The device for `window`, which it claims, or a software presenter when +/// `surface_opacity` (a transparent window, which the GPU API will not +/// claim) or the claim fails. `test_mode` renders every frame into the +/// capture texture. +pub fn init(window: *c.SDL_Window, test_mode: bool, surface_opacity: bool) !Present { + const device = c.SDL_CreateGPUDevice(c.SDL_GPU_SHADERFORMAT_SPIRV, true, null) orelse { + log.err("SDL_CreateGPUDevice: {s}", .{c.SDL_GetError()}); + return error.SdlInit; + }; + errdefer c.SDL_DestroyGPUDevice(device); + var soft_present = false; + var soft_renderer: ?*c.SDL_Renderer = null; + if (surface_opacity or !c.SDL_ClaimWindowForGPUDevice(device, window)) { + if (surface_opacity) + log.info("transparent window: the GPU API will not claim one, presenting through SDL_Renderer", .{}) + else + log.warn("ClaimWindowForGPUDevice: {s}; presenting in software", .{c.SDL_GetError()}); + soft_renderer = c.SDL_CreateRenderer(window, null) orelse { + log.err("SDL_CreateRenderer: {s}", .{c.SDL_GetError()}); + return error.SdlInit; + }; + soft_present = true; + } + const present_mode: c.SDL_GPUPresentMode = blk: { + if (soft_present) break :blk c.SDL_GPU_PRESENTMODE_VSYNC; + for ([_]c.SDL_GPUPresentMode{ c.SDL_GPU_PRESENTMODE_IMMEDIATE, c.SDL_GPU_PRESENTMODE_MAILBOX, c.SDL_GPU_PRESENTMODE_VSYNC }) |mode| { + if (c.SDL_WindowSupportsGPUPresentMode(device, window, mode)) break :blk mode; + } + break :blk c.SDL_GPU_PRESENTMODE_VSYNC; + }; + if (!test_mode) _ = c.SDL_SetGPUAllowedFramesInFlight(device, 1); + if (!soft_present) { + _ = c.SDL_SetGPUSwapchainParameters(device, window, c.SDL_GPU_SWAPCHAINCOMPOSITION_SDR, present_mode); + } + const swapchain_format = if (soft_present) + softTargetFormat(device) + else + c.SDL_GetGPUSwapchainTextureFormat(device, window); + return .{ + .device = device, + .swapchain_format = swapchain_format, + .capture = test_mode, + .soft_present = soft_present, + .surface_opacity = surface_opacity, + .soft_renderer = soft_renderer, + .present_mode = present_mode, + }; +} + +/// The capture's texture and transfer buffer, and the device, let go: what +/// was made on the device is released first. +pub fn deinit(p: *Present) void { + if (p.capture_tex) |texture| c.SDL_ReleaseGPUTexture(p.device, texture); + if (p.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(p.device, transfer); + c.SDL_DestroyGPUDevice(p.device); +} + +/// The window is closing: its renderer (and the soft texture) or its claim +/// let go; the device stays for the next window (attach). +pub fn detach(p: *Present, window: *c.SDL_Window) void { + if (p.soft_renderer) |renderer| { + if (p.soft_texture) |texture| c.SDL_DestroyTexture(texture); + p.soft_texture = null; + p.soft_tex_w = 0; + p.soft_tex_h = 0; + c.SDL_DestroyRenderer(renderer); + p.soft_renderer = null; + } else c.SDL_ReleaseWindowFromGPUDevice(p.device, window); +} + +/// A new window presented to as the first was: false when it will not take +/// a renderer or the claim (the caller destroys it). +pub fn attach(p: *Present, window: *c.SDL_Window) bool { + if (p.soft_present) { + p.soft_renderer = c.SDL_CreateRenderer(window, null) orelse return false; + } else { + if (!c.SDL_ClaimWindowForGPUDevice(p.device, window)) return false; + _ = c.SDL_SetGPUSwapchainParameters(p.device, window, c.SDL_GPU_SWAPCHAINCOMPOSITION_SDR, p.present_mode); + } + return true; +} + +pub const Target = struct { texture: *c.SDL_GPUTexture, w: u32, h: u32 }; + +/// What this frame draws into: the capture texture in test mode (the grid's +/// size plus PARDES_TEST_PAD) or with a soft presenter, else the swapchain's +/// image; null when there is none to be had this time. +pub fn acquireTarget(p: *Present, cmd: *c.SDL_GPUCommandBuffer, window: *c.SDL_Window, capture_w: u32, capture_h: u32) !?Target { + if (p.capture) { + if (capture_w == 0 or capture_h == 0) return null; + try p.ensureCaptureTexture(capture_w, capture_h); + return .{ .texture = p.capture_tex.?, .w = capture_w, .h = capture_h }; + } + if (p.soft_present) { + var pw: c_int = 0; + var ph: c_int = 0; + if (!c.SDL_GetWindowSizeInPixels(window, &pw, &ph) or pw <= 0 or ph <= 0) return null; + try p.ensureCaptureTexture(@intCast(pw), @intCast(ph)); + return .{ .texture = p.capture_tex.?, .w = @intCast(pw), .h = @intCast(ph) }; + } + var swap_tex: ?*c.SDL_GPUTexture = null; + var sw: u32 = 0; + var sh: u32 = 0; + if (!c.SDL_AcquireGPUSwapchainTexture(cmd, window, &swap_tex, &sw, &sh)) return null; + // A busy swapchain skips the frame. + return .{ .texture = swap_tex orelse return null, .w = sw, .h = sh }; +} + +/// What a soft present's phases took, ns: the wait for the GPU, the frame's +/// upload (to the end of the present) and the flip. +pub const Timings = struct { wait_ns: u64 = 0, upload_ns: u64 = 0, flip_ns: u64 = 0 }; + +/// Submits the frame: handed to the soft presenter (`took` its phases, +/// `frame_tap` its pixels as they go out), or presented. A capture's frame +/// is read back instead (readFrame). +pub fn finishFrame( + p: *Present, + cmd: *c.SDL_GPUCommandBuffer, + target: Target, + frame_tap: *const fn (pixels: []const u8, w: u32, h: u32) void, + took: *Timings, +) !bool { + if (p.soft_present) { + took.* = try p.softPresentFrame(cmd, target.texture, target.w, target.h, frame_tap); + return true; + } + return c.SDL_SubmitGPUCommandBuffer(cmd); +} + +fn ensureCaptureTexture(p: *Present, width: u32, height: u32) !void { + if (p.capture_tex != null and p.capture_tex_w == width and p.capture_tex_h == height) return; + const next = Cells.texture2d(p.device, p.swapchain_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, width, height) orelse return error.GpuCreate; + if (p.capture_tex) |texture| c.SDL_ReleaseGPUTexture(p.device, texture); + p.capture_tex = next; + p.capture_tex_w = width; + p.capture_tex_h = height; +} + +fn softTargetFormat(device: *c.SDL_GPUDevice) c.SDL_GPUTextureFormat { + const candidates = [_]c.SDL_GPUTextureFormat{ + c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, + c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, + }; + for (candidates) |format| { + if (c.SDL_GPUTextureSupportsFormat( + device, + format, + c.SDL_GPU_TEXTURETYPE_2D, + c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, + )) return format; + } + return c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM; +} + +fn softPixelFormat(format: c.SDL_GPUTextureFormat) ?c.SDL_PixelFormat { + return switch (format) { + c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, + c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB, + => c.SDL_PIXELFORMAT_ARGB8888, + c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, + c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB, + => c.SDL_PIXELFORMAT_ABGR8888, + else => null, + }; +} + +fn ensureSoftTexture(p: *Present, width: u32, height: u32) !*c.SDL_Texture { + if (p.soft_texture) |texture| { + if (p.soft_tex_w == width and p.soft_tex_h == height) return texture; + c.SDL_DestroyTexture(texture); + p.soft_texture = null; + } + const renderer = p.soft_renderer orelse return error.GpuCreate; + const pixel_format = softPixelFormat(p.swapchain_format) orelse return error.UnsupportedCaptureFormat; + const next = c.SDL_CreateTexture( + renderer, + pixel_format, + c.SDL_TEXTUREACCESS_STREAMING, + @intCast(width), + @intCast(height), + ) orelse return error.GpuCreate; + _ = c.SDL_SetTextureScaleMode(next, c.SDL_SCALEMODE_NEAREST); + _ = c.SDL_SetTextureBlendMode(next, c.SDL_BLENDMODE_NONE); + p.soft_texture = next; + p.soft_tex_w = width; + p.soft_tex_h = height; + return next; +} + +/// The frame in `target`, `size` bytes, copied down into `capture_xfer` +/// (grown to it first) by `cmd`, which the caller submits and waits for. +/// With no buffer to be had the command buffer is submitted as it is. +fn downloadTarget(p: *Present, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTexture, sw: u32, sh: u32, size: u32) !*c.SDL_GPUTransferBuffer { + if (p.capture_xfer == null or p.capture_xfer_size < size) { + var info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = size, .props = 0 }; + const next = c.SDL_CreateGPUTransferBuffer(p.device, &info) orelse { + _ = c.SDL_SubmitGPUCommandBuffer(cmd); + return error.GpuCreate; + }; + if (p.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(p.device, transfer); + p.capture_xfer = next; + p.capture_xfer_size = size; + } + const xfer = p.capture_xfer.?; + const copy = c.SDL_BeginGPUCopyPass(cmd); + const src = c.SDL_GPUTextureRegion{ .texture = target, .mip_level = 0, .layer = 0, .x = 0, .y = 0, .z = 0, .w = sw, .h = sh, .d = 1 }; + const dst = c.SDL_GPUTextureTransferInfo{ .transfer_buffer = xfer, .offset = 0, .pixels_per_row = sw, .rows_per_layer = sh }; + c.SDL_DownloadFromGPUTexture(copy, &src, &dst); + c.SDL_EndGPUCopyPass(copy); + return xfer; +} + +fn softPresentFrame(p: *Present, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTexture, sw: u32, sh: u32, frame_tap: *const fn (pixels: []const u8, w: u32, h: u32) void) !Timings { + const bpp = c.SDL_GPUTextureFormatTexelBlockSize(p.swapchain_format); + const size = c.SDL_CalculateGPUTextureFormatSize(p.swapchain_format, sw, sh, 1); + if (size == 0 or bpp != 4) { + _ = c.SDL_SubmitGPUCommandBuffer(cmd); + return error.UnsupportedCaptureFormat; + } + const xfer = try p.downloadTarget(cmd, target, sw, sh, size); + + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + defer c.SDL_ReleaseGPUFence(p.device, fence); + var fences = [_]*c.SDL_GPUFence{fence}; + const probe_wait = monoNs(); + if (!c.SDL_WaitForGPUFences(p.device, true, &fences, 1)) return error.GpuSubmit; + const wait_ns = monoNs() -| probe_wait; + + const mapped = c.SDL_MapGPUTransferBuffer(p.device, xfer, false) orelse return error.GpuMap; + defer c.SDL_UnmapGPUTransferBuffer(p.device, xfer); + + const texture = try p.ensureSoftTexture(sw, sh); + const renderer = p.soft_renderer orelse return error.GpuCreate; + const probe_upload = monoNs(); + if (!c.SDL_UpdateTexture(texture, null, mapped, @intCast(sw * bpp))) return error.GpuMap; + // PARDES_FRAMES_CAPTURE: every presented frame's pixels as they went + // out, and when, for a recording at the frames' own times. One write a + // frame; nothing else. + frame_tap(@as([*]const u8, @ptrCast(mapped))[0..size], sw, sh); + // The cell shader already produced premultiplied backgrounds with opaque + // foreground ink. Copy that alpha unchanged; never modulate the full frame. + _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_NONE); + _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, if (p.surface_opacity) 0 else 255); + _ = c.SDL_RenderClear(renderer); + _ = c.SDL_RenderTexture(renderer, texture, null, null); + const probe_flip = monoNs(); + _ = c.SDL_RenderPresent(renderer); + const end = monoNs(); + return .{ .wait_ns = wait_ns, .upload_ns = end -| probe_upload, .flip_ns = end -| probe_flip }; +} + +/// A captured frame read back (readFrame): its bytes, mapped until +/// releaseFrame, and when it was submitted and its fence passed +/// (CLOCK_MONOTONIC ns). +pub const Frame = struct { + pixels: []const u8, + xfer: *c.SDL_GPUTransferBuffer, + fence: *c.SDL_GPUFence, + submit_ns: u64, + fence_ns: u64, +}; + +/// Submits the frame, waits for it and maps it, read back into the capture +/// transfer buffer. +pub fn readFrame(p: *Present, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTexture, sw: u32, sh: u32) !Frame { + const size = c.SDL_CalculateGPUTextureFormatSize(p.swapchain_format, sw, sh, 1); + if (size == 0) { + _ = c.SDL_SubmitGPUCommandBuffer(cmd); + return error.UnsupportedCaptureFormat; + } + const xfer = try p.downloadTarget(cmd, target, sw, sh, size); + + const submit_ns = monoNs(); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + errdefer c.SDL_ReleaseGPUFence(p.device, fence); + var fences = [_]*c.SDL_GPUFence{fence}; + if (!c.SDL_WaitForGPUFences(p.device, true, &fences, 1)) return error.GpuSubmit; + const fence_ns = monoNs(); + + const mapped = c.SDL_MapGPUTransferBuffer(p.device, xfer, false) orelse return error.GpuMap; + const pixels: [*]const u8 = @ptrCast(mapped); + return .{ .pixels = pixels[0..size], .xfer = xfer, .fence = fence, .submit_ns = submit_ns, .fence_ns = fence_ns }; +} + +/// The frame readFrame mapped let go: unmapped, then its fence released. +pub fn releaseFrame(p: *Present, frame: Frame) void { + c.SDL_UnmapGPUTransferBuffer(p.device, frame.xfer); + c.SDL_ReleaseGPUFence(p.device, frame.fence); +} + +/// Whether a frame read back is B, G, R, A in memory (else R, G, B, A). +pub fn bgr(p: *const Present) !bool { + const bpp = c.SDL_GPUTextureFormatTexelBlockSize(p.swapchain_format); + if (bpp != 4) return error.UnsupportedCaptureFormat; + return switch (p.swapchain_format) { + c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB => false, + c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB => true, + else => error.UnsupportedCaptureFormat, + }; +} + +fn monoNs() u64 { + var ts: std.c.timespec = undefined; + if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0; + return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); +} -- cgit v1.3 From 3571b3e8bfb54d6f98ad3bc1f89147275716e5fc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 08:04:49 -0300 Subject: examples/live: the storyboard as a script, two minutes typed through the editor test/story.script plays the survey's storyboard (reports/demo1.md, the screen and the storyboard) through the editor and asserts State at each beat: a tone typed (`out` 8 instructions, heard on the stream); 220 dragged with the pointer to 330 (245 on the way); a gradient (`color` 10, ending in hsv); `beat`, `kick` and `thump` opened above, `out = thump`, the colour's brightness times `0.5 + kick` (both tapes now hold the kick's exp: the picture pulses because it says kick); the last `)` deleted (the problem at its byte, 162, the tape unchanged) and typed back; inside thump's `sin(...)` (`f*`, `;`, `mi(`), Alt-o hears and sees the call alone (17 instructions ending in sin) and Alt-i its argument (16), Escape the program again; `root` with `seq`, `bass`, `out = bass + thump`, `r` and the ring (`out` 35, `color` 31). Then the session played back, paused, sought to the tone and the kick (each the live moment, leaf by leaf), stepped back 30 frames (0.9 s, the gradient), live again at the kick with the beat made a third faster (`f2`, `;`, `r3`), and the first session sought to its end as it was: nothing overwritten. Each beat's screenshot lands in .zig-cache/story/, named by it; the test step plays it with test/live.script. The storyboard's programs use the square units (`x / 4`, `y / 2 + 0.5` for a gradient over the window). zig build test: 86 of 86 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 2 +- examples/live/test/story.script | 157 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 examples/live/test/story.script diff --git a/examples/live/build.zig b/examples/live/build.zig index a07fe7d..cd686c0 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -49,7 +49,7 @@ pub fn build(b: *std.Build) void { app.module.linkLibrary(grammar); app.module.addAnonymousImport("highlights.scm", .{ .root_source_file = b.path("queries/highlights.scm") }); app.module.addAnonymousImport("indents.scm", .{ .root_source_file = b.path("queries/indents.scm") }); - const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")}, .tests = &.{b.path("test/grammar.zig")} }); + const steps = app.addSteps(.{ .scripts = &.{ b.path("test/live.script"), b.path("test/story.script") }, .tests = &.{b.path("test/grammar.zig")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); diff --git a/examples/live/test/story.script b/examples/live/test/story.script new file mode 100644 index 0000000..25e66f5 --- /dev/null +++ b/examples/live/test/story.script @@ -0,0 +1,157 @@ +# The storyboard (reports/demo1.md, section 4), two minutes typed through +# the editor: a tone, its pitch dragged, a gradient, a kick shared by sound +# and picture, a `)` deleted and put back, a probe, a bass line and a ring; +# then the session played back, sought, stepped back and branched. State +# is asserted at each beat; each beat's screenshot lands in +# .zig-cache/story/, named by the beat. +console capture +frames 2 +# 0:05 a tone +type i +type out = sin(tau * 220 * t) * 0.3 +key escape +expect problem.len 0 +expect out.len 8 +frames 30 +expect-audio 1 +console save tone +screenshot .zig-cache/story/1-tone.png +# 0:15 the pitch dragged, 220 to 330 (columns 16..18 of line 1; a step +# every 4 pixels: 245 on the way) +down 181 27 +move 281 27 +expect out.code[1].a 1131741184 +move 621 27 +up 621 27 +expect out.code[1].a 1134886912 +frames 10 +screenshot .zig-cache/story/2-drag.png +# 0:25 a gradient behind the code +type o +type color = hsv(x / 4, 0.5, y / 2 + 0.5) +key escape +expect color.len 10 +expect color.code[9].op hsv +frames 10 +screenshot .zig-cache/story/3-gradient.png +# 0:35 a kick, shared: the picture pulses because it says kick too +type gg +type O +type beat = t * 2 +key return +type kick = exp(-7 * fract(beat)) +key return +type thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5 +key escape +type j +type x +type c +type out = thump +key return +key escape +type x +type c +type color = hsv(x / 4, 0.5, (y / 2 + 0.5) * (0.5 + kick)) +key escape +expect problem.len 0 +expect out.len 20 +expect out.code[19].op mul +expect color.len 20 +expect color.code[19].op hsv +# the picture's tape has the kick's exp in it, as the sound's does +expect color.code[16].op exp +console save kick +frames 20 +screenshot .zig-cache/story/4-kick.png +# 0:55 a ) deleted: marked, and the last good program plays on +type gl +type d +expect problem.at 162 +expect color.len 20 +frames 10 +screenshot .zig-cache/story/5-broken.png +# 1:00 put back +type a +type ) +key escape +expect problem.len 0 +frames 10 +screenshot .zig-cache/story/6-mended.png +# 1:05 inside thump's sin, Alt-o: only the call is heard and seen; Alt-i +# back inside it; Escape, the program again +type gg +type jj +type f* +type ; +type mi( +hold left_alt +key o +expect probe.len 17 +expect probe.code[16].op sin +frames 10 +screenshot .zig-cache/story/7-probe.png +key i +expect probe.len 16 +expect probe.code[15].op mul +release left_alt +key escape +expect probe.len 0 +# 1:15 a bass line under it, and the ring +type o +type root = seq(beat / 4, 55, 55, 65.4, 49) +key return +type bass = saw(root * t) * (1 - kick) * 0.25 +key escape +type j +type x +type c +type out = bass + thump +key return +key escape +type O +type r = sqrt(x * x + y * y) +key escape +type j +type x +type c +type color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.06, 0, abs(r - 0.4 - 0.2 * kick)) + 0.2 * kick) +key escape +expect problem.len 0 +expect out.len 35 +expect out.code[34].op add +expect color.len 31 +expect color.code[30].op hsv +console save end +frames 60 +screenshot .zig-cache/story/8-bass-ring.png +# 1:40 played back, paused, sought to the tone and to the kick (each +# moment's text, tapes and time as they were live, leaf by leaf), stepped +# back half a second +console play session 0 +frames 30 +console pause +console seek 590 +expect-snapshot tone +screenshot .zig-cache/story/9-sought-tone.png +console seek 1405 +expect-snapshot kick +console step -30 +expect time 0.9 ~0.001 +expect color.len 10 +screenshot .zig-cache/story/9-stepped-back.png +# 1:55 live again at the kick, another way on: the beat a third faster +console seek 1405 +console stop +type gg +type f2 +type ; +type r3 +expect problem.len 0 +expect out.code[8].a 1077936128 +frames 30 +screenshot .zig-cache/story/10-branched.png +# nothing was overwritten: the first session still ends as it did +console play session 0 +frames 10 +console seek 2890 +expect-snapshot end -- cgit v1.3 From ada7e4137d66aa7c7c463bb8521c0db4e7fac18a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 04:36:56 -0300 Subject: Add pardes's cell resources, pictures, overlay and atlas upload Moved from pardes's src/gui/gui.zig (step S4a of moving its renderer here): Cells is now a struct over the embedder's device and swapchain format holding the four pipelines (and the addon one, made on first use), the glyph atlas's texture, nearest sampler and upload buffer, the pictures' linear sampler, the cells', pictures' and overlay's buffers, and the pictures' textures by key. init and deinit make and release them; image uploads a picture's RGBA, or only its dirty rows into the texture of the picture it replaces; mapImages, uploadImages and drawImages carry the frame's picture instances and draws, an ImageDraw's texture either one of Cells' own or the caller's, borrowed (an addon's premultiplied output); uploadAtlas sends the Atlas's dirty rows; uploadOverlay, pushLayerOpacity and drawOverlay the overlay's triangles. The SPIR-V is the embedder's `.spv` imports; Cells.zig imports the Atlas as `font_atlas`. --- src/gpu/Cells.zig | 343 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 338 insertions(+), 5 deletions(-) diff --git a/src/gpu/Cells.zig b/src/gpu/Cells.zig index 78d7f22..5055d31 100644 --- a/src/gpu/Cells.zig +++ b/src/gpu/Cells.zig @@ -1,10 +1,343 @@ -//! pardes's cell renderer on SDL_GPU, part 1: the shaders' instance ABI -//! (src/gpu/shaders/*.glsl), the pipelines over it and the resource helpers, -//! moved from pardes's gui.zig as they were. `c` is the embedder's own SDL -//! translation (pardes's: SDL3 and font.h), so the embedder's SDL is the one -//! linked. +//! pardes's cell renderer on SDL_GPU: the shaders' instance ABI +//! (src/gpu/shaders/*.glsl), the pipelines over it, the glyph atlas's +//! texture and its upload, the pictures' textures and their draws, the +//! overlay's triangles, and the resource helpers, moved from pardes's +//! gui.zig as they were. `c` is the embedder's own SDL translation (pardes's: +//! SDL3 and font.h), so the embedder's SDL is the one linked; the SPIR-V is +//! the build's (`.spv` imports, from src/gpu/shaders/). const std = @import("std"); const c = @import("c"); +const Atlas = @import("font_atlas"); + +const Cells = @This(); + +/// The embedder's device (Present's), which everything here is made on. +device: *c.SDL_GPUDevice, +/// What every pipeline draws into: the swapchain's format. +format: c.SDL_GPUTextureFormat, +pipeline: *c.SDL_GPUGraphicsPipeline, +decor_pipeline: *c.SDL_GPUGraphicsPipeline, +overlay_pipeline: *c.SDL_GPUGraphicsPipeline, +image_pipeline: *c.SDL_GPUGraphicsPipeline, +/// Premultiplied pictures' (an addon's), made on first use +/// (makeAddonPipeline). +addon_pipeline: ?*c.SDL_GPUGraphicsPipeline = null, +atlas_tex: *c.SDL_GPUTexture, +atlas_sampler: *c.SDL_GPUSampler, +linear_sampler: *c.SDL_GPUSampler, +atlas_xfer: *c.SDL_GPUTransferBuffer, +vbuf: ?*c.SDL_GPUBuffer = null, +vxfer: ?*c.SDL_GPUTransferBuffer = null, +vbuf_cells: u32 = 0, +overlay_vbuf: *c.SDL_GPUBuffer, +overlay_vxfer: *c.SDL_GPUTransferBuffer, +image_vbuf: ?*c.SDL_GPUBuffer = null, +image_vxfer: ?*c.SDL_GPUTransferBuffer = null, +image_capacity: u32 = 0, +/// The textures of the pictures shown, by key (image). +native_images: std.AutoHashMapUnmanaged(u64, *c.SDL_GPUTexture) = .empty, +/// The frame's pictures, instance by instance (mapImages, drawImages). +images: std.ArrayListUnmanaged(ImageDraw) = .empty, + +const vert_spv = @embedFile("ui.vert.spv"); +const frag_spv = @embedFile("ui.frag.spv"); +const decor_frag_spv = @embedFile("decor.frag.spv"); +const overlay_vert_spv = @embedFile("overlay.vert.spv"); +const overlay_frag_spv = @embedFile("overlay.frag.spv"); +const image_vert_spv = @embedFile("image.vert.spv"); +const image_frag_spv = @embedFile("image.frag.spv"); + +/// The atlas's texture, sampler and upload buffer, the pictures' sampler, +/// the four pipelines drawing into `swapchain_format`, and the overlay's +/// buffers, `max_overlay_vertices` long, made on `device`. +pub fn init(device: *c.SDL_GPUDevice, swapchain_format: c.SDL_GPUTextureFormat, max_overlay_vertices: usize) !Cells { + const atlas_tex = texture2d(device, c.SDL_GPU_TEXTUREFORMAT_R8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, Atlas.atlas_w, Atlas.atlas_h) orelse return error.GpuCreate; + + var samp_info = std.mem.zeroes(c.SDL_GPUSamplerCreateInfo); + samp_info.min_filter = c.SDL_GPU_FILTER_NEAREST; + samp_info.mag_filter = c.SDL_GPU_FILTER_NEAREST; + samp_info.mipmap_mode = c.SDL_GPU_FILTER_NEAREST; + samp_info.address_mode_u = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE; + samp_info.address_mode_v = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE; + samp_info.address_mode_w = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE; + const atlas_sampler = c.SDL_CreateGPUSampler(device, &samp_info) orelse return error.GpuCreate; + samp_info.min_filter = c.SDL_GPU_FILTER_LINEAR; + samp_info.mag_filter = c.SDL_GPU_FILTER_LINEAR; + samp_info.mipmap_mode = c.SDL_GPU_FILTER_LINEAR; + const linear_sampler = c.SDL_CreateGPUSampler(device, &samp_info) orelse return error.GpuCreate; + + var ax_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = Atlas.atlas_w * Atlas.atlas_h, .props = 0 }; + const atlas_xfer = c.SDL_CreateGPUTransferBuffer(device, &ax_info) orelse return error.GpuCreate; + + const pipeline = try makePipeline(device, swapchain_format, vert_spv, frag_spv, 1, 1, cell_input, std.mem.zeroes(c.SDL_GPUColorTargetBlendState)); + const decor_pipeline = try makePipeline(device, swapchain_format, vert_spv, decor_frag_spv, 0, 1, cell_input, backgroundLayerBlend()); + const overlay_pipeline = try makePipeline(device, swapchain_format, overlay_vert_spv, overlay_frag_spv, 0, 1, overlay_input, backgroundLayerBlend()); + const image_pipeline = try makePipeline(device, swapchain_format, image_vert_spv, image_frag_spv, 1, 1, image_input, backgroundLayerBlend()); + + const overlay_buffer_size: u32 = @intCast(max_overlay_vertices * @sizeOf(OverlayVertex)); + var ovb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = overlay_buffer_size, .props = 0 }; + const overlay_vbuf = c.SDL_CreateGPUBuffer(device, &ovb_info) orelse return error.GpuCreate; + var ovx_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = overlay_buffer_size, .props = 0 }; + const overlay_vxfer = c.SDL_CreateGPUTransferBuffer(device, &ovx_info) orelse return error.GpuCreate; + + return .{ + .device = device, + .format = swapchain_format, + .pipeline = pipeline, + .decor_pipeline = decor_pipeline, + .overlay_pipeline = overlay_pipeline, + .image_pipeline = image_pipeline, + .atlas_tex = atlas_tex, + .atlas_sampler = atlas_sampler, + .linear_sampler = linear_sampler, + .atlas_xfer = atlas_xfer, + .overlay_vbuf = overlay_vbuf, + .overlay_vxfer = overlay_vxfer, + }; +} + +/// The pipelines, textures and buffers, whatever they were regrown to, and +/// the pictures' textures let go (the device stays: Present's). +pub fn deinit(cells: *Cells, gpa: std.mem.Allocator) void { + const device = cells.device; + c.SDL_ReleaseGPUGraphicsPipeline(device, cells.image_pipeline); + if (cells.addon_pipeline) |pipeline_| c.SDL_ReleaseGPUGraphicsPipeline(device, pipeline_); + cells.clearImages(); + cells.native_images.deinit(gpa); + cells.images.deinit(gpa); + c.SDL_ReleaseGPUGraphicsPipeline(device, cells.pipeline); + c.SDL_ReleaseGPUGraphicsPipeline(device, cells.decor_pipeline); + c.SDL_ReleaseGPUGraphicsPipeline(device, cells.overlay_pipeline); + c.SDL_ReleaseGPUTexture(device, cells.atlas_tex); + c.SDL_ReleaseGPUSampler(device, cells.atlas_sampler); + c.SDL_ReleaseGPUSampler(device, cells.linear_sampler); + c.SDL_ReleaseGPUTransferBuffer(device, cells.atlas_xfer); + c.SDL_ReleaseGPUBuffer(device, cells.overlay_vbuf); + c.SDL_ReleaseGPUTransferBuffer(device, cells.overlay_vxfer); + if (cells.vbuf) |buffer| c.SDL_ReleaseGPUBuffer(device, buffer); + if (cells.vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer); + if (cells.image_vbuf) |buffer| c.SDL_ReleaseGPUBuffer(device, buffer); + if (cells.image_vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer); +} + +/// The pipeline of premultiplied pictures (an addon's GPU scene), made the +/// first time one is drawn. +pub fn makeAddonPipeline(cells: *Cells) !void { + if (cells.addon_pipeline != null) return; + var blend = backgroundLayerBlend(); + blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; + cells.addon_pipeline = try makePipeline(cells.device, cells.format, image_vert_spv, image_frag_spv, 1, 1, image_input, blend); +} + +/// Uploads only the atlas rows drawn into since the last upload. Glyphs fill +/// the atlas pen-style, a band of rows at a time, so new glyphs are a band a +/// cell or two tall, not the whole 4 MB texture. The band sits at the same +/// offset in the transfer buffer as in the stage, and the texture keeps the +/// rows outside it. +pub fn uploadAtlas(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, atlas: *Atlas) void { + const atlas_w = Atlas.atlas_w; + const top = atlas.atlas_dirty_top; + const rows = atlas.atlas_dirty_bottom - top; + const ptr = c.SDL_MapGPUTransferBuffer(cells.device, cells.atlas_xfer, false) orelse return; + const dst: [*]u8 = @ptrCast(ptr); + @memcpy(dst[top * atlas_w ..][0 .. rows * atlas_w], atlas.atlas_stage[top * atlas_w ..][0 .. rows * atlas_w]); + c.SDL_UnmapGPUTransferBuffer(cells.device, cells.atlas_xfer); + + const copy = c.SDL_BeginGPUCopyPass(cmd); + const src = c.SDL_GPUTextureTransferInfo{ .transfer_buffer = cells.atlas_xfer, .offset = top * atlas_w, .pixels_per_row = atlas_w, .rows_per_layer = rows }; + const dstregion = c.SDL_GPUTextureRegion{ .texture = cells.atlas_tex, .mip_level = 0, .layer = 0, .x = 0, .y = top, .z = 0, .w = atlas_w, .h = rows, .d = 1 }; + c.SDL_UploadToGPUTexture(copy, &src, &dstregion, false); + c.SDL_EndGPUCopyPass(copy); + atlas.atlas_dirty_top = Atlas.atlas_h; + atlas.atlas_dirty_bottom = 0; +} + +/// A picture of the frame: its texture (one of `native_images`, or the +/// caller's, borrowed: an addon's, premultiplied) and the target rectangle +/// it is clipped to. +pub const ImageDraw = struct { + premultiplied: bool = false, + texture: *c.SDL_GPUTexture, + clip: c.SDL_Rect, +}; + +/// Rows of a picture that alone differ from the picture under the key +/// `from` (image). +pub const Dirty = struct { from: u64, y: u32, h: u32 }; + +pub fn releaseImage(cells: *Cells, key: u64) void { + if (cells.native_images.fetchRemove(key)) |removed| + c.SDL_ReleaseGPUTexture(cells.device, removed.value); +} + +pub fn clearImages(cells: *Cells) void { + var iterator = cells.native_images.valueIterator(); + while (iterator.next()) |texture| c.SDL_ReleaseGPUTexture(cells.device, texture.*); + cells.native_images.clearRetainingCapacity(); +} + +/// The texture of the picture `key`, `w` by `h` RGBA pixels in `rgba`, a +/// row `stride` bytes: uploaded unless it is held already, or, where +/// `dirty_rows` names a picture held, only those rows written into that +/// picture's texture, which takes the new key. +pub fn image( + cells: *Cells, + gpa: std.mem.Allocator, + cmd: *c.SDL_GPUCommandBuffer, + key: u64, + rgba: []const u8, + w: u32, + h: u32, + stride: u32, + dirty_rows: ?Dirty, +) !void { + if (cells.native_images.contains(key)) return; + const row = stride; + if (dirty_rows) |dirty| dirty: { + // The core changed only these rows since a picture we still hold: + // write them into that texture rather than sending the whole page. + if (dirty.y + dirty.h > h) break :dirty; + const texture = cells.native_images.get(dirty.from) orelse break :dirty; + try uploadTextureRows(cells, cmd, texture, rgba[dirty.y * row ..][0 .. dirty.h * row], w, dirty.y, dirty.h); + try cells.native_images.put(gpa, key, texture); + _ = cells.native_images.remove(dirty.from); + return; + } + + const texture = texture2d(cells.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, w, h) orelse return error.GpuCreate; + errdefer c.SDL_ReleaseGPUTexture(cells.device, texture); + try uploadTextureRows(cells, cmd, texture, rgba, w, 0, h); + try cells.native_images.put(gpa, key, texture); +} + +/// Copy `rows` rows of RGBA, `width` pixels each, into `texture` at row `y`. +fn uploadTextureRows( + cells: *Cells, + cmd: *c.SDL_GPUCommandBuffer, + texture: *c.SDL_GPUTexture, + rgba: []const u8, + width: usize, + y: usize, + rows: usize, +) !void { + var xf_info = c.SDL_GPUTransferBufferCreateInfo{ + .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, + .size = @intCast(rgba.len), + .props = 0, + }; + const transfer = c.SDL_CreateGPUTransferBuffer(cells.device, &xf_info) orelse return error.GpuCreate; + defer c.SDL_ReleaseGPUTransferBuffer(cells.device, transfer); + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(cells.device, transfer, false) orelse return error.GpuMap); + @memcpy(mapped[0..rgba.len], rgba); + c.SDL_UnmapGPUTransferBuffer(cells.device, transfer); + + const copy = c.SDL_BeginGPUCopyPass(cmd); + const src = c.SDL_GPUTextureTransferInfo{ + .transfer_buffer = transfer, + .offset = 0, + .pixels_per_row = @intCast(width), + .rows_per_layer = @intCast(rows), + }; + const dst = c.SDL_GPUTextureRegion{ + .texture = texture, + .mip_level = 0, + .layer = 0, + .x = 0, + .y = @intCast(y), + .z = 0, + .w = @intCast(width), + .h = @intCast(rows), + .d = 1, + }; + c.SDL_UploadToGPUTexture(copy, &src, &dst, false); + c.SDL_EndGPUCopyPass(copy); +} + +/// The frame's `n` picture instances, mapped for the caller to write, and +/// `images` emptied with room for as many draws; uploadImages sends them. +/// Null when the buffers will not grow or map. +pub fn mapImages(cells: *Cells, gpa: std.mem.Allocator, n: u32) ?[*]ImageInstance { + growInstances(cells.device, &cells.image_vbuf, &cells.image_vxfer, &cells.image_capacity, n, @sizeOf(ImageInstance)) catch return null; + cells.images.clearRetainingCapacity(); + cells.images.ensureTotalCapacity(gpa, n) catch return null; + const ptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(cells.device, cells.image_vxfer.?, false) orelse return null); + return @ptrCast(@alignCast(ptr)); +} + +/// The first `n` instances mapImages gave, sent. +pub fn uploadImages(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, n: u32) void { + c.SDL_UnmapGPUTransferBuffer(cells.device, cells.image_vxfer.?); + + const copy = c.SDL_BeginGPUCopyPass(cmd); + const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = cells.image_vxfer.?, .offset = 0 }; + const dst = c.SDL_GPUBufferRegion{ .buffer = cells.image_vbuf.?, .offset = 0, .size = n * @sizeOf(ImageInstance) }; + c.SDL_UploadToGPUBuffer(copy, &src, &dst, false); + c.SDL_EndGPUCopyPass(copy); +} + +/// Draws `count` of the frame's pictures from `start`, each clipped to its +/// rectangle; the scissor is the whole target, `whole`, after. +pub fn drawImages(cells: *Cells, rp: ?*c.SDL_GPURenderPass, start: u32, count: u32, whole: c.SDL_Rect) void { + const pass = rp orelse return; + c.SDL_BindGPUGraphicsPipeline(pass, cells.image_pipeline); + var idx = start; + const end = start + count; + for (cells.images.items[start..end]) |prepared| { + c.SDL_BindGPUGraphicsPipeline(pass, if (prepared.premultiplied) cells.addon_pipeline.? else cells.image_pipeline); + const clip = prepared.clip; + if (clip.w > 0 and clip.h > 0) { + c.SDL_SetGPUScissor(pass, &clip); + const sampler = c.SDL_GPUTextureSamplerBinding{ .texture = prepared.texture, .sampler = cells.linear_sampler }; + c.SDL_BindGPUFragmentSamplers(pass, 0, &sampler, 1); + const binding = c.SDL_GPUBufferBinding{ .buffer = cells.image_vbuf.?, .offset = idx * @sizeOf(ImageInstance) }; + c.SDL_BindGPUVertexBuffers(pass, 0, &binding, 1); + c.SDL_DrawGPUPrimitives(pass, 6, 1, 0, 0); + } + idx += 1; + } + std.debug.assert(idx == start + count); + // Back to the whole target: what draws after a picture may run on past + // the grid, as a rule and a band do. + c.SDL_SetGPUScissor(pass, &whole); +} + +/// The overlay's triangles, `vertices`, sent. +pub fn uploadOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, vertices: []const OverlayVertex) bool { + const vertex_count: u32 = @intCast(vertices.len); + const byte_len: u32 = vertex_count * @sizeOf(OverlayVertex); + const ptr = c.SDL_MapGPUTransferBuffer(cells.device, cells.overlay_vxfer, false) orelse return false; + const dst: [*]u8 = @ptrCast(ptr); + const src: [*]const u8 = @ptrCast(vertices.ptr); + @memcpy(dst[0..byte_len], src[0..byte_len]); + c.SDL_UnmapGPUTransferBuffer(cells.device, cells.overlay_vxfer); + const copy = c.SDL_BeginGPUCopyPass(cmd); + const src_loc = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = cells.overlay_vxfer, .offset = 0 }; + const dst_region = c.SDL_GPUBufferRegion{ .buffer = cells.overlay_vbuf, .offset = 0, .size = byte_len }; + c.SDL_UploadToGPUBuffer(copy, &src_loc, &dst_region, false); + c.SDL_EndGPUCopyPass(copy); + return true; +} + +pub fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPass, opacity: f32) void { + const uniforms = [4]f32{ opacity, 0, 0, 0 }; + c.SDL_PushGPUFragmentUniformData(cmd, 0, &uniforms, @sizeOf(@TypeOf(uniforms))); + // The source alpha is geometric/image coverage, not window opacity. Its + // alpha contribution is multiplied by this constant separately, so a + // rail repainted over a gutter still has alpha p, not p + p*(1-p). + c.SDL_SetGPUBlendConstants(pass, .{ .r = opacity, .g = opacity, .b = opacity, .a = opacity }); +} + +/// The overlay's first `vertex_count` vertices (uploadOverlay), drawn at +/// the layer opacity `opacity`. +pub fn drawOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, rp: ?*c.SDL_GPURenderPass, vertex_count: u32, opacity: f32) void { + if (vertex_count == 0) return; + const pass = rp orelse return; + c.SDL_BindGPUGraphicsPipeline(pass, cells.overlay_pipeline); + const binding = c.SDL_GPUBufferBinding{ .buffer = cells.overlay_vbuf, .offset = 0 }; + c.SDL_BindGPUVertexBuffers(pass, 0, &binding, 1); + pushLayerOpacity(cmd, pass, opacity); + c.SDL_DrawGPUPrimitives(pass, vertex_count, 1, 0, 0); +} pub const CellInstance = extern struct { x0: f32, -- cgit v1.3 From abe2962264d590a6f54d64434222cd4b10b62160 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 08:07:27 -0300 Subject: examples/live: the README, the editor's keys, hear this, the drag, scrubbing The README says the text is pardes's editor and what it needs (pardes beside sketchbook); the keys that matter, helix's in two lines (`i`, Escape, the motions, `x`, `d`, `c`, `r`, `u`/`U`, `%`) and the pointer; hear this (Alt-o, Alt-i, Alt-n, Alt-p: that expression alone, heard and seen grey until the next key; an example in thump); the drag (a step every 4 pixels, by the number's own precision, one step to undo); how to scrub in the console's words (`sessions`, `play session N [at S]`, `pause`, `seek MS`, `step -N`, `stop`); what the tests play, the storyboard among them; and in How it works, the editor's value in State and the grammar, its colours, node walks and indent. The reload is 0.07 to 0.09 s under `zig build lib --watch -fincremental`. zig build test: 86 of 86 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/README.md | 58 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/examples/live/README.md b/examples/live/README.md index e69b583..ea62cec 100644 --- a/examples/live/README.md +++ b/examples/live/README.md @@ -1,6 +1,7 @@ # live -A program typed over its own picture, and heard. Each line names an +A program typed over its own picture, and heard, in pardes's editor +(helix's keys, the language's grammar colouring it). Each line names an expression; the `out` line is what you hear (-1..1), the `color` line what you see. At every keystroke that leaves a program that parses, the program is compiled into two tapes of at most 128 instructions, and one @@ -22,16 +23,44 @@ The picture pulses with the kick because both lines say `kick`. ## Running it -`zig build run` (with `zig build lib --watch` beside it for hot reload). -Type; Backspace deletes the last character, Return starts a line. The -console (backtick) records, plays and seeks as for any sketch: `play -session 0`, `pause`, `seek 5000`, `step -60`, `stop`; a seek brings back -the text, the tapes and the picture of that moment, and the sound with -them. `capture` appends what is heard to `capture.f32` beside the save -(mono f32 at the device's rate) until `capture` again; `set crossfade -false` lets an edit click. `zig build test` runs the language's tests and -test/live.script headless; `zig build probe` times the interpreter on the -GPU (`--profile-gpu`). +`zig build run` (with `zig build lib --watch -fincremental` beside it for +hot reload: an edit of main.zig is back in 0.07-0.09 s). It needs pardes +beside sketchbook (`../02-pardes-code` of sketchbook's directory), whose +editor it takes as a library. + +**Keys** are helix's: `i` (or `a`, `o`, `O`) to type, Escape back to +normal mode, where `h j k l w b` move, `x` takes a line, `d` deletes, `c` +changes, `r` replaces a character, `u` and `U` undo and redo, `%` takes +it all. The mode is in the corner (NOR, INS, SEL). The pointer drags. + +**Hear this.** Alt-o grows the selection to the expression around it, +Alt-i shrinks it back, Alt-n and Alt-p go to the next and previous: after +any of them, that expression alone is heard (as `out`) and seen (grey, +unless it is a colour) until the next key. Escape, or any edit, goes back +to the program. `f*` then `;` then `mi(` then Alt-o, in +`thump = sin(tau * 48 * t ...)`, hears the sine alone. + +**Drag a number.** Press on a number and drag sideways: it moves a step +every 4 pixels, its step from how it is written (`220` by ones, `0.25` +by hundredths), and the program follows as you drag. The whole drag is +one step to undo. + +**Scrub** through the console (backtick): `sessions` lists the sessions +recorded, `play session N` plays one back (`play session N at 12` paused +at 12 s in), +`pause`, `seek 5000` (to 5 s in), `step -60` (60 frames back), `stop` +(live again from that moment, the session kept as it was). A seek brings back the text, the tapes and +the picture of that moment, and the sound with them. `capture` appends +what is heard to `capture.f32` beside the save (mono f32 at the +device's rate) until `capture` again; `set crossfade false` lets an edit +click. + +`zig build test` runs the language's tests, the grammar against the +language (test/grammar.zig), and test/live.script and test/story.script +(the storyboard: a tone, a drag, a gradient, a kick, a `)` broken and +mended, a probe, a bass and a ring, the session played back and sought) +headless; `zig build probe` times the interpreter on the GPU +(`--profile-gpu`). ## The language @@ -77,7 +106,12 @@ minutes or so a high pitch begins to roughen. src/Lang.zig compiles the text in `update` (no allocation: fixed arrays on the stack) into tapes of `{op, a, b, c}`, instruction i writing register -i; State keeps the text, both tapes, the problem and the time. The tapes +i; State keeps the editor's value (pardes-editor: 39 KiB of plain data, +4 KiB of text), both tapes, the probe's, the problem, the drag and the +time. grammar/grammar.js, generated into the committed grammar/src/parser.c, +is the same language for tree-sitter: its parse colours the text +(queries/highlights.scm, in pardes's orchard theme), gives Alt-o its +nodes and a new line its indent (queries/indents.scm). The tapes go to the GPU as data: the color tape as picture.frag's uniforms, the out tape (with the one it replaces, for the crossfade) in a storage buffer for synth.comp, both including shaders/vm.glsl, a loop over the tape with a -- cgit v1.3 From 83527d21ac42adc16d59ad8bfffebd132a5ef98b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:05:41 -0300 Subject: Add pardes's frame recording to Cells Moved from pardes's src/gui/gui.zig (step S4b, the last of moving its renderer here): begin and submit acquire a frame's command buffer and give a frame up; mapCells grows and maps the cell instance buffer for the embedder to write in place; uploadCells sends it after the atlas's dirty rows; record makes the frame's render pass, cleared to the embedder's colour, and draws its groups (Group: cells, then pictures, then decor, each a run of instances), a scroll's shifted rows clipped to the rectangle given, then the overlay. uploadAtlas, drawImages, pushLayerOpacity and drawOverlay are Cells' own now. --- src/gpu/Cells.zig | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 5 deletions(-) diff --git a/src/gpu/Cells.zig b/src/gpu/Cells.zig index 5055d31..9ed1741 100644 --- a/src/gpu/Cells.zig +++ b/src/gpu/Cells.zig @@ -1,7 +1,8 @@ //! pardes's cell renderer on SDL_GPU: the shaders' instance ABI //! (src/gpu/shaders/*.glsl), the pipelines over it, the glyph atlas's //! texture and its upload, the pictures' textures and their draws, the -//! overlay's triangles, and the resource helpers, moved from pardes's +//! overlay's triangles, a frame's uploads and its render pass, and the +//! resource helpers, moved from pardes's //! gui.zig as they were. `c` is the embedder's own SDL translation (pardes's: //! SDL3 and font.h), so the embedder's SDL is the one linked; the SPIR-V is //! the build's (`.spv` imports, from src/gpu/shaders/). @@ -129,12 +130,120 @@ pub fn makeAddonPipeline(cells: *Cells) !void { cells.addon_pipeline = try makePipeline(cells.device, cells.format, image_vert_spv, image_frag_spv, 1, 1, image_input, blend); } +/// What one group draws, in this order: its cells, its images, then its +/// decor. +pub const Group = struct { + cell_start: u32 = 0, + cell_count: u32 = 0, + image_start: u32 = 0, + image_count: u32 = 0, + decor_start: u32 = 0, + decor_count: u32 = 0, +}; + +/// The frame's command buffer: null when the device gives none. +pub fn begin(cells: *Cells) ?*c.SDL_GPUCommandBuffer { + return c.SDL_AcquireGPUCommandBuffer(cells.device); +} + +/// A frame given up: what it recorded so far is submitted, nothing drawn. +pub fn submit(cmd: *c.SDL_GPUCommandBuffer) void { + _ = c.SDL_SubmitGPUCommandBuffer(cmd); +} + +/// The frame's cell and decor instances, room for `n`, mapped for the +/// caller to write; uploadCells sends them. Null when the buffer will not +/// map. +pub fn mapCells(cells: *Cells, n: u32) !?[*]CellInstance { + try growInstances(cells.device, &cells.vbuf, &cells.vxfer, &cells.vbuf_cells, n, @sizeOf(CellInstance)); + const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(cells.device, cells.vxfer.?, false) orelse return null); + return @ptrCast(@alignCast(vptr)); +} + +/// The first `n` instances mapCells gave, sent after the atlas's rows drawn +/// since the last upload (uploadAtlas). +pub fn uploadCells(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, n: u32, atlas: *Atlas) void { + c.SDL_UnmapGPUTransferBuffer(cells.device, cells.vxfer.?); + + if (atlas.atlas_dirty_top < atlas.atlas_dirty_bottom) cells.uploadAtlas(cmd, atlas); + + const copy = c.SDL_BeginGPUCopyPass(cmd); + const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = cells.vxfer.?, .offset = 0 }; + const dst = c.SDL_GPUBufferRegion{ .buffer = cells.vbuf.?, .offset = 0, .size = n * @sizeOf(CellInstance) }; + c.SDL_UploadToGPUBuffer(copy, &src, &dst, false); + c.SDL_EndGPUCopyPass(copy); +} + +/// The frame's render pass into `target`, `sw` by `sh`, cleared to `clear`: +/// group by group, bottom to top, its cells (the first group's followed by +/// a scroll's `shifted` rows, after the `cell_total` instances, clipped to +/// `scroll_clip`), its pictures and its decor (after the shifted rows), at +/// the layer opacity `opacity`, the `ink` group's decor at 1; then the +/// overlay's first `overlay_count` vertices. +pub fn record( + cells: *Cells, + cmd: *c.SDL_GPUCommandBuffer, + target: *c.SDL_GPUTexture, + sw: u32, + sh: u32, + clear: c.SDL_FColor, + opacity: f32, + groups: []const Group, + ink: usize, + cell_total: u32, + shifted: u32, + scroll_clip: c.SDL_Rect, + overlay_count: u32, +) void { + var color_target = std.mem.zeroes(c.SDL_GPUColorTargetInfo); + color_target.texture = target; + color_target.clear_color = clear; + color_target.load_op = c.SDL_GPU_LOADOP_CLEAR; + color_target.store_op = c.SDL_GPU_STOREOP_STORE; + + const rp = c.SDL_BeginGPURenderPass(cmd, &color_target, 1, null); + pushLayerOpacity(cmd, rp, opacity); + const whole = c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(sw), .h = @intCast(sh) }; + const decor_base = cell_total + shifted; + for (groups, 0..) |group, index| { + const has_shifted = index == 0 and shifted != 0; + if ((group.cell_count != 0 or has_shifted) and cells.vbuf != null) { + c.SDL_BindGPUGraphicsPipeline(rp, cells.pipeline); + const samp_binding = c.SDL_GPUTextureSamplerBinding{ .texture = cells.atlas_tex, .sampler = cells.atlas_sampler }; + c.SDL_BindGPUFragmentSamplers(rp, 0, &samp_binding, 1); + if (group.cell_count != 0) { + const binding = c.SDL_GPUBufferBinding{ .buffer = cells.vbuf.?, .offset = group.cell_start * @sizeOf(CellInstance) }; + c.SDL_BindGPUVertexBuffers(rp, 0, &binding, 1); + c.SDL_DrawGPUPrimitives(rp, 6, group.cell_count, 0, 0); + } + if (has_shifted) { + c.SDL_SetGPUScissor(rp, &scroll_clip); + const binding = c.SDL_GPUBufferBinding{ .buffer = cells.vbuf.?, .offset = cell_total * @sizeOf(CellInstance) }; + c.SDL_BindGPUVertexBuffers(rp, 0, &binding, 1); + c.SDL_DrawGPUPrimitives(rp, 6, shifted, 0, 0); + c.SDL_SetGPUScissor(rp, &whole); + } + } + if (group.image_count != 0) cells.drawImages(rp, group.image_start, group.image_count, whole); + if (group.decor_count != 0 and cells.vbuf != null) { + c.SDL_BindGPUGraphicsPipeline(rp, cells.decor_pipeline); + // Ink keeps its colour at any WindowOpacity. + pushLayerOpacity(cmd, rp, if (index == ink) 1 else opacity); + const binding = c.SDL_GPUBufferBinding{ .buffer = cells.vbuf.?, .offset = (decor_base + group.decor_start) * @sizeOf(CellInstance) }; + c.SDL_BindGPUVertexBuffers(rp, 0, &binding, 1); + c.SDL_DrawGPUPrimitives(rp, 6, group.decor_count, 0, 0); + } + } + cells.drawOverlay(cmd, rp, overlay_count, opacity); + c.SDL_EndGPURenderPass(rp); +} + /// Uploads only the atlas rows drawn into since the last upload. Glyphs fill /// the atlas pen-style, a band of rows at a time, so new glyphs are a band a /// cell or two tall, not the whole 4 MB texture. The band sits at the same /// offset in the transfer buffer as in the stage, and the texture keeps the /// rows outside it. -pub fn uploadAtlas(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, atlas: *Atlas) void { +fn uploadAtlas(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, atlas: *Atlas) void { const atlas_w = Atlas.atlas_w; const top = atlas.atlas_dirty_top; const rows = atlas.atlas_dirty_bottom - top; @@ -277,7 +386,7 @@ pub fn uploadImages(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, n: u32) void { /// Draws `count` of the frame's pictures from `start`, each clipped to its /// rectangle; the scissor is the whole target, `whole`, after. -pub fn drawImages(cells: *Cells, rp: ?*c.SDL_GPURenderPass, start: u32, count: u32, whole: c.SDL_Rect) void { +fn drawImages(cells: *Cells, rp: ?*c.SDL_GPURenderPass, start: u32, count: u32, whole: c.SDL_Rect) void { const pass = rp orelse return; c.SDL_BindGPUGraphicsPipeline(pass, cells.image_pipeline); var idx = start; @@ -318,7 +427,7 @@ pub fn uploadOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, vertices: []co return true; } -pub fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPass, opacity: f32) void { +fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPass, opacity: f32) void { const uniforms = [4]f32{ opacity, 0, 0, 0 }; c.SDL_PushGPUFragmentUniformData(cmd, 0, &uniforms, @sizeOf(@TypeOf(uniforms))); // The source alpha is geometric/image coverage, not window opacity. Its @@ -329,7 +438,7 @@ pub fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPas /// The overlay's first `vertex_count` vertices (uploadOverlay), drawn at /// the layer opacity `opacity`. -pub fn drawOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, rp: ?*c.SDL_GPURenderPass, vertex_count: u32, opacity: f32) void { +fn drawOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, rp: ?*c.SDL_GPURenderPass, vertex_count: u32, opacity: f32) void { if (vertex_count == 0) return; const pass = rp orelse return; c.SDL_BindGPUGraphicsPipeline(pass, cells.overlay_pipeline); -- cgit v1.3