fix other things related to reading scri file on repl

This commit is contained in:
Luna 2019-10-06 15:07:30 -03:00
parent 66b262beb2
commit 80036c4c0d
2 changed files with 24 additions and 21 deletions

View file

@ -140,6 +140,7 @@ pub const ArgList = std.ArrayList([]const u8);
pub const KeywordMap = std.StringHashMap(CommandType);
/// A parser.
pub const Lang = struct {
allocator: *std.mem.Allocator,
keywords: KeywordMap,

View file

@ -25,45 +25,49 @@ fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: langs.Command) !langs.Comma
pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
var stdout_file = try std.io.getStdOut();
const stdout = &stdout_file.outStream().stream;
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
var file_read = try std.fs.File.openRead(scri_path);
const total_bytes = try file_read.getEndPos();
// 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");
var cmds = try wrapInCmdList(allocator, langs.Command{
.command = .Load,
.args = loadargs,
});
// TODO load should -1 the index if its on repl
var cmds = langs.CommandList.init(allocator);
defer cmds.deinit();
// if the scri file already had commands, we add them up on the cmd list
var file_read = try std.fs.openReead(scri_path);
const total_bytes = try file_read.getEndPos();
var lang = langs.Lang.init(allocator);
defer lang.deinit();
if (total_bytes > 0) {
// those MUST BE long lived, we can't defer them here
var scri_existing = try allocator.alloc(u8, total_bytes);
defer allocator.free(scri_existing);
_ = try file.read(slice);
file_read.close();
_ = try file_read.read(scri_existing);
var existing_cmds = try lang.parse(scri_existing);
defer existing_cmds.deinit();
// copy the existing command list into the repl's command list
var it = existing_cmds.iterator();
while (it.next()) |existing_cmd| {
try cmds.append(existing_cmd);
}
} else {
// if there isn't any commands on the file, we load our default
// 'load :1' command
var loadargs = langs.ArgList.init(allocator);
// defer loadargs.deinit();
try loadargs.append(":1");
try cmds.append(langs.Command{
.command = .Load,
.args = loadargs,
});
}
file_read.close();
var file = try std.fs.File.openWrite(scri_path);
defer file.close();
@ -72,6 +76,7 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
// since we opened the file for writing, it becomes empty, so, to ensure
// we don't fuck up later on, we print cmds before starting the repl
try printer.printList(cmds, stdout);
try printer.printList(cmds, stream);
// we keep
@ -83,9 +88,6 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
// - one runner that gets copied from the original on every new
// command the user issues
var lang = langs.Lang.init(allocator);
defer lang.deinit();
var current: langs.Command = undefined;
var runner = runners.Runner.init(allocator);