From ae324985a67b1258c226a799f2680906686670f2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 8 Jan 2020 23:55:22 -0500 Subject: [PATCH] clean up a TODO in self-hosted --- src-self-hosted/main.zig | 98 +++++++++++----------------------------- 1 file changed, 27 insertions(+), 71 deletions(-) diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index a19743bd71..af3fd3a015 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -58,8 +58,7 @@ pub fn main() !void { stderr = &stderr_file.outStream().stream; const args = try process.argsAlloc(allocator); - // TODO I'm getting unreachable code here, which shouldn't happen - //defer process.argsFree(allocator, args); + defer process.argsFree(allocator, args); if (args.len <= 1) { try stderr.write("expected command argument\n\n"); @@ -67,64 +66,33 @@ pub fn main() !void { process.exit(1); } - const commands = [_]Command{ - Command{ - .name = "build-exe", - .exec = cmdBuildExe, - }, - Command{ - .name = "build-lib", - .exec = cmdBuildLib, - }, - Command{ - .name = "build-obj", - .exec = cmdBuildObj, - }, - Command{ - .name = "fmt", - .exec = cmdFmt, - }, - Command{ - .name = "libc", - .exec = cmdLibC, - }, - Command{ - .name = "targets", - .exec = cmdTargets, - }, - Command{ - .name = "version", - .exec = cmdVersion, - }, - Command{ - .name = "zen", - .exec = cmdZen, - }, - - // undocumented commands - Command{ - .name = "help", - .exec = cmdHelp, - }, - Command{ - .name = "internal", - .exec = cmdInternal, - }, - }; - - inline for (commands) |command| { - if (mem.eql(u8, command.name, args[1])) { - var frame = try allocator.create(@Frame(command.exec)); - defer allocator.destroy(frame); - frame.* = async command.exec(allocator, args[2..]); - return await frame; - } + const cmd = args[1]; + const cmd_args = args[2..]; + if (mem.eql(u8, cmd, "build-exe")) { + return buildOutputType(allocator, cmd_args, .Exe); + } else if (mem.eql(u8, cmd, "build-lib")) { + return buildOutputType(allocator, cmd_args, .Lib); + } else if (mem.eql(u8, cmd, "build-obj")) { + return buildOutputType(allocator, cmd_args, .Obj); + } else if (mem.eql(u8, cmd, "fmt")) { + return cmdFmt(allocator, cmd_args); + } else if (mem.eql(u8, cmd, "libc")) { + return cmdLibC(allocator, cmd_args); + } else if (mem.eql(u8, cmd, "targets")) { + return cmdTargets(allocator, cmd_args); + } else if (mem.eql(u8, cmd, "version")) { + return cmdVersion(allocator, cmd_args); + } else if (mem.eql(u8, cmd, "zen")) { + return cmdZen(allocator, cmd_args); + } else if (mem.eql(u8, cmd, "help")) { + return cmdHelp(allocator, cmd_args); + } else if (mem.eql(u8, cmd, "internal")) { + return cmdInternal(allocator, cmd_args); + } else { + try stderr.print("unknown command: {}\n\n", .{args[1]}); + try stderr.write(usage); + process.exit(1); } - - try stderr.print("unknown command: {}\n\n", .{args[1]}); - try stderr.write(usage); - process.argsFree(allocator, args); - process.exit(1); } const usage_build_generic = @@ -555,18 +523,6 @@ fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void { } } -fn cmdBuildExe(allocator: *Allocator, args: []const []const u8) !void { - return buildOutputType(allocator, args, Compilation.Kind.Exe); -} - -fn cmdBuildLib(allocator: *Allocator, args: []const []const u8) !void { - return buildOutputType(allocator, args, Compilation.Kind.Lib); -} - -fn cmdBuildObj(allocator: *Allocator, args: []const []const u8) !void { - return buildOutputType(allocator, args, Compilation.Kind.Obj); -} - pub const usage_fmt = \\usage: zig fmt [file]... \\