remove usage of command pointers for most things

this serves as a workaround for the compiler bugs found on zig trunk

 - use std.StringHashMap
This commit is contained in:
Luna 2019-09-03 12:45:17 -03:00
parent cdf2d843d2
commit b3bb56279a
3 changed files with 30 additions and 32 deletions

View File

@ -41,11 +41,11 @@ pub const Command = struct {
args: ArgList,
cur_idx: usize = 0,
pub fn print(self: *const Command) void {
pub fn print(self: Command) void {
std.debug.warn("cmd:{}\n", self.command);
}
pub fn argAt(self: *const Command, idx: usize) ![]const u8 {
pub fn argAt(self: Command, idx: usize) ![]const u8 {
const args = self.args.toSliceConst();
if (idx > (args.len - 1)) {
@ -56,7 +56,7 @@ pub const Command = struct {
return args[idx];
}
pub fn usizeArgAt(self: *const Command, idx: usize) !usize {
pub fn usizeArgAt(self: Command, idx: usize) !usize {
var arg = try self.argAt(idx);
return try std.fmt.parseInt(usize, arg, 10);
}
@ -69,18 +69,18 @@ pub const Command = struct {
};
}
pub fn intArgAt(self: *const Command, idx: usize) !i32 {
pub fn intArgAt(self: Command, idx: usize) !i32 {
var arg = try self.argAt(idx);
return try std.fmt.parseInt(i32, arg, 10);
}
pub fn floatArgAt(self: *const Command, idx: usize) !f32 {
pub fn floatArgAt(self: Command, idx: usize) !f32 {
var arg = try self.argAt(idx);
return try std.fmt.parseFloat(f32, arg);
}
pub fn floatArgMany(
self: *const Command,
self: Command,
allocator: *std.mem.Allocator,
start_index: usize,
elements: usize,
@ -124,25 +124,23 @@ pub const Command = struct {
self.cur_idx += 1;
_ = try map.put(symbol, val);
}
pub fn copy(self: Command, allocator: *std.mem.Allocator) !*Command {
var cmd = try allocator.create(Command);
cmd.* = Command{
.command = self.command,
.args = self.args,
.cur_idx = self.cur_idx,
};
return cmd;
}
};
pub const CommandList = std.ArrayList(*Command);
pub const CommandList = std.ArrayList(Command);
pub const ArgList = std.ArrayList([]const u8);
fn eqlu8(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
fn hashu8(key: []const u8) u32 {
var hasher = std.hash.Wyhash.init(0);
for (key) |value| {
std.hash.autoHash(&hasher, value);
}
return @truncate(u32, hasher.final());
}
pub const KeywordMap = std.HashMap([]const u8, CommandType, hashu8, eqlu8);
pub const KeywordMap = std.StringHashMap(CommandType);
pub const Lang = struct {
allocator: *std.mem.Allocator,
@ -290,7 +288,7 @@ pub const Lang = struct {
}
}
fn validateCommand(self: *Lang, cmd: *Command) !void {
fn validateCommand(self: *Lang, cmd: Command) !void {
switch (cmd.command) {
.Quicksave, .Noop => {},
.Load, .RunQS => try self.expectSingle(cmd.args),
@ -359,16 +357,13 @@ pub const Lang = struct {
}
// construct final Command based on command
var cmd_ptr = try self.allocator.create(Command);
errdefer self.allocator.destroy(cmd_ptr);
cmd_ptr.* = Command{ .command = ctype, .args = args };
self.validateCommand(cmd_ptr) catch |err| {
var cmd = Command{ .command = ctype, .args = args };
self.validateCommand(cmd) catch |err| {
self.doError("Unknown command '{}' (length {})", command, command.len);
continue;
};
try cmds.append(cmd_ptr);
try cmds.append(cmd);
}
if (self.has_error) return ParseError.ParseFail;

View File

@ -16,7 +16,7 @@ pub const Param = struct {
/// List of parameters to be set to control ports.
pub const ParamList = std.ArrayList(Param);
pub const ParamMap = std.AutoHashMap([]const u8, f32);
pub const ParamMap = std.StringHashMap(f32);
/// Represents an absolute position in the image.
pub const SeekPos = struct {

View File

@ -464,10 +464,13 @@ pub const Runner = struct {
cmds: lang.CommandList,
debug_flag: bool,
) !void {
var it = cmds.iterator();
for (cmds.toSlice()) |const_cmd| {
if (debug_flag) const_cmd.print();
// copy the command so we own its memory
var cmd = try const_cmd.copy(self.allocator);
defer self.allocator.destroy(cmd);
while (it.next()) |cmd| {
if (debug_flag) cmd.print();
try self.runCommand(cmd);
}
}