Skip to content

Instantly share code, notes, and snippets.

@paulsmith
Last active April 14, 2021 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsmith/a725621af11f8700f7d291fc74bc19ac to your computer and use it in GitHub Desktop.
Save paulsmith/a725621af11f8700f7d291fc74bc19ac to your computer and use it in GitHub Desktop.
infinite semantic analysis loop
const std = @import("std");
const Node = union(enum) {
leaf: u8,
node: ?*Node,
pub fn format(self: Node, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
switch (self) {
.leaf => {
try writer.writeAll("leaf=\"");
try std.fmt.format(writer, "{}", .{self.leaf});
try writer.writeAll("\"");
},
.node => {
try writer.writeAll("node=[");
if (self.node) |n| try n.format("{}", options, writer);
try writer.writeAll("]");
},
}
}
};
pub fn main() anyerror!void {
var leaf = Node{ .leaf = 127 };
var node = Node{ .node = &leaf };
var root = Node{ .node = &node };
std.debug.print("{}\n", .{root});
}
@paulsmith
Copy link
Author

zig-infinite-poc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment