add basic resolution of arguments

This commit is contained in:
Luna 2019-07-08 13:55:54 -03:00
parent 87459939cf
commit e2f9566529
2 changed files with 41 additions and 0 deletions

View file

@ -52,6 +52,7 @@ pub const Lang = struct {
while (splitted_it.next()) |stmt_orig| {
var stmt = std.mem.trimRight(u8, stmt_orig, "\n");
stmt = std.mem.trimLeft(u8, stmt, "\n");
if (stmt.len == 0) continue;

View file

@ -10,9 +10,49 @@ pub const Runner = struct {
return Runner{ .allocator = allocator };
}
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);
}
fn runCommand(self: *Runner, cmd: *lang.Command) !void {
return switch (cmd.command) {
.Noop => {},
.Load => blk: {
var path = cmd.args.at(0);
try self.loadCmd(path);
break :blk;
},
//.Quicksave => try self.quicksaveCmd(),
else => blk: {
std.debug.warn("Unknown command: {}\n", cmd.command);
break :blk RunError.UnknownCommand;