From 8ce844ceed75fb41abda0cecf2e461c8cff2f9dc Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:10:43 -0300 Subject: [PATCH 1/5] add validation for split/index args --- src/lang.zig | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index a455a1f..89ed749 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -362,9 +362,14 @@ pub const Command = struct { t4d: f32, t4a_db: f32, }); - pub const Moddelay = LV2Command(.moddelay, "http://plugin.org.uk/swh-plugins/modDelay", struct { - base: f32, - }); + + pub const Moddelay = LV2Command( + .moddelay, + "http://plugin.org.uk/swh-plugins/modDelay", + struct { + base: f32, + }, + ); pub const Multichorus = LV2Command(.multichorus, "http://calf.sourceforge.net/plugins/MultiChorus", struct { min_delay: f32, @@ -471,8 +476,20 @@ pub const Lang = struct { // arguments... if (is_lv2_command) { - cmd.split = try std.fmt.parseInt(usize, tok_it.next().?, 10); - cmd.index = try std.fmt.parseInt(usize, tok_it.next().?, 10); + const split = tok_it.next(); + if (split == null) { + self.doError("Expected split parameter, got EOL", .{}); + return; + } + + const index = tok_it.next(); + if (index == null) { + self.doError("Expected index parameter, got EOL", .{}); + return; + } + + cmd.split = try std.fmt.parseInt(usize, split.?, 10); + cmd.index = try std.fmt.parseInt(usize, index.?, 10); inline for (@typeInfo(@TypeOf(cmd.parameters)).Struct.fields) |cmd_field| { const arg = tok_it.next().?; From d9358ed79408a4b4a0a096a9922d2b01860fc92c Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:25:25 -0300 Subject: [PATCH 2/5] add lv2 parameter validation --- src/lang.zig | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index 89ed749..4db0cb7 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -453,7 +453,7 @@ pub const Lang = struct { self.line = 0; } fn doError(self: *Lang, comptime fmt: []const u8, args: var) void { - std.debug.warn("error at line {}: ", .{self.line}); + std.debug.warn("ERROR! at line {}: ", .{self.line}); std.debug.warn(fmt, args); std.debug.warn("\n", .{}); self.has_error = true; @@ -491,19 +491,21 @@ pub const Lang = struct { cmd.split = try std.fmt.parseInt(usize, split.?, 10); cmd.index = try std.fmt.parseInt(usize, index.?, 10); + // All parameters for LV2 plugins are f32. inline for (@typeInfo(@TypeOf(cmd.parameters)).Struct.fields) |cmd_field| { - const arg = tok_it.next().?; + const maybe_arg = tok_it.next(); + if (maybe_arg == null) { + self.doError("Expected parameter for {}, got nothing", .{cmd_field.name}); + return; + } + + const arg = maybe_arg.?; + const argument_value = switch (cmd_field.field_type) { f32 => try std.fmt.parseFloat(f32, arg), else => @compileError("LV2 parameter struct can only have f32 fields"), }; - std.debug.warn("parsing {}, arg of type {} => {}\n", .{ - @typeName(command_struct), - @typeName(@TypeOf(argument_value)), - argument_value, - }); - @field(cmd.parameters, cmd_field.name) = argument_value; } } else { From 82dc99d7d57e74e329799030bc5c95d7090ea7bc Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:26:27 -0300 Subject: [PATCH 3/5] remove unecessary switch --- src/lang.zig | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index 4db0cb7..73ece6a 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -500,13 +500,10 @@ pub const Lang = struct { } const arg = maybe_arg.?; + if (cmd_field.field_type != f32) + @compileError("LV2 parameter struct can only have f32 fields"); - const argument_value = switch (cmd_field.field_type) { - f32 => try std.fmt.parseFloat(f32, arg), - else => @compileError("LV2 parameter struct can only have f32 fields"), - }; - - @field(cmd.parameters, cmd_field.name) = argument_value; + @field(cmd.parameters, cmd_field.name) = try std.fmt.parseFloat(f32, arg); } } else { inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| { From ca751e58f7ea2aeb5b83be38154ca5dcb3791a86 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:33:19 -0300 Subject: [PATCH 4/5] add draft declarations for custom commands --- src/lang.zig | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/lang.zig b/src/lang.zig index 73ece6a..824a3bb 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -113,6 +113,13 @@ pub const Command = struct { .saturator => Saturator, .vintagedelay => Vintagedelay, + .noise => Noise, + .wildnoise => Wildnoise, + .write => Write, + .embed => Embed, + + .rotate => Rotate, + else => @panic("TODO"), }; } @@ -146,8 +153,37 @@ pub const Command = struct { pub const RunQS = struct { pub const base_tag = Tag.runqs; - program: []const u8, base: Command, + program: []const u8, + }; + + pub const Noise = CustomCommand(Tag.noise, custom.RandomNoise, struct { + seed: f32, + fill_bytes: f32, + }); + + pub const Wildnoise = CustomCommand(Tag.wildnoise, custom.WildNoise, struct { + seed: f32, + fill_bytes: f32, + }); + + pub const Write = CustomCommand(Tag.write, custom.Write, struct { + data: f32, + }); + + pub const Embed = struct { + pub const base_tag = Tag.embed; + base: Command, + split: usize, + index: usize, + path: []const u8, + }; + + pub const Rotate = struct { + pub const base_tag = Tag.rotate; + base: Command, + deg: usize, + bgfill: []const u8, }; pub const Amp = LV2Command( From e669b74ffb0b5444f2b77dc157c536bb524d5c8d Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:34:49 -0300 Subject: [PATCH 5/5] add CustomCommand function --- src/lang.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lang.zig b/src/lang.zig index 824a3bb..03a6c52 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -31,6 +31,23 @@ fn LV2Command( }; } +fn CustomCommand( + comptime tag: Command.tag, + comptime plugin: type, + comptime parameters: type, +) type { + return struct { + pub const base_tag = tag; + pub const command_type = CommandType.plugin_command; + pub const plugin_type = plugin; + + base: Command, + split: usize, + index: usize, + parameters: LV2Parameters, + }; +} + pub const Command = struct { tag: Tag, @@ -182,7 +199,7 @@ pub const Command = struct { pub const Rotate = struct { pub const base_tag = Tag.rotate; base: Command, - deg: usize, + deg: f32, bgfill: []const u8, };