custom fmt
This commit is contained in:
parent
91c1cfac42
commit
47606bd082
|
|
@ -14,6 +14,18 @@ pub const sz_obj = struct {
|
||||||
obj: usize = 0,
|
obj: usize = 0,
|
||||||
obj2: usize = 0,
|
obj2: usize = 0,
|
||||||
tag: sz_tag = .int,
|
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) {
|
pub const sz_inst = union(enum) {
|
||||||
|
|
@ -25,6 +37,19 @@ pub const sz_inst = union(enum) {
|
||||||
unop: u16,
|
unop: u16,
|
||||||
jump: u16,
|
jump: u16,
|
||||||
ret,
|
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 {
|
pub const sz_code = struct {
|
||||||
consts: []sz_obj,
|
consts: []sz_obj,
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,7 @@ pub fn main() !void {
|
||||||
var vm = lib.vm.sz_vm.init(allocator, code);
|
var vm = lib.vm.sz_vm.init(allocator, code);
|
||||||
while (true) {
|
while (true) {
|
||||||
try vm.step();
|
try vm.step();
|
||||||
for (vm.stack.items) |i| {
|
std.debug.print("{any}\n", .{vm.stack.items});
|
||||||
std.debug.print("{} ", .{i.obj});
|
|
||||||
}
|
|
||||||
std.debug.print("\n", .{});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue