From dca5d7b644683f1b4c31d84819b9864057ef6b09 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 17:08:35 -0300 Subject: [PATCH] parse lv2 commands' split/index automatically --- src/lang.zig | 65 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index 883c468..fbc5bf7 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -366,28 +366,57 @@ pub const Lang = struct { ) !void { // Based on the command struct fields, we can parse the arguments. var cmd = try self.allocator.create(command_struct); + const is_lv2_command = switch (command_struct.base_tag) { + .noop, .load, .quicksave, .runqs => false, + else => command_struct.command_type == .lv2_command, + }; - inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| { - comptime { - if (std.mem.eql(u8, cmd_field.name, "base")) { - continue; - } + // TODO: crash when no arguments are left but we still need + // 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); + + inline for (@typeInfo(@typeOf(command_struct.parameters)).Struct.fields) |cmd_field| { + const arg = tok_it.next().?; + 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.params, cmd_field.name) = argument_value; } + } else { + inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| { + comptime { + if (std.mem.eql(u8, cmd_field.name, "base")) { + continue; + } + } + const arg = tok_it.next().?; + const argument_value = switch (cmd_field.field_type) { + usize => try std.fmt.parseInt(usize, arg, 10), + i32 => try std.fmt.parseInt(i32, arg, 10), + f32 => try std.fmt.parseFloat(f32, arg), + []const u8 => try self.allocator.dupe(u8, arg), + else => @panic("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."), + }; - // TODO: crash when no arguments are left but we still need - // arguments... - const arg = tok_it.next().?; - const argument_value = switch (cmd_field.field_type) { - usize => try std.fmt.parseInt(usize, arg, 10), - i32 => try std.fmt.parseInt(i32, arg, 10), - f32 => try std.fmt.parseFloat(f32, arg), - []const u8 => try self.allocator.dupe(u8, arg), - else => @panic("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."), - }; + std.debug.warn("parsing {}, arg of type {} => {}\n", .{ + @typeName(command_struct), + @typeName(@TypeOf(argument_value)), + argument_value, + }); - std.debug.warn("parsing {}, arg of type {} => {}\n", .{ @typeName(command_struct), @typeName(@TypeOf(argument_value)), argument_value }); - - @field(cmd, cmd_field.name) = argument_value; + @field(cmd.params, cmd_field.name) = argument_value; + } } cmd.base.tag = command_struct.base_tag;