diff --git a/src/lang.zig b/src/lang.zig index b76e337..0916aeb 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -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| {