Skip to content

Commit

Permalink
ComposeOptions should not have activeEdge -- should have `current…
Browse files Browse the repository at this point in the history
….start` and `current.end` node
  • Loading branch information
xieyuheng committed Aug 2, 2023
1 parent 29336ee commit 0684298
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# type

`ComposeOptions` should not have `activeEdge` -- should have `start` and `end` node

extract `compose`

- let `Word` be algebraic data type
Expand All @@ -25,7 +23,7 @@ extract `cut`

# example

Nat -- `mul` -- with `nat_dup` and `nat_drop`
Nat -- `mul` -- with `nat_dup` and `nat_erase`

Nat -- `max` -- with `max_aux`

Expand Down
18 changes: 8 additions & 10 deletions src/lang/graph/findPortInActiveEdge.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { ActiveEdge, Node, Port } from "../graph"
import { Node, Port } from "../graph"

export function findPortInActiveEdge(
export function findPortInNodes(
nodeName: string,
portName: string,
activeEdge: ActiveEdge,
nodes: Array<Node>,
): 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)
for (const node of nodes) {
if (nodeName === node.name) {
return findPortInNode(portName, node)
}
}
}

function findPortInNode(portName: string, node: Node): Port | undefined {
export function findPortInNode(portName: string, node: Node): Port | undefined {
for (const port of node.input) {
if (port.name === portName) {
return port.connection?.port
Expand Down
7 changes: 6 additions & 1 deletion src/lang/run/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export function interact(mod: Mod, net: Net, activeEdge: ActiveEdge): void {
removeNodeAndEdges(net, activeEdge.start.node)

for (const word of activeEdge.rule.words) {
word.compose(mod, net, { activeEdge })
word.compose(mod, net, {
current: {
start: activeEdge.start.node,
end: activeEdge.end.node,
},
})
}
}
4 changes: 2 additions & 2 deletions src/lang/word/Word.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Ctx } from "../ctx"
import { ActiveEdge } from "../graph"
import { Node } from "../graph"
import { Mod } from "../mod"
import { Net } from "../net"
import { Span } from "../span"

export interface ComposeOptions {
activeEdge?: ActiveEdge
current?: { start: Node; end: Node }
}

export interface CutOptions {
Expand Down
11 changes: 7 additions & 4 deletions src/lang/words/PortPush.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ctx } from "../ctx"
import { findPortInActiveEdge } from "../graph/findPortInActiveEdge"
import { findPortInNodes } from "../graph/findPortInActiveEdge"
import { Mod } from "../mod"
import { Net } from "../net"
import { disconnect } from "../net/disconnect"
Expand All @@ -14,13 +14,16 @@ export class PortPush implements Word {
) {}

compose(mod: Mod, net: Net, options?: ComposeOptions): void {
const { activeEdge } = options || {}
const { current } = options || {}

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

const found = findPortInActiveEdge(this.nodeName, this.portName, activeEdge)
const found = findPortInNodes(this.nodeName, this.portName, [
current.start,
current.end,
])

if (found === undefined) {
throw new Error(
Expand Down
11 changes: 7 additions & 4 deletions src/lang/words/PortReconnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ctx } from "../ctx"
import { findPortInActiveEdge } from "../graph/findPortInActiveEdge"
import { findPortInNodes } from "../graph/findPortInActiveEdge"
import { Mod } from "../mod"
import { Net } from "../net"
import { connect } from "../net/connect"
Expand All @@ -15,13 +15,16 @@ export class PortReconnect implements Word {
) {}

compose(mod: Mod, net: Net, options?: ComposeOptions): void {
const { activeEdge } = options || {}
const { current } = options || {}

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

const found = findPortInActiveEdge(this.nodeName, this.portName, activeEdge)
const found = findPortInNodes(this.nodeName, this.portName, [
current.start,
current.end,
])

if (found === undefined) {
throw new Error(
Expand Down

0 comments on commit 0684298

Please sign in to comment.