Compare commits
2 commits
73c5214146
...
a9e4c5823a
Author | SHA1 | Date | |
---|---|---|---|
a9e4c5823a | |||
79d7e137d2 |
1 changed files with 28 additions and 26 deletions
54
src/main.zig
54
src/main.zig
|
@ -237,23 +237,41 @@ fn doHelp() void {
|
|||
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(.{}){};
|
||||
defer {
|
||||
_ = allocator_instance.deinit();
|
||||
}
|
||||
const allocator = &allocator_instance.allocator;
|
||||
|
||||
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 {
|
||||
var allocator_instance = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer {
|
||||
_ = allocator_instance.deinit();
|
||||
}
|
||||
const allocator = &allocator_instance.allocator;
|
||||
|
||||
var args_it = std.process.args();
|
||||
|
||||
// TODO print help
|
||||
_ = args_it.skip();
|
||||
const cli_command_opt = args_it.next(allocator);
|
||||
if (cli_command_opt == null) {
|
||||
|
@ -268,23 +286,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