From a31fe0ff12270ba2f957c2a957941a23f2143ad5 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Mon, 21 Mar 2022 14:48:47 +0200 Subject: stage2: add way to print values with types --- src/value.zig | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/value.zig') diff --git a/src/value.zig b/src/value.zig index 269b13b099..76ab20c7ab 100644 --- a/src/value.zig +++ b/src/value.zig @@ -600,9 +600,17 @@ pub const Value = extern union { return Value{ .ptr_otherwise = &new_payload.base }; } + pub fn format(val: Value, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = val; + _ = fmt; + _ = options; + _ = writer; + @compileError("do not use format values directly; use either fmtDebug or fmtValue"); + } + /// TODO this should become a debug dump() function. In order to print values in a meaningful way /// we also need access to the type. - pub fn format( + pub fn dump( start_val: Value, comptime fmt: []const u8, options: std.fmt.FormatOptions, @@ -766,6 +774,16 @@ pub const Value = extern union { }; } + pub fn fmtDebug(val: Value) std.fmt.Formatter(dump) { + return .{ .data = val }; + } + + const TypedValue = @import("TypedValue.zig"); + + pub fn fmtValue(val: Value, ty: Type) std.fmt.Formatter(TypedValue.format) { + return .{ .data = .{ .ty = ty, .val = val } }; + } + /// Asserts that the value is representable as an array of bytes. /// Copies the value into a freshly allocated slice of memory, which is owned by the caller. pub fn toAllocatedBytes(val: Value, ty: Type, allocator: Allocator) ![]u8 { -- cgit v1.2.3