2019-07-08 15:38:16 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const lang = @import("lang.zig");
|
|
|
|
|
|
|
|
pub const RunError = error{UnknownCommand};
|
|
|
|
|
|
|
|
pub const Runner = struct {
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
|
|
|
|
pub fn init(allocator: *std.mem.Allocator) Runner {
|
|
|
|
return Runner{ .allocator = allocator };
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:55:54 +00:00
|
|
|
fn resolveArg(self: *Runner, load_path: []const u8) ![]const u8 {
|
|
|
|
if (load_path[0] == ':') {
|
|
|
|
// parse the index from 1 to end
|
|
|
|
const index = try std.fmt.parseInt(usize, load_path[1..], 10);
|
|
|
|
var args_it = std.process.args();
|
|
|
|
_ = args_it.skip();
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i <= index) : (i += 1) {
|
|
|
|
_ = args_it.skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
const arg = try (args_it.next(self.allocator) orelse @panic("expected argument"));
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
} else {
|
|
|
|
return load_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolveArgPath(self: *Runner, path_or_argidx: []const u8) ![]const u8 {
|
|
|
|
const path = try self.resolveArg(path_or_argidx);
|
|
|
|
const resolved_path = try std.fs.path.resolve(
|
|
|
|
self.allocator,
|
|
|
|
[_][]const u8{path},
|
|
|
|
);
|
|
|
|
return resolved_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn loadCmd(self: *Runner, path_or_argidx: []const u8) !void {
|
|
|
|
var load_path = try self.resolveArgPath(path_or_argidx);
|
|
|
|
std.debug.warn("load path: {}\n", load_path);
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:38:16 +00:00
|
|
|
fn runCommand(self: *Runner, cmd: *lang.Command) !void {
|
|
|
|
return switch (cmd.command) {
|
|
|
|
.Noop => {},
|
2019-07-08 16:55:54 +00:00
|
|
|
.Load => blk: {
|
|
|
|
var path = cmd.args.at(0);
|
|
|
|
try self.loadCmd(path);
|
|
|
|
break :blk;
|
|
|
|
},
|
|
|
|
//.Quicksave => try self.quicksaveCmd(),
|
2019-07-08 15:38:16 +00:00
|
|
|
else => blk: {
|
|
|
|
std.debug.warn("Unknown command: {}\n", cmd.command);
|
|
|
|
break :blk RunError.UnknownCommand;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:13:03 +00:00
|
|
|
pub fn runCommands(
|
|
|
|
self: *Runner,
|
|
|
|
cmds: lang.CommandList,
|
|
|
|
debug_flag: bool,
|
|
|
|
) !void {
|
2019-07-08 15:38:16 +00:00
|
|
|
var it = cmds.iterator();
|
|
|
|
|
|
|
|
while (it.next()) |cmd| {
|
2019-07-08 16:13:03 +00:00
|
|
|
if (debug_flag) cmd.print();
|
2019-07-08 15:38:16 +00:00
|
|
|
try self.runCommand(cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|