-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |