Use comptime for fully declarative LV2 and Custom commands #14
1 changed files with 47 additions and 18 deletions
65
src/lang.zig
65
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;
|
||||
|
|
Loading…
Reference in a new issue