From ee78756a504dc61f50422298cc1123c5ac6b3b69 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Sat, 31 May 2025 02:12:02 +0200 Subject: 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 --- src/main.zig | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'src/main.zig') 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; } -- cgit v1.2.3