From c90590e3b53f4953569dec8a128a02f4cd7c31b7 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 10 Sep 2019 12:12:10 -0300 Subject: [PATCH] add Lang.reset, open scri_file and write current commands to it --- src/lang.zig | 5 +++++ src/main.zig | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index fe31d11..9724dfb 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -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); diff --git a/src/main.zig b/src/main.zig index 5c414a8..c0af701 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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); } }