Use comptime for fully declarative LV2 and Custom commands #14

Merged
luna merged 69 commits from declarative-commands into master 2020-06-02 21:37:47 +00:00
Showing only changes of commit dca5d7b644 - Show all commits

View file

@ -366,16 +366,40 @@ pub const Lang = struct {
) !void { ) !void {
// Based on the command struct fields, we can parse the arguments. // Based on the command struct fields, we can parse the arguments.
var cmd = try self.allocator.create(command_struct); 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,
};
// 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| { inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| {
comptime { comptime {
if (std.mem.eql(u8, cmd_field.name, "base")) { if (std.mem.eql(u8, cmd_field.name, "base")) {
continue; continue;
} }
} }
// TODO: crash when no arguments are left but we still need
// arguments...
const arg = tok_it.next().?; const arg = tok_it.next().?;
const argument_value = switch (cmd_field.field_type) { const argument_value = switch (cmd_field.field_type) {
usize => try std.fmt.parseInt(usize, arg, 10), usize => try std.fmt.parseInt(usize, arg, 10),
@ -385,9 +409,14 @@ pub const Lang = struct {
else => @panic("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."), 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; cmd.base.tag = command_struct.base_tag;