refactor into 'doRun' function
This commit is contained in:
parent
73c5214146
commit
79d7e137d2
1 changed files with 27 additions and 23 deletions
50
src/main.zig
50
src/main.zig
|
@ -237,6 +237,32 @@ fn doHelp() void {
|
|||
std.debug.warn("\tscritcher repl path_to_script.scri path_to_input_file.bmp\n", .{});
|
||||
}
|
||||
|
||||
fn doRun(allocator: *std.mem.Allocator, args_it: anytype) !void {
|
||||
var lang = langs.Lang.init(allocator);
|
||||
defer lang.deinit();
|
||||
|
||||
var runner = runners.Runner.init(allocator, false);
|
||||
defer runner.deinit();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
// const allocator = std.heap.page_allocator;
|
||||
var allocator_instance = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
@ -245,12 +271,6 @@ pub fn main() !void {
|
|||
}
|
||||
const allocator = &allocator_instance.allocator;
|
||||
|
||||
var lang = langs.Lang.init(allocator);
|
||||
defer lang.deinit();
|
||||
|
||||
var runner = runners.Runner.init(allocator, false);
|
||||
defer runner.deinit();
|
||||
|
||||
var args_it = std.process.args();
|
||||
|
||||
// TODO print help
|
||||
|
@ -268,23 +288,7 @@ pub fn main() !void {
|
|||
} 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);
|
||||
return try doRun(allocator, &args_it);
|
||||
} else {
|
||||
std.debug.warn("unknown command: '{s}'\n", .{cli_command});
|
||||
return error.UnknownCommand;
|
||||
|
|
Loading…
Reference in a new issue