From 525e25c6eac0af10c2ec98bf88afdc47a7417ca9 Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Sun, 30 Jul 2023 13:23:31 +0800 Subject: [PATCH] `PortPush` -- `apply` --- TODO.md | 6 +++-- src/lang/words/PortPush.ts | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/lang/words/PortPush.ts diff --git a/TODO.md b/TODO.md index 42c1e414..60310f0f 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/src/lang/words/PortPush.ts b/src/lang/words/PortPush.ts new file mode 100644 index 00000000..204e63d9 --- /dev/null +++ b/src/lang/words/PortPush.ts @@ -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 + } + } +}