@@ -191,35 +191,68 @@ pub fn main() !void {
191
191
std .debug .print ("merge '{s}'...\n " , .{h_path });
192
192
193
193
// parse the headers into graphs
194
+ var graph_set = std .ArrayList (Graph ).init (arena );
194
195
for (entry .value_ptr .items ) | header | {
195
196
std .debug .print ("doing {s} now\n " , .{header .input .path });
196
197
const graph = try parseHeaderIntoGraph (arena , h_path , header , sys_include );
197
198
198
- for (graph .nodes ) | node | {
199
+ for (graph .nodes . items ) | node | {
199
200
std .debug .print ("node: definition: '{s}' => '{s}'\n " , .{ node .definition .name , node .definition .source });
200
201
}
202
+ try graph_set .append (graph );
201
203
}
202
204
203
205
// topological sort the graphs
206
+ // TODO
204
207
205
208
// 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
+ }
206
223
207
224
// render output
225
+ if (fs .path .dirname (h_path )) | dirname | {
226
+ try out_dir .dir .makePath (dirname );
227
+ }
208
228
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 ();
211
231
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 ();
214
234
215
- //// try w.writeAll("#pragma once\n");
235
+ //try w.writeAll("#pragma once\n");
216
236
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 ();
218
247
}
219
248
}
220
249
221
250
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
+ }
223
256
};
224
257
225
258
const Node = union (enum ) {
@@ -386,7 +419,7 @@ fn parseHeaderIntoGraph(arena: Allocator, h_path: []const u8, header: Header, sy
386
419
}
387
420
388
421
return .{
389
- .nodes = nodes . items ,
422
+ .nodes = nodes ,
390
423
};
391
424
}
392
425
@@ -497,3 +530,10 @@ pub fn tokSlice(source: arocc.Source, tok: Tokenizer.Token) []const u8 {
497
530
if (tok .id .lexeme ()) | s | return s ;
498
531
return source .buf [tok .start .. tok .end ];
499
532
}
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