Skip to content

Commit

Permalink
PortPush -- apply
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 30, 2023
1 parent 66b7395 commit 525e25c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
6 changes: 4 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# named port

`PortPush` -- new word
`PortConnect` -- `apply`

`PortConnect` -- new word
`PortPush` -- syntax

`PortConnect` -- syntax

no `defru` just `defrule` -- add feature when very very needed

Expand Down
54 changes: 54 additions & 0 deletions src/lang/words/PortPush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ActiveEdge, Net, Node, Port } from "../graph"
import { Mod } from "../mod"
import { Span } from "../span"
import { Word, WordOptions } from "../word"

export class PortPush implements Word {
constructor(
public nodeName: string,
public portName: string,
public span: Span,
) {}

apply(mod: Mod, net: Net, options: WordOptions): void {
const { activeEdge } = options

if (activeEdge === undefined) {
throw new Error(`[PortPush.apply] expect current activeEdge`)
}

const found = findPortInActiveEdge(this.nodeName, this.portName, activeEdge)

if (found !== undefined) {
net.portStack.push(found)
}
}
}

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
}
}

for (const port of node.output) {
if (port.name === portName) {
return port
}
}
}

0 comments on commit 525e25c

Please sign in to comment.