examples.reify: implement updated union/enum semantics

This commit is contained in:
torque 2023-11-06 20:42:39 -08:00
parent 2f90ccba6f
commit e8ddee5ab2
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk

View File

@ -5,14 +5,18 @@ const std = @import("std");
const nice = @import("nice"); const nice = @import("nice");
const Enum = enum { first, second, third };
const TagUnion = union(Enum) { first: []const u8, second: i32, third: void };
const Example = struct { const Example = struct {
useful: bool, useful: bool,
number: i32, number: i32,
string: []const u8, string: []const u8,
longstring: [:0]const u8, longstring: [:0]const u8,
tuple: struct { bool, i8 }, tuple: struct { bool, i8 },
enume: enum { first, second, third }, enume: Enum,
taggart: union(enum) { first: []const u8, second: i32 }, taggart: TagUnion,
voidtag: TagUnion,
exist: ?bool, exist: ?bool,
again: ?bool, again: ?bool,
array: [5]i16, array: [5]i16,
@ -32,9 +36,9 @@ const source =
\\ # and a trailing newline for good measure \\ # and a trailing newline for good measure
\\ > \\ >
\\tuple: [ no, 127 ] \\tuple: [ no, 127 ]
\\enume: .third \\enume: .second
\\taggart: \\taggart: {.first: string a thing}
\\ first: string a thing \\voidtag: .third
\\list: \\list:
\\ - I am a list item \\ - I am a list item
\\exist: null \\exist: null
@ -69,11 +73,18 @@ pub fn main() !void {
std.debug.print(" string: {s}\n", .{loaded.value.string}); std.debug.print(" string: {s}\n", .{loaded.value.string});
std.debug.print(" longstring: {s}\n", .{loaded.value.longstring}); std.debug.print(" longstring: {s}\n", .{loaded.value.longstring});
std.debug.print(" tuple: {{ {}, {d} }}\n", .{ loaded.value.tuple[0], loaded.value.tuple[1] }); std.debug.print(" tuple: {{ {}, {d} }}\n", .{ loaded.value.tuple[0], loaded.value.tuple[1] });
std.debug.print(" enume: {s}\n", .{@tagName(loaded.value.enume)}); std.debug.print(" enume: .{s}\n", .{@tagName(loaded.value.enume)});
std.debug.print(" taggart: ", .{}); std.debug.print(" taggart: ", .{});
switch (loaded.value.taggart) { switch (loaded.value.taggart) {
.first => |val| std.debug.print(".first = {s}\n", .{val}), .first => |val| std.debug.print(".first = {s}\n", .{val}),
.second => |val| std.debug.print(".second = {d}\n", .{val}), .second => |val| std.debug.print(".second = {d}\n", .{val}),
.third => std.debug.print(".third\n", .{}),
}
std.debug.print(" voidtag: ", .{});
switch (loaded.value.voidtag) {
.first => |val| std.debug.print(".first = {s}\n", .{val}),
.second => |val| std.debug.print(".second = {d}\n", .{val}),
.third => std.debug.print(".third\n", .{}),
} }
std.debug.print(" exist: {?}\n", .{loaded.value.exist}); std.debug.print(" exist: {?}\n", .{loaded.value.exist});
std.debug.print(" again: {?}\n", .{loaded.value.again}); std.debug.print(" again: {?}\n", .{loaded.value.again});