Skip to content

Commit

Permalink
drop the net prefix for graph/ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 30, 2023
1 parent 70683a4 commit 13644da
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 57 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
drop the `net` prefix for `graph/` functions

use unicode for subscript number

rename PortConnect to PortReconnect
Expand Down
4 changes: 2 additions & 2 deletions src/console/commands/RenderCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ty from "@xieyuheng/ty"
import fs from "fs"
import Path from "path"
import { Net } from "../../lang/graph"
import { netRun } from "../../lang/graph/netRun"
import { run } from "../../lang/graph/run"
import { Mod } from "../../lang/mod"
import { createMod } from "../../lang/mod/createMod"
import { modAllNetNames } from "../../lang/mod/modAllNetNames"
Expand Down Expand Up @@ -80,7 +80,7 @@ export class RenderCommand extends Command<Args, Opts> {
async function renderNet(mod: Mod, file: string, name: string): Promise<void> {
const net = modBuildNet(mod, name)
renderFile(net, `${file}.${name}.initial.txt`)
netRun(net)
run(net)
renderFile(net, `${file}.${name}.finial.txt`)
}

Expand Down
4 changes: 2 additions & 2 deletions src/lang/definitions/NodeDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Definition } from "../definition"
import { Net, Node, createNode } from "../graph"
import { netConnectPorts } from "../graph/netConnectPorts"
import { connectPorts } from "../graph/connectPorts"
import { Mod } from "../mod"
import { PortExp } from "../stmts"

Expand All @@ -22,7 +22,7 @@ export class NodeDefinition implements Definition {
throw new Error(`I expect a port on top of the stack`)
}

netConnectPorts(net, top, port)
connectPorts(net, top, port)
}

net.portStack.push(...node.output)
Expand Down
20 changes: 20 additions & 0 deletions src/lang/graph/cleanUpWires.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Net } from "./Net"
import { connectPorts } from "./connectPorts"
import { removeEdge } from "./removeEdge"
import { removeNode } from "./removeNode"

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

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

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

net.wires = []
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Node } from "."
import * as Definitions from "../definitions"
import { Node } from "../graph"
import { Net } from "./Net"

export function netCloseFreePorts(net: Net): Node | undefined {
export function closeFreePorts(net: Net): Node | undefined {
if (net.portStack.length === 0) return undefined

const name = "*free-ports-closer*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Net, Port, createEdge } from "."
import { lookupRuleByPorts } from "../mod/lookupRuleByPorts"
import { createActiveEdge } from "./createActiveEdge"

export function netConnectPorts(net: Net, start: Port, end: Port): void {
export function connectPorts(net: Net, start: Port, end: Port): void {
const rule = lookupRuleByPorts(net.mod, start, end)

if (rule !== undefined) {
Expand Down
10 changes: 5 additions & 5 deletions src/lang/graph/interact.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActiveEdge, Net, Node } from "../graph"
import { Mod } from "../mod"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"
import { removeEdge } from "./removeEdge"
import { removeNode } from "./removeNode"

export function interact(mod: Mod, net: Net, activeEdge: ActiveEdge): void {
disconnectNode(net, activeEdge.end.node)
Expand All @@ -14,12 +14,12 @@ export function interact(mod: Mod, net: Net, activeEdge: ActiveEdge): void {

function disconnectNode(net: Net, node: Node): void {
for (const port of node.input) {
port.connection && netRemoveEdge(net, port.connection.edge)
port.connection && removeEdge(net, port.connection.edge)
}

for (const port of node.output) {
port.connection && netRemoveEdge(net, port.connection.edge)
port.connection && removeEdge(net, port.connection.edge)
}

netRemoveNode(net, node)
removeNode(net, node)
}
20 changes: 0 additions & 20 deletions src/lang/graph/netCleanUpWires.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Node } from "."
import { InternalError } from "../errors"
import { Node } from "../graph"
import { Net } from "./Net"
import { netRemoveEdge } from "./netRemoveEdge"
import { netRemoveNode } from "./netRemoveNode"
import { removeEdge } from "./removeEdge"
import { removeNode } from "./removeNode"

export function netReleaseFreePorts(net: Net, closer: Node | undefined): void {
export function releaseFreePorts(net: Net, closer: Node | undefined): void {
if (closer === undefined) return

for (const port of closer.input.reverse()) {
Expand All @@ -15,8 +15,8 @@ export function netReleaseFreePorts(net: Net, closer: Node | undefined): void {
}

net.portStack.push(port.connection.port)
netRemoveEdge(net, port.connection.edge)
removeEdge(net, port.connection.edge)
}

netRemoveNode(net, closer)
removeNode(net, closer)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Edge, Net } from "../graph"
import { Edge, Net } from "."

export function netRemoveEdge(net: Net, edge: Edge): void {
export function removeEdge(net: Net, edge: Edge): void {
const index = net.edges.indexOf(edge)
if (index !== -1) {
net.edges.splice(index, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Net, Node } from "../graph"
import { Net, Node } from "."

export function netRemoveNode(net: Net, node: Node): void {
export function removeNode(net: Net, node: Node): void {
const index = net.nodes.indexOf(node)
if (index !== -1) {
net.nodes.splice(index, 1)
Expand Down
14 changes: 7 additions & 7 deletions src/lang/graph/netRun.ts → src/lang/graph/run.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { InternalError } from "../errors"
import { Net } from "./Net"
import { cleanUpWires } from "./cleanUpWires"
import { closeFreePorts } from "./closeFreePorts"
import { interact } from "./interact"
import { netCleanUpWires } from "./netCleanUpWires"
import { netCloseFreePorts } from "./netCloseFreePorts"
import { netReleaseFreePorts } from "./netReleaseFreePorts"
import { releaseFreePorts } from "./releaseFreePorts"

export function netRun(net: Net): void {
const closer = netCloseFreePorts(net)
export function run(net: Net): void {
const closer = closeFreePorts(net)

while (net.activeEdges.length > 0) {
netStep(net)
}

netCleanUpWires(net)
netReleaseFreePorts(net, closer)
cleanUpWires(net)
releaseFreePorts(net, closer)
}

function netStep(net: Net): void {
Expand Down
4 changes: 2 additions & 2 deletions src/lang/mod/defineBuiltInOperators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Port, createNode } from "../graph"
import { netConnectPorts } from "../graph/netConnectPorts"
import { connectPorts } from "../graph/connectPorts"
import { Mod } from "../mod"
import { defineOperator } from "./defineOperator"

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

defineOperator(mod, "wire", (net) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lang/mod/modBuildNet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Net } from "../graph"
import { cleanUpWires } from "../graph/cleanUpWires"
import { createNet } from "../graph/createNet"
import { netCleanUpWires } from "../graph/netCleanUpWires"
import { Mod } from "./Mod"
import { lookupNetDefinitionOrFail } from "./lookupNetDefinitionOrFail"

export function modBuildNet(mod: Mod, name: string): Net {
const net = createNet(mod)
lookupNetDefinitionOrFail(mod, name).meaning(net)
netCleanUpWires(net)
cleanUpWires(net)
return net
}
4 changes: 2 additions & 2 deletions src/lang/words/PortConnect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Net } from "../graph"
import { connectPorts } from "../graph/connectPorts"
import { findPortInActiveEdge } from "../graph/findPortInActiveEdge"
import { netConnectPorts } from "../graph/netConnectPorts"
import { Mod } from "../mod"
import { Span } from "../span"
import { Word, WordOptions } from "../word"
Expand Down Expand Up @@ -33,6 +33,6 @@ export class PortConnect implements Word {
throw new Error(`[PortConnect.apply] expect top port`)
}

netConnectPorts(net, topPort, found)
connectPorts(net, topPort, found)
}
}

0 comments on commit 13644da

Please sign in to comment.