use a hashmap for CommandType conversion
This commit is contained in:
parent
8b189914f7
commit
c534721e88
1 changed files with 27 additions and 20 deletions
47
src/lang.zig
47
src/lang.zig
|
@ -52,33 +52,32 @@ pub const Command = struct {
|
||||||
pub const CommandList = std.ArrayList(*Command);
|
pub const CommandList = std.ArrayList(*Command);
|
||||||
pub const ArgList = std.ArrayList([]const u8);
|
pub const ArgList = std.ArrayList([]const u8);
|
||||||
|
|
||||||
pub fn commandToCmdType(command: []const u8) ParseError!CommandType {
|
pub const PluginKeywords = [_]CommandType{ .Amp, .RFlanger };
|
||||||
// TODO make a hashmap for those
|
pub const KeywordMap = std.AutoHashMap([]const u8, CommandType);
|
||||||
if (std.mem.eql(u8, command, "noop")) {
|
|
||||||
return CommandType.Noop;
|
|
||||||
} else if (std.mem.eql(u8, command, "load")) {
|
|
||||||
return CommandType.Load;
|
|
||||||
} else if (std.mem.eql(u8, command, "amp")) {
|
|
||||||
return CommandType.Amp;
|
|
||||||
} else if (std.mem.eql(u8, command, "rflanger")) {
|
|
||||||
return CommandType.RFlanger;
|
|
||||||
} else if (std.mem.eql(u8, command, "quicksave")) {
|
|
||||||
return CommandType.Quicksave;
|
|
||||||
} else {
|
|
||||||
std.debug.warn("Unknown command: '{}'\n", command);
|
|
||||||
return ParseError.UnknownCommand;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const Lang = struct {
|
pub const Lang = struct {
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
|
keywords: KeywordMap,
|
||||||
|
|
||||||
pub fn init(allocator: *std.mem.Allocator) Lang {
|
pub fn init(allocator: *std.mem.Allocator) Lang {
|
||||||
return Lang{ .allocator = allocator };
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(self: *Lang, data: []const u8) !CommandList {
|
pub fn parse(self: *Lang, data: []const u8) !CommandList {
|
||||||
var splitted_it = std.mem.separate(data, ";");
|
var splitted_it = std.mem.separate(data, ";");
|
||||||
|
try self.fillKeywords();
|
||||||
var cmds = CommandList.init(self.allocator);
|
var cmds = CommandList.init(self.allocator);
|
||||||
|
|
||||||
while (splitted_it.next()) |stmt_orig| {
|
while (splitted_it.next()) |stmt_orig| {
|
||||||
|
@ -93,9 +92,17 @@ pub const Lang = struct {
|
||||||
|
|
||||||
var cmd_opt = tok_it.next();
|
var cmd_opt = tok_it.next();
|
||||||
if (cmd_opt == null) return ParseError.NoCommandGiven;
|
if (cmd_opt == null) return ParseError.NoCommandGiven;
|
||||||
|
|
||||||
var command = cmd_opt.?;
|
var command = cmd_opt.?;
|
||||||
var ctype = try commandToCmdType(command);
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
var args = ArgList.init(self.allocator);
|
var args = ArgList.init(self.allocator);
|
||||||
|
|
||||||
while (tok_it.next()) |arg| {
|
while (tok_it.next()) |arg| {
|
||||||
|
|
Loading…
Reference in a new issue