diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2025-06-02 16:03:20 +0200 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2025-06-02 16:03:20 +0200 |
commit | d456c74b918d309158dc7b075fb28e32b1a18ed8 (patch) | |
tree | 70597ca593f506c1e9a6454bdb8423cdf156d196 /src/main.zig | |
parent | a39f75cae1d74d89efc0a871bba953c1f1af3b1b (diff) | |
download | huginn-d456c74b918d309158dc7b075fb28e32b1a18ed8.tar.gz |
run program output automatically & print parse tree better
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/main.zig b/src/main.zig index d4951e6..dd34c53 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); const codegen = @import("./codegen.zig"); +const target = @import("builtin").target; pub const parse = @import("./parse.zig"); pub const Lexer = @import("./Lexer.zig"); @@ -12,7 +13,16 @@ pub fn main() !void { var args = std.process.args(); _ = args.next(); - const out_file = if (args.next()) |path| try std.fs.cwd().createFile(path, .{ .mode = 0o777 }) else std.io.getStdOut(); + const arg = args.next() orelse ""; + + const run_path, const out_file = if (std.mem.eql(u8, arg, "run")) blk: { + const path = try std.fmt.allocPrint(allocator, "/tmp/{}.elf", .{std.crypto.random.int(u32)}); + break :blk .{ path, try std.fs.createFileAbsolute(path, .{ .mode = 0o777 }) }; + } else if (arg.len > 0) + .{ null, try std.fs.cwd().createFile(arg, .{ .mode = 0o777 }) } + else + .{ null, std.io.getStdOut() }; + defer if (run_path) |path| allocator.free(path); const output = out_file.writer(); // var br = std.io.bufferedReader(std.io.getStdIn().reader()); @@ -28,7 +38,6 @@ pub fn main() !void { // try stdout.print("{s}\n", .{line.items}); // } - // const source = "17216961135462248174 + 4095 - 4294967295 + 2147483647"; const source = \\print 18446744073709551615; \\print (0 - 1); @@ -45,7 +54,7 @@ pub fn main() !void { const stmts = try parse.statements(allocator, &lexer); std.debug.print("Statements:\n", .{}); for (stmts) |stmt| { - std.debug.print(" {}\n", .{stmt}); + std.debug.print(" {}\n", .{stmt.fmt(source)}); } if (lexer.peek().type != .eof) { std.debug.print("Unexpected token {}, expected end of file\n", .{lexer.next()}); @@ -57,7 +66,18 @@ pub fn main() !void { } const elf = try codegen.create_elf(allocator, block); try output.writeAll(elf); - // try stdout.print("{x}\n", .{elf}); + std.debug.print("Run output:\n", .{}); + if (run_path) |path| { + out_file.close(); + + std.debug.print("{}\n", .{std.process.execv( + allocator, + if (target.cpu.arch == .riscv64 and target.os.tag == .linux) + &.{path} + else + &.{ "qemu-riscv64", path }, + )}); + } } fn HashMapFormatter(HashMap: type) type { |