Phase 6A: .aji layer 2 (structs, enums, exhaustive match)

Owner rules: struct/enum declarations in Rust form; one construction
form each (named struct literal, Enum::Variant); struct fields lowered
in declaration order, so field order in the source does not change the
AIR (golden test with two orders, one AIR); match over enums exhaustive
with no '_' (E7004 lists missing variants, E7006 for '_'), integer
match needs '_'; payload names scoped to their arm (enum.switch block
parameters); match as an expression with a merge block; arm-level
source map; no destructuring, guards or nested patterns. Type inference
limits written into spec 15 (one function, monomorphic, no search,
E7007). aji lower --stats: source/AIR size and expansion ratio.

Tests: 4 run (one shared golden), 7 rejects incl. verifier-caught E1003.
This commit is contained in:
dev committed 2026-10-05 11:31:56 +03:00
1 parent ba3e9b3719
commit 0fdd04179b
22 files changed
+702 -121

No files matched your search

+29 -3
View File
@@ -29,9 +29,10 @@ const usage =
\\ --seed N; scheduler: --quantum N, --task-limit N,
\\ --schedule-seed N; --profile hosted uses the real OS and
\\ writes the record only to --result-file PATH
\\ lower [--annotate] <file.aji>
\\ lower [--annotate | --stats] <file.aji>
\\ print the canonical AIR a source module lowers to;
\\ --annotate adds source spans as comments (not canonical)
\\ --annotate adds source spans as comments (not canonical);
\\ --stats prints source/AIR size and expansion ratio
\\ report <file.air> verify, then print the audit report (spec 10 §6.1)
\\ codes list diagnostic codes as JSON Lines
\\ features print supported AIR format, language version and features
@@ -88,7 +89,8 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
}
if (std.mem.eql(u8, cmd, "lower")) {
const annotate = args.len == 4 and std.mem.eql(u8, args[2], "--annotate");
if (args.len != 3 and !annotate) return usageError("lower takes [--annotate] and one .aji file");
const stats = args.len == 4 and std.mem.eql(u8, args[2], "--stats");
if (args.len != 3 and !annotate and !stats) return usageError("lower takes [--annotate | --stats] and one .aji file");
const path = args[args.len - 1];
const source = std.Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 << 20)) catch |err| {
std.debug.print("aji: cannot read {s}: {t}\n", .{ path, err });
@@ -96,6 +98,30 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
};
switch (try aji.frontend.compile(arena, path, source)) {
.ok => |o| {
if (stats) {
// How much mechanical work the frontend took over.
const lines = struct {
fn count(t: []const u8) usize {
var n: usize = 0;
var it = std.mem.splitScalar(u8, t, '\n');
while (it.next()) |ln| n += @intFromBool(std.mem.trim(u8, ln, " \t\r").len != 0);
return n;
}
}.count;
const sl = lines(source);
const al = lines(o.air);
var s: std.json.Stringify = .{ .writer = out };
try s.write(.{
.source_bytes = source.len,
.source_lines = sl,
.air_bytes = o.air.len,
.air_lines = al,
.expansion_bytes = @as(f64, @floatFromInt(o.air.len)) / @as(f64, @floatFromInt(@max(source.len, 1))),
.expansion_lines = @as(f64, @floatFromInt(al)) / @as(f64, @floatFromInt(@max(sl, 1))),
});
try out.writeByte('\n');
return .accepted;
}
try out.writeAll(if (annotate) o.annotated else o.air);
return .accepted;
},