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,28 +366,57 @@ 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,
};
inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| { // TODO: crash when no arguments are left but we still need
comptime { // arguments...
if (std.mem.eql(u8, cmd_field.name, "base")) {
continue; 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 std.debug.warn("parsing {}, arg of type {} => {}\n", .{
// arguments... @typeName(command_struct),
const arg = tok_it.next().?; @typeName(@TypeOf(argument_value)),
const argument_value = switch (cmd_field.field_type) { argument_value,
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 }); @field(cmd.params, cmd_field.name) = argument_value;
}
@field(cmd, cmd_field.name) = argument_value;
} }
cmd.base.tag = command_struct.base_tag; cmd.base.tag = command_struct.base_tag;