Skip to content

Commit

Permalink
presentFunction should still return Env
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 23, 2023
1 parent e021bdf commit ee5225f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
`presentFunction` should still return `Env`

add playground links to readme
update articles
41 changes: 17 additions & 24 deletions src/lang/env/formatEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,33 @@ import { indent } from "../../utils/indent"
import { formatNet } from "../net/formatNet"
import { netIsEmpty } from "../net/netIsEmpty"
import { formatValue } from "../value"
import { formatValues } from "../value/formatValues"
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((value) => formatValue(env, value)).join(" ")),
`end`,
].join("\n")
? "net {}"
: `net {\n${indent(formatNet(env.net))}\n}`

const localsText =
env.locals.size === 0
? "locals end"
: [
`locals`,
indent(
Array.from(env.locals.entries())
.map(([name, value]) => `${formatValue(env, value)} $${name}`)
.join("\n"),
),
`end`,
].join("\n")
? "locals {}"
: `locals {\n${indent(
Array.from(env.locals.entries())
.map(([name, value]) => `${name}: ${formatValue(env, value)}.`)
.join("\n"),
)}\n}`

const stackText =
env.stack.length === 0
? "stack []"
: `stack [${formatValues(env, env.stack)}]`

return [
`env`,
`env {`,
indent(netText),
indent(stackText),
indent(localsText),
`end`,
indent(stackText),
`}`,
].join("\n")
}
5 changes: 1 addition & 4 deletions src/lang/half-edge/formatHalfEdge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import { HalfEdge } from "./HalfEdge"
export function formatHalfEdge(net: Net, halfEdge: HalfEdge): string {
const halfEdgeEntry = findHalfEdgeEntryOrFail(net, halfEdge)
const port = halfEdgeEntry.port
if (port !== undefined) {
return formatPort(net, port)
}
const portText = maybeFormatPort(net, port)

const otherHalfEdge = halfEdgeEntry.otherHalfEdge
const otherHalfEdgeEntry = findHalfEdgeEntryOrFail(net, otherHalfEdge)
const otherPort = otherHalfEdgeEntry.port
const otherPortText = maybeFormatPort(net, otherPort)
const portText = maybeFormatPort(net, port)

return `#HalfEdge(${portText}, ${otherPortText})`
}
Expand Down
34 changes: 23 additions & 11 deletions src/lang/present/presentFunction.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"
import { presentFunction } from "./presentFunction"

test("presentFunction", async () => {
Expand Down Expand Up @@ -32,17 +32,29 @@ function addadd(x: Nat, y: Nat, z: Nat): Nat {
const url = new URL("test://presentFunction")
const mod = await loader.load(url, { text })

expect(formatNet(presentFunction(mod, "two"))).toMatchInlineSnapshot(`
"(add₃)-target!value-(add1₃)
(add₃)-addend value-(add1₄)
(add1₃)-prev value-(zero₃)
(add1₄)-prev value-(zero₄)"
expect(formatEnv(presentFunction(mod, "two"))).toMatchInlineSnapshot(`
"env {
net {
(add₃)-target!value-(add1₃)
(add₃)-addend value-(add1₄)
(add1₃)-prev value-(zero₃)
(add1₄)-prev value-(zero₄)
}
locals {}
stack [#HalfEdge(#unconnected, result-(add₃))]
}"
`)

expect(formatNet(presentFunction(mod, "addadd"))).toMatchInlineSnapshot(`
"(@typeCap₃)-covering addend-(add₄)
(@typeCap₄)-covering addend-(add₅)
(@typeCap₅)-covering target-(add₅)
(add₄)-target result-(add₅)"
expect(formatEnv(presentFunction(mod, "addadd"))).toMatchInlineSnapshot(`
"env {
net {
(@typeCap₃)-covering addend-(add₄)
(@typeCap₄)-covering addend-(add₅)
(@typeCap₅)-covering target-(add₅)
(add₄)-target result-(add₅)
}
locals {}
stack [#HalfEdge(#unconnected, result-(add₄))]
}"
`)
})
10 changes: 6 additions & 4 deletions src/lang/present/presentFunction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { capType } from "../cap"
import { connectHalfEdgeWithPort } from "../connect/connectHalfEdgeWithPort"
import { Env } from "../env"
import { createEnv } from "../env/createEnv"
import { defineLocals } from "../env/defineLocals"
import { evaluateBlock } from "../evaluate/evaluateBlock"
import { Mod, findDefinitionOrFail } from "../mod"
import { Net } from "../net"
import { addEdge } from "../net/addEdge"

export function presentFunction(mod: Mod, name: string): Net {
export function presentFunction(mod: Mod, name: string): Env {
const definition = findDefinitionOrFail(mod, name)

if (definition["@kind"] !== "FunctionDefinition") {
Expand Down Expand Up @@ -36,7 +36,9 @@ export function presentFunction(mod: Mod, name: string): Net {
defineLocals(env, [parameter.name], [edge.second])
}

evaluateBlock(mod, env, body, {})
const values = evaluateBlock(mod, env, body, {})

return env.net
env.stack.push(...values)

return env
}

0 comments on commit ee5225f

Please sign in to comment.