Skip to content

Commit

Permalink
createNode take net and create nodePorts
Browse files Browse the repository at this point in the history
- use `findNodePortsOrFail` instead of `findNodePortsOrCreate`
  • Loading branch information
xieyuheng committed Aug 15, 2023
1 parent e96a633 commit b823e4e
Show file tree
Hide file tree
Showing 22 changed files with 340 additions and 274 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# net -- graph theory

`createNode` take `net` and create `nodePorts`

rename `node/createNode` to `net/addNode`

use `findNodePortsOrFail` instead of `findNodePortsOrCreate`
rename `placeholder/create*` to `placeholder/add*`

`Node` should not `input` and `output` ports

Expand Down
10 changes: 7 additions & 3 deletions src/lang/check/checkRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ export function checkRule(
): void {
const checking = createChecking()

const first = createNodeFromDefinition(lookupDefinitionOrFail(mod, firstName))
const env = createEnv(mod)

const first = createNodeFromDefinition(
env.net,
lookupDefinitionOrFail(mod, firstName),
)

refreshNode(checking.typeVarCounters, first)

const second = createNodeFromDefinition(
env.net,
lookupDefinitionOrFail(mod, secondName),
)

refreshNode(checking.typeVarCounters, second)

const env = createEnv(mod)

connectNodeWithPlaceholderPorts(mod, env.net, first)
connectNodeWithPlaceholderPorts(mod, env.net, second)

Expand Down
29 changes: 24 additions & 5 deletions src/lang/check/checkRuleNodeOrder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { createNodeFromDefinition } from "../node/createNodeFromDefinition"

export function checkRuleNodeOrder(
mod: Mod,
firstName: string,
secondName: string,
): void {
const first = createNodeFromDefinition(lookupDefinitionOrFail(mod, firstName))
const first = lookupDefinitionOrFail(mod, firstName)

if (first["@kind"] !== "NodeDefinition") {
throw new Error(
[
`[checkRuleNodeOrder] I expect the first name to be a NodeDefinition.`,
``,
` first node: ${firstName}`,
` second node: ${secondName}`,
].join("\n"),
)
}

if (!first.output.some((port) => port.isPrincipal)) {
throw new Error(
Expand All @@ -20,9 +30,18 @@ export function checkRuleNodeOrder(
)
}

const second = createNodeFromDefinition(
lookupDefinitionOrFail(mod, secondName),
)
const second = lookupDefinitionOrFail(mod, secondName)

if (second["@kind"] !== "NodeDefinition") {
throw new Error(
[
`[checkRuleNodeOrder] I expect the first name to be a NodeDefinition.`,
``,
` first node: ${firstName}`,
` second node: ${secondName}`,
].join("\n"),
)
}

if (!second.input.some((port) => port.isPrincipal)) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion src/lang/check/checkWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function checkWords(

const placeholderOutputPorts = inputValues
.reverse()
.map((t) => createPlaceholderOutputPortFromType(mod, t))
.map((t) => createPlaceholderOutputPortFromType(env.net, mod, t))

env.stack.push(...placeholderOutputPorts)

Expand Down
13 changes: 9 additions & 4 deletions src/lang/compose/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,15 @@ export function compose(

case "NodeRearrange": {
const definition = lookupDefinitionOrFail(mod, word.name)
composeNode(env, createNodeFromDefinition(definition), options, {
input: word.input,
output: word.output,
})
composeNode(
env,
createNodeFromDefinition(env.net, definition),
options,
{
input: word.input,
output: word.output,
},
)

return
}
Expand Down
2 changes: 1 addition & 1 deletion src/lang/compose/composeDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function composeDefinition(
): void {
switch (definition["@kind"]) {
case "NodeDefinition": {
composeNode(env, createNodeFromDefinition(definition), options)
composeNode(env, createNodeFromDefinition(env.net, definition), options)
return
}

Expand Down
10 changes: 5 additions & 5 deletions src/lang/connect/connect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { checkPortSigns } from "../check/checkPortSigns"
import { lookupRuleByPorts } from "../mod/lookupRuleByPorts"
import { Net } from "../net"
import { findNodePortsOrCreate } from "../net/findNodePortsOrCreate"
import { findNodePortsOrFail } from "../net/findNodePortsOrFail"
import { findPortConnection } from "../net/findPortConnection"
import { Port } from "../port"
import { formatValue } from "../value/formatValue"
Expand Down Expand Up @@ -36,20 +36,20 @@ export function connect(net: Net, first: Port, second: Port): void {
if (rule !== undefined) {
const edge = { first, second, rule }

const firstNodePorts = findNodePortsOrCreate(net, first.node)
const firstNodePorts = findNodePortsOrFail(net, first.node)
firstNodePorts[first.name] = { edge, port: second }

const secondNodePorts = findNodePortsOrCreate(net, second.node)
const secondNodePorts = findNodePortsOrFail(net, second.node)
secondNodePorts[second.name] = { edge, port: first }

net.activeEdges.push(edge)
} else {
const edge = { first, second }

const firstNodePorts = findNodePortsOrCreate(net, first.node)
const firstNodePorts = findNodePortsOrFail(net, first.node)
firstNodePorts[first.name] = { edge, port: second }

const secondNodePorts = findNodePortsOrCreate(net, second.node)
const secondNodePorts = findNodePortsOrFail(net, second.node)
secondNodePorts[second.name] = { edge, port: first }

net.edges.push(edge)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Node } from "../node"
import { formatNode } from "../node/formatNode"
import { nodeKeyId } from "../node/nodeKeyId"
import { Net, PortConnection } from "./Net"

export function findNodePortsOrCreate(
export function findNodePortsOrFail(
net: Net,
node: Node,
): Record<string, PortConnection> {
let ports = net.nodePorts.get(nodeKeyId(node))
if (ports === undefined) {
ports = {}
net.nodePorts.set(nodeKeyId(node), ports)
throw new Error(
[
`[findNodePortsOrFail] I can not find nodePorts for node.`,
``,
` node: ${formatNode(node)}`,
].join("\n"),
)
}

return ports
Expand Down
5 changes: 5 additions & 0 deletions src/lang/node/createNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Mod } from "../mod"
import { Net } from "../net"
import { Node } from "../node"
import { PortExp } from "../port/PortExp"
import { createInputPort } from "../port/createInputPort"
import { createOutputPort } from "../port/createOutputPort"
import { createNodeId } from "./createNodeId"
import { nodeKeyId } from "./nodeKeyId"

export function createNode(
net: Net,
mod: Mod,
name: string,
input: Array<PortExp>,
Expand All @@ -19,6 +22,8 @@ export function createNode(
output: [],
}

net.nodePorts.set(nodeKeyId(node), {})

node.input = input.map((port) => createInputPort(node, port))
node.output = output.map((port) => createOutputPort(node, port))

Expand Down
7 changes: 6 additions & 1 deletion src/lang/node/createNodeFromDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Definition } from "../definition"
import { Net } from "../net"
import { Node } from "../node"
import { createNode } from "../node/createNode"

export function createNodeFromDefinition(definition: Definition): Node {
export function createNodeFromDefinition(
net: Net,
definition: Definition,
): Node {
if (definition["@kind"] !== "NodeDefinition") {
throw new Error(
[
Expand All @@ -14,6 +18,7 @@ export function createNodeFromDefinition(definition: Definition): Node {
}

return createNode(
net,
definition.mod,
definition.name,
definition.input,
Expand Down
4 changes: 2 additions & 2 deletions src/lang/placeholder/connectNodeWithPlaceholderPorts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export function connectNodeWithPlaceholderPorts(
): void {
for (const port of node.input) {
if (!port.isPrincipal) {
const placeholderPort = createPlaceholderOutputPortForPort(mod, port)
const placeholderPort = createPlaceholderOutputPortForPort(net, mod, port)
connect(net, port, placeholderPort)
}
}

for (const port of node.output) {
if (!port.isPrincipal) {
const placeholderPort = createPlaceholderInputPortForPort(mod, port)
const placeholderPort = createPlaceholderInputPortForPort(net, mod, port)
connect(net, port, placeholderPort)
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/lang/placeholder/createPlaceholderInputPortForPort.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Mod } from "../mod"
import { Net } from "../net"
import { createNode } from "../node/createNode"
import { Port } from "../port"

export function createPlaceholderInputPortForPort(mod: Mod, port: Port): Port {
export function createPlaceholderInputPortForPort(
net: Net,
mod: Mod,
port: Port,
): Port {
const nodeName = `_placeholder_input_node_for_${port.name}_of_${port.node.name}`
const portName = `_placeholder_input_port_for_${port.name}_of_${port.node.name}`
const node = createNode(
net,
mod,
nodeName,
[
Expand Down
8 changes: 7 additions & 1 deletion src/lang/placeholder/createPlaceholderOutputPortForPort.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Mod } from "../mod"
import { Net } from "../net"
import { createNode } from "../node/createNode"
import { Port } from "../port"

export function createPlaceholderOutputPortForPort(mod: Mod, port: Port): Port {
export function createPlaceholderOutputPortForPort(
net: Net,
mod: Mod,
port: Port,
): Port {
const nodeName = `_placeholder_output_node_for_${port.name}_of_${port.node.name}`
const portName = `_placeholder_output_port_for_${port.name}_of_${port.node.name}`
const node = createNode(
net,
mod,
nodeName,
[],
Expand Down
8 changes: 7 additions & 1 deletion src/lang/placeholder/createPlaceholderOutputPortFromType.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Mod } from "../mod"
import { Net } from "../net"
import { createNode } from "../node/createNode"
import { Port } from "../port"
import { Value } from "../value"

export function createPlaceholderOutputPortFromType(mod: Mod, t: Value): Port {
export function createPlaceholderOutputPortFromType(
net: Net,
mod: Mod,
t: Value,
): Port {
const nodeName = `_placeholder_output_node_from_type`
const portName = `_placeholder_output_port_from_type`
const node = createNode(
net,
mod,
nodeName,
[],
Expand Down
8 changes: 7 additions & 1 deletion src/lang/run/closeFreePorts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export function closeFreePorts(env: Env): Node | undefined {
isPrincipal: false,
}))

const node = createNode(env.mod, "_temporary_closing_node", ports, [])
const node = createNode(
env.net,
env.mod,
"_temporary_closing_node",
ports,
[],
)

return composeNode(env, node, {})
}
Loading

0 comments on commit b823e4e

Please sign in to comment.