Skip to content

Commit

Permalink
extract netCleanUpWires
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 28, 2023
1 parent a7aa159 commit e5bd9de
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extract `netCleanUpWires`
extract `netStep`
extract `netRun`

Expand Down
22 changes: 2 additions & 20 deletions src/lang/graph/Net.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { InternalError } from "../errors"
import { Action, Edge, Node, Port } from "../graph"
import { Mod } from "../mod"
import { netCleanUpWires } from "./netCleanUpWires"
import { netCloseFreePorts } from "./netCloseFreePorts"
import { netConnect } from "./netConnect"
import { netReleaseFreePorts } from "./netReleaseFreePorts"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"

export class Net {
mod: Mod
Expand All @@ -24,26 +22,10 @@ export class Net {
run(): void {
const closer = netCloseFreePorts(this)
while (this.actions.length > 0) this.step()
this.cleanUpWires()
netCleanUpWires(this)
netReleaseFreePorts(this, closer)
}

cleanUpWires(): void {
for (const wire of this.wires) {
if (wire.start.connection && wire.end.connection) {
netRemoveEdge(this, wire.start.connection.edge)
netRemoveEdge(this, wire.end.connection.edge)

netRemoveNode(this, wire.start.node)
netRemoveNode(this, wire.end.node)

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

this.wires = []
}

private step(): void {
if (this.portStack.length !== 0) {
throw new InternalError("I can not handle free port during stepping.")
Expand Down
20 changes: 20 additions & 0 deletions src/lang/graph/netCleanUpWires.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Net } from "./Net"
import { netConnect } from "./netConnect"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"

export function netCleanUpWires(net: Net): void {
for (const wire of net.wires) {
if (wire.start.connection && wire.end.connection) {
netRemoveEdge(net, wire.start.connection.edge)
netRemoveEdge(net, wire.end.connection.edge)

netRemoveNode(net, wire.start.node)
netRemoveNode(net, wire.end.node)

netConnect(net, wire.start.connection.port, wire.end.connection.port)
}
}

net.wires = []
}
3 changes: 2 additions & 1 deletion src/lang/mod/Mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Def } from "../def"
import * as Defs from "../defs"
import { Net, Port } from "../graph"
import { netCleanUpWires } from "../graph/netCleanUpWires"
import { Rule } from "../rule"
import { builtInOperators } from "./builtInOperators"

Expand Down Expand Up @@ -64,7 +65,7 @@ export class Mod {
buildNet(name: string): Net {
const net = new Net(this)
this.getNetDefOrFail(name).refer(net)
net.cleanUpWires()
netCleanUpWires(net)
return net
}

Expand Down

0 comments on commit e5bd9de

Please sign in to comment.