Skip to content

Commit 093de8c

Browse files
committed
simple PoC for trivial inputs
1 parent 5ff8b51 commit 093de8c

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

src/main.zig

+49-9
Original file line numberDiff line numberDiff line change
@@ -191,35 +191,68 @@ pub fn main() !void {
191191
std.debug.print("merge '{s}'...\n", .{h_path});
192192

193193
// parse the headers into graphs
194+
var graph_set = std.ArrayList(Graph).init(arena);
194195
for (entry.value_ptr.items) |header| {
195196
std.debug.print("doing {s} now\n", .{header.input.path});
196197
const graph = try parseHeaderIntoGraph(arena, h_path, header, sys_include);
197198

198-
for (graph.nodes) |node| {
199+
for (graph.nodes.items) |node| {
199200
std.debug.print("node: definition: '{s}' => '{s}'\n", .{ node.definition.name, node.definition.source });
200201
}
202+
try graph_set.append(graph);
201203
}
202204

203205
// topological sort the graphs
206+
// TODO
204207

205208
// consume nodes from inputs to output
209+
var output_nodes = std.ArrayList(Node).init(arena);
210+
while (anyLeft(&graph_set)) {
211+
const first_graph = &graph_set.items[0];
212+
const first_node = first_graph.popFirst();
213+
// figure out where to put this node in the output
214+
try output_nodes.append(first_node);
215+
for (graph_set.items[1..]) |*graph| {
216+
const node = graph.popFirst();
217+
if (!mem.eql(u8, node.definition.name, first_node.definition.name)) {
218+
std.debug.print("non-matching name: {s} {s}\n", .{ node.definition.name, first_node.definition.name });
219+
@panic("TODO");
220+
}
221+
}
222+
}
206223

207224
// render output
225+
if (fs.path.dirname(h_path)) |dirname| {
226+
try out_dir.dir.makePath(dirname);
227+
}
208228

209-
//var out_file = try out_dir.dir.createFile(h_path, .{});
210-
//defer out_file.close();
229+
var out_file = try out_dir.dir.createFile(h_path, .{});
230+
defer out_file.close();
211231

212-
//var bw = std.io.bufferedWriter(out_file.writer());
213-
//const w = bw.writer();
232+
var bw = std.io.bufferedWriter(out_file.writer());
233+
const w = bw.writer();
214234

215-
////try w.writeAll("#pragma once\n");
235+
//try w.writeAll("#pragma once\n");
216236

217-
//try bw.flush();
237+
for (output_nodes.items) |node| {
238+
switch (node) {
239+
.definition => |definition| {
240+
try w.print("{s}\n", .{definition.source});
241+
},
242+
.condition => @panic("TODO"),
243+
}
244+
}
245+
246+
try bw.flush();
218247
}
219248
}
220249

221250
const Graph = struct {
222-
nodes: []Node,
251+
nodes: std.ArrayList(Node),
252+
253+
fn popFirst(graph: *Graph) Node {
254+
return graph.nodes.orderedRemove(0);
255+
}
223256
};
224257

225258
const Node = union(enum) {
@@ -386,7 +419,7 @@ fn parseHeaderIntoGraph(arena: Allocator, h_path: []const u8, header: Header, sy
386419
}
387420

388421
return .{
389-
.nodes = nodes.items,
422+
.nodes = nodes,
390423
};
391424
}
392425

@@ -497,3 +530,10 @@ pub fn tokSlice(source: arocc.Source, tok: Tokenizer.Token) []const u8 {
497530
if (tok.id.lexeme()) |s| return s;
498531
return source.buf[tok.start..tok.end];
499532
}
533+
534+
fn anyLeft(graph_set: *std.ArrayList(Graph)) bool {
535+
for (graph_set.items) |graph| {
536+
if (graph.nodes.items.len > 0) return true;
537+
}
538+
return false;
539+
}

0 commit comments

Comments
 (0)