aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-03-21 14:48:47 +0200
committerVeikka Tuominen <git@vexu.eu>2022-03-21 15:03:42 +0200
commita31fe0ff12270ba2f957c2a957941a23f2143ad5 (patch)
tree3381f01ab6a370f0718c24732adf90cb03370a20 /src/value.zig
parent3d8d6c0a6d7a29396725467672023b5ec3adbce6 (diff)
downloadzig-a31fe0ff12270ba2f957c2a957941a23f2143ad5.tar.gz
zig-a31fe0ff12270ba2f957c2a957941a23f2143ad5.zip
stage2: add way to print values with types
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig20
1 files changed, 19 insertions, 1 deletions
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 {