Skip to content

Commit

Permalink
[builtin] runPort -- only run the top port
Browse files Browse the repository at this point in the history
- move connected component out and back
- run-only-top-port.i
  • Loading branch information
xieyuheng committed Aug 17, 2023
1 parent 66a4570 commit 2f58d65
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 132 deletions.
16 changes: 3 additions & 13 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
# connected component
# books

[builtin] `run` -- only run the top port
反应网编程

- `run` take `mod` and `net` and `port` and return `port`

- move connected component out and back

- run-only-top-port.i

# docs

update the article

new manual
programming-with-interaction-nets

# propaganda

Expand Down
4 changes: 2 additions & 2 deletions src/lang/builtins/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Env } from "../env"
import { run } from "../run"
import { runPort } from "../run/runPort"
import { formatValue } from "../value/formatValue"

export function compose(env: Env): void {
Expand All @@ -18,5 +18,5 @@ export function compose(env: Env): void {
)
}

env.stack.push(run(env.mod, env.net, port))
env.stack.push(runPort(env.mod, env.net, port))
}
3 changes: 2 additions & 1 deletion src/lang/net/copyConnectedComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export function copyConnectedComponent(
return
}

const portRecord = findPortRecordOrFail(net, node)

copyNode(net, component, node)

const portRecord = findPortRecordOrFail(net, node)
for (const portEntry of Object.values(portRecord)) {
if (portEntry.connection) {
copyConnectedComponent(net, component, portEntry.connection.port.node)
Expand Down
3 changes: 2 additions & 1 deletion src/lang/net/moveConnectedComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export function moveConnectedComponent(
return
}

const portRecord = findPortRecordOrFail(net, node)

moveNode(net, component, node)

const portRecord = findPortRecordOrFail(net, node)
for (const portEntry of Object.values(portRecord)) {
if (portEntry.connection) {
moveConnectedComponent(net, component, portEntry.connection.port.node)
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions src/lang/run/closePort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Mod } from "../mod"
import { Net } from "../net"
import { connectPlaceholderInputPort } from "../placeholder/connectPlaceholderInputPort"
import { connectPlaceholderOutputPort } from "../placeholder/connectPlaceholderOutputPort"
import { Port } from "../port"

export function closePort(mod: Mod, component: Net, port: Port): Port {
return port.sign === 1
? connectPlaceholderInputPort(mod, component, port)
: connectPlaceholderOutputPort(mod, component, port)
}
26 changes: 26 additions & 0 deletions src/lang/run/collectConnectedPort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Net } from "../net"
import { deleteNodeEntry } from "../net/deleteNodeEntry"
import { disconnectPort } from "../net/disconnectPort"
import { findPortEntry } from "../net/findPortEntry"
import { Port } from "../port"
import { formatValue } from "../value/formatValue"

export function collectResultPort(component: Net, placeholderPort: Port): Port {
const placeholderPortEntry = findPortEntry(component, placeholderPort)
if (placeholderPortEntry?.connection === undefined) {
throw new Error(
[
`[run] I expect the placeholderPort to be connected.`,
``,
` placeholderPort: ${formatValue(placeholderPort)}`,
].join("\n"),
)
}

const connectedPort = placeholderPortEntry.connection.port

disconnectPort(component, placeholderPort)
deleteNodeEntry(component, placeholderPort.node)

return connectedPort
}
1 change: 0 additions & 1 deletion src/lang/run/index.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/lang/run/releasePlaceholderPorts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Net } from "../net"
import { deleteNodeEntry } from "../net/deleteNodeEntry"
import { disconnectPort } from "../net/disconnectPort"
import { Port } from "../port"

export function releaseFreePorts(
component: Net,
placeholderPorts: Array<Port>,
): void {
for (const placeholderPort of placeholderPorts) {
disconnectPort(component, placeholderPort)
deleteNodeEntry(component, placeholderPort.node)
}
}
53 changes: 0 additions & 53 deletions src/lang/run/run.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/lang/run/runNet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createEnv } from "../env/createEnv"
import { interact } from "../interact"
import { Mod } from "../mod"
import { Net } from "../net"

export function runNet(mod: Mod, net: Net): void {
const env = createEnv(mod, { net })
while (net.activeEdges.length > 0) {
const activeEdge = net.activeEdges.pop()
if (activeEdge !== undefined) {
interact(env, activeEdge, {})
}
}
}
28 changes: 28 additions & 0 deletions src/lang/run/runPort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Mod } from "../mod"
import { Net } from "../net"
import { createNet } from "../net/createNet"
import { moveConnectedComponent } from "../net/moveConnectedComponent"
import { Port } from "../port"
import { closeFreePorts as closeAllFreePorts } from "./closeAllFreePorts"
import { closePort } from "./closePort"
import { collectResultPort as collectConnectedPort } from "./collectConnectedPort"
import { releaseFreePorts as releasePlaceholderPorts } from "./releasePlaceholderPorts"
import { runNet } from "./runNet"

export function runPort(mod: Mod, net: Net, port: Port): Port {
const component = createNet()

moveConnectedComponent(net, component, port.node)

const placeholderPort = closePort(mod, component, port)
const placeholderForFreePorts = closeAllFreePorts(mod, component)

runNet(mod, component)

releasePlaceholderPorts(component, placeholderForFreePorts)
const connectedPort = collectConnectedPort(component, placeholderPort)

moveConnectedComponent(component, net, connectedPort.node)

return connectedPort
}
2 changes: 1 addition & 1 deletion tests/builtin/run-only-top-port.i
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ begin
one one add
one one add
run inspect
// swap inspect
swap inspect
end
6 changes: 6 additions & 0 deletions tests/builtin/run-only-top-port.i.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ net_from_port (add1₂₅)-value!
(add1₂₅)-prev value-(add1₂₃)
(add1₂₃)-prev value-(zero₂₁)
end
net_from_port (add₁₀)-return
(add₁₀)-target!value-(add1₂₂)
(add₁₀)-addend value-(add1₂₁)
(add1₂₂)-prev value-(zero₂₀)
(add1₂₁)-prev value-(zero₁₉)
end
120 changes: 60 additions & 60 deletions tests/datatype/Nat.test.i.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ net_from_port (add₁₅)-return
(add1₂₅)-prev value-(zero₂₄)
(add1₂₄)-prev value-(zero₂₃)
end
net_from_port (add1₂₉)-value!
(add1₂₉)-prev value-(add1₃₀)
net_from_port (add1₃₀)-value!
(add1₃₀)-prev value-(add1₃₁)
(add1₃₁)-prev value-(add1₂₄)
(add1₃₁)-prev value-(add1₂₈)
(add1₂₈)-prev value-(add1₂₄)
(add1₂₄)-prev value-(zero₂₃)
end
net_from_port (add₂₀)-return
Expand All @@ -53,73 +53,73 @@ net_from_port (zero₃₄)-value!

end
net_from_port (nat_dup₄)-first
(nat_dup₄)-target return-(add₃)
(add₃)-target!value-(add1₄)
(add₃)-addend value-(add1₄)
(add1₄)-prev value-(zero₃₆)
(add1₄)-prev value-(zero₃₅)
(nat_dup₄)-target return-(add₃)
(add₃)-target!value-(add1₄)
(add₃)-addend value-(add1₄)
(add1₄)-prev value-(zero₃₆)
(add1₄)-prev value-(zero₃₅)
end
net_from_port (add1₄)-value!
(add1₄)-prev value-(add1₄)
(add1₄)-prev value-(zero₃₇)
net_from_port (add1₄)-value!
(add1₄)-prev value-(add1₄)
(add1₄)-prev value-(zero₃₇)
end
net_from_port (mul₃)-return
(mul₃)-target return-(add₃)
(mul₃)-mulend return-(add₃)
(add₃)-target!value-(add1₅)
(add₃)-addend value-(add1₅)
(add1₅)-prev value-(zero₄₂)
(add1₅)-prev value-(zero₄₁)
(add₃)-target!value-(add1₅)
(add₃)-addend value-(add1₅₀)
(add1₅)-prev value-(zero₄₀)
(add1₅₀)-prev value-(zero₃₉)
end
net_from_port (add1₅)-value!
(add1₅)-prev value-(add1₆)
(add1₆)-prev value-(add1₆)
(add1₆)-prev value-(add1₆)
(add1₆)-prev value-(zero₄₇)
(mul₃)-target return-(add₃)
(mul₃)-mulend return-(add₃)
(add₃)-target!value-(add1₅)
(add₃)-addend value-(add1₅)
(add1₅)-prev value-(zero₄₂)
(add1₅)-prev value-(zero₄₁)
(add₃)-target!value-(add1₅)
(add₃)-addend value-(add1₄₉)
(add1₅)-prev value-(zero₄₀)
(add1₄₉)-prev value-(zero₃₉)
end
net_from_port (add1₅)-value!
(add1₅)-prev value-(add1₆)
(add1₆)-prev value-(add1₆)
(add1₆)-prev value-(add1₆)
(add1₆)-prev value-(zero₅₁)
end
net_from_port (mul₈)-return
(mul₈)-target return-(add₄₈)
(mul₈)-mulend return-(add₄₆)
(add₄₈)-target!value-(add1₇₃)
(add₄₈)-addend return-(add₄₇)
(add1₇₃)-prev value-(zero₅₇)
(mul₈)-target return-(add₄₇)
(mul₈)-mulend return-(add₄₅)
(add₄₇)-target!value-(add1₇₂)
(add₄₇)-addend value-(add1₇₁)
(add1₇₂)-prev value-(zero₅)
(add1₇₁)-prev value-(zero₅₅)
(add₄₆)-target!value-(add1₇₀)
(add₄₆)-addend return-(add₄₅)
(add1₇₀)-prev value-(zero₅)
(add₄₇)-addend return-(add₄₆)
(add1₇₂)-prev value-(zero₅)
(add₄₆)-target!value-(add1₇₁)
(add₄₆)-addend value-(add1₇₀)
(add1₇₁)-prev value-(zero₅₆)
(add1₇₀)-prev value-(zero₅)
(add₄₅)-target!value-(add1₆₉)
(add₄₅)-addend value-(add1₆₈)
(add1₆₉)-prev value-(zero₅₃)
(add1₆₈)-prev value-(zero₅₂)
(add₄₅)-addend return-(add₄₄)
(add1₆₉)-prev value-(zero₅₄)
(add₄₄)-target!value-(add1₆₈)
(add₄₄)-addend value-(add1₆₇)
(add1₆₈)-prev value-(zero₅₃)
(add1₆₇)-prev value-(zero₅₂)
end
net_from_port (add1₇₉)-value!
(add1₇₉)-prev value-(add1₈₉)
(add1₈₉)-prev value-(add1₉₈)
(add1₉₈)-prev value-(add1₈₂)
(add1₇₉)-prev value-(add1₈₂)
(add1₈₂)-prev value-(add1₈₅)
(add1₈₅)-prev value-(add1₉₂)
(add1₉₂)-prev value-(add1₁₀₁)
(add1₁₀₁)-prev value-(add1₉₅)
(add1₉₅)-prev value-(add1₁₀₄)
(add1₁₀₄)-prev value-(zero₆₄)
end
net_from_port (add1₁₀₈)-value!
(add1₁₀₈)-prev value-(add1₁₀₅)
(add1₁₀₅)-prev value-(zero₇₂)
end
net_from_port (add1₁₁₃)-value!
(add1₁₁₃)-prev value-(add1₁₁₄)
(add1₁₁₄)-prev value-(zero₇₅)
(add1₈₅)-prev value-(add1₈₈)
(add1₈₈)-prev value-(add1₉₇)
(add1₉₇)-prev value-(add1₉₁)
(add1₉₁)-prev value-(add1₉₄)
(add1₉₄)-prev value-(add1₁₀₀)
(add1₁₀₀)-prev value-(add1₁₀₃)
(add1₁₀₃)-prev value-(zero₇₀)
end
net_from_port (add1₁₀₇)-value!
(add1₁₀₇)-prev value-(add1₁₀₄)
(add1₁₀₄)-prev value-(zero₇₂)
end
net_from_port (add1₁₁₂)-value!
(add1₁₁₂)-prev value-(add1₁₁₃)
(add1₁₁₃)-prev value-(zero₇₅)
end
net_from_port (add1₁₂₂)-value!
(add1₁₂₂)-prev value-(add1₁₂)
(add1₁₂)-prev value-(add1₁₁)
(add1₁₁)-prev value-(zero₇₇)
(add1₁₂₂)-prev value-(add1₁₂)
(add1₁₂)-prev value-(add1₁₁)
(add1₁₁)-prev value-(zero₇₇)
end

0 comments on commit 2f58d65

Please sign in to comment.