parse lv2 commands' split/index automatically

This commit is contained in:
Luna 2020-05-31 17:08:35 -03:00
parent b21960d372
commit dca5d7b644

View file

@ -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;