Skip to content

Commit

Permalink
extract netConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 28, 2023
1 parent fef30e3 commit a7aa159
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extract `netConnect`

extract `netCleanUpWires`
extract `netStep`
extract `netRun`
Expand Down
3 changes: 2 additions & 1 deletion src/lang/defs/NodeDef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Def } from "../def"
import { Net, Node, createNode } from "../graph"
import { netConnect } from "../graph/netConnect"
import { Mod } from "../mod"
import { Rule } from "../rule"
import { Type } from "../type"
Expand Down Expand Up @@ -42,7 +43,7 @@ export class NodeDef extends Def {
throw new Error(`I expect a port on top of the stack`)
}

net.connect(top, port)
netConnect(net, top, port)
}

net.portStack.push(...node.output)
Expand Down
3 changes: 2 additions & 1 deletion src/lang/graph/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { InternalError } from "../errors"
import { Net, Node, Port } from "../graph"
import { Mod } from "../mod"
import { Rule } from "../rule"
import { netConnect } from "./netConnect"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"

Expand Down Expand Up @@ -94,6 +95,6 @@ function reconnectOutput(net: Net, output: Array<Port>): void {
while (net.portStack.length > 0) {
const start = net.portStack.pop() as Port
const end = output.pop() as Port
net.connect(start, end)
netConnect(net, start, end)
}
}
15 changes: 3 additions & 12 deletions src/lang/graph/Net.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { InternalError } from "../errors"
import { Action, Edge, Node, Port, createEdge } from "../graph"
import { Action, Edge, Node, Port } from "../graph"
import { Mod } from "../mod"
import { netCloseFreePorts } from "./netCloseFreePorts"
import { netConnect } from "./netConnect"
import { netReleaseFreePorts } from "./netReleaseFreePorts"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"
Expand Down Expand Up @@ -36,7 +37,7 @@ export class Net {
netRemoveNode(this, wire.start.node)
netRemoveNode(this, wire.end.node)

this.connect(wire.start.connection.port, wire.end.connection.port)
netConnect(this, wire.start.connection.port, wire.end.connection.port)
}
}

Expand All @@ -52,14 +53,4 @@ export class Net {
if (action === undefined) return
else action.act(this.mod, this)
}

connect(start: Port, end: Port): void {
const rule = this.mod.getRuleByPorts(start, end)

if (rule) {
this.actions.push(new Action(start, end, rule))
} else {
this.edges.push(createEdge(start, end))
}
}
}
11 changes: 11 additions & 0 deletions src/lang/graph/netConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Action, Net, Port, createEdge } from "../graph"

export function netConnect(net: Net, start: Port, end: Port): void {
const rule = net.mod.getRuleByPorts(start, end)

if (rule) {
net.actions.push(new Action(start, end, rule))
} else {
net.edges.push(createEdge(start, end))
}
}
3 changes: 2 additions & 1 deletion src/lang/mod/builtInOperators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Mod } from "."
import * as Defs from "../defs"
import { Port } from "../graph"
import { netConnect } from "../graph/netConnect"
import { buildTypes } from "../types"

export function builtInOperators(mod: Mod): void {
Expand All @@ -20,7 +21,7 @@ export function builtInOperators(mod: Mod): void {
mod.defineOperator("connect", (net) => {
const start = net.portStack.pop() as Port
const end = net.portStack.pop() as Port
net.connect(start, end)
netConnect(net, start, end)
})

mod.defineOperator("wire", (net) => {
Expand Down

0 comments on commit a7aa159

Please sign in to comment.