Skip to content

Commit

Permalink
presentWordAsEnv -- improve snapshot test -- use formatEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 29, 2023
1 parent 32420c1 commit 992d121
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
8 changes: 0 additions & 8 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# present

`presentWordAsEnv`

`formatEnv`

`presentWordAsEnv` -- improve snapshot test

# check

`checkAllLocalVariableAreUsed`
Expand Down
6 changes: 3 additions & 3 deletions src/lang/builtins/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions src/lang/env/formatEnv.ts
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 2 additions & 0 deletions src/lang/env/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./Env"
export * from "./createEnv"
export * from "./formatEnv"
5 changes: 5 additions & 0 deletions src/lang/net/netIsEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Net } from "./Net"

export function netIsEmpty(net: Net): boolean {
return net.activeEdges.length === 0 && net.nodeEntries.size === 0
}
38 changes: 27 additions & 11 deletions src/lang/present/presentWordAsEnv.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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"
`)
})

0 comments on commit 992d121

Please sign in to comment.