Phase 6A: .aji frontend, layer 1 (scalars, expressions, functions, control flow)

src/frontend: lexer, AST, parser for the full 6A grammar (first error
only: one E7001 with span, expected and found), lowering to canonical
text AIR with a line map. Variables become SSA with block parameters
carried across if/while/for/loop joins; short-circuit and/or; checked,
wrapping and cast forms; calls; untyped let literals typed from later
uses, never guessed (E7007). The frontend repairs nothing.

check.checkFile: .aji → frontend → existing AIR parser and verifier;
every diagnostic at its source location, AIR location kept as
air_line/air_col; trap records gain source_location. CLI: check/run on
.aji, aji lower [--annotate].

Runner: .aji tests with the same headers; every accepted module must
lower deterministically, be canonical (lower → parse → print
unchanged) and match <file>.expected if present. Layer-1 tests: 6 run,
6 E7xxx rejects, 2 verifier-caught (E1002, E3101).
This commit is contained in:
dev committed 2026-10-05 11:23:18 +03:00
1 parent bfb4c445cd
commit ba3e9b3719
26 files changed
+2478 -13

No files matched your search

+57 -2
View File
@@ -29,6 +29,9 @@ 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>
\\ print the canonical AIR a source module lowers to;
\\ --annotate adds source spans as comments (not canonical)
\\ 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
@@ -83,6 +86,30 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
try out.writeByte('\n');
return .accepted;
}
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 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 });
return .usage;
};
switch (try aji.frontend.compile(arena, path, source)) {
.ok => |o| {
try out.writeAll(if (annotate) o.annotated else o.air);
return .accepted;
},
.err => |d| {
try d.writeJson(out);
try out.writeByte('\n');
return .rejected;
},
.unimplemented => |what| {
std.debug.print("aji: not implemented yet: {s}\n", .{what});
return .unimplemented;
},
}
}
if (std.mem.eql(u8, cmd, "features")) {
var s: std.json.Stringify = .{ .writer = out };
try s.write(.{
@@ -188,7 +215,7 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
std.debug.print("aji: cannot read {s}: {t}\n", .{ path, err });
return .usage;
};
const report = try aji.check.checkAir(arena, path, source);
const report = try aji.check.checkFile(arena, path, source);
for (report.diagnostics) |d| {
try d.writeJson(out);
try out.writeByte('\n');
@@ -208,7 +235,7 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
};
const captured: ?aji.interp.Output = if (hosted) null else output;
if (!hosted) {
try aji.interp.writeOutcome(module, outcome, captured, out);
try writeRecord(arena, module, outcome, captured, report, path, out);
try out.writeByte('\n');
} else if (result_file == null) {
// Human summary only; the machine interface is --result-file (spec 11 §8).
@@ -245,6 +272,34 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
return usageError("unknown command");
}
/// The run record; for a `.aji` module a trap also carries its
/// `source_location` (spec 15 §10).
fn writeRecord(arena: std.mem.Allocator, module: aji.air.Module, outcome: aji.interp.Outcome, captured: ?aji.interp.Output, report: aji.check.Report, path: []const u8, out: *std.Io.Writer) !void {
var rec: std.Io.Writer.Allocating = .init(arena);
try aji.interp.writeOutcome(module, outcome, captured, &rec.writer);
const text = rec.written();
const lowered = report.lowered orelse return out.writeAll(text);
const t = switch (outcome) {
.trapped => |t| t,
else => return out.writeAll(text),
};
var loc: ?aji.frontend.Span = null;
for (module.functions) |f| {
if (!std.mem.eql(u8, f.name, t.function)) continue;
for (f.blocks) |b| {
if (!std.mem.eql(u8, b.name, t.block)) continue;
const air_line = b.insts[t.inst].span.line;
if (air_line >= 1 and air_line <= lowered.lines.len) loc = lowered.lines[air_line - 1];
}
}
const s = loc orelse return out.writeAll(text);
try out.writeAll(text[0 .. text.len - 1]);
var j: std.json.Stringify = .{ .writer = out };
try out.writeAll(",\"source_location\":");
try j.write(.{ .file = path, .line = s.start.line, .col = s.start.col });
try out.writeAll("}");
}
fn writeCode(out: *std.Io.Writer, c: aji.diag.Code) !void {
var s: std.json.Stringify = .{ .writer = out };
try s.write(.{ .code = c.id, .kind = c.kind, .meaning = c.meaning });