Skip to content

Commit 4ff9363

Browse files
committed
Loader
1 parent 28a10c1 commit 4ff9363

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

TODO.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# module system
22

33
Mod -- has loader
4-
54
Place and Transition -- has modId
65

76
Mod has definitions: Map<string, Definition>
87

98
# loader
109

11-
copy `loader/` from inet-js
10+
fix `Loader` -- parse and execute
1211

1312
# command-line
1413

src/lang/mod/createMod.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Loader } from "../../loader"
2+
// import { defineBuiltins } from "../builtins/defineBuiltins"
3+
// import { createChecking } from "../checking/createChecking"
4+
// import { createEnv } from "../env/createEnv"
5+
// import { Stmt } from "../stmt"
6+
import { Mod } from "./Mod"
7+
8+
export function createMod(options: {
9+
url: URL
10+
// text: string
11+
// stmts: Array<Stmt>
12+
// loader: Loader
13+
}): Mod {
14+
const mod = {
15+
// loader: options.loader,
16+
url: options.url,
17+
// text: options.text,
18+
// stmts: options.stmts,
19+
definitions: new Map(),
20+
// builtins: new Map(),
21+
// ruleEntries: new Map(),
22+
// requiredMods: new Map(),
23+
} as Mod
24+
25+
// mod.env = createEnv(mod)
26+
// mod.checking = createChecking()
27+
28+
// defineBuiltins(mod)
29+
30+
return mod
31+
}

src/lang/mod/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./Mod"
2+
export * from "./createMod"

src/loader/Loader.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Fetcher } from "../fetcher"
2+
// import { execute } from "../lang/execute"
3+
import { Mod } from "../lang/mod"
4+
import { createMod } from "../lang/mod/createMod"
5+
// import { parseStmts } from "../lang/syntax"
6+
7+
export class Loader {
8+
loaded: Map<string, Mod> = new Map()
9+
loading: Set<string> = new Set()
10+
fetcher: Fetcher
11+
onOutput = (output: string) => {
12+
console.log(output)
13+
}
14+
15+
constructor(options: { fetcher: Fetcher }) {
16+
this.fetcher = options.fetcher
17+
}
18+
19+
async load(url: URL, options?: { text?: string }): Promise<Mod> {
20+
const found = this.loaded.get(url.href)
21+
if (found !== undefined) return found
22+
23+
const text =
24+
options?.text === undefined
25+
? await this.fetcher.fetchText(url)
26+
: options.text
27+
28+
// const stmts = parseStmts(text)
29+
const mod = createMod({
30+
// loader: this,
31+
url,
32+
// text,
33+
// stmts,
34+
})
35+
36+
this.loading.add(url.href)
37+
38+
// for (const stmt of stmts) {
39+
// await execute(mod, stmt)
40+
// }
41+
42+
this.loading.delete(url.href)
43+
44+
this.loaded.set(url.href, mod)
45+
46+
return mod
47+
}
48+
}

src/loader/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./Loader"

0 commit comments

Comments
 (0)