11import { ParsingError } from "@xieyuheng/x-data.js"
22import 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"
412import { parseStmts } from "../parse/index.ts"
513import { 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
818export 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+ }
0 commit comments