|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// gen_result_codes.zig — extract the canonical result-code contract from the |
| 5 | +// Idris2 model (src/interface/abi/Types.idr) and emit the Zig source file |
| 6 | +// (src/interface/ffi/src/result_codes_expected.zig) consumed by the comptime |
| 7 | +// cross-check next to GsaResult in main.zig. |
| 8 | +// |
| 9 | +// Types.idr is the single source of truth: its `resultToInt` clauses define |
| 10 | +// the stable (constructor, code) pairs. This tool lifts them into Zig so the |
| 11 | +// compiler can assert GsaResult agrees name-for-name and value-for-value — |
| 12 | +// the drift this closes shipped once already (codes 5-12 diverged silently). |
| 13 | +// |
| 14 | +// Run from the repository root: |
| 15 | +// zig run scripts/gen_result_codes.zig |
| 16 | +// CI re-runs this and `git diff --exit-code`s the result (abi-contract job). |
| 17 | + |
| 18 | +const std = @import("std"); |
| 19 | + |
| 20 | +const TYPES_IDR = "src/interface/abi/Types.idr"; |
| 21 | +const OUT_ZIG = "src/interface/ffi/src/result_codes_expected.zig"; |
| 22 | + |
| 23 | +// Constructors whose Zig field name is not the mechanical CamelCase -> |
| 24 | +// snake_case conversion. A constructor missing from both this table and the |
| 25 | +// mechanical rule fails the build of the *generator*, forcing a conscious |
| 26 | +// mapping decision for every new code. |
| 27 | +const Override = struct { idris: []const u8, zig: []const u8 }; |
| 28 | +const overrides = [_]Override{ |
| 29 | + .{ .idris = "Error", .zig = "err" }, |
| 30 | + .{ .idris = "VeriSimDBUnavailable", .zig = "verisimdb_unavailable" }, |
| 31 | +}; |
| 32 | + |
| 33 | +const HEADER = |
| 34 | + \\// SPDX-License-Identifier: MPL-2.0 |
| 35 | + \\// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 36 | + \\// |
| 37 | + \\// @generated by scripts/gen_result_codes.zig from src/interface/abi/Types.idr |
| 38 | + \\// DO NOT EDIT. Regenerate with: zig run scripts/gen_result_codes.zig |
| 39 | + \\// |
| 40 | + \\// Canonical result-code contract lifted from the Idris2 model |
| 41 | + \\// (GSA.ABI.Types.resultToInt). Consumed by the comptime block next to |
| 42 | + \\// GsaResult in main.zig to assert the two enums agree. |
| 43 | + \\ |
| 44 | + \\pub const ResultCode = struct { |
| 45 | + \\ idris_ctor: []const u8, |
| 46 | + \\ zig_field: []const u8, |
| 47 | + \\ code: c_int, |
| 48 | + \\}; |
| 49 | + \\ |
| 50 | + \\pub const all = [_]ResultCode{ |
| 51 | + \\ |
| 52 | +; |
| 53 | + |
| 54 | +fn zigFieldFor(alloc: std.mem.Allocator, ctor: []const u8) ![]const u8 { |
| 55 | + for (overrides) |o| { |
| 56 | + if (std.mem.eql(u8, o.idris, ctor)) return try alloc.dupe(u8, o.zig); |
| 57 | + } |
| 58 | + // Mechanical CamelCase -> snake_case (no consecutive-capital handling: |
| 59 | + // constructors needing that must go in `overrides`). |
| 60 | + var out = std.array_list.Managed(u8).init(alloc); |
| 61 | + errdefer out.deinit(); |
| 62 | + for (ctor, 0..) |c, i| { |
| 63 | + if (std.ascii.isUpper(c)) { |
| 64 | + if (i != 0) try out.append('_'); |
| 65 | + try out.append(std.ascii.toLower(c)); |
| 66 | + } else { |
| 67 | + try out.append(c); |
| 68 | + } |
| 69 | + } |
| 70 | + return out.toOwnedSlice(); |
| 71 | +} |
| 72 | + |
| 73 | +pub fn main() !void { |
| 74 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 75 | + defer _ = gpa.deinit(); |
| 76 | + const alloc = gpa.allocator(); |
| 77 | + |
| 78 | + const src = try std.fs.cwd().readFileAlloc(alloc, TYPES_IDR, 4 * 1024 * 1024); |
| 79 | + defer alloc.free(src); |
| 80 | + |
| 81 | + var out = std.array_list.Managed(u8).init(alloc); |
| 82 | + defer out.deinit(); |
| 83 | + try out.appendSlice(HEADER); |
| 84 | + |
| 85 | + var count: usize = 0; |
| 86 | + var lines = std.mem.splitScalar(u8, src, '\n'); |
| 87 | + while (lines.next()) |line| { |
| 88 | + const trimmed = std.mem.trim(u8, line, " \r\t"); |
| 89 | + if (!std.mem.startsWith(u8, trimmed, "resultToInt ")) continue; |
| 90 | + if (std.mem.indexOfScalar(u8, trimmed, ':') != null) continue; // type signature |
| 91 | + |
| 92 | + var it = std.mem.tokenizeAny(u8, trimmed, " \t"); |
| 93 | + _ = it.next(); // "resultToInt" |
| 94 | + const ctor = it.next() orelse return error.BadClause; |
| 95 | + const eq = it.next() orelse return error.BadClause; |
| 96 | + if (!std.mem.eql(u8, eq, "=")) return error.BadClause; |
| 97 | + const code = try std.fmt.parseInt(c_int, it.next() orelse return error.BadClause, 10); |
| 98 | + |
| 99 | + const field = try zigFieldFor(alloc, ctor); |
| 100 | + defer alloc.free(field); |
| 101 | + try out.writer().print( |
| 102 | + " .{{ .idris_ctor = \"{s}\", .zig_field = \"{s}\", .code = {d} }},\n", |
| 103 | + .{ ctor, field, code }, |
| 104 | + ); |
| 105 | + count += 1; |
| 106 | + } |
| 107 | + try out.appendSlice("};\n"); |
| 108 | + |
| 109 | + if (count == 0) return error.NoClausesFound; |
| 110 | + |
| 111 | + try std.fs.cwd().writeFile(.{ .sub_path = OUT_ZIG, .data = out.items }); |
| 112 | + std.debug.print("wrote {s} ({d} result codes)\n", .{ OUT_ZIG, count }); |
| 113 | +} |
0 commit comments