diff --git a/TODO.md b/TODO.md index 7240b1a2..bdf65cb6 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,14 @@ -`interact` -- be sure about memeory leak of `connection` +drop the `net` prefix for `graph/` functions + +use unicode for subscript number + +rename PortConnect to PortReconnect + +`connect` v.s. `reconnect` + +- `reconnect` is used in `PortReconnect` + +`interact` -- be sure about memeory leak from `port.connection` # bug diff --git a/src/lang/graph/findPortInActiveEdge.ts b/src/lang/graph/findPortInActiveEdge.ts new file mode 100644 index 00000000..71f28daa --- /dev/null +++ b/src/lang/graph/findPortInActiveEdge.ts @@ -0,0 +1,29 @@ +import { ActiveEdge, Node, Port } from "../graph" + +export function findPortInActiveEdge( + nodeName: string, + portName: string, + activeEdge: ActiveEdge, +): Port | undefined { + if (nodeName === activeEdge.start.node.name) { + return findPortInNode(portName, activeEdge.start.node) + } + + if (nodeName === activeEdge.end.node.name) { + return findPortInNode(portName, activeEdge.end.node) + } +} + +function findPortInNode(portName: string, node: Node): Port | undefined { + for (const port of node.input) { + if (port.name === portName) { + return port.connection?.port + } + } + + for (const port of node.output) { + if (port.name === portName) { + return port.connection?.port + } + } +} diff --git a/src/lang/words/PortConnect.ts b/src/lang/words/PortConnect.ts index 2f301b8f..3a60e885 100644 --- a/src/lang/words/PortConnect.ts +++ b/src/lang/words/PortConnect.ts @@ -1,9 +1,9 @@ import { Net } from "../graph" +import { findPortInActiveEdge } from "../graph/findPortInActiveEdge" import { netConnectPorts } from "../graph/netConnectPorts" import { Mod } from "../mod" import { Span } from "../span" import { Word, WordOptions } from "../word" -import { findPortInActiveEdge } from "./PortPush" export class PortConnect implements Word { constructor( diff --git a/src/lang/words/PortPush.ts b/src/lang/words/PortPush.ts index 2756d4d9..1a0e9830 100644 --- a/src/lang/words/PortPush.ts +++ b/src/lang/words/PortPush.ts @@ -1,4 +1,5 @@ -import { ActiveEdge, Net, Node, Port } from "../graph" +import { Net } from "../graph" +import { findPortInActiveEdge } from "../graph/findPortInActiveEdge" import { Mod } from "../mod" import { Span } from "../span" import { Word, WordOptions } from "../word" @@ -28,31 +29,3 @@ export class PortPush implements Word { net.portStack.push(found) } } - -export function findPortInActiveEdge( - nodeName: string, - portName: string, - activeEdge: ActiveEdge, -): Port | undefined { - if (nodeName === activeEdge.start.node.name) { - return findPortInNode(portName, activeEdge.start.node) - } - - if (nodeName === activeEdge.end.node.name) { - return findPortInNode(portName, activeEdge.end.node) - } -} - -function findPortInNode(portName: string, node: Node): Port | undefined { - for (const port of node.input) { - if (port.name === portName) { - return port.connection?.port - } - } - - for (const port of node.output) { - if (port.name === portName) { - return port.connection?.port - } - } -}