add better errors

This commit is contained in:
Luna 2019-07-08 00:23:12 -03:00
parent be280d44f3
commit ac4624ae58

View file

@ -1,13 +1,15 @@
const std = @import("std"); const std = @import("std");
pub const ParseError = error{
NoCommandGiven,
UnknownCommand,
};
pub const CommandType = enum { pub const CommandType = enum {
Noop, Noop,
Load, Load,
Amp, Amp,
Quicksave, Quicksave,
// TODO proper errs
Error,
}; };
pub const Command = struct { pub const Command = struct {
@ -22,17 +24,18 @@ pub const Command = struct {
pub const CommandList = std.ArrayList(*Command); pub const CommandList = std.ArrayList(*Command);
pub const ArgList = std.ArrayList([]const u8); pub const ArgList = std.ArrayList([]const u8);
pub fn commandToCmdType(command: []const u8) CommandType { pub fn commandToCmdType(command: []const u8) ParseError!CommandType {
if (std.mem.eql(u8, command, "noop")) { if (std.mem.eql(u8, command, "noop")) {
return .Noop; return CommandType.Noop;
} else if (std.mem.eql(u8, command, "load")) { } else if (std.mem.eql(u8, command, "load")) {
return .Load; return CommandType.Load;
} else if (std.mem.eql(u8, command, "amp")) { } else if (std.mem.eql(u8, command, "amp")) {
return .Amp; return CommandType.Amp;
} else if (std.mem.eql(u8, command, "quicksave")) { } else if (std.mem.eql(u8, command, "quicksave")) {
return .Quicksave; return CommandType.Quicksave;
} else { } else {
return .Error; std.debug.warn("Unknown command: '{}'\n", command);
return ParseError.UnknownCommand;
} }
} }
@ -55,9 +58,11 @@ pub const Lang = struct {
// TODO better tokenizer instead of just tokenize(" "); // TODO better tokenizer instead of just tokenize(" ");
var tok_it = std.mem.tokenize(stmt, " "); var tok_it = std.mem.tokenize(stmt, " ");
// TODO send errors var cmd_opt = tok_it.next();
var command = tok_it.next().?; if (cmd_opt == null) return ParseError.NoCommandGiven;
var ctype = commandToCmdType(command);
var command = cmd_opt.?;
var ctype = try commandToCmdType(command);
var args = ArgList.init(self.allocator); var args = ArgList.init(self.allocator);
while (tok_it.next()) |arg| { while (tok_it.next()) |arg| {