add parsing of given repl line

This commit is contained in:
Luna 2019-09-08 23:28:09 -03:00
parent 34d3b12c56
commit 736277db04
2 changed files with 16 additions and 8 deletions

View file

@ -3,10 +3,8 @@ const std = @import("std");
const plugin = @import("plugin.zig"); const plugin = @import("plugin.zig");
pub const ParseError = error{ pub const ParseError = error{
NoCommandGiven, OutOfMemory,
UnknownCommand,
ArgRequired, ArgRequired,
FloatParseFail,
ParseFail, ParseFail,
}; };
@ -261,7 +259,7 @@ pub const Lang = struct {
fn expectAny(self: *Lang, count: usize, args: ArgList) !void { fn expectAny(self: *Lang, count: usize, args: ArgList) !void {
if (args.len != count) { 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; return error.ArgRequired;
} }
} }
@ -274,7 +272,7 @@ pub const Lang = struct {
var i: usize = 0; var i: usize = 0;
if (args.len != count) { 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; return error.ArgRequired;
} }
@ -319,7 +317,7 @@ pub const Lang = struct {
self.has_error = true; 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, ";"); var splitted_it = std.mem.separate(data, ";");
try self.fillKeywords(); try self.fillKeywords();
var cmds = CommandList.init(self.allocator); var cmds = CommandList.init(self.allocator);
@ -359,7 +357,7 @@ pub const Lang = struct {
// construct final Command based on command // construct final Command based on command
var cmd = Command{ .command = ctype, .args = args }; var cmd = Command{ .command = ctype, .args = args };
self.validateCommand(cmd) catch |err| { self.validateCommand(cmd) catch |err| {
self.doError("Unknown command '{}' (length {})", command, command.len); //self.doError("error validating command '{}': {}", command, err);
continue; continue;
}; };

View file

@ -8,6 +8,9 @@ test "scritcher" {
} }
pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void { 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")); 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 // 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 // TODO start a runner and keep it hot
var lang = langs.Lang.init(allocator);
defer lang.deinit();
while (true) { while (true) {
try stdout.print("> "); try stdout.print("> ");
@ -38,7 +44,11 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
return err; 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` // TODO save the line to scri_file? or should we have a special `push`
// to do so? // to do so?