-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.mts
70 lines (62 loc) · 2.2 KB
/
api.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { ParsedFiles, parseDirectory } from "./lib/parse-directory.mjs";
import { parseFile } from "./lib/parse-file.mjs";
import { parseStd, stdPath } from "./lib/parse-std.mjs";
import { resolveSrc } from "./lib/resolve-src.mjs";
import { File } from "./lib/file.mjs";
import { parse } from "./parser.mjs";
import { expandSyntaxMacrosOfFiles } from "./syntax-macros/index.mjs";
import { resolveFileModules } from "./modules.mjs";
import { expandRegularMacros } from "./regular-macros.mjs";
import { typeCheck } from "./semantics/index.mjs";
import binaryen from "binaryen";
import { genWasmCode } from "./wasm-code-gen.mjs";
export type ParsedModule = {
files: ParsedFiles;
/** Path to src directory (a folder containing index.void that acts as entry) if available */
srcPath?: string;
/** Path to root void file */
indexPath: string;
};
export const compileText = async (text: string) => {
const parsedModule = await parseText(text);
return compileParsedModule(parsedModule);
};
export const compilePath = async (path: string) => {
const parsedModule = await parsePath(path);
return compileParsedModule(parsedModule);
};
export const compileParsedModule = (module: ParsedModule): binaryen.Module => {
const syntaxExpandedFiles = expandSyntaxMacrosOfFiles(module.files);
const moduleResolvedModule = resolveFileModules({
...module,
files: syntaxExpandedFiles,
stdPath: stdPath,
});
const regularMacroExpandedModule = expandRegularMacros(moduleResolvedModule);
const typeCheckedModule = typeCheck(regularMacroExpandedModule);
return genWasmCode(typeCheckedModule);
};
export const parseText = async (text: string): Promise<ParsedModule> => {
const file = new File(text, "index");
return {
files: {
index: parse(file),
...(await parseStd()),
},
indexPath: "index",
};
};
export const parsePath = async (path: string): Promise<ParsedModule> => {
const src = await resolveSrc(path);
const srcFiles = src.srcRootPath
? await parseDirectory(src.srcRootPath)
: { [src.indexPath]: await parseFile(src.indexPath) };
return {
files: {
...srcFiles,
...(await parseStd()),
},
srcPath: src.srcRootPath,
indexPath: src.indexPath,
};
};