From 32b01976d86f49afcbeb63c48f6b6c15a512faf0 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 3 Apr 2021 23:05:56 -0300 Subject: [PATCH 1/2] change default scri path to 'run' cli command --- src/main.zig | 43 +++++++++++++++++++++++++------------------ src/runner.zig | 2 +- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main.zig b/src/main.zig index 6ebe124..d810652 100644 --- a/src/main.zig +++ b/src/main.zig @@ -248,25 +248,32 @@ pub fn main() !void { // TODO print help _ = args_it.skip(); - const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path or 'repl'")); - defer allocator.free(scri_path); + const cli_command = try (args_it.next(allocator) orelse @panic("expected 'run', 'help', or 'repl'")); + defer allocator.free(cli_command); - if (std.mem.eql(u8, scri_path, "repl")) { + if (std.mem.eql(u8, cli_command, "help")) { + //return try doHelp(); + } else if (std.mem.eql(u8, cli_command, "repl")) { return try doRepl(allocator, &args_it); + } else if (std.mem.eql(u8, cli_command, "run")) { + const scri_path = try (args_it.next(allocator) orelse @panic("run: expected scri path")); + defer allocator.free(scri_path); + + var file = try std.fs.cwd().openFile(scri_path, .{}); + defer file.close(); + + // sadly, we read it all into memory. such is life + const total_bytes = try file.getEndPos(); + + var data = try allocator.alloc(u8, total_bytes); + defer allocator.free(data); + _ = try file.read(data); + + var cmds = try lang.parse(data); + defer cmds.deinit(); + + try runner.runCommands(cmds, true); + } else { + @panic("expected 'run', 'help', or 'repl'"); } - - var file = try std.fs.cwd().openFile(scri_path, .{}); - defer file.close(); - - // sadly, we read it all into memory. such is life - const total_bytes = try file.getEndPos(); - - var data = try allocator.alloc(u8, total_bytes); - defer allocator.free(data); - _ = try file.read(data); - - var cmds = try lang.parse(data); - defer cmds.deinit(); - - try runner.runCommands(cmds, true); } diff --git a/src/runner.zig b/src/runner.zig index 1bec75a..16bbc12 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -65,7 +65,7 @@ pub const Runner = struct { // 'scritcher repl ./script ./image' // ':0' should ALWAYS point to the image. - if (self.repl) index += 3 else index += 2; + if (self.repl) index += 4 else index += 3; for (self.args) |arg, idx| { std.debug.warn("arg{d} = {s}\n", .{ idx, arg }); From 73c52141468b59d48750ca29c7cc03e859a8eacc Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 3 Apr 2021 23:20:17 -0300 Subject: [PATCH 2/2] add 'help' command --- src/main.zig | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index d810652..865ce42 100644 --- a/src/main.zig +++ b/src/main.zig @@ -230,6 +230,13 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: anytype) !void { } } +fn doHelp() void { + std.debug.warn("scritcher!\n", .{}); + std.debug.warn("usage: scritcher [run|help|repl]\n", .{}); + std.debug.warn("\tscritcher run path_to_script.scri path_to_input_file.bmp\n", .{}); + std.debug.warn("\tscritcher repl path_to_script.scri path_to_input_file.bmp\n", .{}); +} + pub fn main() !void { // const allocator = std.heap.page_allocator; var allocator_instance = std.heap.GeneralPurposeAllocator(.{}){}; @@ -248,11 +255,16 @@ pub fn main() !void { // TODO print help _ = args_it.skip(); - const cli_command = try (args_it.next(allocator) orelse @panic("expected 'run', 'help', or 'repl'")); + const cli_command_opt = args_it.next(allocator); + if (cli_command_opt == null) { + return doHelp(); + } + + const cli_command = try cli_command_opt.?; defer allocator.free(cli_command); if (std.mem.eql(u8, cli_command, "help")) { - //return try doHelp(); + return doHelp(); } else if (std.mem.eql(u8, cli_command, "repl")) { return try doRepl(allocator, &args_it); } else if (std.mem.eql(u8, cli_command, "run")) { @@ -274,6 +286,7 @@ pub fn main() !void { try runner.runCommands(cmds, true); } else { - @panic("expected 'run', 'help', or 'repl'"); + std.debug.warn("unknown command: '{s}'\n", .{cli_command}); + return error.UnknownCommand; } }