diff --git a/TODO.md b/TODO.md index bdf65cb6..8e22d09c 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,3 @@ -drop the `net` prefix for `graph/` functions - use unicode for subscript number rename PortConnect to PortReconnect diff --git a/src/console/commands/RenderCommand.ts b/src/console/commands/RenderCommand.ts index 455ebf26..2772a211 100644 --- a/src/console/commands/RenderCommand.ts +++ b/src/console/commands/RenderCommand.ts @@ -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" @@ -80,7 +80,7 @@ export class RenderCommand extends Command { async function renderNet(mod: Mod, file: string, name: string): Promise { const net = modBuildNet(mod, name) renderFile(net, `${file}.${name}.initial.txt`) - netRun(net) + run(net) renderFile(net, `${file}.${name}.finial.txt`) } diff --git a/src/lang/definitions/NodeDefinition.ts b/src/lang/definitions/NodeDefinition.ts index 81b0c713..39eb27b4 100644 --- a/src/lang/definitions/NodeDefinition.ts +++ b/src/lang/definitions/NodeDefinition.ts @@ -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" @@ -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) diff --git a/src/lang/graph/cleanUpWires.ts b/src/lang/graph/cleanUpWires.ts new file mode 100644 index 00000000..105b9621 --- /dev/null +++ b/src/lang/graph/cleanUpWires.ts @@ -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 = [] +} diff --git a/src/lang/graph/netCloseFreePorts.ts b/src/lang/graph/closeFreePorts.ts similarity index 78% rename from src/lang/graph/netCloseFreePorts.ts rename to src/lang/graph/closeFreePorts.ts index 1f003202..45ce72a5 100644 --- a/src/lang/graph/netCloseFreePorts.ts +++ b/src/lang/graph/closeFreePorts.ts @@ -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*" diff --git a/src/lang/graph/netConnectPorts.ts b/src/lang/graph/connectPorts.ts similarity index 83% rename from src/lang/graph/netConnectPorts.ts rename to src/lang/graph/connectPorts.ts index 9dadc925..280771dd 100644 --- a/src/lang/graph/netConnectPorts.ts +++ b/src/lang/graph/connectPorts.ts @@ -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) { diff --git a/src/lang/graph/interact.ts b/src/lang/graph/interact.ts index 049ed252..8886c056 100644 --- a/src/lang/graph/interact.ts +++ b/src/lang/graph/interact.ts @@ -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) @@ -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) } diff --git a/src/lang/graph/netCleanUpWires.ts b/src/lang/graph/netCleanUpWires.ts deleted file mode 100644 index d1772ae8..00000000 --- a/src/lang/graph/netCleanUpWires.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Net } from "./Net" -import { netConnectPorts } from "./netConnectPorts" -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) - - netConnectPorts(net, wire.start.connection.port, wire.end.connection.port) - } - } - - net.wires = [] -} diff --git a/src/lang/graph/netReleaseFreePorts.ts b/src/lang/graph/releaseFreePorts.ts similarity index 55% rename from src/lang/graph/netReleaseFreePorts.ts rename to src/lang/graph/releaseFreePorts.ts index 62459106..a89b9b33 100644 --- a/src/lang/graph/netReleaseFreePorts.ts +++ b/src/lang/graph/releaseFreePorts.ts @@ -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()) { @@ -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) } diff --git a/src/lang/graph/netRemoveEdge.ts b/src/lang/graph/removeEdge.ts similarity index 50% rename from src/lang/graph/netRemoveEdge.ts rename to src/lang/graph/removeEdge.ts index 1bfb752a..6f4913d8 100644 --- a/src/lang/graph/netRemoveEdge.ts +++ b/src/lang/graph/removeEdge.ts @@ -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) diff --git a/src/lang/graph/netRemoveNode.ts b/src/lang/graph/removeNode.ts similarity index 50% rename from src/lang/graph/netRemoveNode.ts rename to src/lang/graph/removeNode.ts index 9064b3fe..c3079e16 100644 --- a/src/lang/graph/netRemoveNode.ts +++ b/src/lang/graph/removeNode.ts @@ -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) diff --git a/src/lang/graph/netRun.ts b/src/lang/graph/run.ts similarity index 60% rename from src/lang/graph/netRun.ts rename to src/lang/graph/run.ts index 57e99eac..01c7c2af 100644 --- a/src/lang/graph/netRun.ts +++ b/src/lang/graph/run.ts @@ -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 { diff --git a/src/lang/mod/defineBuiltInOperators.ts b/src/lang/mod/defineBuiltInOperators.ts index 3d9698e3..7ae4350b 100644 --- a/src/lang/mod/defineBuiltInOperators.ts +++ b/src/lang/mod/defineBuiltInOperators.ts @@ -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" @@ -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) => { diff --git a/src/lang/mod/modBuildNet.ts b/src/lang/mod/modBuildNet.ts index 19afb81e..fad0b657 100644 --- a/src/lang/mod/modBuildNet.ts +++ b/src/lang/mod/modBuildNet.ts @@ -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 } diff --git a/src/lang/words/PortConnect.ts b/src/lang/words/PortConnect.ts index 3a60e885..0ab03dee 100644 --- a/src/lang/words/PortConnect.ts +++ b/src/lang/words/PortConnect.ts @@ -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" @@ -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) } }