diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2025-05-31 02:12:02 +0200 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2025-05-31 02:12:02 +0200 |
commit | ee78756a504dc61f50422298cc1123c5ac6b3b69 (patch) | |
tree | fe99400865ef7fe652cee6678f27d7e8093b3e23 /src/main.zig | |
parent | 1ba97916933d858434294b3fde9631873bbf16c8 (diff) | |
download | huginn-ee78756a504dc61f50422298cc1123c5ac6b3b69.tar.gz |
actually codegen the provided code
... well, since all we can do is to add integer literals, we produce
code for the calculations and then perform the exit syscall with the
result
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/main.zig b/src/main.zig index 34da76b..8f81f1d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,14 +1,13 @@ 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 const compile = @import("./compile.zig"); pub fn main() !void { var arena: std.heap.ArenaAllocator = .init(std.heap.smp_allocator); @@ -33,7 +32,7 @@ pub fn main() !void { // try stdout.print("{s}\n", .{line.items}); // } - const source = "1 + 2 + 3"; + const source = "420 + 1337 + 42"; // var lexer = Lexer{ .source = source }; // while (true) { // const token = lexer.next().?; @@ -47,7 +46,7 @@ pub fn main() !void { if (lexer.next()) |token| if (token.type != .eof) { std.debug.print("Unexpected token {}, expected end of file\n", .{token}); }; - const block = try compile.compile(allocator, expr); + const block = try compile.compile(allocator, source, expr); for (block.instrs) |instr| { std.debug.print("{}\n", .{instr}); } @@ -56,6 +55,32 @@ pub fn main() !void { // try stdout.print("{x}\n", .{elf}); } +fn HashMapFormatter(HashMap: type) type { + return std.fmt.Formatter(struct { + fn formatHashMap( + hash_map: HashMap, + comptime fmt: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) !void { + _ = fmt; + _ = options; + try writer.writeAll("{"); + var it = hash_map.iterator(); + var first = true; + while (it.next()) |kv| { + try writer.print("{s} {any}: {any}", .{ if (first) "" else ",", kv.key_ptr.*, kv.value_ptr.* }); + first = false; + } + try writer.writeAll(if (first) "}" else " }"); + } + }.formatHashMap); +} + +pub fn fmtHashMap(hash_map: anytype) HashMapFormatter(@TypeOf(hash_map)) { + return .{ .data = hash_map }; +} + test { _ = peek; } |