eagerly load crates from extern mod items

This commit is contained in:
nora 2023-08-02 19:32:49 +02:00
parent 2f1f4a9798
commit e455e71aa2
2 changed files with 21 additions and 12 deletions

View file

@ -13,6 +13,7 @@ import { GlobalContext, parseArgs } from "./context";
import { loadCrate } from "./loader";
const INPUT = `
extern mod a;
type A = { a: Int };
function main() = (
@ -22,13 +23,8 @@ function main() = (
function printA(a: A) = (
print("ABCDEFGH\\n");
std.printlnInt(a.a);
print("ABCDEFGH\\n");
);
function linkStd() = (
std.println("a");
);
`;
function main() {
@ -47,12 +43,12 @@ function main() {
const gcx = new GlobalContext(opts, loadCrate);
const mainCrate = gcx.crateId.next();
gcx.crateLoader(gcx, "std", Span.startOfFile(file));
withErrorPrinter(
() => {
const start = Date.now();
gcx.crateLoader(gcx, "std", Span.startOfFile(file));
const tokens = tokenize(file);
if (debug.has("tokens")) {
console.log("-----TOKENS------------");

View file

@ -19,7 +19,7 @@ import {
ExternItem,
} from "./ast";
import { GlobalContext } from "./context";
import { CompilerError } from "./error";
import { CompilerError, Span } from "./error";
import { ComplexMap } from "./utils";
const BUILTIN_SET = new Set<string>(BUILTINS);
@ -31,6 +31,16 @@ type Context = {
newItemsById: ComplexMap<ItemId, Item<Resolved>>;
};
function loadCrate(cx: Context, name: string, span: Span): Map<string, ItemId> {
const loadedCrate = cx.gcx.crateLoader(cx.gcx, name, span);
const contents = new Map(
loadedCrate.rootItems.map((item) => [item.node.name, item.id])
);
return contents;
}
function resolveModItem(
cx: Context,
mod: ModItem<Built> | ExternItem,
@ -47,10 +57,7 @@ function resolveModItem(
if ("contents" in mod) {
contents = new Map(mod.contents.map((item) => [item.node.name, item.id]));
} else {
const loadedCrate = cx.gcx.crateLoader(cx.gcx, item.node.name, item.span);
contents = new Map(
loadedCrate.rootItems.map((item) => [item.node.name, item.id])
);
contents = loadCrate(cx, item.node.name, item.span);
}
cx.modContentsCache.set(item.id, contents);
@ -191,6 +198,12 @@ function resolveModule(
};
}
case "extern": {
// Eagerly resolve the crate.
// Note that because you can reference extern crates before the item,
// we still need the loadCrate in the field access code above.
loadCrate(cx, item.node.name, item.span);
const node: ExternItem = {
...item.node,
};