-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lang0] add an extra pass --
defineMod
before executeMod
- Loading branch information
Showing
6 changed files
with
71 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as Exps from "../exp/index.js" | ||
import { formatExp } from "../format/formatExp.js" | ||
import { modDefine, modFind } from "../mod/index.js" | ||
import type { Mod } from "../mod/Mod.js" | ||
import type { Define, Stmt } from "../stmt/Stmt.js" | ||
import { importOne } from "./importOne.js" | ||
|
||
export function define(mod: Mod, stmt: Stmt): null { | ||
switch (stmt["@kind"]) { | ||
case "Define": { | ||
assertAllNamesDefined(mod, stmt) | ||
|
||
modDefine(mod, stmt.name, { | ||
mod, | ||
name: stmt.name, | ||
exp: stmt.exp, | ||
}) | ||
|
||
return null | ||
} | ||
|
||
case "Import": { | ||
for (const entry of stmt.entries) { | ||
importOne(mod, stmt.path, entry) | ||
} | ||
|
||
return null | ||
} | ||
|
||
default: { | ||
return null | ||
} | ||
} | ||
} | ||
|
||
function assertAllNamesDefined(mod: Mod, stmt: Define): void { | ||
const freeNames = Exps.freeNames(new Set([stmt.name]), stmt.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)}`, | ||
].join("\n"), | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { Mod } from "../mod/index.js" | ||
import { define } from "./define.js" | ||
|
||
export function defineMod(mod: Mod): boolean { | ||
if (mod.isDefined) { | ||
return false | ||
} | ||
|
||
for (const stmt of mod.stmts) { | ||
define(mod, stmt) | ||
} | ||
|
||
mod.isDefined = true | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { executeMod } from "./executeMod.js" | ||
import { defineMod } from "./defineMod.js" | ||
import { load } from "./load.js" | ||
|
||
export async function run(url: URL): Promise<void> { | ||
const mod = await load(url, new Map()) | ||
defineMod(mod) | ||
executeMod(mod) | ||
} |