//! Closed, compiler-pinned registry of safe `.aji` standard modules. //! No host file lookup, runtime initialization or verifier privilege. const std = @import("std"); const ast = @import("ast.zig"); const parse = @import("parse.zig"); const lower = @import("lower.zig"); const Entry = struct { name: []const u8, file: []const u8, source: []const u8 }; const registry = [_]Entry{ .{ .name = "std.bytes", .file = "std/bytes.aji", .source = @embedFile("std_bytes_aji") }, .{ .name = "std.math", .file = "std/math.aji", .source = @embedFile("std_math_aji") }, }; pub const Result = union(enum) { ok: ast.Module, err: lower.Problem }; pub fn link(arena: std.mem.Allocator, module: ast.Module) std.mem.Allocator.Error!Result { // Validate in source order, before lowering, so the first error is stable. for (module.imports, 0..) |imp, i| { for (module.imports[0..i]) |prev| { if (std.mem.eql(u8, imp.path, prev.path)) return problem(arena, "E7006", imp, "module imported twice"); } const known = for (registry) |entry| { if (std.mem.eql(u8, entry.name, imp.path)) break true; } else false; if (!known) return problem(arena, "E7002", imp, "unknown built-in standard module"); } if (module.imports.len == 0) return .{ .ok = module }; var functions: std.ArrayList(ast.FnDecl) = .empty; try functions.appendSlice(arena, module.functions); // Registry order is lexicographic; import declaration order never affects AIR. for (registry) |entry| { const imported = for (module.imports) |imp| { if (std.mem.eql(u8, imp.path, entry.name)) break true; } else false; if (!imported) continue; const library = switch (try parse.parseWithFile(arena, entry.source, entry.file)) { .ok => |m| m, .err => |failure| return .{ .err = .{ .code = "E7900", .span = .{ .start = failure.pos, .end = failure.pos, .file = entry.file }, .message = "malformed bundled standard source: a compiler bug", } }, }; // The bootstrap registry intentionally exposes no imported types/dependencies. std.debug.assert(std.mem.eql(u8, library.name, entry.name)); std.debug.assert(library.types.len == 0 and library.imports.len == 0); for (library.functions) |f| { var qualified = f; qualified.name = try std.fmt.allocPrint(arena, "{s}.{s}", .{ entry.name, f.name }); const base = try std.fmt.allocPrint(arena, "__aji_{s}_{s}", .{ entry.name, f.name }); for (base) |*c| if (c.* == '.') { c.* = '_'; }; var candidate: []const u8 = base; var suffix: usize = 0; while (airNameUsed(module.types, functions.items, candidate)) { suffix += 1; candidate = try std.fmt.allocPrint(arena, "{s}_{d}", .{ base, suffix }); } qualified.air_name = candidate; try functions.append(arena, qualified); } } var combined = module; combined.functions = functions.items; return .{ .ok = combined }; } fn airNameUsed(types: []const ast.TypeDecl, functions: []const ast.FnDecl, name: []const u8) bool { for (types) |t| if (std.mem.eql(u8, t.name, name)) return true; for (functions) |f| if (std.mem.eql(u8, f.air_name orelse f.name, name)) return true; return false; } fn problem(arena: std.mem.Allocator, code: []const u8, imp: ast.Import, message: []const u8) std.mem.Allocator.Error!Result { return .{ .err = .{ .code = code, .span = imp.span, .message = message, .fields = try arena.dupe(lower.Problem.Field, &.{.{ .name = "name", .value = imp.path }}), } }; }