diff --git a/TODO.md b/TODO.md index 58429e4f..d8d857c7 100644 --- a/TODO.md +++ b/TODO.md @@ -1,11 +1,3 @@ -# present - -`presentWordAsEnv` - -`formatEnv` - -`presentWordAsEnv` -- improve snapshot test - # check `checkAllLocalVariableAreUsed` diff --git a/src/lang/builtins/inspect.ts b/src/lang/builtins/inspect.ts index b627c0fb..18e27a72 100644 --- a/src/lang/builtins/inspect.ts +++ b/src/lang/builtins/inspect.ts @@ -12,12 +12,12 @@ export function compose(env: Env): void { if (value["@kind"] === "Port") { const connectedcomponent = findConnectedComponent(env.net, value.node) - const netString = formatNet(connectedcomponent) - if (netString.length === 0) { + const netText = formatNet(connectedcomponent) + if (netText.length === 0) { env.mod.loader.onOutput(`net_from_port ${formatValue(value)} end`) } else { env.mod.loader.onOutput(`net_from_port ${formatValue(value)}`) - env.mod.loader.onOutput(indent(netString)) + env.mod.loader.onOutput(indent(netText)) env.mod.loader.onOutput("end") } } else { diff --git a/src/lang/env/formatEnv.ts b/src/lang/env/formatEnv.ts new file mode 100644 index 00000000..60810b39 --- /dev/null +++ b/src/lang/env/formatEnv.ts @@ -0,0 +1,39 @@ +import { indent } from "../../utils/indent" +import { formatNet } from "../net/formatNet" +import { netIsEmpty } from "../net/netIsEmpty" +import { formatValue } from "../value" +import { Env } from "./Env" + +export function formatEnv(env: Env): string { + const netText = netIsEmpty(env.net) + ? "net end" + : [`net`, indent(formatNet(env.net)), `end`].join("\n") + + const stackText = + env.stack.length === 0 + ? "stack end" + : [`stack`, indent(env.stack.map(formatValue).join(" ")), `end`].join( + "\n", + ) + + const localsText = + env.locals.size === 0 + ? "locals end" + : [ + `locals`, + indent( + Array.from(env.locals.entries()) + .map(([name, value]) => `${formatValue(value)} $${name}`) + .join("\n"), + ), + `end`, + ].join("\n") + + return [ + `env`, + indent(netText), + indent(stackText), + indent(localsText), + `end`, + ].join("\n") +} diff --git a/src/lang/env/index.ts b/src/lang/env/index.ts index 466305ec..38aff7d8 100644 --- a/src/lang/env/index.ts +++ b/src/lang/env/index.ts @@ -1 +1,3 @@ export * from "./Env" +export * from "./createEnv" +export * from "./formatEnv" diff --git a/src/lang/net/netIsEmpty.ts b/src/lang/net/netIsEmpty.ts new file mode 100644 index 00000000..6cc5cf09 --- /dev/null +++ b/src/lang/net/netIsEmpty.ts @@ -0,0 +1,5 @@ +import { Net } from "./Net" + +export function netIsEmpty(net: Net): boolean { + return net.activeEdges.length === 0 && net.nodeEntries.size === 0 +} diff --git a/src/lang/present/presentWordAsEnv.test.ts b/src/lang/present/presentWordAsEnv.test.ts index e2ddfec9..ba413b0f 100644 --- a/src/lang/present/presentWordAsEnv.test.ts +++ b/src/lang/present/presentWordAsEnv.test.ts @@ -1,7 +1,7 @@ import { expect, test } from "vitest" import { Fetcher } from "../../fetcher" import { Loader } from "../../loader" -import { formatNet } from "../net/formatNet" +import { formatEnv } from "../env/formatEnv" import { presentWordAsEnv } from "./presentWordAsEnv" test("presentWordAsEnv", async () => { @@ -55,17 +55,33 @@ define addadd add add end const url = new URL("test://presentNodeAsNet") const mod = await loader.load(url, { text }) - expect(formatNet(presentWordAsEnv(mod, "two").net)).toMatchInlineSnapshot(` - "(zero₄)-value prev-(add1₅) - (add1₅)-value addend-(add₆) - (zero₅)-value prev-(add1₆) - (add1₆)-value!target-(add₆)" + expect(formatEnv(presentWordAsEnv(mod, "two"))).toMatchInlineSnapshot(` + "env + net + (zero₄)-value prev-(add1₅) + (add1₅)-value addend-(add₆) + (zero₅)-value prev-(add1₆) + (add1₆)-value!target-(add₆) + end + stack + (add₆)-return + end + locals end + end" `) - expect(formatNet(presentWordAsEnv(mod, "addadd").net)).toMatchInlineSnapshot(` - "(@type_cap₃)-covering addend-(add₈) - (@type_cap₄)-covering addend-(add₇) - (@type_cap₅)-covering!target-(add₇) - (add₇)-return target-(add₈)" + expect(formatEnv(presentWordAsEnv(mod, "addadd"))).toMatchInlineSnapshot(` + "env + net + (@type_cap₃)-covering addend-(add₈) + (@type_cap₄)-covering addend-(add₇) + (@type_cap₅)-covering!target-(add₇) + (add₇)-return target-(add₈) + end + stack + (add₈)-return + end + locals end + end" `) })