2019-07-08 02:03:55 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2019-07-10 19:06:44 +00:00
|
|
|
const plugin = @import("plugin.zig");
|
|
|
|
|
2019-07-08 03:23:12 +00:00
|
|
|
pub const ParseError = error{
|
|
|
|
NoCommandGiven,
|
|
|
|
UnknownCommand,
|
2019-07-09 03:04:01 +00:00
|
|
|
ArgRequired,
|
2019-07-08 03:23:12 +00:00
|
|
|
};
|
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
pub const CommandType = enum {
|
|
|
|
Noop,
|
|
|
|
Load,
|
2019-07-10 19:06:44 +00:00
|
|
|
Quicksave,
|
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
Amp,
|
2019-07-10 16:24:03 +00:00
|
|
|
RFlanger,
|
2019-07-10 19:06:44 +00:00
|
|
|
Eq,
|
2019-07-10 19:24:43 +00:00
|
|
|
Phaser,
|
2019-07-08 02:03:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub const Command = struct {
|
|
|
|
command: CommandType,
|
2019-07-08 03:09:34 +00:00
|
|
|
args: ArgList,
|
2019-07-08 02:03:55 +00:00
|
|
|
|
|
|
|
pub fn print(self: *const Command) void {
|
2019-07-08 03:09:34 +00:00
|
|
|
std.debug.warn("cmd:{}\n", self.command);
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|
2019-07-09 01:40:52 +00:00
|
|
|
|
2019-07-09 03:04:01 +00:00
|
|
|
pub fn argAt(self: *const Command, idx: usize) ![]const u8 {
|
|
|
|
const args = self.args.toSliceConst();
|
|
|
|
|
|
|
|
if (idx > (args.len - 1)) {
|
|
|
|
std.debug.warn("Expected argument at index {}\n", idx);
|
|
|
|
return ParseError.ArgRequired;
|
|
|
|
}
|
|
|
|
|
|
|
|
return args[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn usizeArgAt(self: *const Command, idx: usize) !usize {
|
|
|
|
var arg = try self.argAt(idx);
|
|
|
|
return try std.fmt.parseInt(usize, arg, 10);
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:06:44 +00:00
|
|
|
pub fn consumePosition(self: *const Command) !plugin.Position {
|
|
|
|
return plugin.Position{
|
|
|
|
.split = try self.usizeArgAt(0),
|
|
|
|
.index = try self.usizeArgAt(1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-09 01:40:52 +00:00
|
|
|
pub fn intArgAt(self: *const Command, idx: usize) !i32 {
|
2019-07-09 03:04:01 +00:00
|
|
|
var arg = try self.argAt(idx);
|
2019-07-09 01:40:52 +00:00
|
|
|
return try std.fmt.parseInt(i32, arg, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn floatArgAt(self: *const Command, idx: usize) !f32 {
|
2019-07-09 03:04:01 +00:00
|
|
|
var arg = try self.argAt(idx);
|
2019-07-09 01:40:52 +00:00
|
|
|
return try std.fmt.parseFloat(f32, arg);
|
|
|
|
}
|
2019-07-08 02:03:55 +00:00
|
|
|
};
|
|
|
|
|
2019-07-08 03:09:34 +00:00
|
|
|
pub const CommandList = std.ArrayList(*Command);
|
|
|
|
pub const ArgList = std.ArrayList([]const u8);
|
|
|
|
|
2019-07-10 17:44:22 +00:00
|
|
|
pub const PluginKeywords = [_]CommandType{ .Amp, .RFlanger };
|
|
|
|
pub const KeywordMap = std.AutoHashMap([]const u8, CommandType);
|
2019-07-08 02:03:55 +00:00
|
|
|
|
|
|
|
pub const Lang = struct {
|
|
|
|
allocator: *std.mem.Allocator,
|
2019-07-10 17:44:22 +00:00
|
|
|
keywords: KeywordMap,
|
2019-07-08 02:03:55 +00:00
|
|
|
|
|
|
|
pub fn init(allocator: *std.mem.Allocator) Lang {
|
2019-07-10 17:44:22 +00:00
|
|
|
return Lang{
|
|
|
|
.allocator = allocator,
|
|
|
|
.keywords = KeywordMap.init(allocator),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fillKeywords(self: *Lang) !void {
|
|
|
|
_ = try self.keywords.put("noop", .Noop);
|
|
|
|
_ = try self.keywords.put("load", .Load);
|
|
|
|
_ = try self.keywords.put("quicksave", .Quicksave);
|
|
|
|
|
|
|
|
_ = try self.keywords.put("amp", .Amp);
|
|
|
|
_ = try self.keywords.put("rflanger", .RFlanger);
|
2019-07-10 19:06:44 +00:00
|
|
|
_ = try self.keywords.put("eq", .Eq);
|
2019-07-10 19:24:43 +00:00
|
|
|
_ = try self.keywords.put("phaser", .Phaser);
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 03:09:34 +00:00
|
|
|
pub fn parse(self: *Lang, data: []const u8) !CommandList {
|
2019-07-08 02:03:55 +00:00
|
|
|
var splitted_it = std.mem.separate(data, ";");
|
2019-07-10 17:44:22 +00:00
|
|
|
try self.fillKeywords();
|
2019-07-08 02:03:55 +00:00
|
|
|
var cmds = CommandList.init(self.allocator);
|
|
|
|
|
|
|
|
while (splitted_it.next()) |stmt_orig| {
|
|
|
|
var stmt = std.mem.trimRight(u8, stmt_orig, "\n");
|
2019-07-08 16:55:54 +00:00
|
|
|
stmt = std.mem.trimLeft(u8, stmt, "\n");
|
2019-07-08 02:03:55 +00:00
|
|
|
|
|
|
|
if (stmt.len == 0) continue;
|
2019-07-09 01:41:38 +00:00
|
|
|
if (stmt[0] == '#') continue;
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2019-07-08 03:09:34 +00:00
|
|
|
// TODO better tokenizer instead of just tokenize(" ");
|
|
|
|
var tok_it = std.mem.tokenize(stmt, " ");
|
|
|
|
|
2019-07-08 03:23:12 +00:00
|
|
|
var cmd_opt = tok_it.next();
|
|
|
|
if (cmd_opt == null) return ParseError.NoCommandGiven;
|
|
|
|
var command = cmd_opt.?;
|
2019-07-10 17:44:22 +00:00
|
|
|
|
|
|
|
var kv_opt = self.keywords.get(command);
|
|
|
|
var ctype: CommandType = undefined;
|
|
|
|
if (kv_opt) |kv| {
|
|
|
|
ctype = kv.value;
|
|
|
|
} else {
|
|
|
|
std.debug.warn("Unknown command: '{}'\n", command);
|
|
|
|
return ParseError.UnknownCommand;
|
|
|
|
}
|
|
|
|
|
2019-07-08 03:09:34 +00:00
|
|
|
var args = ArgList.init(self.allocator);
|
|
|
|
|
|
|
|
while (tok_it.next()) |arg| {
|
|
|
|
try args.append(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct final Command based on command
|
|
|
|
var cmd_ptr = try self.allocator.create(Command);
|
|
|
|
cmd_ptr.* = Command{ .command = ctype, .args = args };
|
|
|
|
try cmds.append(cmd_ptr);
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
};
|
2019-07-08 03:09:34 +00:00
|
|
|
|
|
|
|
// TODO tests
|