Skip to content

Commit a93673e

Browse files
committed
extract handleImport from handleDefine
1 parent f711fc9 commit a93673e

File tree

4 files changed

+49
-43
lines changed

4 files changed

+49
-43
lines changed

TODO.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
> refactor `load` to support circular imports
2-
>
3-
> - if we can support mutually defined recursive function,
4-
> we should also support circular imports.
5-
6-
extract `handleImport` from `handleDefine`
71
`run` should be async
82
call `run` in `load`
93
rename `run/` to `load/`
104

5+
call `handleImport` after `handleDefine` -- to support circular imports
6+
7+
- if we can support mutually defined recursive function,
8+
we should also support circular imports.
9+
1110
# lazy evaluation
1211

1312
[problem] we can use `Y`! -- `DelayedApply` can replace `Lazy`? -- what evaluation strategy is this?

src/lang/run/handleDefine.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { emptyEnv } from "../env/Env.ts"
22
import { evaluate } from "../evaluate/evaluate.ts"
3-
import { modDefine, modFind, modResolve } from "../mod/index.ts"
3+
import { modDefine } from "../mod/index.ts"
44
import type { Mod } from "../mod/Mod.ts"
5-
import type { ImportEntry, Stmt } from "../stmt/Stmt.ts"
6-
import { globalLoadedMods } from "./globalLoadedMods.ts"
7-
import { run } from "./run.ts"
5+
import type { Stmt } from "../stmt/Stmt.ts"
86

97
export function handleDefine(mod: Mod, stmt: Stmt): void {
108
if (stmt.kind === "Define") {
@@ -22,37 +20,4 @@ export function handleDefine(mod: Mod, stmt: Stmt): void {
2220

2321
return
2422
}
25-
26-
if (stmt.kind === "Import") {
27-
for (const entry of stmt.entries) {
28-
importOne(mod, stmt.path, entry)
29-
}
30-
31-
return
32-
}
33-
}
34-
35-
function importOne(mod: Mod, path: string, entry: ImportEntry): void {
36-
const url = modResolve(mod, path)
37-
if (url.href === mod.url.href) {
38-
throw new Error(`I can not circular import: ${path}`)
39-
}
40-
41-
const found = globalLoadedMods.get(url.href)
42-
if (found === undefined) {
43-
throw new Error(`Mod is not loaded: ${path}`)
44-
}
45-
46-
run(found.mod)
47-
48-
const { name, rename } = entry
49-
50-
const def = modFind(found.mod, name)
51-
if (def === undefined) {
52-
throw new Error(
53-
`I can not import undefined name: ${name}, from path: ${path}`,
54-
)
55-
}
56-
57-
modDefine(mod, rename || name, def)
5823
}

src/lang/run/handleImport.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { modDefine, modFind, modResolve } from "../mod/index.ts"
2+
import type { Mod } from "../mod/Mod.ts"
3+
import type { ImportEntry, Stmt } from "../stmt/Stmt.ts"
4+
import { globalLoadedMods } from "./globalLoadedMods.ts"
5+
import { run } from "./run.ts"
6+
7+
export function handleImport(mod: Mod, stmt: Stmt): void {
8+
if (stmt.kind === "Import") {
9+
for (const entry of stmt.entries) {
10+
importOne(mod, stmt.path, entry)
11+
}
12+
13+
return
14+
}
15+
}
16+
17+
function importOne(mod: Mod, path: string, entry: ImportEntry): void {
18+
const url = modResolve(mod, path)
19+
if (url.href === mod.url.href) {
20+
throw new Error(`I can not circular import: ${path}`)
21+
}
22+
23+
const found = globalLoadedMods.get(url.href)
24+
if (found === undefined) {
25+
throw new Error(`Mod is not loaded: ${path}`)
26+
}
27+
28+
run(found.mod)
29+
30+
const { name, rename } = entry
31+
32+
const def = modFind(found.mod, name)
33+
if (def === undefined) {
34+
throw new Error(
35+
`I can not import undefined name: ${name}, from path: ${path}`,
36+
)
37+
}
38+
39+
modDefine(mod, rename || name, def)
40+
}

src/lang/run/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { formatExp } from "../format/formatExp.ts"
33
import { modFind, modOwnDefs, type Def, type Mod } from "../mod/index.ts"
44
import { handleDefine } from "./handleDefine.ts"
55
import { handleEffect } from "./handleEffect.ts"
6+
import { handleImport } from "./handleImport.ts"
67

78
export function run(mod: Mod): void {
89
if (mod.isFinished) return
910

11+
for (const stmt of mod.stmts) handleImport(mod, stmt)
1012
for (const stmt of mod.stmts) handleDefine(mod, stmt)
1113

1214
for (const def of modOwnDefs(mod).values()) assertAllNamesDefined(mod, def)

0 commit comments

Comments
 (0)