Skip to content

Commit d15e536

Browse files
committed
inline run to load
1 parent d2dd1b5 commit d15e536

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[run] I find undefined name: g
1+
[load] I find undefined name: g
22
defining: f
33
body: (lambda (x) (g x))

src/commands/runCommand.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ export const runCommand: Command = {
1616
},
1717

1818
async run(commander) {
19-
const url = createURL(String(commander.args[0]))
20-
2119
try {
20+
if (typeof commander.args[0] !== "string") {
21+
throw new Error(
22+
`[run] I expect the first argument to be a path, instead of: ${commander.args[0]}`,
23+
)
24+
}
25+
26+
const url = createURL(commander.args[0])
2227
await load(url)
2328
} catch (error) {
2429
if (error instanceof Error) {

src/lang/load/load.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { ParsingError } from "@xieyuheng/x-data.js"
22
import fs from "node:fs"
3-
import { createMod, type Mod } from "../mod/index.ts"
3+
import { expFreeNames } from "../exp/expFreeNames.ts"
4+
import { formatExp } from "../format/formatExp.ts"
5+
import {
6+
createMod,
7+
modFind,
8+
modOwnDefs,
9+
type Def,
10+
type Mod,
11+
} from "../mod/index.ts"
412
import { parseStmts } from "../parse/index.ts"
513
import { globalLoadedMods } from "./globalLoadedMods.ts"
6-
import { run } from "./run.ts"
14+
import { handleDefine } from "./handleDefine.ts"
15+
import { handleEffect } from "./handleEffect.ts"
16+
import { handleImport } from "./handleImport.ts"
717

818
export async function load(url: URL): Promise<Mod> {
919
const found = globalLoadedMods.get(url.href)
@@ -25,3 +35,29 @@ export async function load(url: URL): Promise<Mod> {
2535
throw error
2636
}
2737
}
38+
39+
async function run(mod: Mod): Promise<void> {
40+
if (mod.isFinished) return
41+
42+
for (const stmt of mod.stmts) await handleDefine(mod, stmt)
43+
for (const stmt of mod.stmts) await handleImport(mod, stmt)
44+
for (const def of modOwnDefs(mod).values()) assertAllNamesDefined(mod, def)
45+
for (const stmt of mod.stmts) await handleEffect(mod, stmt)
46+
47+
mod.isFinished = true
48+
}
49+
50+
function assertAllNamesDefined(mod: Mod, def: Def): void {
51+
const freeNames = expFreeNames(new Set(), def.exp)
52+
for (const name of freeNames) {
53+
if (modFind(mod, name) === undefined) {
54+
throw new Error(
55+
[
56+
`[load] I find undefined name: ${name}`,
57+
` defining: ${def.name}`,
58+
` body: ${formatExp(def.exp)}`,
59+
].join("\n"),
60+
)
61+
}
62+
}
63+
}

src/lang/load/run.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)