Compare commits

...

5 Commits

Author SHA1 Message Date
Luna 1987c4d497 fix command parsing 2022-04-27 20:01:14 -03:00
Luna 5d1bcf33ee fixes for latest zig: a lot of stuff 2022-04-27 20:01:12 -03:00
Luna c817170a04 port to latest zig: use std.log 2022-04-27 20:01:09 -03:00
Luna 268be1074c port to latest zig fs api 2022-04-27 20:01:06 -03:00
Luna d7d4385242 fix memory leak on string arguments 2022-04-27 20:01:00 -03:00
11 changed files with 175 additions and 134 deletions

View File

@ -24,19 +24,19 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
for (possible_lilv_include_dirs) |possible_lilv_dir| { for (possible_lilv_include_dirs) |possible_lilv_dir| {
var possible_dir = std.fs.cwd().openDir(possible_lilv_dir, .{}) catch |err| { var possible_dir = std.fs.cwd().openDir(possible_lilv_dir, .{}) catch |err| {
std.debug.warn("possible lilv {s} fail: {s}\n", .{ possible_lilv_dir, err }); std.debug.print("possible lilv {s} fail: {s}\n", .{ possible_lilv_dir, err });
continue; continue;
}; };
possible_dir.close(); possible_dir.close();
found_any_lilv = true; found_any_lilv = true;
std.debug.warn("found lilv at '{s}'\n", .{possible_lilv_dir}); std.debug.print("found lilv at '{s}'\n", .{possible_lilv_dir});
step.addIncludeDir(possible_lilv_dir); step.addIncludeDir(possible_lilv_dir);
} }
if (!found_any_lilv) { if (!found_any_lilv) {
std.debug.warn("No LILV library was found :(\n", .{}); std.debug.print("No LILV library was found :(\n", .{});
@panic("no lilv found"); @panic("no lilv found");
} }
} }

View File

@ -1,11 +1,10 @@
const std = @import("std"); const std = @import("std");
const log = std.log.scoped(.scritcher_bmp);
pub const BMPValidError = error{InvalidMagic}; pub const BMPValidError = error{InvalidMagic};
const VALID_MAGICS = [_][]const u8{ const VALID_MAGICS = [_][]const u8{
"BM", "BM",
"BA", "BA",
"CI", "CI",
"CP", "CP",
"IC", "IC",
@ -20,7 +19,7 @@ pub fn magicValid(magic: []const u8) !void {
} }
if (!valid) { if (!valid) {
std.debug.warn("\tINVALID HEADER: '{s}'\n", .{magic}); log.debug("\tINVALID HEADER: '{s}'\n", .{magic});
return BMPValidError.InvalidMagic; return BMPValidError.InvalidMagic;
} }
} }

View File

@ -4,6 +4,7 @@ const lv2 = @import("lv2_helpers.zig");
const plugins = @import("plugin.zig"); const plugins = @import("plugin.zig");
const image = @import("image.zig"); const image = @import("image.zig");
const log = std.log.scoped(.scritcher_custom);
const c = lv2.c; const c = lv2.c;
const RunBuffers = plugins.RunBuffers; const RunBuffers = plugins.RunBuffers;
@ -11,11 +12,11 @@ const RunBuffers = plugins.RunBuffers;
pub const RandomNoise = struct { pub const RandomNoise = struct {
r: std.rand.DefaultPrng, r: std.rand.DefaultPrng,
rand_buf: ?[]f32 = null, rand_buf: ?[]f32 = null,
allocator: ?*std.mem.Allocator = null, allocator: ?std.mem.Allocator = null,
cnt: usize = 0, cnt: usize = 0,
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
params: anytype, params: anytype,
) ?RandomNoise { ) ?RandomNoise {
var r = std.rand.DefaultPrng.init(params.seed); var r = std.rand.DefaultPrng.init(params.seed);
@ -24,7 +25,7 @@ pub const RandomNoise = struct {
var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null;
for (rand_buf) |_, idx| { for (rand_buf) |_, idx| {
rand_buf[idx] = r.random.float(f32); rand_buf[idx] = r.random().float(f32);
} }
return RandomNoise{ return RandomNoise{
@ -51,7 +52,7 @@ pub const RandomNoise = struct {
bufs.out[0] = rand_buf[self.cnt]; bufs.out[0] = rand_buf[self.cnt];
self.cnt += 1; self.cnt += 1;
} else { } else {
bufs.out[0] = self.r.random.float(f32); bufs.out[0] = self.r.random().float(f32);
} }
} }
}; };
@ -59,11 +60,11 @@ pub const RandomNoise = struct {
pub const WildNoise = struct { pub const WildNoise = struct {
r: std.rand.DefaultPrng, r: std.rand.DefaultPrng,
rand_buf: ?[]f32 = null, rand_buf: ?[]f32 = null,
allocator: ?*std.mem.Allocator = null, allocator: ?std.mem.Allocator = null,
cnt: usize = 0, cnt: usize = 0,
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
params: anytype, params: anytype,
) ?WildNoise { ) ?WildNoise {
var r = std.rand.DefaultPrng.init(params.seed); var r = std.rand.DefaultPrng.init(params.seed);
@ -72,7 +73,7 @@ pub const WildNoise = struct {
var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null;
for (rand_buf) |_, idx| { for (rand_buf) |_, idx| {
rand_buf[idx] = @intToFloat(f32, r.random.int(u1)); rand_buf[idx] = @intToFloat(f32, r.random().int(u1));
} }
return WildNoise{ return WildNoise{
@ -98,7 +99,7 @@ pub const WildNoise = struct {
bufs.out[0] = rand_buf[self.cnt]; bufs.out[0] = rand_buf[self.cnt];
self.cnt += 1; self.cnt += 1;
} else { } else {
bufs.out[0] = @intToFloat(f32, self.r.random.int(u1)); bufs.out[0] = @intToFloat(f32, self.r.random().int(u1));
} }
} }
}; };
@ -111,15 +112,18 @@ pub const Write = struct {
data: f32, data: f32,
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
params: anytype, params: anytype,
) Write { ) Write {
_ = allocator;
return Write{ return Write{
.data = params.data, .data = params.data,
}; };
} }
pub fn deinit(self: *Write) void {} pub fn deinit(self: *Write) void {
_ = self;
}
pub fn run(self: *Write, bufs: *RunBuffers) void { pub fn run(self: *Write, bufs: *RunBuffers) void {
bufs.out[0] = self.data; bufs.out[0] = self.data;
@ -127,13 +131,13 @@ pub const Write = struct {
}; };
pub const Embed = struct { pub const Embed = struct {
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
filepath: []const u8, filepath: []const u8,
sndfile: *c.SNDFILE = undefined, sndfile: *c.SNDFILE = undefined,
buf: []f32 = undefined, buf: []f32 = undefined,
pub fn init(allocator: *std.mem.Allocator, params: anytype) @This() { pub fn init(allocator: std.mem.Allocator, params: anytype) @This() {
return Embed{ return Embed{
.allocator = allocator, .allocator = allocator,
.filepath = params.path, .filepath = params.path,
@ -162,7 +166,9 @@ pub const Embed = struct {
self.buf = try self.allocator.alloc(f32, @intCast(usize, in_fmt.channels)); self.buf = try self.allocator.alloc(f32, @intCast(usize, in_fmt.channels));
} }
pub fn deinit(self: *@This()) void {} pub fn deinit(self: *@This()) void {
_ = self;
}
pub fn run(self: *@This(), bufs: *RunBuffers) void { pub fn run(self: *@This(), bufs: *RunBuffers) void {
const read_bytes = c.sf_readf_float(self.sndfile, self.buf.ptr, 1); const read_bytes = c.sf_readf_float(self.sndfile, self.buf.ptr, 1);
@ -174,7 +180,7 @@ pub const Embed = struct {
if (read_bytes < 0) { if (read_bytes < 0) {
const st: i32 = c.sf_error(self.sndfile); const st: i32 = c.sf_error(self.sndfile);
std.debug.warn("Failed to read {s} ({s})\n", .{ log.debug("Failed to read {s} ({s})\n", .{
self.filepath, self.filepath,
c.sf_error_number(st), c.sf_error_number(st),
}); });

View File

@ -3,6 +3,7 @@ const lv2 = @import("lv2_helpers.zig");
const c = lv2.c; const c = lv2.c;
const bmp = @import("bmp_valid.zig"); const bmp = @import("bmp_valid.zig");
const log = std.log.scoped(.scritcher_image);
const plugins = @import("plugin.zig"); const plugins = @import("plugin.zig");
/// Buffer size for main image copying. /// Buffer size for main image copying.
@ -20,7 +21,7 @@ pub const ImageError = error{
/// Low level integration function with libsndfile. /// Low level integration function with libsndfile.
pub fn sopen( pub fn sopen(
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
path: []const u8, path: []const u8,
mode: i32, mode: i32,
fmt: *c.SF_INFO, fmt: *c.SF_INFO,
@ -32,7 +33,7 @@ pub fn sopen(
const st: i32 = c.sf_error(file); const st: i32 = c.sf_error(file);
if (st != 0) { if (st != 0) {
std.debug.warn("Failed to open {s} ({s})\n", .{ log.debug("Failed to open {s} ({s})\n", .{
path, path,
c.sf_error_number(st), c.sf_error_number(st),
}); });
@ -42,12 +43,12 @@ pub fn sopen(
const frames_on_end = c.sf_seek(file, 0, c.SEEK_END); const frames_on_end = c.sf_seek(file, 0, c.SEEK_END);
_ = c.sf_seek(file, 0, c.SEEK_SET); _ = c.sf_seek(file, 0, c.SEEK_SET);
std.testing.expectEqual(fmt.frames, frames_on_end); try std.testing.expectEqual(fmt.frames, frames_on_end);
const frames_on_end_by_end = c.sf_seek(file, frames_on_end, c.SEEK_SET); const frames_on_end_by_end = c.sf_seek(file, frames_on_end, c.SEEK_SET);
std.testing.expectEqual(frames_on_end, frames_on_end_by_end); try std.testing.expectEqual(frames_on_end, frames_on_end_by_end);
std.debug.warn("frames on end: {}, frame on end (2): {}\n", .{ frames_on_end, frames_on_end_by_end }); log.debug("frames on end: {}, frame on end (2): {}\n", .{ frames_on_end, frames_on_end_by_end });
return file.?; return file.?;
} }
@ -56,7 +57,7 @@ pub fn swrite(file: *c.SNDFILE, buf: [*]f32, frames: i64) !void {
const count = c.sf_writef_float(file, buf, frames); const count = c.sf_writef_float(file, buf, frames);
if (count != frames) { if (count != frames) {
std.debug.warn("Wanted to write {}, got {}\n", .{ frames, count }); log.debug("Wanted to write {}, got {}\n", .{ frames, count });
return ImageError.WriteFail; return ImageError.WriteFail;
} }
} }
@ -65,15 +66,15 @@ pub fn sseek(file: *c.SNDFILE, offset: usize) void {
const offset_i64 = @intCast(i64, offset); const offset_i64 = @intCast(i64, offset);
const frames = c.sf_seek(file, offset_i64, c.SEEK_SET); const frames = c.sf_seek(file, offset_i64, c.SEEK_SET);
const frames_current = c.sf_seek(file, 0, c.SEEK_CUR); const frames_current = c.sf_seek(file, 0, c.SEEK_CUR);
std.testing.expectEqual(frames, frames_current); std.debug.assert(frames == frames_current);
if (frames != offset_i64) { if (frames != offset_i64) {
std.debug.warn("failed to seek to {} (seeked {} frames, offset_i64={})\n", .{ offset, frames, offset_i64 }); log.debug("failed to seek to {} (seeked {} frames, offset_i64={})\n", .{ offset, frames, offset_i64 });
} }
} }
/// Caller owns the returned memory. /// Caller owns the returned memory.
pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_"; const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXXXXXXXXXXXXXXX"; const template = "/tmp/temp_XXXXXXXXXXXXXXXXXXXXX";
var nam = try allocator.alloc(u8, template.len); var nam = try allocator.alloc(u8, template.len);
@ -88,7 +89,7 @@ pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 {
while (i < 100) : (i += 1) { while (i < 100) : (i += 1) {
// generate a random uppercase letter, that is, 65 + random number. // generate a random uppercase letter, that is, 65 + random number.
for (fill) |_, f_idx| { for (fill) |_, f_idx| {
var idx = @intCast(u8, r.random.uintLessThan(u5, 24)); var idx = @intCast(u8, r.random().uintLessThan(u5, 24));
var letter = @as(u8, 65) + idx; var letter = @as(u8, 65) + idx;
fill[f_idx] = letter; fill[f_idx] = letter;
} }
@ -96,7 +97,7 @@ pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 {
// if we fail to access it, we assume it doesn't exist and return it. // if we fail to access it, we assume it doesn't exist and return it.
var tmp_file: std.fs.File = std.fs.cwd().openFile( var tmp_file: std.fs.File = std.fs.cwd().openFile(
nam, nam,
.{ .read = true, .write = false }, .{ .mode = .read_only },
) catch |err| { ) catch |err| {
if (err == error.FileNotFound) return nam else continue; if (err == error.FileNotFound) return nam else continue;
}; };
@ -121,7 +122,7 @@ pub fn mkSfInfo() c.SF_INFO {
} }
pub const Image = struct { pub const Image = struct {
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
/// Pointer to the underlying libsndfile's SNDFILE struct. /// Pointer to the underlying libsndfile's SNDFILE struct.
sndfile: *c.SNDFILE, sndfile: *c.SNDFILE,
@ -136,7 +137,7 @@ pub const Image = struct {
curpath: []const u8, curpath: []const u8,
/// Open a BMP image for later. /// Open a BMP image for later.
pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image { pub fn open(allocator: std.mem.Allocator, path: []const u8) !*Image {
var in_fmt = mkSfInfo(); var in_fmt = mkSfInfo();
var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
@ -181,7 +182,7 @@ pub const Image = struct {
pub fn close(self: *Image) void { pub fn close(self: *Image) void {
var st: i32 = c.sf_close(self.sndfile); var st: i32 = c.sf_close(self.sndfile);
if (st != 0) { if (st != 0) {
std.debug.warn("Failed to close {s} ({s})\n", .{ log.debug("Failed to close {s} ({s})\n", .{
self.path, self.path,
c.sf_error_number(st), c.sf_error_number(st),
}); });
@ -196,9 +197,7 @@ pub const Image = struct {
} }
pub fn read(self: *Image, file_chans: c_int, buf: []f32) bool { pub fn read(self: *Image, file_chans: c_int, buf: []f32) bool {
var file = file_opt.?; const n_read: c.sf_count_t = c.sf_readf_float(self.sndfile, buf.ptr, 1);
const n_read: c.sf_count_t = c.sf_readf_float(file, buf.ptr, 1);
const buf_chans = @intCast(c_int, buf.len); const buf_chans = @intCast(c_int, buf.len);
var i = file_chans - 1; var i = file_chans - 1;
@ -228,7 +227,7 @@ pub const Image = struct {
sseek(out_file, start); sseek(out_file, start);
while (i <= end) : (i += buf.len) { while (i <= end) : (i += buf.len) {
std.debug.warn("\t\ti={d}, buf.len={d}, end={d}\n", .{ i, buf.len, end }); log.debug("\t\ti={d}, buf.len={d}, end={d}\n", .{ i, buf.len, end });
sseek(self.sndfile, i); sseek(self.sndfile, i);
sseek(out_file, i); sseek(out_file, i);
@ -254,7 +253,7 @@ pub const Image = struct {
fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos {
const file_end = self.frames; const file_end = self.frames;
var seek_pos = position.seekPos(file_end); var seek_pos = position.seekPos(file_end);
std.debug.warn("\tstart {d} end {d}\n", .{ seek_pos.start, seek_pos.end }); log.debug("\tstart {d} end {d}\n", .{ seek_pos.start, seek_pos.end });
return seek_pos; return seek_pos;
} }
@ -267,7 +266,7 @@ pub const Image = struct {
self.curpath = path; self.curpath = path;
self.frames = @intCast(usize, in_fmt.frames); self.frames = @intCast(usize, in_fmt.frames);
std.debug.warn("\timage: reopened on '{s}' (frames={d}, fmt.frames={d})\n", .{ log.debug("\timage: reopened on '{s}' (frames={d}, fmt.frames={d})\n", .{
self.curpath, self.curpath,
self.frames, self.frames,
in_fmt.frames, in_fmt.frames,
@ -275,7 +274,7 @@ pub const Image = struct {
} }
pub fn checkValid(self: *Image) !void { pub fn checkValid(self: *Image) !void {
var file = try std.fs.cwd().openFile(self.path, .{ .read = true }); var file = try std.fs.cwd().openFile(self.path, .{ .mode = .read_only });
defer file.close(); defer file.close();
// main bmp header: // main bmp header:
@ -310,12 +309,12 @@ pub const Image = struct {
defer ctx.allocator.free(ports); defer ctx.allocator.free(ports);
if (ctx.n_audio_in > 2) { if (ctx.n_audio_in > 2) {
std.debug.warn("plugin <{s}> has more than two inputs.\n", .{plugin_uri}); log.debug("plugin <{s}> has more than two inputs.\n", .{plugin_uri});
return ImageError.InvalidPlugin; return ImageError.InvalidPlugin;
} }
if (ctx.n_audio_out > 2) { if (ctx.n_audio_out > 2) {
std.debug.warn("plugin <{s}> has more than two outputs.\n", .{plugin_uri}); log.debug("plugin <{s}> has more than two outputs.\n", .{plugin_uri});
return ImageError.InvalidPlugin; return ImageError.InvalidPlugin;
} }
@ -327,14 +326,14 @@ pub const Image = struct {
var 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 { const port = c.lilv_plugin_get_port_by_symbol(ctx.plugin, sym) orelse {
std.debug.warn("assert fail: symbol {s} not found on port\n", .{param.sym}); log.debug("assert fail: symbol {s} not found on port\n", .{param.sym});
return ImageError.InvalidSymbol; return ImageError.InvalidSymbol;
}; };
c.lilv_node_free(sym); c.lilv_node_free(sym);
var idx = c.lilv_port_get_index(ctx.plugin, port); var idx = c.lilv_port_get_index(ctx.plugin, port);
std.debug.warn("\tset sym={s}, idx={d} to val={}\n", .{ log.debug("\tset sym={s}, idx={d} to val={}\n", .{
param.sym, param.sym,
idx, idx,
param.value, param.value,
@ -345,7 +344,7 @@ pub const Image = struct {
// now we need to generate a temporary file and put the output of // now we need to generate a temporary file and put the output of
// running the plugin on that file // running the plugin on that file
var tmpnam = try temporaryName(self.allocator); var tmpnam = try temporaryName(self.allocator);
std.debug.warn("\trunning plugin from '{s}' to '{s}'\n", .{ self.curpath, tmpnam }); log.debug("\trunning plugin from '{s}' to '{s}'\n", .{ self.curpath, tmpnam });
var out_fmt = mkSfInfo(); var out_fmt = mkSfInfo();
var 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);
@ -378,7 +377,7 @@ pub const Image = struct {
sseek(self.sndfile, seek_pos.start); sseek(self.sndfile, seek_pos.start);
var i: usize = seek_pos.start; var i: usize = seek_pos.start;
std.debug.warn("\tseek pos start: {d} end: {d}\n", .{ seek_pos.start, seek_pos.end }); log.debug("\tseek pos start: {d} end: {d}\n", .{ seek_pos.start, seek_pos.end });
var inbuf = &rctx.buffers.in; var inbuf = &rctx.buffers.in;
var outbuf = &rctx.buffers.out; var outbuf = &rctx.buffers.out;
@ -389,7 +388,7 @@ pub const Image = struct {
const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1); const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1);
if (read_bytes == 0) { if (read_bytes == 0) {
std.debug.warn("WARN! reached EOF at idx={d}\n", .{i}); log.debug("WARN! reached EOF at idx={d}\n", .{i});
break; break;
} }
@ -418,11 +417,11 @@ pub const Image = struct {
try self.checkValid(); try self.checkValid();
var time_taken = timer.read(); var time_taken = timer.read();
std.debug.warn("\ttook {d:.2}ms running plugin\n", .{time_taken / std.time.us_per_ms}); log.debug("\ttook {d:.2}ms running plugin\n", .{time_taken / std.time.us_per_ms});
} }
pub fn saveTo(self: *Image, out_path: []const u8) !void { pub fn saveTo(self: *Image, out_path: []const u8) !void {
std.debug.warn("\timg: copy from '{s}' to '{s}'\n", .{ self.curpath, out_path }); log.debug("\timg: copy from '{s}' to '{s}'\n", .{ self.curpath, out_path });
try std.fs.copyFileAbsolute(self.curpath, out_path, .{}); try std.fs.copyFileAbsolute(self.curpath, out_path, .{});
} }
@ -450,7 +449,7 @@ pub const Image = struct {
// the code here is a copypaste of runPlugin() without the specific // the code here is a copypaste of runPlugin() without the specific
// lilv things. // lilv things.
var tmpnam = try temporaryName(self.allocator); var tmpnam = try temporaryName(self.allocator);
std.debug.warn("\trunning CUSTOM plugin from '{s}' to '{s}'\n", .{ self.curpath, tmpnam }); log.debug("\trunning CUSTOM plugin from '{s}' to '{s}'\n", .{ self.curpath, tmpnam });
var out_fmt = mkSfInfo(); var out_fmt = mkSfInfo();
var 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);
@ -477,7 +476,7 @@ pub const Image = struct {
sseek(self.sndfile, seek_pos.start); sseek(self.sndfile, seek_pos.start);
var i: usize = seek_pos.start; var i: usize = seek_pos.start;
std.debug.warn("\tseek pos start: {d} end: {d}\n", .{ seek_pos.start, seek_pos.end }); log.debug("\tseek pos start: {d} end: {d}\n", .{ seek_pos.start, seek_pos.end });
var inbuf = &bufs.in; var inbuf = &bufs.in;
var outbuf = &bufs.out; var outbuf = &bufs.out;
@ -485,7 +484,7 @@ pub const Image = struct {
while (i <= seek_pos.end) : (i += 1) { while (i <= seek_pos.end) : (i += 1) {
const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1); const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1);
if (read_bytes == 0) { if (read_bytes == 0) {
std.debug.warn("WARN! reached EOF at idx={d}\n", .{i}); log.debug("WARN! reached EOF at idx={d}\n", .{i});
break; break;
} }

View File

@ -3,6 +3,7 @@ const std = @import("std");
const plugin = @import("plugin.zig"); const plugin = @import("plugin.zig");
const custom = @import("custom.zig"); const custom = @import("custom.zig");
const log = std.log.scoped(.scritcher_lang);
pub const ParseError = error{ParseFail}; pub const ParseError = error{ParseFail};
pub const CommandType = enum { pub const CommandType = enum {
@ -146,7 +147,7 @@ pub const Command = struct {
} }
pub fn print(base: *const @This()) void { pub fn print(base: *const @This()) void {
std.debug.warn("tag: {s}\n", .{base.tag}); log.debug("tag: {s}\n", .{base.tag});
} }
pub const Noop = struct { pub const Noop = struct {
@ -470,7 +471,7 @@ pub const CommandList = struct {
list: CmdArrayList, list: CmdArrayList,
const Self = @This(); const Self = @This();
pub fn init(allocator: *std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
return .{ return .{
.list = CmdArrayList.init(allocator), .list = CmdArrayList.init(allocator),
}; };
@ -478,7 +479,25 @@ pub const CommandList = struct {
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
for (self.list.items) |cmd_ptr| { for (self.list.items) |cmd_ptr| {
self.list.allocator.destroy(cmd_ptr); //self.list.allocator.destroy(cmd_ptr);
inline for (@typeInfo(Command.Tag).Enum.fields) |field| {
if (cmd_ptr.tag == @field(Command.Tag, field.name)) {
const actual_tag =
@field(Command.Tag, field.name);
// if we find a match on the tag, we can get the type
const typ = Command.tagToType(actual_tag);
_ = typ;
// TODO fix
//inline for (@typeInfo(typ).Struct.fields) |cmd_field| {
// switch (cmd_field.field_type) {
// []u8, []const u8 => self.list.allocator.destroy(@field(typ, cmd_field.name)),
// else => {},
// }
//}
}
}
} }
self.list.deinit(); self.list.deinit();
} }
@ -490,18 +509,20 @@ pub const CommandList = struct {
/// A parser. /// A parser.
pub const Lang = struct { pub const Lang = struct {
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
has_error: bool = false, has_error: bool = false,
line: usize = 0, line: usize = 0,
pub fn init(allocator: *std.mem.Allocator) Lang { pub fn init(allocator: std.mem.Allocator) Lang {
return Lang{ return Lang{
.allocator = allocator, .allocator = allocator,
}; };
} }
pub fn deinit(self: *Lang) void {} pub fn deinit(self: *Lang) void {
_ = self;
}
pub fn reset(self: *Lang) void { pub fn reset(self: *Lang) void {
self.has_error = false; self.has_error = false;
@ -509,16 +530,16 @@ pub const Lang = struct {
} }
fn doError(self: *Lang, comptime fmt: []const u8, args: anytype) void { fn doError(self: *Lang, comptime fmt: []const u8, args: anytype) void {
std.debug.warn("ERROR! at line {}: ", .{self.line}); log.err("ERROR! at line {}: ", .{self.line});
std.debug.warn(fmt, args); log.err(fmt, args);
std.debug.warn("\n", .{}); log.err("\n", .{});
self.has_error = true; self.has_error = true;
} }
fn parseCommandArguments( fn parseCommandArguments(
self: *@This(), self: *@This(),
comptime command_struct: type, comptime command_struct: type,
tok_it: *std.mem.TokenIterator, tok_it: *std.mem.SplitIterator(u8),
commands: *CommandList, commands: *CommandList,
) !void { ) !void {
// Based on the command struct fields, we can parse the arguments. // Based on the command struct fields, we can parse the arguments.
@ -561,7 +582,7 @@ pub const Lang = struct {
f32 => try std.fmt.parseFloat(f32, arg), f32 => try std.fmt.parseFloat(f32, arg),
u64 => try std.fmt.parseInt(u64, arg, 10), u64 => try std.fmt.parseInt(u64, arg, 10),
usize => try std.fmt.parseInt(usize, arg, 10), usize => try std.fmt.parseInt(usize, arg, 10),
[]const u8 => arg, []const u8 => @as([]const u8, try self.allocator.dupe(u8, arg)),
else => @compileError("parameter struct has unsupported type " ++ @typeName(cmd_field.field_type)), else => @compileError("parameter struct has unsupported type " ++ @typeName(cmd_field.field_type)),
}; };
@ -586,11 +607,11 @@ pub const Lang = struct {
usize => try std.fmt.parseInt(usize, arg, 10), usize => try std.fmt.parseInt(usize, arg, 10),
i32 => try std.fmt.parseInt(i32, arg, 10), i32 => try std.fmt.parseInt(i32, arg, 10),
f32 => try std.fmt.parseFloat(f32, arg), f32 => try std.fmt.parseFloat(f32, arg),
[]const u8 => arg, []const u8 => @as([]const u8, try self.allocator.dupe(u8, arg)),
else => @compileError("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."), else => @compileError("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."),
}; };
std.debug.warn("parsing {s}, arg of type {s} => {any}\n", .{ log.debug("parsing {s}, arg of type {s} => {any}\n", .{
@typeName(command_struct), @typeName(command_struct),
@typeName(@TypeOf(argument_value)), @typeName(@TypeOf(argument_value)),
argument_value, argument_value,
@ -602,11 +623,11 @@ pub const Lang = struct {
cmd.base.tag = command_struct.base_tag; cmd.base.tag = command_struct.base_tag;
const command = cmd.base.cast(command_struct).?; const command = cmd.base.cast(command_struct).?;
std.debug.warn("cmd: {s}\n", .{command}); log.debug("cmd: {s}\n", .{command});
} }
pub fn parse(self: *Lang, data: []const u8) !CommandList { pub fn parse(self: *Lang, data: []const u8) !CommandList {
var splitted_it = std.mem.split(data, ";"); var splitted_it = std.mem.split(u8, data, ";");
var cmds = CommandList.init(self.allocator); var cmds = CommandList.init(self.allocator);
errdefer cmds.deinit(); errdefer cmds.deinit();
@ -619,7 +640,7 @@ pub const Lang = struct {
if (std.mem.startsWith(u8, stmt, "#")) continue; if (std.mem.startsWith(u8, stmt, "#")) continue;
// TODO better tokenizer instead of just tokenize(" ")...maybe???? // TODO better tokenizer instead of just tokenize(" ")...maybe????
var tok_it = std.mem.tokenize(stmt, " "); var tok_it = std.mem.split(u8, stmt, " ");
var cmd_opt = tok_it.next(); var cmd_opt = tok_it.next();
if (cmd_opt == null) { if (cmd_opt == null) {
@ -631,15 +652,23 @@ pub const Lang = struct {
var found: bool = false; var found: bool = false;
inline for (@typeInfo(Command).Struct.decls) |cmd_struct_decl| { inline for (@typeInfo(Command).Struct.decls) |cmd_struct_decl| {
switch (cmd_struct_decl.data) { const struct_name = cmd_struct_decl.name;
.Type => |typ| switch (@typeInfo(typ)) { const cmd_struct_type = @field(Command, struct_name);
.Struct => {},
else => continue, const info_of_info = @typeInfo(@TypeOf(cmd_struct_type));
},
switch (info_of_info) {
.Type => {},
else => continue,
}
const info = @typeInfo(cmd_struct_type);
switch (info) {
.Struct => {},
else => continue, else => continue,
} }
const struct_name = cmd_struct_decl.name;
comptime var lowered_command_name = [_]u8{0} ** struct_name.len; comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
comptime { comptime {
for (struct_name) |c, i| { for (struct_name) |c, i| {
@ -657,9 +686,8 @@ pub const Lang = struct {
// Attempting to use ComptimeHashMap hits compiler bugs and I'm // Attempting to use ComptimeHashMap hits compiler bugs and I'm
// not sure if we can map strings to *types* in it. // not sure if we can map strings to *types* in it.
if (std.mem.eql(u8, &lowered_command_name, command_string)) { if ((!found) and std.mem.eql(u8, &lowered_command_name, command_string)) {
found = true; found = true;
const cmd_struct_type = cmd_struct_decl.data.Type;
try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds); try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds);
} }
} }
@ -683,8 +711,8 @@ test "noop" {
var cmds = try lang.parse("noop;"); var cmds = try lang.parse("noop;");
defer cmds.deinit(); defer cmds.deinit();
std.testing.expectEqual(cmds.list.items.len, 1); try std.testing.expectEqual(cmds.list.items.len, 1);
std.testing.expectEqual(cmds.list.items[0].tag, .noop); try std.testing.expectEqual(cmds.list.items[0].tag, .noop);
} }
test "load, phaser, quicksave" { test "load, phaser, quicksave" {
@ -700,10 +728,10 @@ test "load, phaser, quicksave" {
var cmds = try lang.parse(prog); var cmds = try lang.parse(prog);
defer cmds.deinit(); defer cmds.deinit();
std.testing.expectEqual(cmds.list.items.len, 3); try std.testing.expectEqual(cmds.list.items.len, 3);
std.testing.expectEqual(cmds.list.items[0].tag, .load); try std.testing.expectEqual(cmds.list.items[0].tag, .load);
std.testing.expectEqual(cmds.list.items[1].tag, .phaser); try std.testing.expectEqual(cmds.list.items[1].tag, .phaser);
std.testing.expectEqual(cmds.list.items[2].tag, .quicksave); try std.testing.expectEqual(cmds.list.items[2].tag, .quicksave);
} }
test "load, phaser with errors, quicksave" { test "load, phaser with errors, quicksave" {
@ -716,9 +744,5 @@ test "load, phaser with errors, quicksave" {
\\quicksave; \\quicksave;
; ;
var cmds = lang.parse(prog) catch |err| { try std.testing.expectError(error.ParseFail, lang.parse(prog));
return;
};
unreachable;
} }

View File

@ -1,6 +1,8 @@
const std = @import("std"); const std = @import("std");
const plugin = @import("plugin.zig"); const plugin = @import("plugin.zig");
const log = std.log.scoped(.scritcher_lv2);
pub const c = @cImport({ pub const c = @cImport({
@cInclude("sndfile.h"); @cInclude("sndfile.h");
@cInclude("lilv/lilv.h"); @cInclude("lilv/lilv.h");
@ -21,7 +23,7 @@ const LV2_CORE__connectionOptional = Lv2Core("#connectionOptional");
pub fn lilv_instance_connect_port( pub fn lilv_instance_connect_port(
instance: [*c]c.LilvInstance, instance: [*c]c.LilvInstance,
port_index: u32, port_index: u32,
data_location: ?*c_void, data_location: ?*anyopaque,
) void { ) void {
instance.?.*.lv2_descriptor.?.*.connect_port.?(instance.?.*.lv2_handle, port_index, data_location); instance.?.*.lv2_descriptor.?.*.connect_port.?(instance.?.*.lv2_handle, port_index, data_location);
} }
@ -118,7 +120,7 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port {
if (c.lilv_port_is_a(ctx.plugin, lport, lv2_InputPort)) { if (c.lilv_port_is_a(ctx.plugin, lport, lv2_InputPort)) {
port.is_input = true; port.is_input = true;
} else if (!c.lilv_port_is_a(ctx.plugin, lport, lv2_OutputPort) and !port.optional) { } else if (!c.lilv_port_is_a(ctx.plugin, lport, lv2_OutputPort) and !port.optional) {
std.debug.warn("Port {d} is neither input or output\n", .{i}); log.debug("Port {d} is neither input or output\n", .{i});
return error.UnassignedIOPort; return error.UnassignedIOPort;
} }
@ -134,7 +136,7 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port {
ctx.n_audio_out += 1; ctx.n_audio_out += 1;
} }
} else if (!port.optional) { } else if (!port.optional) {
std.debug.warn("Port {d} has unsupported type\n", .{i}); log.debug("Port {d} has unsupported type\n", .{i});
return error.UnsupportedPortType; return error.UnsupportedPortType;
} }
} }

View File

@ -2,6 +2,7 @@
const std = @import("std"); const std = @import("std");
const images = @import("image.zig"); const images = @import("image.zig");
const log = std.log.scoped(.scritcher_magick);
const Image = images.Image; const Image = images.Image;
const mc = @cImport({ const mc = @cImport({
@ -28,6 +29,7 @@ pub const MagickContext = struct {
} }
pub fn doErr(self: *MagickContext) !void { pub fn doErr(self: *MagickContext) !void {
_ = self;
return error.WandError; return error.WandError;
} }
}; };
@ -39,7 +41,7 @@ fn magickLoad(image: *Image) !MagickContext {
var curpath = try std.cstr.addNullByte(image.allocator, image.curpath); var curpath = try std.cstr.addNullByte(image.allocator, image.curpath);
defer image.allocator.free(curpath); defer image.allocator.free(curpath);
std.debug.warn("loading '{s}'\n", .{curpath}); log.debug("loading '{s}'\n", .{curpath});
if (mc.MagickReadImage(mctx.wand, curpath.ptr) != 1) if (mc.MagickReadImage(mctx.wand, curpath.ptr) != 1)
return error.MagickReadFail; return error.MagickReadFail;
@ -54,12 +56,12 @@ fn magickSave(image: *Image, wand: *mc.MagickWand) !void {
var c_tmpnam = try std.cstr.addNullByte(allocator, tmpnam); var c_tmpnam = try std.cstr.addNullByte(allocator, tmpnam);
defer allocator.free(c_tmpnam); defer allocator.free(c_tmpnam);
std.debug.warn("\tmagick: saving to '{s}'..", .{c_tmpnam}); log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam});
if (mc.MagickWriteImage(wand, c_tmpnam.ptr) != 1) if (mc.MagickWriteImage(wand, c_tmpnam.ptr) != 1)
return error.MagickWriteFail; return error.MagickWriteFail;
std.debug.warn("OK\n", .{}); log.debug("OK\n", .{});
try image.reopen(tmpnam); try image.reopen(tmpnam);
} }

View File

@ -3,6 +3,8 @@ const langs = @import("lang.zig");
const runners = @import("runner.zig"); const runners = @import("runner.zig");
const printer = @import("printer.zig"); const printer = @import("printer.zig");
const log = std.log.scoped(.scritcher);
test "scritcher" { test "scritcher" {
_ = @import("lang.zig"); _ = @import("lang.zig");
_ = @import("runner.zig"); _ = @import("runner.zig");
@ -16,13 +18,13 @@ const readline = @cImport({
@cInclude("readline/history.h"); @cInclude("readline/history.h");
}); });
fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: *langs.Command) !langs.CommandList { fn wrapInCmdList(allocator: std.mem.Allocator, cmd: *langs.Command) !langs.CommandList {
var cmds = langs.CommandList.init(allocator); var cmds = langs.CommandList.init(allocator);
try cmds.append(cmd); try cmds.append(cmd);
return cmds; return cmds;
} }
fn copyCommandToHeap(allocator: *std.mem.Allocator, command: langs.Command, comptime tag: langs.Command.Tag) !*langs.Command { fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, comptime tag: langs.Command.Tag) !*langs.Command {
const CommandStruct = langs.Command.tagToType(tag); const CommandStruct = langs.Command.tagToType(tag);
const casted = command.cast(CommandStruct).?; const casted = command.cast(CommandStruct).?;
var heap_cmd = try allocator.create(CommandStruct); var heap_cmd = try allocator.create(CommandStruct);
@ -36,10 +38,12 @@ fn copyCommandToHeap(allocator: *std.mem.Allocator, command: langs.Command, comp
return &heap_cmd.base; return &heap_cmd.base;
} }
pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void { pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
var stdout_file = std.io.getStdOut(); var stdout_file = std.io.getStdOut();
const stdout = &stdout_file.writer(); const stdout = &stdout_file.writer();
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path")); const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
errdefer allocator.free(scri_path);
defer allocator.free(scri_path);
var file_read_opt: ?std.fs.File = std.fs.cwd().openFile(scri_path, .{}) catch |err| blk: { var file_read_opt: ?std.fs.File = std.fs.cwd().openFile(scri_path, .{}) catch |err| blk: {
if (err == error.FileNotFound) break :blk null; if (err == error.FileNotFound) break :blk null;
@ -54,11 +58,9 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
defer lang.deinit(); defer lang.deinit();
if (total_bytes > 0) { if (total_bytes > 0) {
// this MUST BE long lived (a reference to it is kept inside
// existing_cmds, and then passed along to cmds),
// we can't defer them here
var scri_existing = try allocator.alloc(u8, total_bytes); var scri_existing = try allocator.alloc(u8, total_bytes);
_ = try file_read_opt.?.read(scri_existing); _ = try file_read_opt.?.read(scri_existing);
defer allocator.free(scri_existing);
// we can't defer this directly because we copy the // we can't defer this directly because we copy the
// Command pointers to the cmds list. running deinit() directly // Command pointers to the cmds list. running deinit() directly
@ -89,9 +91,9 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
file_read.close(); file_read.close();
} }
var file = try std.fs.cwd().openFile(scri_path, .{ var file = try std.fs.cwd().createFile(scri_path, .{
.write = true,
.read = false, .read = false,
.truncate = true,
}); });
defer file.close(); defer file.close();
@ -132,7 +134,7 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
var rd_line = readline.readline("> "); var rd_line = readline.readline("> ");
if (rd_line == null) { if (rd_line == null) {
std.debug.warn("leaving from eof\n", .{}); log.debug("leaving from eof\n", .{});
break; break;
} }
readline.add_history(rd_line); readline.add_history(rd_line);
@ -200,14 +202,14 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
try printer.printList(cmds, stream); try printer.printList(cmds, stream);
continue; continue;
} else if (std.mem.eql(u8, line, "quit") or std.mem.eql(u8, line, "q")) { } else if (std.mem.eql(u8, line, "quit") or std.mem.eql(u8, line, "q")) {
std.debug.warn("leaving\n", .{}); log.debug("leaving\n", .{});
break; break;
} else if (std.mem.startsWith(u8, line, "#")) { } else if (std.mem.startsWith(u8, line, "#")) {
continue; continue;
} }
var cmds_parsed = lang.parse(line) catch |err| { var cmds_parsed = lang.parse(line) catch |err| {
std.debug.warn("repl: error while parsing: {}\n", .{err}); log.debug("repl: error while parsing: {}\n", .{err});
continue; continue;
}; };
@ -231,13 +233,13 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void {
} }
fn doHelp() void { fn doHelp() void {
std.debug.warn("scritcher!\n", .{}); log.debug("scritcher!\n", .{});
std.debug.warn("usage: scritcher [run|help|repl]\n", .{}); log.debug("usage: scritcher [run|help|repl]\n", .{});
std.debug.warn("\tscritcher run path_to_script.scri path_to_input_file.bmp\n", .{}); log.debug("\tscritcher run path_to_script.scri path_to_input_file.bmp\n", .{});
std.debug.warn("\tscritcher repl path_to_script.scri path_to_input_file.bmp\n", .{}); log.debug("\tscritcher repl path_to_script.scri path_to_input_file.bmp\n", .{});
} }
fn doRun(allocator: *std.mem.Allocator, args_it: anytype) !void { fn doRun(allocator: std.mem.Allocator, args_it: anytype) !void {
var lang = langs.Lang.init(allocator); var lang = langs.Lang.init(allocator);
defer lang.deinit(); defer lang.deinit();
@ -288,7 +290,7 @@ pub fn main() !void {
} else if (std.mem.eql(u8, cli_command, "run")) { } else if (std.mem.eql(u8, cli_command, "run")) {
return try doRun(allocator, &args_it); return try doRun(allocator, &args_it);
} else { } else {
std.debug.warn("unknown command: '{s}'\n", .{cli_command}); log.debug("unknown command: '{s}'\n", .{cli_command});
return error.UnknownCommand; return error.UnknownCommand;
} }
} }

View File

@ -3,6 +3,7 @@ const std = @import("std");
const lv2 = @import("lv2_helpers.zig"); const lv2 = @import("lv2_helpers.zig");
const c = lv2.c; const c = lv2.c;
const log = std.log.scoped(.scritcher_plugin);
const ImageError = @import("image.zig").ImageError; const ImageError = @import("image.zig").ImageError;
/// Control port /// Control port
@ -46,7 +47,7 @@ pub const Position = struct {
/// Represents the starting context for a single plugin run. /// Represents the starting context for a single plugin run.
pub const Context = struct { pub const Context = struct {
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
world: *c.LilvWorld, world: *c.LilvWorld,
plugin: *const c.LilvPlugin, plugin: *const c.LilvPlugin,
@ -74,9 +75,11 @@ pub const RunContext = struct {
instance: *c.LilvInstance, instance: *c.LilvInstance,
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
plugin: *const c.LilvPlugin, plugin: *const c.LilvPlugin,
) !RunContext { ) !RunContext {
_ = allocator; // TODO batch RunBuffers?
var 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); errdefer c.lilv_instance_free(instance);
@ -127,7 +130,7 @@ pub const RunContext = struct {
} }
}; };
pub fn makeContext(allocator: *std.mem.Allocator, plugin_uri: []const u8) !Context { pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Context {
const cstr_plugin_uri = try std.cstr.addNullByte(allocator, plugin_uri); const cstr_plugin_uri = try std.cstr.addNullByte(allocator, plugin_uri);
defer allocator.free(cstr_plugin_uri); defer allocator.free(cstr_plugin_uri);
@ -137,7 +140,7 @@ pub fn makeContext(allocator: *std.mem.Allocator, plugin_uri: []const u8) !Conte
c.lilv_world_load_all(world); c.lilv_world_load_all(world);
var 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 {
std.debug.warn("Invalid plugin URI <{s}>\n", .{plugin_uri}); log.debug("Invalid plugin URI <{s}>\n", .{plugin_uri});
return ImageError.InvalidPlugin; return ImageError.InvalidPlugin;
}; };
defer c.lilv_node_free(uri); defer c.lilv_node_free(uri);
@ -145,7 +148,7 @@ pub fn makeContext(allocator: *std.mem.Allocator, plugin_uri: []const u8) !Conte
const plugins: *const c.LilvPlugins = c.lilv_world_get_all_plugins(world).?; const plugins: *const c.LilvPlugins = c.lilv_world_get_all_plugins(world).?;
var 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 {
std.debug.warn("Plugin <{s}> not found\n", .{plugin_uri}); log.debug("Plugin <{s}> not found\n", .{plugin_uri});
return ImageError.UnknownPlugin; return ImageError.UnknownPlugin;
}; };

View File

@ -1,6 +1,8 @@
const std = @import("std"); const std = @import("std");
const langs = @import("lang.zig"); const langs = @import("lang.zig");
const log = std.log.scoped(.scritcher_printer);
fn printCommandWithParams(stream: anytype, command: anytype) !void { fn printCommandWithParams(stream: anytype, command: anytype) !void {
const Parameters = @TypeOf(command.parameters); const Parameters = @TypeOf(command.parameters);
try stream.print(" {d} {d}", .{ command.split, command.index }); try stream.print(" {d} {d}", .{ command.split, command.index });

View File

@ -5,6 +5,8 @@ const plugin = @import("plugin.zig");
const custom = @import("custom.zig"); const custom = @import("custom.zig");
const magick = @import("magick.zig"); const magick = @import("magick.zig");
const log = std.log.scoped(.scritcher_runner);
const Position = plugin.Position; const Position = plugin.Position;
const ParamList = plugin.ParamList; const ParamList = plugin.ParamList;
const ParamMap = plugin.ParamMap; const ParamMap = plugin.ParamMap;
@ -18,7 +20,7 @@ pub const RunError = error{
}; };
pub const Runner = struct { pub const Runner = struct {
allocator: *std.mem.Allocator, allocator: std.mem.Allocator,
/// The currently opened image in the runner /// The currently opened image in the runner
image: ?*Image = null, image: ?*Image = null,
@ -28,7 +30,7 @@ pub const Runner = struct {
args: []const [:0]u8, args: []const [:0]u8,
pub fn init(allocator: *std.mem.Allocator, repl: bool) Runner { pub fn init(allocator: std.mem.Allocator, repl: bool) Runner {
return Runner{ return Runner{
.allocator = allocator, .allocator = allocator,
.repl = repl, .repl = repl,
@ -65,12 +67,12 @@ pub const Runner = struct {
// 'scritcher repl ./script ./image' // 'scritcher repl ./script ./image'
// ':0' should ALWAYS point to the image. // ':0' should ALWAYS point to the image.
if (self.repl) index += 4 else index += 3; if (self.repl) index += 3 else index += 3;
for (self.args) |arg, idx| { for (self.args) |arg, idx| {
std.debug.warn("arg{d} = {s}\n", .{ idx, arg }); log.debug("arg{d} = {s}\n", .{ idx, arg });
} }
std.debug.warn("fetch arg idx={d}, val={s}\n", .{ index, self.args[index] }); log.debug("fetch arg idx={d}, val={s}\n", .{ index, self.args[index] });
return self.args[index]; return self.args[index];
} else { } else {
return load_path; return load_path;
@ -90,7 +92,7 @@ pub const Runner = struct {
fn loadCmd(self: *Runner, path_or_argidx: []const u8) !void { fn loadCmd(self: *Runner, path_or_argidx: []const u8) !void {
const load_path = try self.resolveArgPath(path_or_argidx); const load_path = try self.resolveArgPath(path_or_argidx);
std.debug.warn("\tload path: {s}\n", .{load_path}); log.debug("\tload path: {s}\n", .{load_path});
// we could use ImageMagick to convert from X to BMP // we could use ImageMagick to convert from X to BMP
// but i can't find an easy way to do things in memory. // but i can't find an easy way to do things in memory.
@ -100,7 +102,7 @@ pub const Runner = struct {
// krita/gimp and make it export a bmp and while in the program you can // krita/gimp and make it export a bmp and while in the program you can
// apply filters, etc. // apply filters, etc.
if (!std.mem.endsWith(u8, load_path, ".bmp") and !std.mem.endsWith(u8, load_path, ".ppm")) { if (!std.mem.endsWith(u8, load_path, ".bmp") and !std.mem.endsWith(u8, load_path, ".ppm")) {
std.debug.warn("Only BMP files are allowed to be loaded. Got path '{s}'\n", .{load_path}); log.debug("Only BMP files are allowed to be loaded. Got path '{s}'\n", .{load_path});
return RunError.NoBMP; return RunError.NoBMP;
} }
@ -115,7 +117,7 @@ pub const Runner = struct {
if (self.image) |image| { if (self.image) |image| {
return image; return image;
} else { } else {
std.debug.warn("image is required!\n", .{}); log.debug("image is required!\n", .{});
return RunError.ImageRequired; return RunError.ImageRequired;
} }
} }
@ -162,6 +164,8 @@ pub const Runner = struct {
// if N isn't a number, we just ignore that file // if N isn't a number, we just ignore that file
const idx_str = entry.name[entry_gidx + 2 .. entry_pidx]; const idx_str = entry.name[entry_gidx + 2 .. entry_pidx];
const idx = std.fmt.parseInt(usize, idx_str, 10) catch |err| { const idx = std.fmt.parseInt(usize, idx_str, 10) catch |err| {
log.debug("ignoring file {s}", .{@errorName(err)});
break :blk {}; break :blk {};
}; };
@ -201,7 +205,7 @@ pub const Runner = struct {
); );
defer proc.deinit(); defer proc.deinit();
std.debug.warn("running '{s} {s}'\n", .{ runqs.program, out_path }); log.debug("running '{s} {s}'\n", .{ runqs.program, out_path });
_ = try proc.spawnAndWait(); _ = try proc.spawnAndWait();
} }
@ -252,7 +256,7 @@ pub const Runner = struct {
cmd: lang.Command, cmd: lang.Command,
comptime tag: lang.Command.Tag, comptime tag: lang.Command.Tag,
) !void { ) !void {
comptime const typ = lang.Command.tagToType(tag); const typ = lang.Command.tagToType(tag);
const command = cmd.cast(typ).?; const command = cmd.cast(typ).?;
const ctype = typ.command_type; const ctype = typ.command_type;
switch (ctype) { switch (ctype) {
@ -313,6 +317,7 @@ pub const Runner = struct {
cmds: lang.CommandList, cmds: lang.CommandList,
debug_flag: bool, debug_flag: bool,
) !void { ) !void {
_ = debug_flag;
for (cmds.list.items) |cmd| { for (cmds.list.items) |cmd| {
cmd.print(); cmd.print();
try self.runCommand(cmd.*); try self.runCommand(cmd.*);
@ -321,18 +326,15 @@ pub const Runner = struct {
}; };
test "running noop" { test "running noop" {
const allocator = std.heap.direct_allocator; const allocator = std.testing.allocator;
var cmds = lang.CommandList.init(allocator); var cmds = lang.CommandList.init(allocator);
defer cmds.deinit(); defer cmds.deinit();
var cmd_ptr = try allocator.create(lang.Command); var command = lang.Command{ .tag = .noop };
cmd_ptr.* = lang.Command{ var noop = lang.Command.Noop{ .base = command };
.command = .Noop, try cmds.append(&noop.base);
.args = lang.ArgList.init(allocator),
};
try cmds.append(cmd_ptr);
var runner = Runner.init(allocator); var runner = Runner.init(allocator, false);
defer runner.deinit(); defer runner.deinit();
try runner.runCommands(cmds, false); try runner.runCommands(cmds, false);