tadpole/engine/tools/interface-parser.zig

43 lines
1.6 KiB
Zig

// this thing eats a top level zig file and generates a C header
pub fn main(init: std.process.Init) !void {
const args = init.minimal.args;
const argsSlice = try args.toSlice(init.arena.allocator());
_ = argsSlice;
const io = init.io;
core.setupPrint(io);
const gpa = init.arena.allocator();
const source = try std.Io.Dir.cwd().readFileAllocOptions(io, args[1], gpa, .unlimited, .of(u8), 0);
var tree = try zig.Ast.parse(gpa, source, .{ .mode = .zig });
defer tree.deinit(gpa);
for (tree.rootDecls()) |decl| {
var buf: [12]zig.Ast.Node.Index = undefined;
const proto = tree.fullFnProto(&buf, decl) orelse continue;
const export_tok = proto.extern_export_inline_token orelse continue; // not an extern, move on
if (tree.tokenTag(export_tok) != .keyword_export) continue;
var first = tree.firstToken(decl);
while (first > 0 and tree.tokenTag(first - 1) == .doc_comment) first -= 1;
var t = first;
while (tree.tokenTag(t) == .doc_comment) : (t += 1) {
// skip over all docstrings
}
core.println("func detected: ", .{tree.tokenSlice(proto.name_token.?)});
var iter = proto.iterate(&tree);
while (iter.next()) |p| {
const name = if (p.name_token) |nametok| tree.tokenSlice(nametok) else "_";
core.printn(" {s}: {s}", .{ name, tree.getNodeSource(p.type_expr.?) });
}
const ret = if (proto.ast.return_type.unwrap()) |r| tree.getNodeSource(r) else "?";
core.println(" returns {s}\n", .{ret});
}
}
const std = @import("std");
const core = @import("core");
const zig = std.zig;