cli: add deterministic source formatter

This commit is contained in:
dev committed 2026-10-07 18:21:06 +03:00
1 parent d901d5227b
commit 462ddd2c5c
14 files changed
+787 -13

No files matched your search

+15 -9
View File
@@ -11,6 +11,7 @@ pub const lower_mod = @import("frontend/lower.zig");
pub const generics = @import("frontend/generics.zig");
pub const modules = @import("frontend/modules.zig");
pub const stdlib = @import("frontend/stdlib.zig");
pub const format = @import("frontend/format.zig");
pub const Span = ast.Span;
@@ -30,15 +31,7 @@ pub fn compileWithModules(arena: std.mem.Allocator, file: []const u8, source: []
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 } },
}),
} },
.err => |f| return .{ .err = try syntaxDiagnostic(arena, file, f) },
};
const linked = try modules.link(arena, m, sources);
const result = switch (linked) {
@@ -64,6 +57,18 @@ pub fn compileWithModules(arena: std.mem.Allocator, file: []const u8, source: []
};
}
pub fn syntaxDiagnostic(arena: std.mem.Allocator, file: []const u8, f: parse.Failure) std.mem.Allocator.Error!diag.Diagnostic {
return .{
.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 } },
}),
};
}
/// 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 {
@@ -83,6 +88,7 @@ test {
_ = lex;
_ = parse;
_ = lower_mod;
_ = format;
}
pub const Size = struct { bytes: usize, lines: usize };