Use comptime for fully declarative LV2 and Custom commands #14
1 changed files with 1 additions and 92 deletions
93
src/lang.zig
93
src/lang.zig
|
@ -335,7 +335,6 @@ pub const KeywordMap = std.StringHashMap(CommandType);
|
||||||
/// A parser.
|
/// A parser.
|
||||||
pub const Lang = struct {
|
pub const Lang = struct {
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
keywords: KeywordMap,
|
|
||||||
|
|
||||||
has_error: bool = false,
|
has_error: bool = false,
|
||||||
line: usize = 0,
|
line: usize = 0,
|
||||||
|
@ -343,105 +342,15 @@ pub const Lang = struct {
|
||||||
pub fn init(allocator: *std.mem.Allocator) Lang {
|
pub fn init(allocator: *std.mem.Allocator) Lang {
|
||||||
return Lang{
|
return Lang{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.keywords = KeywordMap.init(allocator),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Lang) void {
|
pub fn deinit(self: *Lang) void {}
|
||||||
self.keywords.deinit();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn reset(self: *Lang) void {
|
pub fn reset(self: *Lang) void {
|
||||||
self.has_error = false;
|
self.has_error = false;
|
||||||
self.line = 0;
|
self.line = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fillKeywords(self: *Lang) !void {
|
|
||||||
inline for (@typeInfo(NewCommand).Struct.decls) |cmd_struct_decl| {
|
|
||||||
switch (cmd_struct_decl.data) {
|
|
||||||
.Type => |typ| switch (@typeInfo(typ)) {
|
|
||||||
.Struct => {},
|
|
||||||
else => continue,
|
|
||||||
},
|
|
||||||
else => continue,
|
|
||||||
}
|
|
||||||
|
|
||||||
const struct_name = cmd_struct_decl.name;
|
|
||||||
|
|
||||||
comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
|
|
||||||
comptime {
|
|
||||||
for (struct_name) |c, i| {
|
|
||||||
lowered_command_name[i] = std.ascii.toLower(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = try self.keywords.put(&lowered_command_name, @field(CommandType, struct_name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getCommand(self: *Lang, stmt: []const u8) ?CommandType {
|
|
||||||
var kv_opt = self.keywords.get(stmt);
|
|
||||||
|
|
||||||
if (kv_opt) |kv| {
|
|
||||||
return kv.value;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expectAny(self: *Lang, count: usize, args: ArgList) !void {
|
|
||||||
if (args.items.len != count) {
|
|
||||||
self.doError("expected {} arguments, found {}", .{ count, args.items.len });
|
|
||||||
return error.ArgRequired;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expectSingle(self: *Lang, args: ArgList) !void {
|
|
||||||
return try self.expectAny(1, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expectFloat(self: *Lang, count: usize, args: ArgList) !void {
|
|
||||||
var i: usize = 0;
|
|
||||||
|
|
||||||
if (args.items.len != count) {
|
|
||||||
self.doError("expected {} arguments, found {}", .{ count, args.items.len });
|
|
||||||
return error.ArgRequired;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (i < count) : (i += 1) {
|
|
||||||
var arg = args.items[i];
|
|
||||||
_ = std.fmt.parseFloat(f32, arg) catch |err| {
|
|
||||||
std.debug.warn("failed to parse f32: {}\n", .{err});
|
|
||||||
return error.FloatParseFail;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validateCommand(self: *Lang, cmd: Command) !void {
|
|
||||||
switch (cmd.command) {
|
|
||||||
.Quicksave, .Noop => {},
|
|
||||||
.Load, .RunQS => try self.expectSingle(cmd.args),
|
|
||||||
.Amp => try self.expectFloat(3, cmd.args),
|
|
||||||
.RFlanger => try self.expectFloat(4, cmd.args),
|
|
||||||
.Eq => try self.expectFloat(5, cmd.args),
|
|
||||||
.Phaser => try self.expectFloat(6, cmd.args),
|
|
||||||
.Mbeq => try self.expectFloat(17, cmd.args),
|
|
||||||
.Chorus => try self.expectFloat(8, cmd.args),
|
|
||||||
.PitchScaler => try self.expectFloat(3, cmd.args),
|
|
||||||
.Reverb => try self.expectFloat(12, cmd.args),
|
|
||||||
.Highpass => try self.expectFloat(5, cmd.args),
|
|
||||||
.Delay => try self.expectFloat(12, cmd.args),
|
|
||||||
.Vinyl => try self.expectFloat(7, cmd.args),
|
|
||||||
.RevDelay => try self.expectFloat(7, cmd.args),
|
|
||||||
.Noise => try self.expectFloat(4, cmd.args),
|
|
||||||
.WildNoise => try self.expectFloat(4, cmd.args),
|
|
||||||
.Write => try self.expectFloat(3, cmd.args),
|
|
||||||
.Rotate => try self.expectAny(2, cmd.args),
|
|
||||||
.Embed => try self.expectAny(3, cmd.args),
|
|
||||||
else => std.debug.warn("WARN unchecked command {}\n", .{cmd.command}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn doError(self: *Lang, comptime fmt: []const u8, args: var) void {
|
fn doError(self: *Lang, comptime fmt: []const u8, args: var) void {
|
||||||
std.debug.warn("error at line {}: ", .{self.line});
|
std.debug.warn("error at line {}: ", .{self.line});
|
||||||
std.debug.warn(fmt, args);
|
std.debug.warn(fmt, args);
|
||||||
|
|
Loading…
Reference in a new issue