aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-08-04 17:23:44 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-08-04 17:23:44 +0200
commit22f24043755ed320eff8a121aa1b80ede3b3a37f (patch)
tree559f97e250b898405d52e0a2f59fc096d462ce0a /src/main.zig
parent0cd88f7549afbaab837392f8fc3e5b537551d5b8 (diff)
downloadhuginn-22f24043755ed320eff8a121aa1b80ede3b3a37f.tar.gz
improve cli a tiny bit
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/main.zig b/src/main.zig
index 2e89029..205c452 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -18,25 +18,44 @@ pub fn main() !u8 {
var args = std.process.args();
_ = args.next();
+ const Action = enum {
+ run,
+ compile,
+ };
+ const action = (if (args.next()) |act| std.meta.stringToEnum(Action, act) else null) orelse {
+ std.debug.print("Invalid action. Supply `run` or `compile`.", .{});
+ return 1;
+ };
+
+ const dir = switch (action) {
+ .run => blk: {
+ const dir = try std.fmt.allocPrint(allocator, "/tmp/huginn-build-{}", .{std.crypto.random.int(u32)});
+ try std.fs.makeDirAbsolute(dir);
+ break :blk dir;
+ },
+ .compile => ".",
+ };
+
const in_path = args.next();
const in_file = if (in_path) |path|
try std.fs.cwd().openFile(path, .{})
else
std.io.getStdIn();
- const out_path = args.next();
- const out_file = if (out_path) |path|
- try std.fs.cwd().createFile(path, .{ .mode = 0o777 })
- else
- std.io.getStdOut();
-
- const run = if (args.next()) |arg| std.mem.eql(u8, arg, "run") else false;
+ const out_name = if (in_path) |p| blk: {
+ if (!std.mem.endsWith(u8, p, ".hgn")) {
+ std.debug.print("Invalid input file extension. Must be `.hgn`.", .{});
+ return 1;
+ }
+ break :blk p[0 .. p.len - 4];
+ } else "a.out";
+ const out_path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ dir, out_name });
+ const out_file = try std.fs.cwd().createFile(out_path, .{ .mode = 0o777 });
const source = try in_file.readToEndAlloc(
allocator,
640 * 1024, // ought to be enough for anyone
);
- defer allocator.free(source);
var lexer: Lexer = .{ .source = source };
const ast = parse.file(allocator, &lexer) catch |err| {
@@ -58,8 +77,8 @@ pub fn main() !u8 {
return 1;
};
try out_file.writer().writeAll(elf);
- std.debug.print(blue ++ "running program:" ++ normal ++ "\n", .{});
- if (run) {
+ if (action == .run) {
+ std.debug.print(blue ++ "running program:" ++ normal ++ "\n", .{});
out_file.close();
const err = std.process.execv(
@@ -67,7 +86,7 @@ pub fn main() !u8 {
if (target.cpu.arch == .riscv64 and target.os.tag == .linux)
&.{out_path}
else
- &.{ "qemu-riscv64", out_path.? },
+ &.{ "qemu-riscv64", out_path },
);
std.debug.print(red ++ "{}\n" ++ normal ++ "{any}\n", .{ err, @errorReturnTrace() });
return 1;