custom fmt

This commit is contained in:
caandt 2025-06-08 14:37:15 -05:00
parent 91c1cfac42
commit 47606bd082
2 changed files with 26 additions and 4 deletions

View file

@ -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,

View file

@ -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});
}
}