From 3b8943731eb8deecd7a95a690bcae8e6aef9c1ca Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 18 Sep 2019 12:11:08 -0300 Subject: [PATCH] main: add Result enum --- src/main.zig | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main.zig b/src/main.zig index 5a8e280..45c2407 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,43 @@ const std = @import("std"); +pub const Result = enum { + Ok, + TokenizeError, + ParseError, + CompileError, +}; + +pub fn run(allocator: *std.mem.Allocator, slice: []const u8) Result { + return Result.Ok; +} + pub fn main() anyerror!void { const allocator = std.heap.direct_allocator; var args_it = std.process.args(); _ = args_it.skip(); const filepath = try (args_it.next(allocator) orelse @panic("expected file path")); + + var file = try std.fs.File.openRead(filepath); + defer file.close(); + + const total_bytes = try file.getEndPos(); + + var slice = try allocator.alloc(u8, total_bytes); + defer allocator.free(slice); + + _ = try file.read(slice); + + //switch (try run(allocator, slice)) { + switch (run(allocator, slice)) { + .Ok => {}, + + .TokenizeError, + .ParseError, + .CompileError, + => |res| { + std.debug.warn("error: {}\n", res); + std.os.exit(1); + }, + } }