mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-16 09:25:03 +01:00
eagerly load crates from extern mod items
This commit is contained in:
parent
2f1f4a9798
commit
e455e71aa2
2 changed files with 21 additions and 12 deletions
10
src/index.ts
10
src/index.ts
|
|
@ -13,6 +13,7 @@ import { GlobalContext, parseArgs } from "./context";
|
||||||
import { loadCrate } from "./loader";
|
import { loadCrate } from "./loader";
|
||||||
|
|
||||||
const INPUT = `
|
const INPUT = `
|
||||||
|
extern mod a;
|
||||||
type A = { a: Int };
|
type A = { a: Int };
|
||||||
|
|
||||||
function main() = (
|
function main() = (
|
||||||
|
|
@ -22,13 +23,8 @@ function main() = (
|
||||||
|
|
||||||
function printA(a: A) = (
|
function printA(a: A) = (
|
||||||
print("ABCDEFGH\\n");
|
print("ABCDEFGH\\n");
|
||||||
std.printlnInt(a.a);
|
|
||||||
print("ABCDEFGH\\n");
|
print("ABCDEFGH\\n");
|
||||||
);
|
);
|
||||||
|
|
||||||
function linkStd() = (
|
|
||||||
std.println("a");
|
|
||||||
);
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
|
|
@ -47,12 +43,12 @@ function main() {
|
||||||
const gcx = new GlobalContext(opts, loadCrate);
|
const gcx = new GlobalContext(opts, loadCrate);
|
||||||
const mainCrate = gcx.crateId.next();
|
const mainCrate = gcx.crateId.next();
|
||||||
|
|
||||||
gcx.crateLoader(gcx, "std", Span.startOfFile(file));
|
|
||||||
|
|
||||||
withErrorPrinter(
|
withErrorPrinter(
|
||||||
() => {
|
() => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
|
gcx.crateLoader(gcx, "std", Span.startOfFile(file));
|
||||||
|
|
||||||
const tokens = tokenize(file);
|
const tokens = tokenize(file);
|
||||||
if (debug.has("tokens")) {
|
if (debug.has("tokens")) {
|
||||||
console.log("-----TOKENS------------");
|
console.log("-----TOKENS------------");
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ import {
|
||||||
ExternItem,
|
ExternItem,
|
||||||
} from "./ast";
|
} from "./ast";
|
||||||
import { GlobalContext } from "./context";
|
import { GlobalContext } from "./context";
|
||||||
import { CompilerError } from "./error";
|
import { CompilerError, Span } from "./error";
|
||||||
import { ComplexMap } from "./utils";
|
import { ComplexMap } from "./utils";
|
||||||
|
|
||||||
const BUILTIN_SET = new Set<string>(BUILTINS);
|
const BUILTIN_SET = new Set<string>(BUILTINS);
|
||||||
|
|
@ -31,6 +31,16 @@ type Context = {
|
||||||
newItemsById: ComplexMap<ItemId, Item<Resolved>>;
|
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(
|
function resolveModItem(
|
||||||
cx: Context,
|
cx: Context,
|
||||||
mod: ModItem<Built> | ExternItem,
|
mod: ModItem<Built> | ExternItem,
|
||||||
|
|
@ -47,10 +57,7 @@ function resolveModItem(
|
||||||
if ("contents" in mod) {
|
if ("contents" in mod) {
|
||||||
contents = new Map(mod.contents.map((item) => [item.node.name, item.id]));
|
contents = new Map(mod.contents.map((item) => [item.node.name, item.id]));
|
||||||
} else {
|
} else {
|
||||||
const loadedCrate = cx.gcx.crateLoader(cx.gcx, item.node.name, item.span);
|
contents = loadCrate(cx, item.node.name, item.span);
|
||||||
contents = new Map(
|
|
||||||
loadedCrate.rootItems.map((item) => [item.node.name, item.id])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.modContentsCache.set(item.id, contents);
|
cx.modContentsCache.set(item.id, contents);
|
||||||
|
|
@ -191,6 +198,12 @@ function resolveModule(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
case "extern": {
|
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 = {
|
const node: ExternItem = {
|
||||||
...item.node,
|
...item.node,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue