Skip to content

Commit

Permalink
[lang0] modOwnDefinitions
Browse files Browse the repository at this point in the history
- `assertAllNamesDefined` should take `Definition`
  • Loading branch information
xieyuheng committed Apr 5, 2024
1 parent d06735d commit d63c68c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lang0/mod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./createMod.js"
export * from "./modDefine.js"
export * from "./modFind.js"
export * from "./modFindValue.js"
export * from "./modOwnDefinitions.js"
export * from "./modResolve.js"
13 changes: 13 additions & 0 deletions src/lang0/mod/modOwnDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Definition } from "../definition/Definition.js"
import type { Mod } from "./Mod.js"

export function modOwnDefinitions(mod: Mod): Map<string, Definition> {
const ownDefinitions = new Map()
for (const [name, definition] of mod.definitions) {
if (definition.mod.url.href === mod.url.href) {
ownDefinitions.set(name, definition)
}
}

return ownDefinitions
}
10 changes: 5 additions & 5 deletions src/lang0/run/assertAllNamesDefined.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Definition } from "../definition/Definition.js"
import { expFreeNames } from "../exp/expFreeNames.js"
import { formatExp } from "../format/formatExp.js"
import { modFind, type Mod } from "../mod/index.js"
import type { Define } from "../stmt/Stmt.js"

export function assertAllNamesDefined(mod: Mod, stmt: Define): void {
const freeNames = expFreeNames(new Set([stmt.name]), stmt.exp)
export function assertAllNamesDefined(mod: Mod, definition: Definition): void {
const freeNames = expFreeNames(new Set([definition.name]), definition.exp)

for (const name of freeNames) {
if (modFind(mod, name) === undefined) {
throw new Error(
[
`I find undefined name: ${name}`,
` defining: ${stmt.name}`,
` body: ${formatExp(stmt.exp)}`,
` defining: ${definition.name}`,
` body: ${formatExp(definition.exp)}`,
].join("\n"),
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/lang0/run/runMod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Mod } from "../mod/index.js"
import { modOwnDefinitions, type Mod } from "../mod/index.js"
import { assertAllNamesDefined } from "./assertAllNamesDefined.js"
import { define } from "./define.js"
import { execute } from "./execute.js"
Expand All @@ -8,8 +8,8 @@ export function runMod(mod: Mod): void {

for (const stmt of mod.stmts) define(mod, stmt)

for (const stmt of mod.stmts)
if (stmt["@kind"] === "Define") assertAllNamesDefined(mod, stmt)
for (const definition of modOwnDefinitions(mod).values())
assertAllNamesDefined(mod, definition)

for (const stmt of mod.stmts) {
const output = execute(mod, stmt)
Expand Down

0 comments on commit d63c68c

Please sign in to comment.