2019-07-05 19:59:45 +00:00
|
|
|
const std = @import("std");
|
2019-07-08 02:03:55 +00:00
|
|
|
const langs = @import("lang.zig");
|
2019-07-08 15:38:16 +00:00
|
|
|
const runners = @import("runner.zig");
|
2019-09-10 14:45:04 +00:00
|
|
|
const printer = @import("printer.zig");
|
2019-07-05 19:59:45 +00:00
|
|
|
|
2019-08-08 00:04:51 +00:00
|
|
|
test "scritcher" {
|
|
|
|
_ = @import("lang.zig");
|
2019-08-08 19:48:31 +00:00
|
|
|
_ = @import("runner.zig");
|
2019-08-08 00:04:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 13:53:09 +00:00
|
|
|
const readline = @cImport({
|
|
|
|
@cInclude("stdio.h");
|
|
|
|
@cInclude("stdlib.h");
|
|
|
|
|
|
|
|
@cInclude("readline/readline.h");
|
|
|
|
@cInclude("readline/history.h");
|
|
|
|
});
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: langs.Command) !langs.CommandList {
|
2019-09-11 01:00:07 +00:00
|
|
|
var cmds = langs.CommandList.init(allocator);
|
2019-09-11 00:31:23 +00:00
|
|
|
try cmds.append(cmd);
|
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
|
2019-09-08 20:06:19 +00:00
|
|
|
pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
|
2019-09-09 02:28:09 +00:00
|
|
|
var stdout_file = try std.io.getStdOut();
|
|
|
|
const stdout = &stdout_file.outStream().stream;
|
|
|
|
|
2019-09-08 20:06:19 +00:00
|
|
|
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
|
|
|
|
|
2019-09-10 15:12:10 +00:00
|
|
|
var file = try std.fs.File.openWrite(scri_path);
|
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
var out = file.outStream();
|
|
|
|
var stream = &out.stream;
|
|
|
|
|
2019-09-08 20:06:19 +00:00
|
|
|
// the thing here is that 'load :0' would load the scri_path, since we
|
|
|
|
// now have a 'repl' argument right before it. to counteract that, the
|
|
|
|
// initial repl buffer contains 'load :1' instead.
|
|
|
|
|
|
|
|
var loadargs = langs.ArgList.init(allocator);
|
|
|
|
defer loadargs.deinit();
|
|
|
|
try loadargs.append(":1");
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
var cmds = try wrapInCmdList(allocator, langs.Command{
|
2019-09-08 20:06:19 +00:00
|
|
|
.command = .Load,
|
|
|
|
.args = loadargs,
|
|
|
|
});
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
defer cmds.deinit();
|
2019-09-08 20:27:59 +00:00
|
|
|
|
2019-09-10 18:44:10 +00:00
|
|
|
// we keep
|
|
|
|
// - a CommandList with the full commands we have right now
|
|
|
|
// - a Command with the current last typed successful command
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
// - one runner that contains the current full state of the image
|
|
|
|
// as if the current cmds was ran over it (TODO better wording)
|
|
|
|
// - one runner that gets copied from the original on every new
|
|
|
|
// command the user issues
|
|
|
|
|
2019-09-09 02:28:09 +00:00
|
|
|
var lang = langs.Lang.init(allocator);
|
|
|
|
defer lang.deinit();
|
|
|
|
|
2019-09-10 18:44:10 +00:00
|
|
|
var current: langs.Command = undefined;
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
var runner = runners.Runner.init(allocator);
|
|
|
|
defer runner.deinit();
|
|
|
|
|
|
|
|
// run the load command
|
|
|
|
try runner.runCommands(cmds, true);
|
|
|
|
|
2019-09-11 01:00:07 +00:00
|
|
|
var runqs_args = langs.ArgList.init(allocator);
|
|
|
|
defer runqs_args.deinit();
|
|
|
|
|
|
|
|
// TODO change the runqs command to something given in an env var
|
2019-10-06 00:42:16 +00:00
|
|
|
try runqs_args.append("ristretto");
|
2019-09-11 01:00:07 +00:00
|
|
|
|
2019-09-08 20:27:59 +00:00
|
|
|
while (true) {
|
2019-09-10 17:51:41 +00:00
|
|
|
lang.reset();
|
2019-09-08 20:27:59 +00:00
|
|
|
|
2019-10-06 13:53:09 +00:00
|
|
|
var rd_line = readline.readline(c"> ");
|
|
|
|
if (rd_line == null) break;
|
|
|
|
readline.add_history(rd_line);
|
|
|
|
defer std.heap.c_allocator.destroy(rd_line);
|
|
|
|
|
|
|
|
var line = rd_line[0..std.mem.len(u8, rd_line)];
|
2019-09-08 20:27:59 +00:00
|
|
|
|
2019-09-10 17:51:41 +00:00
|
|
|
if (std.mem.eql(u8, line, "push")) {
|
2019-09-10 18:44:10 +00:00
|
|
|
try cmds.append(current);
|
2019-09-11 00:35:22 +00:00
|
|
|
|
|
|
|
// run the current added command to main cmds list
|
|
|
|
// with the main parent runner
|
2019-09-11 00:31:23 +00:00
|
|
|
var cmds_wrapped = try wrapInCmdList(allocator, current);
|
|
|
|
defer cmds_wrapped.deinit();
|
|
|
|
try runner.runCommands(cmds_wrapped, true);
|
2019-09-11 00:35:22 +00:00
|
|
|
|
2019-09-10 17:51:41 +00:00
|
|
|
continue;
|
|
|
|
} else if (std.mem.eql(u8, line, "list")) {
|
2019-09-10 18:44:10 +00:00
|
|
|
try printer.printList(cmds, stdout);
|
|
|
|
continue;
|
|
|
|
} else if (std.mem.eql(u8, line, "save")) {
|
|
|
|
try file.seekTo(0);
|
2019-09-10 17:51:41 +00:00
|
|
|
try printer.printList(cmds, stream);
|
|
|
|
continue;
|
2019-10-06 00:44:44 +00:00
|
|
|
} else if (std.mem.eql(u8, line, "quit") or std.mem.eql(u8, line, "q")) {
|
|
|
|
std.debug.warn("leaving\n");
|
|
|
|
break;
|
2019-09-10 17:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 02:28:09 +00:00
|
|
|
var cmds_parsed = lang.parse(line) catch |err| {
|
|
|
|
std.debug.warn("repl: error while parsing: {}\n", err);
|
|
|
|
continue;
|
|
|
|
};
|
2019-09-10 18:44:10 +00:00
|
|
|
current = cmds_parsed.at(0);
|
2019-09-11 00:31:23 +00:00
|
|
|
|
2019-09-11 00:35:22 +00:00
|
|
|
// by cloning the parent runner, we can iteratively write
|
|
|
|
// whatever command we want and only commit the good results
|
|
|
|
// back to the parent runner
|
2019-09-11 00:31:23 +00:00
|
|
|
var runner_clone = try runner.clone();
|
|
|
|
defer runner_clone.deinit();
|
|
|
|
|
2019-09-11 01:00:07 +00:00
|
|
|
try cmds_parsed.append(langs.Command{
|
|
|
|
.command = .RunQS,
|
|
|
|
.args = runqs_args,
|
|
|
|
});
|
|
|
|
|
2019-09-11 00:31:23 +00:00
|
|
|
try runner_clone.runCommands(cmds_parsed, true);
|
2019-09-30 02:57:24 +00:00
|
|
|
try stdout.write("\n");
|
2019-09-08 20:27:59 +00:00
|
|
|
}
|
2019-09-08 20:06:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-07 05:26:05 +00:00
|
|
|
pub fn main() !void {
|
2019-08-14 19:11:09 +00:00
|
|
|
const allocator = std.heap.direct_allocator;
|
2019-07-08 02:03:55 +00:00
|
|
|
|
|
|
|
var lang = langs.Lang.init(allocator);
|
2019-08-08 00:04:51 +00:00
|
|
|
defer lang.deinit();
|
2019-07-08 02:03:55 +00:00
|
|
|
|
2019-07-08 16:13:03 +00:00
|
|
|
var runner = runners.Runner.init(allocator);
|
2019-07-08 17:43:58 +00:00
|
|
|
defer runner.deinit();
|
2019-07-08 16:13:03 +00:00
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
var args_it = std.process.args();
|
|
|
|
|
2019-09-08 20:06:19 +00:00
|
|
|
// TODO print help
|
|
|
|
|
2019-08-13 13:19:39 +00:00
|
|
|
_ = try (args_it.next(allocator) orelse @panic("expected exe name"));
|
2019-07-08 02:03:55 +00:00
|
|
|
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
|
|
|
|
|
2019-09-08 20:06:19 +00:00
|
|
|
if (std.mem.eql(u8, scri_path, "repl")) {
|
|
|
|
return try doRepl(allocator, &args_it);
|
|
|
|
}
|
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
var file = try std.fs.File.openRead(scri_path);
|
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
// sadly, we read it all into memory. such is life
|
|
|
|
const total_bytes = try file.getEndPos();
|
2019-07-10 15:13:44 +00:00
|
|
|
|
2019-07-08 02:03:55 +00:00
|
|
|
var data = try allocator.alloc(u8, total_bytes);
|
2019-07-10 15:13:44 +00:00
|
|
|
defer allocator.free(data);
|
2019-07-08 02:03:55 +00:00
|
|
|
_ = try file.read(data);
|
|
|
|
|
2019-07-08 03:09:34 +00:00
|
|
|
var cmds = try lang.parse(data);
|
2019-07-10 15:13:44 +00:00
|
|
|
defer cmds.deinit();
|
|
|
|
|
2019-07-08 16:13:03 +00:00
|
|
|
try runner.runCommands(cmds, true);
|
2019-07-08 02:03:55 +00:00
|
|
|
}
|