//! Front door used by the CLI, the conformance runner and (later) `ajid`: //! parse → verify, collecting diagnostics (spec 07 §8.6). const std = @import("std"); const air = @import("air.zig"); const diag = @import("diag.zig"); const verifier = @import("verifier.zig"); const frontend = @import("frontend.zig"); pub const Status = enum { /// Module verified; no error diagnostics. accepted, /// At least one error diagnostic. rejected, /// The pipeline cannot yet decide this input. unimplemented, }; pub const Report = struct { status: Status, diagnostics: []const diag.Diagnostic, /// Present when `status == .accepted`. module: ?air.Module = null, /// Pipeline stage that is missing when `status == .unimplemented`. missing_stage: []const u8 = "", /// For `.aji` input: the lowered AIR and its line map (spec 15 §10). lowered: ?frontend.lower_mod.Output = null, }; /// Checks a `.aji` module (frontend, then the AIR pipeline, with every /// diagnostic at its source location) or a text AIR module. pub fn checkFile(arena: std.mem.Allocator, file_name: []const u8, source: []const u8) !Report { if (!std.mem.endsWith(u8, file_name, ".aji")) return checkAir(arena, file_name, source); const lowered = switch (try frontend.compile(arena, file_name, source)) { .ok => |o| o, .err => |d| return .{ .status = .rejected, .diagnostics = try arena.dupe(diag.Diagnostic, &.{d}) }, .unimplemented => |what| return .{ .status = .unimplemented, .diagnostics = &.{}, .missing_stage = what }, }; var r = try checkAir(arena, file_name, lowered.air); // One source construct often lowers to several AIR instructions that the // verifier reports separately: keep one diagnostic per code and source // location (the first). var mapped: std.ArrayList(diag.Diagnostic) = .empty; for (r.diagnostics) |d| { const m = try frontend.mapDiagnostic(arena, d, file_name, lowered.lines); const dup = for (mapped.items) |prev| { if (prev.code == m.code and prev.span.?.line == m.span.?.line and prev.span.?.col == m.span.?.col) break true; } else false; if (!dup) try mapped.append(arena, m); } r.diagnostics = mapped.items; r.lowered = lowered; return r; } /// Checks a text AIR module. All allocations go into `arena`. pub fn checkAir(arena: std.mem.Allocator, file_name: []const u8, source: []const u8) !Report { const parsed = try air.parse.parse(arena, file_name, source); if (parsed.diagnostics.len != 0) return .{ .status = .rejected, .diagnostics = parsed.diagnostics }; const module = parsed.module.?; const ds = try verifier.verify(arena, file_name, module); if (ds.len != 0) return .{ .status = .rejected, .diagnostics = ds }; return .{ .status = .accepted, .diagnostics = &.{}, .module = module }; }