From 736277db049fdffb6f8d3eddfde1bcd4c77fbc48 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 8 Sep 2019 23:28:09 -0300 Subject: [PATCH] add parsing of given repl line --- src/lang.zig | 12 +++++------- src/main.zig | 12 +++++++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index 3af632c..fe31d11 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -3,10 +3,8 @@ const std = @import("std"); const plugin = @import("plugin.zig"); pub const ParseError = error{ - NoCommandGiven, - UnknownCommand, + OutOfMemory, ArgRequired, - FloatParseFail, ParseFail, }; @@ -261,7 +259,7 @@ pub const Lang = struct { fn expectAny(self: *Lang, count: usize, args: ArgList) !void { if (args.len != count) { - std.debug.warn("expected {} arguments, found {}\n", count, args.len); + self.doError("expected {} arguments, found {}", count, args.len); return error.ArgRequired; } } @@ -274,7 +272,7 @@ pub const Lang = struct { var i: usize = 0; if (args.len != count) { - std.debug.warn("expected {} arguments, found {}\n", count, args.len); + self.doError("expected {} arguments, found {}", count, args.len); return error.ArgRequired; } @@ -319,7 +317,7 @@ pub const Lang = struct { self.has_error = true; } - pub fn parse(self: *Lang, data: []const u8) !CommandList { + pub fn parse(self: *Lang, data: []const u8) ParseError!CommandList { var splitted_it = std.mem.separate(data, ";"); try self.fillKeywords(); var cmds = CommandList.init(self.allocator); @@ -359,7 +357,7 @@ pub const Lang = struct { // construct final Command based on command var cmd = Command{ .command = ctype, .args = args }; self.validateCommand(cmd) catch |err| { - self.doError("Unknown command '{}' (length {})", command, command.len); + //self.doError("error validating command '{}': {}", command, err); continue; }; diff --git a/src/main.zig b/src/main.zig index fe3ce9a..1ac9680 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,6 +8,9 @@ test "scritcher" { } 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")); // the thing here is that 'load :0' would load the scri_path, since we @@ -29,6 +32,9 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void { // TODO start a runner and keep it hot + var lang = langs.Lang.init(allocator); + defer lang.deinit(); + while (true) { try stdout.print("> "); @@ -38,7 +44,11 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void { return err; }; - // TODO parse the line through langs.parse, then add the command to cmds + 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)); // TODO save the line to scri_file? or should we have a special `push` // to do so?