mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 16:35:03 +01:00
refactor file loading
This commit is contained in:
parent
beb0321382
commit
7eeaf548d0
9 changed files with 236 additions and 193 deletions
77
src/loader.ts
Normal file
77
src/loader.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { DepCrate } from "./ast";
|
||||
import { CrateLoader, GlobalContext } from "./context";
|
||||
import { CompilerError, LoadedFile, Span, withErrorPrinter } from "./error";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { tokenize } from "./lexer";
|
||||
import { ParseState, parse } from "./parser";
|
||||
import { resolve } from "./resolve";
|
||||
import { typeck } from "./typeck";
|
||||
|
||||
export function loadModuleFile(
|
||||
relativeTo: string,
|
||||
moduleName: string,
|
||||
span: Span
|
||||
): LoadedFile {
|
||||
const options = [
|
||||
path.join(relativeTo, `${moduleName}.nil`),
|
||||
path.join(relativeTo, moduleName, `${moduleName}.mod.nil`),
|
||||
];
|
||||
|
||||
let content: string | undefined = undefined;
|
||||
let filePath: string | undefined = undefined;
|
||||
options.forEach((tryPath) => {
|
||||
try {
|
||||
content = fs.readFileSync(tryPath, { encoding: "utf-8" });
|
||||
filePath = tryPath;
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
if (content === undefined || filePath === undefined) {
|
||||
throw new CompilerError(
|
||||
`failed to load ${moduleName}, could not find ${options.join(" or ")}`,
|
||||
span
|
||||
);
|
||||
}
|
||||
|
||||
return { content, path: filePath };
|
||||
}
|
||||
|
||||
export const loadCrate: CrateLoader = (
|
||||
gcx: GlobalContext,
|
||||
name: string,
|
||||
span: Span
|
||||
): DepCrate => {
|
||||
// We really, really want a good algorithm for finding crates.
|
||||
// But right now we just look for files in the CWD.
|
||||
|
||||
const existing = gcx.depCrates.find((crate) => crate.packageName === name);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
const file = loadModuleFile(".", name, span);
|
||||
|
||||
return withErrorPrinter(
|
||||
(): DepCrate => {
|
||||
const crateId = gcx.crateId.next();
|
||||
|
||||
const tokens = tokenize(file);
|
||||
const parseState: ParseState = { tokens, file };
|
||||
const ast = parse(name, parseState, crateId);
|
||||
const resolved = resolve(gcx, ast);
|
||||
console.log(resolved);
|
||||
|
||||
const typecked = typeck(gcx, resolved);
|
||||
|
||||
gcx.depCrates.push(typecked);
|
||||
return typecked;
|
||||
},
|
||||
() => {
|
||||
throw new CompilerError(
|
||||
`failed to load crate ${name}: crate contains errors`,
|
||||
span
|
||||
);
|
||||
}
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue