diff --git a/src/index.ts b/src/index.ts index 26d6247..f7c1cc1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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------------"); diff --git a/src/resolve.ts b/src/resolve.ts index 0129816..7d2115e 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -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(BUILTINS); @@ -31,6 +31,16 @@ type Context = { newItemsById: ComplexMap>; }; +function loadCrate(cx: Context, name: string, span: Span): Map { + 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 | 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, };