aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-05-29 20:48:22 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-05-29 20:48:34 +0200
commitac74431a29a8b31d368ad1daba561281e94f46d4 (patch)
treebac17fba025d752573158f54a5e1d70635600785 /src/main.zig
parentdb112f0d384ca991e1477913b8af7a2c8dc80088 (diff)
downloadhuginn-ac74431a29a8b31d368ad1daba561281e94f46d4.tar.gz
create initial simple elf file
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/main.zig b/src/main.zig
index d938d9b..a7874d8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2,19 +2,23 @@ const std = @import("std");
const parse = @import("./parse.zig");
const peek = @import("./peek.zig");
const compile = @import("./compile.zig");
+const codegen = @import("./codegen.zig");
pub const peekable = peek.peekable;
pub const Peekable = peek.Peekable;
pub const Expr = parse.Expr;
pub const Lexer = @import("./lexer.zig");
+pub const Block = compile.Block;
pub fn main() !void {
var arena: std.heap.ArenaAllocator = .init(std.heap.smp_allocator);
defer arena.deinit();
const allocator = arena.allocator();
- var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
- const stdout = bw.writer();
+ var args = std.process.args();
+ _ = args.next();
+ const out_file = if (args.next()) |path| try std.fs.cwd().createFile(path, .{}) else std.io.getStdOut();
+ const output = out_file.writer();
// var br = std.io.bufferedReader(std.io.getStdIn().reader());
// const stdin = br.reader();
@@ -33,22 +37,23 @@ pub fn main() !void {
// var lexer = Lexer{ .source = source };
// while (true) {
// const token = lexer.next().?;
- // try stdout.print("{}\n", .{token});
+ // std.debug.print("{}\n", .{token});
// if (token.type == .Eof) break;
// }
- // try stdout.print("\n", .{});
+ // std.debug.print("\n", .{});
var lexer = peekable(Lexer{ .source = source });
const expr = try parse.expression(allocator, &lexer);
- try stdout.print("{}\n", .{expr});
+ std.debug.print("{}\n", .{expr});
if (lexer.next()) |token| if (token.type != .eof) {
- try stdout.print("Unexpected token {}, expected end of file\n", .{token});
+ std.debug.print("Unexpected token {}, expected end of file\n", .{token});
};
const block = try compile.compile(allocator, expr);
for (block.instrs) |instr| {
- try stdout.print("{}\n", .{instr});
+ std.debug.print("{}\n", .{instr});
}
-
- try bw.flush(); // Don't forget to flush!
+ const elf = try codegen.create_elf(allocator, block);
+ try output.writeAll(elf);
+ // try stdout.print("{x}\n", .{elf});
}
test {