diff --git a/build.zig b/build.zig index 5f2ff5e..e969b99 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,8 @@ const std = @import("std"); +const builds = std.build; +const Builder = std.build.Builder; -fn setupLinks(step: *std.Build.Step.Compile) void { +fn setupLinks(step: *builds.LibExeObjStep) void { step.linkSystemLibrary("c"); step.linkSystemLibrary("lilv-0"); @@ -39,13 +41,13 @@ fn setupLinks(step: *std.Build.Step.Compile) void { } } -pub fn build(b: *std.Build) void { +pub fn build(b: *Builder) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const exe = b.addExecutable(.{ .name = "scritcher", - .root_source_file = b.path("src/main.zig"), + .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); @@ -62,7 +64,7 @@ pub fn build(b: *std.Build) void { run_step.dependOn(&run_cmd.step); const test_step = b.addTest(.{ - .root_source_file = b.path("src/main.zig"), + .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); diff --git a/src/image.zig b/src/image.zig index e20cfe7..3905ea5 100644 --- a/src/image.zig +++ b/src/image.zig @@ -26,10 +26,10 @@ pub fn sopen( mode: i32, fmt: *c.SF_INFO, ) !*c.SNDFILE { - const cstr_path = try allocator.dupeZ(u8, path); + var cstr_path = try allocator.dupeZ(u8, path); defer allocator.free(cstr_path); - const file = c.sf_open(cstr_path.ptr, mode, fmt); + var file = c.sf_open(cstr_path.ptr, mode, fmt); const st: i32 = c.sf_error(file); if (st != 0) { @@ -78,7 +78,7 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 { const template_start = "/temp/temp_"; const template = "/tmp/temp_XXXXXXXXXXXXXXXXXXXXX"; var nam = try allocator.alloc(u8, template.len); - std.mem.copyForwards(u8, nam, template); + std.mem.copy(u8, nam, template); const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp())))); var r = std.rand.DefaultPrng.init(seed); @@ -89,8 +89,8 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 { while (i < 100) : (i += 1) { // generate a random uppercase letter, that is, 65 + random number. for (fill, 0..) |_, f_idx| { - const idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24))); - const letter = @as(u8, 65) + idx; + var idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24))); + var letter = @as(u8, 65) + idx; fill[f_idx] = letter; } @@ -139,9 +139,9 @@ pub const Image = struct { /// Open a BMP image for later. pub fn open(allocator: std.mem.Allocator, path: []const u8) !*Image { var in_fmt = mkSfInfo(); - const sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); + var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); - const image = try allocator.create(Image); + var image = try allocator.create(Image); std.debug.assert(in_fmt.frames > @as(i64, 0)); std.debug.assert(in_fmt.seekable == @as(i32, 1)); @@ -160,10 +160,10 @@ pub const Image = struct { pub fn clone(self: *Image) !*Image { var in_fmt = mkSfInfo(); // clone sndfile - const sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); + var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); std.debug.assert(self.frames == @as(usize, @intCast(in_fmt.frames))); - const image = try self.allocator.create(Image); + var image = try self.allocator.create(Image); std.debug.assert(in_fmt.frames > @as(i64, 0)); std.debug.assert(in_fmt.seekable == @as(i32, 1)); @@ -180,7 +180,7 @@ pub const Image = struct { } pub fn close(self: *Image) void { - const st: i32 = c.sf_close(self.sndfile); + var st: i32 = c.sf_close(self.sndfile); if (st != 0) { log.debug("Failed to close {s} ({s})", .{ self.path, @@ -252,7 +252,7 @@ pub const Image = struct { fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { const file_end = self.frames; - const seek_pos = position.seekPos(file_end); + var seek_pos = position.seekPos(file_end); log.debug("\tstart {d} end {d}", .{ seek_pos.start, seek_pos.end }); return seek_pos; } @@ -321,10 +321,10 @@ pub const Image = struct { // now, for each param for the plugin, we find its port, and set // the value for the port there. for (params.items) |param| { - const sym_cstr = try self.allocator.dupeZ(u8, param.sym); + var sym_cstr = try self.allocator.dupeZ(u8, param.sym); defer self.allocator.free(sym_cstr); - const sym = c.lilv_new_string(ctx.world, sym_cstr.ptr); + var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr); const port = c.lilv_plugin_get_port_by_symbol(ctx.plugin, sym) orelse { log.debug("assert fail: symbol {s} not found on port", .{param.sym}); return ImageError.InvalidSymbol; @@ -332,7 +332,7 @@ pub const Image = struct { c.lilv_node_free(sym); - const idx = c.lilv_port_get_index(ctx.plugin, port); + var idx = c.lilv_port_get_index(ctx.plugin, port); log.debug("\tset sym={s}, idx={d} to val={}", .{ param.sym, idx, @@ -343,11 +343,11 @@ pub const Image = struct { // now we need to generate a temporary file and put the output of // running the plugin on that file - const tmpnam = try temporaryName(self.allocator); + var tmpnam = try temporaryName(self.allocator); log.debug("\trunning plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam }); var out_fmt = mkSfInfo(); - const out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); + var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); var rctx = try plugins.RunContext.init(self.allocator, ctx.plugin); defer rctx.deinit(); @@ -380,7 +380,7 @@ pub const Image = struct { log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end }); var inbuf = &rctx.buffers.in; - const outbuf = &rctx.buffers.out; + var outbuf = &rctx.buffers.out; while (i <= seek_pos.end) : (i += 1) { inbuf[0] = 0; @@ -416,7 +416,7 @@ pub const Image = struct { try self.reopen(tmpnam); try self.checkValid(); - const time_taken = timer.read(); + var time_taken = timer.read(); log.debug("\ttook {d:.2}ms running plugin", .{time_taken / std.time.us_per_ms}); } @@ -431,7 +431,7 @@ pub const Image = struct { position: plugins.Position, extra: anytype, ) !void { - const plugin_opt: ?Plugin = Plugin.init(self.allocator, extra); + var plugin_opt: ?Plugin = Plugin.init(self.allocator, extra); if (plugin_opt == null) { return ImageError.PluginLoadFail; } @@ -448,11 +448,11 @@ pub const Image = struct { // the code here is a copypaste of runPlugin() without the specific // lilv things. - const tmpnam = try temporaryName(self.allocator); + var tmpnam = try temporaryName(self.allocator); log.debug("\trunning CUSTOM plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam }); var out_fmt = mkSfInfo(); - const out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); + var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); var bufs = plugins.RunBuffers{}; const seek_pos = self.getSeekPos(position); @@ -478,8 +478,8 @@ pub const Image = struct { var i: usize = seek_pos.start; log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end }); - const inbuf = &bufs.in; - const outbuf = &bufs.out; + var inbuf = &bufs.in; + var outbuf = &bufs.out; while (i <= seek_pos.end) : (i += 1) { const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1); diff --git a/src/lang.zig b/src/lang.zig index 31e5a73..b18b0fb 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -143,19 +143,7 @@ pub const Command = struct { if (base.tag != T.base_tag) return null; - //const baseInt = @intFromPtr(base); - - //log.debug("casting from {d}", .{baseInt}); - //log.debug("aligns from 8? {d}", .{baseInt % 8}); - //log.debug("align T: {d} {s}", .{ @alignOf(*T), @typeName(T) }); - //log.debug("align base: {d} {s}", .{ @alignOf(*const @This()), @typeName(@This()) }); - const base_aligned: *const @This() = @alignCast(base); - - const parented = @as(*const T, @alignCast(@fieldParentPtr("base", base_aligned))); - - const ptr: *const T = @alignCast(parented); - //log.debug("align: {d}\n", .{@alignOf(@TypeOf(ptr))}); - return ptr; + return @fieldParentPtr(T, "base", base); } pub fn print(base: *const @This()) void { @@ -653,7 +641,7 @@ pub const Lang = struct { // TODO better tokenizer instead of just tokenize(" ")...maybe???? var tok_it = std.mem.splitSequence(u8, stmt, " "); - const cmd_opt = tok_it.next(); + var cmd_opt = tok_it.next(); if (cmd_opt == null) { self.doError("No command given", .{}); continue; @@ -681,14 +669,11 @@ pub const Lang = struct { } comptime var lowered_command_name = [_]u8{0} ** struct_name.len; - var runtime_lowered_command_name = [_]u8{0} ** struct_name.len; comptime { for (struct_name, 0..) |c, i| { lowered_command_name[i] = std.ascii.toLower(c); } } - const c_l = lowered_command_name; - std.mem.copyForwards(u8, &runtime_lowered_command_name, &c_l); // if we have a match, we know the proper struct type // to use. this actually works compared to storing command_struct @@ -700,7 +685,7 @@ pub const Lang = struct { // Attempting to use ComptimeHashMap hits compiler bugs and I'm // not sure if we can map strings to *types* in it. - if ((!found) and std.mem.eql(u8, &runtime_lowered_command_name, command_string)) { + if ((!found) and std.mem.eql(u8, &lowered_command_name, command_string)) { found = true; try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds); } diff --git a/src/lv2_helpers.zig b/src/lv2_helpers.zig index a59babd..1d3df64 100644 --- a/src/lv2_helpers.zig +++ b/src/lv2_helpers.zig @@ -64,13 +64,13 @@ pub const Port = struct { /// /// Caller owns returned memory. pub fn setupPorts(ctx: *plugin.Context) ![]Port { - const world = ctx.world; + var world = ctx.world; const n_ports: u32 = c.lilv_plugin_get_num_ports(ctx.plugin); var ports = try ctx.allocator.alloc(Port, n_ports); for (ports, 0..) |_, idx| { - const port: *Port = &ports[idx]; + var port: *Port = &ports[idx]; port.* = Port{ .lilv_port = null, .ptype = .Control, @@ -81,23 +81,23 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port { }; } - const values: []f32 = try ctx.allocator.alloc(f32, n_ports); + var values: []f32 = try ctx.allocator.alloc(f32, n_ports); defer ctx.allocator.free(values); c.lilv_plugin_get_port_ranges_float(ctx.plugin, null, null, values.ptr); - const lv2_InputPort = c.lilv_new_uri(world, LV2_CORE__InputPort.ptr).?; + var lv2_InputPort = c.lilv_new_uri(world, LV2_CORE__InputPort.ptr).?; //defer std.heap.c_allocator.destroy(lv2_InputPort); - const lv2_OutputPort = c.lilv_new_uri(world, LV2_CORE__OutputPort.ptr).?; + var lv2_OutputPort = c.lilv_new_uri(world, LV2_CORE__OutputPort.ptr).?; //defer std.heap.c_allocator.destroy(lv2_OutputPort); - const lv2_AudioPort = c.lilv_new_uri(world, LV2_CORE__AudioPort.ptr).?; + var lv2_AudioPort = c.lilv_new_uri(world, LV2_CORE__AudioPort.ptr).?; //defer std.heap.c_allocator.destroy(lv2_AudioPort); - const lv2_ControlPort = c.lilv_new_uri(world, LV2_CORE__ControlPort.ptr).?; + var lv2_ControlPort = c.lilv_new_uri(world, LV2_CORE__ControlPort.ptr).?; //defer std.heap.c_allocator.destroy(lv2_ControlPort); - const lv2_connection_string = c.lilv_new_uri(world, LV2_CORE__connectionOptional.ptr).?; + var lv2_connection_string = c.lilv_new_uri(world, LV2_CORE__connectionOptional.ptr).?; //defer std.heap.c_allocator.destroy(lv2_connection_string); var i: u32 = 0; diff --git a/src/magick.zig b/src/magick.zig index ab59f0a..e79f513 100644 --- a/src/magick.zig +++ b/src/magick.zig @@ -15,7 +15,7 @@ pub const MagickContext = struct { pub fn init() !MagickContext { mc.InitializeMagick(null); - const wand = mc.NewMagickWand(); + var wand = mc.NewMagickWand(); if (wand == null) return error.WandCreateFail; return MagickContext{ @@ -38,7 +38,7 @@ fn magickLoad(image: *Image) !MagickContext { var mctx = try MagickContext.init(); errdefer mctx.deinit(); - const curpath = try image.allocator.dupeZ(u8, image.curpath); + var curpath = try image.allocator.dupeZ(u8, image.curpath); defer image.allocator.free(curpath); log.debug("loading '{s}'", .{curpath}); @@ -52,8 +52,8 @@ fn magickLoad(image: *Image) !MagickContext { fn magickSave(image: *Image, wand: *mc.MagickWand) !void { const allocator = image.allocator; - const tmpnam = try images.temporaryName(allocator); - const c_tmpnam = try allocator.dupeZ(u8, tmpnam); + var tmpnam = try images.temporaryName(allocator); + var c_tmpnam = try allocator.dupeZ(u8, tmpnam); defer allocator.free(c_tmpnam); log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam}); @@ -72,7 +72,7 @@ pub fn runRotate(image: *Image, deg: f32, bgfill: []const u8) !void { var mctx = try magickLoad(image); defer mctx.deinit(); - const bg = mc.NewPixelWand(); + var bg = mc.NewPixelWand(); defer mc.DestroyPixelWand(bg); if (mc.PixelSetColor(bg, bgfill.ptr) != 1) diff --git a/src/main.zig b/src/main.zig index f21087e..7563392 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,8 +30,6 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt var heap_cmd = try allocator.create(CommandStruct); heap_cmd.* = casted.*; - log.debug("casted: {}", .{casted}); - log.debug("heap_cmd: {}", .{heap_cmd}); return &heap_cmd.base; } @@ -56,7 +54,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void { defer lang.deinit(); if (total_bytes > 0) { - const scri_existing = try allocator.alloc(u8, total_bytes); + var scri_existing = try allocator.alloc(u8, total_bytes); _ = try file_read_opt.?.read(scri_existing); defer allocator.free(scri_existing); @@ -96,7 +94,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void { defer file.close(); var out = file.writer(); - const stream = &out; + var stream = &out; // since we opened the file for writing, it becomes empty, so, to ensure // we don't fuck up later on, we print cmds before starting the repl @@ -120,7 +118,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void { // run the load command try runner.runCommands(cmds, true); - const wanted_runner: []const u8 = std.posix.getenv("SCRITCHER_RUNNER") orelse "ristretto"; + const wanted_runner: []const u8 = std.os.getenv("SCRITCHER_RUNNER") orelse "ristretto"; var runqs_cmd = langs.Command.RunQS{ .base = langs.Command{ .tag = langs.Command.Tag.runqs }, @@ -138,7 +136,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void { readline.add_history(rd_line); //defer std.heap.c_allocator.destroy(rd_line); - const line = rd_line[0..std.mem.len(rd_line)]; + var line = rd_line[0..std.mem.len(rd_line)]; if (std.mem.eql(u8, line, "push")) { const heap_cmd = switch (current.tag) { @@ -252,7 +250,7 @@ fn doRun(allocator: std.mem.Allocator, args_it: anytype) !void { // sadly, we read it all into memory. such is life const total_bytes = try file.getEndPos(); - const data = try allocator.alloc(u8, total_bytes); + var data = try allocator.alloc(u8, total_bytes); defer allocator.free(data); _ = try file.read(data); diff --git a/src/plugin.zig b/src/plugin.zig index 29ec224..f5f6517 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -36,7 +36,7 @@ pub const Position = struct { pub fn seekPos(self: Position, total_size: usize) SeekPos { std.debug.assert(self.index <= self.split); - const tot = total_size / self.split; + var tot = total_size / self.split; return SeekPos{ .start = self.index * tot, @@ -80,7 +80,7 @@ pub const RunContext = struct { ) !RunContext { _ = allocator; // TODO batch RunBuffers? - const instance = c.lilv_plugin_instantiate(plugin, @as(f64, 44100), null); + var instance = c.lilv_plugin_instantiate(plugin, @as(f64, 44100), null); errdefer c.lilv_instance_free(instance); if (instance == null) { @@ -102,7 +102,7 @@ pub const RunContext = struct { var o: usize = 0; for (ports, 0..) |_, p_idx| { - const p = @as(u32, @intCast(p_idx)); + var p = @as(u32, @intCast(p_idx)); var port: *lv2.Port = &ports[p_idx]; switch (port.ptype) { @@ -134,12 +134,12 @@ pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Contex const cstr_plugin_uri = try allocator.dupeZ(u8, plugin_uri); defer allocator.free(cstr_plugin_uri); - const world: *c.LilvWorld = c.lilv_world_new().?; + var world: *c.LilvWorld = c.lilv_world_new().?; errdefer c.lilv_world_free(world); c.lilv_world_load_all(world); - const uri: *c.LilvNode = c.lilv_new_uri(world, cstr_plugin_uri.ptr) orelse { + var uri: *c.LilvNode = c.lilv_new_uri(world, cstr_plugin_uri.ptr) orelse { log.debug("Invalid plugin URI <{s}>", .{plugin_uri}); return ImageError.InvalidPlugin; }; @@ -147,7 +147,7 @@ pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Contex const plugins: *const c.LilvPlugins = c.lilv_world_get_all_plugins(world).?; - const plugin: *const c.LilvPlugin = c.lilv_plugins_get_by_uri(plugins, uri) orelse { + var plugin: *const c.LilvPlugin = c.lilv_plugins_get_by_uri(plugins, uri) orelse { log.debug("Plugin <{s}> not found", .{plugin_uri}); return ImageError.UnknownPlugin; }; diff --git a/src/runner.zig b/src/runner.zig index 85b8d79..0eea50b 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -47,7 +47,7 @@ pub const Runner = struct { } pub fn clone(self: *Runner) !Runner { - const cloned_image = if (self.image) |image| try image.clone() else null; + var cloned_image = if (self.image) |image| try image.clone() else null; return Runner{ .allocator = self.allocator, .image = cloned_image, @@ -128,12 +128,12 @@ pub const Runner = struct { fn makeGlitchedPath(self: *Runner) ![]const u8 { // we want to transform basename, if it is "x.bmp" to "x_gN.bmp", where // N is the maximum non-used integer. - const image = try self.getImage(); + var image = try self.getImage(); const basename = std.fs.path.basename(image.path); const dirname = std.fs.path.dirname(image.path).?; - var dir = try std.fs.cwd().openDir(dirname, .{ .iterate = true }); + var dir = try std.fs.cwd().openIterableDir(dirname, .{}); defer dir.close(); const period_idx = std.mem.lastIndexOf(u8, basename, ".").?; @@ -194,7 +194,7 @@ pub const Runner = struct { try image.saveTo(out_path); } - fn runQSCmd(self: *Runner, cmd: *lang.Command) !void { + fn runQSCmd(self: *Runner, cmd: lang.Command) !void { const runqs = cmd.cast(lang.Command.RunQS).?; var image = try self.getImage(); const out_path = try self.makeGlitchedPath(); @@ -211,11 +211,11 @@ pub const Runner = struct { _ = try proc.spawnAndWait(); } - fn rotateCmd(self: *Runner, cmd: *lang.Command) !void { + fn rotateCmd(self: *Runner, cmd: lang.Command) !void { const rotate_cmd = cmd.cast(lang.Command.Rotate).?; - const image = try self.getImage(); - const c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill); + var image = try self.getImage(); + var c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill); defer self.allocator.free(c_bgfill); try magick.runRotate(image, rotate_cmd.deg, c_bgfill); @@ -255,7 +255,7 @@ pub const Runner = struct { fn runSingleCommand( self: *@This(), - cmd: *lang.Command, + cmd: lang.Command, comptime tag: lang.Command.Tag, ) !void { const typ = lang.Command.tagToType(tag); @@ -267,7 +267,7 @@ pub const Runner = struct { } } - fn runCommand(self: *@This(), cmd: *lang.Command) !void { + fn runCommand(self: *@This(), cmd: lang.Command) !void { switch (cmd.tag) { .noop => {}, .load => { @@ -322,7 +322,7 @@ pub const Runner = struct { _ = debug_flag; for (cmds.list.items) |cmd| { cmd.print(); - try self.runCommand(cmd); + try self.runCommand(cmd.*); } } }; @@ -332,7 +332,7 @@ test "running noop" { var cmds = lang.CommandList.init(allocator); defer cmds.deinit(); - const command = lang.Command{ .tag = .noop }; + var command = lang.Command{ .tag = .noop }; var noop = try allocator.create(lang.Command.Noop); noop.* = lang.Command.Noop{ .base = command };