Skip to content

Instantly share code, notes, and snippets.

@Sh4pe
Last active February 21, 2022 20:59
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 Sh4pe/23e3fdd96df79f3ae817a9b0b6d3a3e4 to your computer and use it in GitHub Desktop.
Save Sh4pe/23e3fdd96df79f3ae817a9b0b6d3a3e4 to your computer and use it in GitHub Desktop.
Zig compilation problem. Place main.zig in folder src.
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const exe = exe: {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const e = b.addExecutable("foo", "src/main.zig");
e.setTarget(target);
e.setBuildMode(mode);
e.install();
break :exe e;
};
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
const std = @import("std");
const json = std.json;
const Allocator = std.mem.Allocator;
const AutoHashMap = std.AutoHashMap;
const ArrayList = std.ArrayList;
const TailQueue = std.TailQueue;
pub fn PointerGraph(comptime T: type) type {
return struct {
const Self = @This();
pub const Edge = struct { a: usize, b: usize, };
vertices: AutoHashMap(usize, *T),
edges: ArrayList(Edge),
pub fn init() Self {
unreachable;
}
pub fn deinit(self: *Self) void {
self.vertices.deinit();
self.edges.deinit();
}
};
}
pub const Schema = struct {
root_node: *Node,
const NodesList = TailQueue(Node);
const Self = @This();
pub const Node = struct {};
pub fn init(allocator: Allocator, jsonString: []const u8) !Schema {
var parser = json.Parser.init(allocator, true);
defer parser.deinit();
var value_tree = try parser.parse(jsonString);
errdefer value_tree.deinit();
var resolvables = PointerGraph(ResolvableEntity).init();
defer resolvables.deinit();
var parse_context: ParseContext = .{
.resolvables = &resolvables,
};
var root_node = try parse(&value_tree.root, parse_context);
return Schema{
.root_node = root_node,
};
}
pub fn deinit(_: *Self) void {
unreachable;
}
// Now this seems to trigger the compilation error. To 'fix' it, uncomment the other
// definition of ResolvableEntity and comment out this one.
pub const ResolvableEntity = union(enum) {
foo: u8,
};
// pub const ResolvableEntity = struct {
// foo: u8,
// };
const ParseContext = struct {
resolvables: *PointerGraph(ResolvableEntity),
};
fn parse(_: *const json.Value, _: ParseContext) !*Node {
unreachable;
}
};
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
var schema = try Schema.init(alloc, "foo");
defer schema.deinit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment