diff --git a/src/lang/connect/connect.ts b/src/lang/connect/connect.ts index 63a93b51..350f1e62 100644 --- a/src/lang/connect/connect.ts +++ b/src/lang/connect/connect.ts @@ -1,4 +1,5 @@ import { checkPortSigns } from "../check/checkPortSigns" +import { edgeEqual } from "../edge/edgeEqual" import { Net } from "../net" import { findPortEntry } from "../net/findPortEntry" import { findPortRecordOrFail } from "../net/findPortRecordOrFail" @@ -48,7 +49,11 @@ export function connect(net: Net, first: Port, second: Port): void { const secondPortRecord = findPortRecordOrFail(net, second.node) secondPortRecord[second.name].connection = { port: first } - if (first.isPrincipal && second.isPrincipal) { + if ( + first.isPrincipal && + second.isPrincipal && + !net.activeEdges.find((edge) => edgeEqual(edge, { first, second })) + ) { net.activeEdges.push(edge) } } diff --git a/src/lang/edge/edgeEqual.ts b/src/lang/edge/edgeEqual.ts new file mode 100644 index 00000000..e0f0bca0 --- /dev/null +++ b/src/lang/edge/edgeEqual.ts @@ -0,0 +1,9 @@ +import { portEqual } from "../port/portEqual" +import { Edge } from "./Edge" + +export function edgeEqual(x: Edge, y: Edge): boolean { + return ( + (portEqual(x.first, y.first) && portEqual(x.second, y.second)) || + (portEqual(x.first, y.second) && portEqual(x.second, y.first)) + ) +} diff --git a/src/lang/node/nodeEqual.ts b/src/lang/node/nodeEqual.ts new file mode 100644 index 00000000..80db4354 --- /dev/null +++ b/src/lang/node/nodeEqual.ts @@ -0,0 +1,5 @@ +import { Node } from "./Node" + +export function nodeEqual(x: Node, y: Node): boolean { + return x.name === y.name && x.id === y.id +} diff --git a/src/lang/port/portEqual.ts b/src/lang/port/portEqual.ts new file mode 100644 index 00000000..cd832231 --- /dev/null +++ b/src/lang/port/portEqual.ts @@ -0,0 +1,6 @@ +import { nodeEqual } from "../node/nodeEqual" +import { Port } from "./Port" + +export function portEqual(x: Port, y: Port): boolean { + return nodeEqual(x.node, y.node) && x.name === y.name +} diff --git a/tests/statement/define-without-claim.error.i.err b/tests/statement/define-without-claim.error.i.err index b414397e..d6cd0c0e 100644 --- a/tests/statement/define-without-claim.error.i.err +++ b/tests/statement/define-without-claim.error.i.err @@ -1,4 +1,4 @@ -[lookupDefinitionOrFail] I meet undefined name. +[findDefinitionOrFail] I meet undefined name. name: swapswap diff --git a/tests/statement/rule-before-node.error.i.err b/tests/statement/rule-before-node.error.i.err index 7ea6f90e..fbac249a 100644 --- a/tests/statement/rule-before-node.error.i.err +++ b/tests/statement/rule-before-node.error.i.err @@ -1,4 +1,4 @@ -[lookupDefinitionOrFail] I meet undefined name. +[findDefinitionOrFail] I meet undefined name. name: add