Skip to content

Commit

Permalink
closeFreePorts should not use stack but traverse the PortEntry
Browse files Browse the repository at this point in the history
…of the net
  • Loading branch information
xieyuheng committed Aug 17, 2023
1 parent 4669eb0 commit df280bf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 80 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# connected component

`closeFreePorts` should not use `stack` but traverse the ports

`run` take `mod` and `net` -- instead of `env`
`run` take `mod` and `net` and `port` and return `port`

[builtin] `run` -- only run the top port

Expand Down
14 changes: 12 additions & 2 deletions src/lang/connect/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,34 @@ import { Port } from "../port"
import { formatValue } from "../value/formatValue"

export function connect(net: Net, first: Port, second: Port): void {
if (findPortEntry(net, first)?.connection !== undefined) {
const firstPortEntry = findPortEntry(net, first)

if (firstPortEntry?.connection !== undefined) {
throw new Error(
[
`[connect] The first port is already connected.`,
``,
` first port: ${formatValue(first)}`,
` first connected port: ${formatValue(
firstPortEntry.connection.port,
)}`,
` second port: ${formatValue(second)}`,
].join("\n"),
)
}

if (findPortEntry(net, second)?.connection !== undefined) {
const secondPortEntry = findPortEntry(net, second)

if (secondPortEntry?.connection !== undefined) {
throw new Error(
[
`[connect] The second port is already connected.`,
``,
` first port: ${formatValue(first)}`,
` second port: ${formatValue(second)}`,
` second connected port: ${formatValue(
secondPortEntry.connection.port,
)}`,
].join("\n"),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lang/net/findNodeEntryOrFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function findNodeEntryOrFail(net: Net, node: Node): NodeEntry {
if (nodeEntry === undefined) {
throw new Error(
[
`[findNodeEntryOrFail] I can not find nodePorts for node.`,
`[findNodeEntryOrFail] I can not find nodeEntry for node.`,
``,
` node: ${formatNode(node)}`,
].join("\n"),
Expand Down
68 changes: 25 additions & 43 deletions src/lang/run/closeFreePorts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { composeNode } from "../compose/composeNode"
import { Env } from "../env"
import { addNode } from "../net/addNode"
import { Node } from "../node"
import { createNodeFromNodeEntry } from "../net/createNodeFromNodeEntry"
import { createPortFromPortEntry } from "../net/createPortFromPortEntry"
import { connectPlaceholderInputPort } from "../placeholder/connectPlaceholderInputPort"
import { connectPlaceholderOutputPort } from "../placeholder/connectPlaceholderOutputPort"
import { Port } from "../port"
import { PortExp } from "../port/PortExp"

/*
Expand All @@ -12,45 +12,27 @@ import { PortExp } from "../port/PortExp"
*/

export function closeFreePorts(env: Env): Node | undefined {
if (env.stack.length === 0) {
return undefined
export function closeFreePorts(env: Env): Array<Port> {
const placeholderPorts: Array<Port> = []
for (const nodeEntry of env.net.nodeEntries.values()) {
const node = createNodeFromNodeEntry(nodeEntry)

for (const portEntry of Object.values(nodeEntry.ports)) {
if (portEntry.connection === undefined) {
const port = createPortFromPortEntry(node, portEntry)

if (portEntry.sign === 1) {
placeholderPorts.push(
connectPlaceholderInputPort(env.mod, env.net, port),
)
} else {
placeholderPorts.push(
connectPlaceholderOutputPort(env.mod, env.net, port),
)
}
}
}
}

// const placeholderPorts: Array<Port> = []
// for (const nodeEntry of env.net.nodeEntries.values()) {
// for (const portEntry of Object.values(nodeEntry.ports)) {
// if (portEntry.connection === undefined) {
// switch (portEntry.sign) {
// case 1: {
// placeholderPorts.push(
// connectPlaceholderInputPort(
// env.mod,
// env.net,
// createPortFromPortEntry(portEntry),
// ),
// )
// }
// case -1: {
// placeholderPorts.push(
// connectPlaceholderOutputPort(env.mod, env.net, port),
// )
// }
// }
// }
// }
// }

const ports = env.stack
.filter((value): value is Port => value["@kind"] === "Port")
.map<PortExp>((port) => ({
"@type": "PortExp",
name: `_temporary_closing_port_for_${port.name}_of_${port.node.name}`,
t: port.t,
isPrincipal: false,
}))

const node = addNode(env.net, env.mod, "_temporary_closing_node", ports, [])

return composeNode(env, node, {})
return placeholderPorts
}
26 changes: 0 additions & 26 deletions src/lang/run/releaseFreePorts.ts

This file was deleted.

50 changes: 47 additions & 3 deletions src/lang/run/run.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
import { Env } from "../env"
import { interact } from "../interact"
import { deleteNodeEntry } from "../net/deleteNodeEntry"
import { disconnectPort } from "../net/disconnectPort"
import { findPortEntry } from "../net/findPortEntry"
import { connectPlaceholderInputPort } from "../placeholder/connectPlaceholderInputPort"
import { connectPlaceholderOutputPort } from "../placeholder/connectPlaceholderOutputPort"
import { formatValue } from "../value/formatValue"
import { closeFreePorts } from "./closeFreePorts"
import { releaseFreePorts } from "./releaseFreePorts"

export function run(env: Env): void {
const closer = closeFreePorts(env)
const port = env.stack.pop()
if (port === undefined) {
throw new Error(`[run] I expect a top value on the stack.`)
}

if (port["@kind"] !== "Port") {
throw new Error(
[
`[run] I expect the top value on the stack to be a Port.`,
``,
` value: ${formatValue(port)}`,
].join("\n"),
)
}

const placeholderPort =
port.sign === 1
? connectPlaceholderInputPort(env.mod, env.net, port)
: connectPlaceholderOutputPort(env.mod, env.net, port)

const placeholderPorts = closeFreePorts(env)

while (env.net.activeEdges.length > 0) {
step(env)
}

releaseFreePorts(env, closer)
const placeholderPortEntry = findPortEntry(env.net, placeholderPort)
if (placeholderPortEntry?.connection === undefined) {
throw new Error(
[
`[run] I expect the placeholderPort to be connected.`,
``,
` placeholderPort: ${formatValue(placeholderPort)}`,
].join("\n"),
)
}

for (const placeholderPort of placeholderPorts) {
disconnectPort(env.net, placeholderPort)
deleteNodeEntry(env.net, placeholderPort.node)
}

env.stack.push(placeholderPortEntry.connection.port)

disconnectPort(env.net, placeholderPort)
deleteNodeEntry(env.net, placeholderPort.node)
}

function step(env: Env): void {
Expand Down
4 changes: 2 additions & 2 deletions tests/datatype/Nat.test.i.out
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ net_from_port (nat_dup)-first
(add1₄₃)-prev value-(zero₃₅)
end
net_from_port (add1)-value!
(add1₄)-prev value-(add1₄)
(add1₄)-prev value-(zero₃)
(add1₄)-prev value-(add1₄)
(add1₄)-prev value-(zero₃)
end
net_from_port (mul)-return
(mul₃)-target return-(add₃₄)
Expand Down

0 comments on commit df280bf

Please sign in to comment.