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).
72 lines
3.0 KiB
Zig
72 lines
3.0 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;
|
|
}
|