commit 0ff4cdb63217f16c7511533384fa9efa40b7aa8d
parent c0f799930ce32c93c19540c1700867d0760e0245
Author: Christian Ermann <christianermann@gmail.com>
Date: Sun, 1 Mar 2026 18:47:41 -0800
Improve constant rendering
Diffstat:
1 file changed, 24 insertions(+), 15 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -17,14 +17,32 @@ const Api = struct {
objects: []Object,
};
+const Constant64 = enum {
+ usize_max,
+ uint32_max,
+ uint64_max,
+ nan,
+
+ pub fn render(self: Constant64, writer: anytype) !void {
+ switch (self) {
+ .usize_max => try writer.writeAll("std.math.maxInt(usize)"),
+ .uint32_max => try writer.writeAll("std.math.maxInt(u32)"),
+ .uint64_max => try writer.writeAll("std.math.maxInt(u64)"),
+ .nan => try writer.writeAll("std.math.nan(f64)"),
+ }
+ }
+};
+
const Value64 = union(enum) {
int: u64,
float: f64,
+ constant: Constant64,
pub fn render(self: Value64, writer: anytype) !void {
switch (self) {
.int => |val| try writer.print("{d}", .{ val }),
.float => |val| try writer.print("{d}", .{ val }),
+ .constant => |val| try val.render(writer),
}
}
@@ -42,21 +60,10 @@ const Value64 = union(enum) {
return .{ .int = val };
},
.string => |inner| {
- const val_enum = std.meta.stringToEnum(
- enum {
- usize_max,
- uint32_max,
- uint64_max,
- nan,
- },
- inner
- ) orelse return error.UnexpectedToken;
- switch (val_enum) {
- .usize_max => return .{ .int = std.math.maxInt(usize) },
- .uint32_max => return .{ .int = std.math.maxInt(u32) },
- .uint64_max => return .{ .int = std.math.maxInt(u64) },
- .nan => return .{ .float = std.math.nan(f64) },
- }
+ const val = std.meta.stringToEnum(Constant64, inner) orelse {
+ return error.UnexpectedToken;
+ };
+ return .{ .constant = val };
},
else => error.UnexpectedToken,
};
@@ -748,6 +755,8 @@ fn renderHeader(writer: anytype) !void {
const header =
\\// auto-generated zig binding for webgpu
\\
+ \\const std = @import("std");
+ \\
\\pub const ChainedStruct = extern struct {
\\ next: ?*const ChainedStruct,
\\ struct_type: SType,