From 47606bd082dd79e1d5ad3816bf5dde4057530163 Mon Sep 17 00:00:00 2001 From: caandt Date: Sun, 8 Jun 2025 14:37:15 -0500 Subject: [PATCH] custom fmt --- src/interp/compile.zig | 25 +++++++++++++++++++++++++ src/main.zig | 5 +---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/interp/compile.zig b/src/interp/compile.zig index 7f918f4..1cc25a7 100644 --- a/src/interp/compile.zig +++ b/src/interp/compile.zig @@ -14,6 +14,18 @@ pub const sz_obj = struct { obj: usize = 0, obj2: usize = 0, tag: sz_tag = .int, + pub fn format(value: sz_obj, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + switch (value.tag) { + .int => try writer.print("i[{}]", .{@as(isize, @bitCast(value.obj))}), + .num => try writer.print("n[{}]", .{@as(f64, @bitCast(value.obj))}), + .bool => try writer.print("b[{}]", .{value.obj}), + .str => try writer.print("s[{s}]", .{@as([*]u8, @ptrFromInt(value.obj))[0..value.obj2]}), + .list => try writer.print("l[{s}]", .{@as([*]sz_obj, @ptrFromInt(value.obj))[0..value.obj2]}), + .err => try writer.print("err[]", .{}), + } + } }; pub const sz_inst = union(enum) { @@ -25,6 +37,19 @@ pub const sz_inst = union(enum) { unop: u16, jump: u16, ret, + pub fn format(value: sz_inst, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + switch (value) { + inline else => |n| { + if (@TypeOf(n) == void) { + try writer.print("[{s}]", .{@tagName(value)}); + } else { + try writer.print("[{s} {}]", .{@tagName(value), n}); + } + } + } + } }; pub const sz_code = struct { consts: []sz_obj, diff --git a/src/main.zig b/src/main.zig index ade0589..a1a81e8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -23,10 +23,7 @@ pub fn main() !void { var vm = lib.vm.sz_vm.init(allocator, code); while (true) { try vm.step(); - for (vm.stack.items) |i| { - std.debug.print("{} ", .{i.obj}); - } - std.debug.print("\n", .{}); + std.debug.print("{any}\n", .{vm.stack.items}); } }