diff --git a/src/lang.zig b/src/lang.zig index 0916aeb..8b91e8f 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -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; diff --git a/src/runner.zig b/src/runner.zig index c1ac101..8d6eed6 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -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;