- source bundle: a /docs directory with QUICK.md (Quick Reference plus a where-to-read table), language/*.md, diagnostics/errors.md and the AIR bundle only under air/; built from docs/agent/source-bundle.txt (> output files, @manifest includes) by zig build agent-reference - Quick Reference examples are checked: every aji block must be an excerpt of a conformance program in tests/conformance/source/quickref (zig build test fails otherwise); QR fixes found by the check (mut d = data, region not auto-borrowed, statement/borrow blocks that could not verify, io_error match) - frontend: task.*/channel.*/mutex.* are not-yet-implemented, not E7006 - aji lower --stats: mechanical_air_eliminated_raw and _normalized (each byte-string materialization counted as one constant; spec 15 §11) - harness: profile aji_source (doc_groups), per-file and per-group docs metrics on distinctive lines, profile columns (tool calls, lowered AIR raw/normalized, docs files read, IR-form and type-mismatch counts), dev-echo aji_source variant, scripted run and test, source token report - plan: P6A-8 done, P6A-9 criteria; Q-021/Q-022 resolved; unsafe block deferred
127 lines
5.2 KiB
Zig
127 lines
5.2 KiB
Zig
//! `.aji` frontend (spec 15): source → canonical text AIR + line map.
|
|
//! Outside the trusted computing base: its output is checked by the
|
|
//! normative verifier like any other AIR.
|
|
|
|
const std = @import("std");
|
|
const diag = @import("diag.zig");
|
|
pub const lex = @import("frontend/lex.zig");
|
|
pub const ast = @import("frontend/ast.zig");
|
|
pub const parse = @import("frontend/parse.zig");
|
|
pub const lower_mod = @import("frontend/lower.zig");
|
|
|
|
pub const Span = ast.Span;
|
|
|
|
pub const Compiled = union(enum) {
|
|
/// Canonical AIR, its line map and the annotated form.
|
|
ok: lower_mod.Output,
|
|
/// One source diagnostic (`E7xxx`): the first problem only.
|
|
err: diag.Diagnostic,
|
|
unimplemented: []const u8,
|
|
};
|
|
|
|
pub fn compile(arena: std.mem.Allocator, file: []const u8, source: []const u8) std.mem.Allocator.Error!Compiled {
|
|
const parsed = try parse.parse(arena, source);
|
|
const m = switch (parsed) {
|
|
.ok => |m| m,
|
|
.err => |f| return .{ .err = .{
|
|
.code = diag.lookup("E7001").?,
|
|
.span = .{ .file = file, .line = f.pos.line, .col = f.pos.col },
|
|
.message = try std.fmt.allocPrint(arena, "expected {s}", .{f.expected}),
|
|
.fields = try arena.dupe(diag.Field, &.{
|
|
.{ .name = "expected", .value = .{ .str = f.expected } },
|
|
.{ .name = "found", .value = .{ .str = f.found } },
|
|
}),
|
|
} },
|
|
};
|
|
return switch (try lower_mod.lower(arena, m)) {
|
|
.ok => |o| .{ .ok = o },
|
|
.err => |p| blk: {
|
|
const fields = try arena.alloc(diag.Field, p.fields.len);
|
|
for (p.fields, fields) |pf, *f| f.* = .{ .name = pf.name, .value = .{ .str = pf.value } };
|
|
break :blk .{ .err = .{
|
|
.code = diag.lookup(p.code).?,
|
|
.span = .{ .file = file, .line = p.span.start.line, .col = p.span.start.col },
|
|
.message = p.message,
|
|
.fields = fields,
|
|
} };
|
|
},
|
|
.unimplemented => |u| .{ .unimplemented = try std.fmt.allocPrint(arena, "{s}:{d}:{d}: {s}", .{ file, u.span.start.line, u.span.start.col, u.construct }) },
|
|
};
|
|
}
|
|
|
|
/// Moves a diagnostic on lowered AIR to its source location; the AIR
|
|
/// location stays as `air_line`/`air_col` metadata (spec 15 §10).
|
|
pub fn mapDiagnostic(arena: std.mem.Allocator, d: diag.Diagnostic, file: []const u8, lines: []const ?Span) std.mem.Allocator.Error!diag.Diagnostic {
|
|
var out = d;
|
|
const sp = d.span orelse return out;
|
|
const fields = try arena.alloc(diag.Field, d.fields.len + 2);
|
|
@memcpy(fields[0..d.fields.len], d.fields);
|
|
fields[d.fields.len] = .{ .name = "air_line", .value = .{ .int = sp.line } };
|
|
fields[d.fields.len + 1] = .{ .name = "air_col", .value = .{ .int = sp.col } };
|
|
out.fields = fields;
|
|
const src: ?Span = if (sp.line >= 1 and sp.line <= lines.len) lines[sp.line - 1] else null;
|
|
out.span = .{ .file = file, .line = if (src) |s| s.start.line else 1, .col = if (src) |s| s.start.col else 1 };
|
|
return out;
|
|
}
|
|
|
|
test {
|
|
_ = lex;
|
|
_ = parse;
|
|
_ = lower_mod;
|
|
}
|
|
|
|
pub const Size = struct { bytes: usize, lines: usize };
|
|
|
|
/// AIR size with every byte-string materialization (the instructions from
|
|
/// its `local.zero` to its final `borrow.end`) replaced by one notional
|
|
/// line ` const.bytes %<storage>, b"<canonical literal>"` (spec 15 §11).
|
|
/// Blank lines are not counted, as for the raw size.
|
|
pub fn normalizedSize(arena: std.mem.Allocator, o: lower_mod.Output) std.mem.Allocator.Error!Size {
|
|
var size: Size = .{ .bytes = 0, .lines = 0 };
|
|
var it = std.mem.splitScalar(u8, o.air, '\n');
|
|
var i: usize = 0;
|
|
var last: u32 = 0;
|
|
while (it.next()) |ln| : (i += 1) {
|
|
const k: u32 = if (i < o.constant_of.len) o.constant_of[i] else 0;
|
|
if (k != 0) {
|
|
if (k != last) {
|
|
const c = o.constants[k - 1];
|
|
const text = try std.fmt.allocPrint(arena, " const.bytes %{s}, {s}", .{ c.storage, try canonicalBytes(arena, c.bytes) });
|
|
size.bytes += text.len + 1;
|
|
size.lines += 1;
|
|
}
|
|
last = k;
|
|
continue;
|
|
}
|
|
last = 0;
|
|
if (std.mem.trim(u8, ln, " \t\r").len == 0) continue;
|
|
size.bytes += ln.len + 1;
|
|
size.lines += 1;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/// `b"…"` with printable ASCII as is and `\n \r \t \0 \\ \" \xNN` otherwise.
|
|
pub fn canonicalBytes(arena: std.mem.Allocator, bytes: []const u8) std.mem.Allocator.Error![]const u8 {
|
|
var out: std.ArrayList(u8) = .empty;
|
|
try out.appendSlice(arena, "b\"");
|
|
for (bytes) |c| switch (c) {
|
|
'\n' => try out.appendSlice(arena, "\\n"),
|
|
'\r' => try out.appendSlice(arena, "\\r"),
|
|
'\t' => try out.appendSlice(arena, "\\t"),
|
|
0 => try out.appendSlice(arena, "\\0"),
|
|
'\\' => try out.appendSlice(arena, "\\\\"),
|
|
'"' => try out.appendSlice(arena, "\\\""),
|
|
0x20...0x21, 0x23...0x5b, 0x5d...0x7e => try out.append(arena, c),
|
|
else => try out.print(arena, "\\x{x:0>2}", .{c}),
|
|
};
|
|
try out.append(arena, '"');
|
|
return out.items;
|
|
}
|
|
|
|
test canonicalBytes {
|
|
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
defer a.deinit();
|
|
try std.testing.expectEqualStrings("b\"hi\\n\\\"\\x01\"", try canonicalBytes(a.allocator(), "hi\n\"\x01"));
|
|
}
|