Skip to content

Commit

Permalink
extract importAll from Require.execute
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 29, 2023
1 parent 54786d2 commit 3d24c86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extract `importBindings` from `Require.import`

`Mod` should import all rules of a node -- no indirect rule lookup

test -- reuqire new rules
Expand Down
22 changes: 22 additions & 0 deletions src/lang/import/importAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Mod } from "../mod"

export function importAll(mod: Mod, targetMod: Mod): void {
for (const [name, definition] of targetMod.definitions) {
if (definition.isPrivate) {
continue
}

const found = mod.definitions.get(name)
if (found !== undefined) {
throw new Error(
[
`[Require.execute] I can not import already defined name.`,
``,
` name: ${name}`,
].join("\n"),
)
}

mod.definitions.set(name, definition)
}
}
19 changes: 2 additions & 17 deletions src/lang/stmts/Require.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { appendReport } from "../errors/appendReport"
import { importAll } from "../import/importAll"
import { Mod } from "../mod"
import { Span } from "../span"
import { Stmt } from "../stmt"
Expand Down Expand Up @@ -30,24 +31,8 @@ export class Require implements Stmt {
}

const loadedMod = await mod.loader.load(url)
for (const [name, definition] of loadedMod.definitions) {
if (definition.isPrivate) {
continue
}

const found = mod.definitions.get(name)
if (found !== undefined) {
throw new Error(
[
`[Require.execute] I can not import already defined name.`,
``,
` name: ${name}`,
].join("\n"),
)
}

mod.definitions.set(name, definition)
}
importAll(mod, loadedMod)

for (const [key, requiredMod] of loadedMod.requiredMods) {
mod.requiredMods.set(key, requiredMod)
Expand Down

0 comments on commit 3d24c86

Please sign in to comment.