add better errors

This commit is contained in:
Luna 2019-07-08 00:23:12 -03:00
parent be280d44f3
commit ac4624ae58
1 changed files with 17 additions and 12 deletions

View File

@ -1,13 +1,15 @@
const std = @import("std");
pub const ParseError = error{
NoCommandGiven,
UnknownCommand,
};
pub const CommandType = enum {
Noop,
Load,
Amp,
Quicksave,
// TODO proper errs
Error,
};
pub const Command = struct {
@ -22,17 +24,18 @@ pub const Command = struct {
pub const CommandList = std.ArrayList(*Command);
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")) {
return .Noop;
return CommandType.Noop;
} else if (std.mem.eql(u8, command, "load")) {
return .Load;
return CommandType.Load;
} else if (std.mem.eql(u8, command, "amp")) {
return .Amp;
return CommandType.Amp;
} else if (std.mem.eql(u8, command, "quicksave")) {
return .Quicksave;
return CommandType.Quicksave;
} 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(" ");
var tok_it = std.mem.tokenize(stmt, " ");
// TODO send errors
var command = tok_it.next().?;
var ctype = commandToCmdType(command);
var cmd_opt = tok_it.next();
if (cmd_opt == null) return ParseError.NoCommandGiven;
var command = cmd_opt.?;
var ctype = try commandToCmdType(command);
var args = ArgList.init(self.allocator);
while (tok_it.next()) |arg| {