scritcher/src/main.zig

93 lines
2.5 KiB
Zig
Raw Normal View History

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-07-05 19:59:45 +00:00
2019-08-08 00:04:51 +00:00
test "scritcher" {
_ = @import("lang.zig");
_ = @import("runner.zig");
2019-08-08 00:04:51 +00:00
}
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"));
// 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.
2019-09-08 20:27:59 +00:00
var cmds = langs.CommandList.init(allocator);
defer cmds.deinit();
2019-09-08 20:06:19 +00:00
var loadargs = langs.ArgList.init(allocator);
defer loadargs.deinit();
try loadargs.append(":1");
2019-09-08 20:27:59 +00:00
try cmds.append(langs.Command{
2019-09-08 20:06:19 +00:00
.command = .Load,
.args = loadargs,
});
2019-09-08 20:27:59 +00:00
// TODO start a runner and keep it hot
2019-09-09 02:28:09 +00:00
var lang = langs.Lang.init(allocator);
defer lang.deinit();
2019-09-08 20:27:59 +00:00
while (true) {
try stdout.print("> ");
var buffer = try std.Buffer.init(allocator, ""[0..]);
var line = std.io.readLine(&buffer) catch |err| {
if (err == error.EndOfStream) return;
return err;
};
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;
};
try cmds.append(cmds_parsed.at(0));
2019-09-08 20:27:59 +00:00
// TODO save the line to scri_file? or should we have a special `push`
// to do so?
}
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);
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-08 02:03:55 +00:00
var data = try allocator.alloc(u8, total_bytes);
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);
defer cmds.deinit();
2019-07-08 16:13:03 +00:00
try runner.runCommands(cmds, true);
2019-07-08 02:03:55 +00:00
}