add Lang.reset, open scri_file and write current commands to it

This commit is contained in:
Luna 2019-09-10 12:12:10 -03:00
parent 8b67ccc1bf
commit c90590e3b5
2 changed files with 16 additions and 3 deletions

View File

@ -158,6 +158,11 @@ pub const Lang = struct {
self.keywords.deinit();
}
pub fn reset(self: *Lang) void {
self.has_error = false;
self.line = 0;
}
fn fillKeywords(self: *Lang) !void {
_ = try self.keywords.put("noop", .Noop);
_ = try self.keywords.put("load", .Load);

View File

@ -14,6 +14,12 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
const scri_path = try (args_it.next(allocator) orelse @panic("expected scri path"));
var file = try std.fs.File.openWrite(scri_path);
defer file.close();
var out = file.outStream();
var stream = &out.stream;
// 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.
@ -41,7 +47,7 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
var buffer = try std.Buffer.init(allocator, ""[0..]);
var line = std.io.readLine(&buffer) catch |err| {
if (err == error.EndOfStream) return;
if (err == error.EndOfStream) break;
return err;
};
@ -51,11 +57,13 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
};
try cmds.append(cmds_parsed.at(0));
lang.reset();
// TODO save the line to scri_file? or should we have a special `push`
// to do so?
// note this is debug
try printer.printList(cmds, stdout);
try file.seekTo(0);
try printer.printList(cmds, stream);
}
}