Skip to content

Commit

Permalink
rename Net to Env
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 7, 2023
1 parent 330779c commit f2f23eb
Show file tree
Hide file tree
Showing 29 changed files with 76 additions and 74 deletions.
16 changes: 9 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
`rule` check node order

- The first node must have its principal port in the output,
and the second node must have its principal port in the input.

# value

rename `Net` to `Env`
`Value`

rename `Env.ports` to `Env.stack`
rename `Env.ports` to `Env.values`

merge `Ctx` to `Env`

Expand All @@ -19,6 +14,13 @@ the stack can take other types of values -- not only port

`value :key` should be of type `Labeled("key", 'a)`

# check

`rule` check node order

- The first node must have its principal port in the output,
and the second node must have its principal port in the input.

# module

design module system
Expand Down
6 changes: 3 additions & 3 deletions src/lang/builtins/connect.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Ctx } from "../ctx"
import { Env } from "../env"
import { connect } from "../env/connect"
import { Port } from "../graph"
import { Net } from "../net"
import { connect } from "../net/connect"
import { SignedType } from "../type"
import { unifySignedTypes } from "../unify/unifySignedTypes"

export function compose(net: Net): void {
export function compose(net: Env): void {
const start = net.ports.pop() as Port
const end = net.ports.pop() as Port
connect(net, start, end)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/builtins/rot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Ctx } from "../ctx"
import { Env } from "../env"
import { Port } from "../graph"
import { Net } from "../net"
import { SignedType } from "../type"

export function compose(net: Net): void {
export function compose(net: Env): void {
const x2 = net.ports.pop() as Port
const x1 = net.ports.pop() as Port
const x0 = net.ports.pop() as Port
Expand Down
4 changes: 2 additions & 2 deletions src/lang/builtins/swap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Ctx } from "../ctx"
import { Env } from "../env"
import { Port } from "../graph"
import { Net } from "../net"
import { SignedType } from "../type"

export function compose(net: Net): void {
export function compose(net: Env): void {
const x1 = net.ports.pop() as Port
const x0 = net.ports.pop() as Port
net.ports.push(x1, x0)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/builtins/wire.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Ctx } from "../ctx"
import { Env } from "../env"
import { createNode } from "../graph/createNode"
import { Mod } from "../mod"
import { Net } from "../net"
import * as Types from "../type"
import { Sign } from "../type"
import { freshenType } from "../unify/freshenType"

let wireCounter = 0

export default function (mod: Mod) {
function compose(net: Net): void {
function compose(net: Env): void {
const node = createNode(
mod,
"wire",
Expand Down
8 changes: 4 additions & 4 deletions src/lang/compose/compose.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Env } from "../env"
import { connect } from "../env/connect"
import { disconnect } from "../env/disconnect"
import { Node } from "../graph"
import { Mod } from "../mod"
import { lookupDefinitionOrFail } from "../mod/lookupDefinitionOrFail"
import { Net } from "../net"
import { connect } from "../net/connect"
import { disconnect } from "../net/disconnect"
import { Word } from "../word"
import { composeDefinition } from "./composeDefinition"
import { findCurrentPortOrFail } from "./findCurrentPortOrFail"
Expand All @@ -14,7 +14,7 @@ export interface ComposeOptions {

export function compose(
mod: Mod,
net: Net,
net: Env,
word: Word,
options: ComposeOptions,
): void {
Expand Down
4 changes: 2 additions & 2 deletions src/lang/compose/composeDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Definition } from "../definition"
import { Net } from "../net"
import { Env } from "../env"
import { ComposeOptions } from "./compose"
import { composeNodeDefinition } from "./composeNodeDefinition"
import { composeWords } from "./composeWords"

export function composeDefinition(
net: Net,
net: Env,
definition: Definition,
options: ComposeOptions,
): void {
Expand Down
6 changes: 3 additions & 3 deletions src/lang/compose/composeNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Env } from "../env"
import { connect } from "../env/connect"
import { Node } from "../graph"
import { Net } from "../net"
import { connect } from "../net/connect"

export function composeNode(net: Net, node: Node): Node {
export function composeNode(net: Env, node: Node): Node {
// Be careful about the order:
// The first input port connects
// with the port on the top of the stack.
Expand Down
4 changes: 2 additions & 2 deletions src/lang/compose/composeNodeDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NodeDefinition } from "../definition"
import { Env } from "../env"
import { Node } from "../graph"
import { createNode } from "../graph/createNode"
import { Net } from "../net"
import { composeNode } from "./composeNode"

export function composeNodeDefinition(
net: Net,
net: Env,
definition: NodeDefinition,
): Node {
const node = createNode(
Expand Down
6 changes: 3 additions & 3 deletions src/lang/compose/composeWords.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Env } from "../env"
import { cleanUpWires } from "../env/cleanUpWires"
import { Mod } from "../mod"
import { Net } from "../net"
import { cleanUpWires } from "../net/cleanUpWires"
import { Word } from "../word"
import { ComposeOptions, compose } from "./compose"

export function composeWords(
mod: Mod,
net: Net,
net: Env,
words: Array<Word>,
options: ComposeOptions,
): void {
Expand Down
6 changes: 3 additions & 3 deletions src/lang/definition/Definition.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Ctx } from "../ctx"
import { Env } from "../env"
import { PortExp } from "../graph/PortExp"
import { Mod } from "../mod"
import { Net } from "../net"
import { Span } from "../span"
import { SignedType } from "../type"
import { Word } from "../word"
Expand Down Expand Up @@ -69,14 +69,14 @@ export type OperatorDefinition = {
kind: "OperatorDefinition"
mod: Mod
name: string
compose: (net: Net) => void
compose: (net: Env) => void
cut: (ctx: Ctx) => void
}

export function OperatorDefinition(
mod: Mod,
name: string,
compose: (net: Net) => void,
compose: (net: Env) => void,
cut: (ctx: Ctx) => void,
): OperatorDefinition {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/lang/net/Net.ts → src/lang/env/Env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActiveEdge, Edge, Node, Port } from "../graph"
import { Mod } from "../mod"

export type Net = {
export type Env = {
mod: Mod
nodes: Array<Node>
edges: Array<Edge>
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/cleanUpWires.ts → src/lang/env/cleanUpWires.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Net } from "./Net"
import { Env } from "./Env"
import { connect } from "./connect"
import { disconnect } from "./disconnect"
import { removeNode } from "./removeNode"

export function cleanUpWires(net: Net): void {
export function cleanUpWires(net: Env): void {
for (const wire of net.wires) {
if (wire.start.connection && wire.end.connection) {
removeNode(net, wire.start.node)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/connect.ts → src/lang/env/connect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Env } from "../env"
import { Port } from "../graph/Port"
import { lookupRuleByPorts } from "../mod/lookupRuleByPorts"
import { Net } from "../net"

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

if (start.connection !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/createNet.ts → src/lang/env/createEnv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Env } from "../env"
import { Mod } from "../mod"
import { Net } from "../net"

export function createNet(mod: Mod): Net {
export function createEnv(mod: Mod): Env {
return {
mod,
nodes: [],
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/disconnect.ts → src/lang/env/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Env } from "../env"
import { Edge } from "../graph/Edge"
import { Net } from "../net"
import { removeEdge } from "./removeEdge"

export function disconnect(net: Net, edge: Edge): void {
export function disconnect(net: Env, edge: Edge): void {
removeEdge(net, edge)

// Be careful about memory leak.
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/formatNet.ts → src/lang/env/formatEnv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Env } from "../env"
import { formatNode } from "../graph/formatNode"
import { Net } from "../net"

export function formatNet(net: Net): string {
export function formatEnv(net: Env): string {
const lines: Array<string> = []

for (const edge of [...net.activeEdges].reverse()) {
Expand Down
1 change: 1 addition & 0 deletions src/lang/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Env"
4 changes: 2 additions & 2 deletions src/lang/net/removeEdge.ts → src/lang/env/removeEdge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Env } from "../env"
import { Edge } from "../graph/Edge"
import { Net } from "../net"

export function removeEdge(net: Net, edge: Edge): void {
export function removeEdge(net: Env, edge: Edge): void {
const index = net.edges.indexOf(edge)
if (index !== -1) {
net.edges.splice(index, 1)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/net/removeNode.ts → src/lang/env/removeNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Env } from "../env"
import { Node } from "../graph/Node"
import { Net } from "../net"

export function removeNode(net: Net, node: Node): void {
export function removeNode(net: Env, node: Node): void {
const index = net.nodes.indexOf(node)
if (index !== -1) {
net.nodes.splice(index, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Node } from "../graph"
import { Net } from "./Net"
import { Env } from "./Env"
import { removeEdge } from "./removeEdge"
import { removeNode } from "./removeNode"

export function removeNodeAndEdges(net: Net, node: Node): void {
export function removeNodeAndEdges(net: Env, node: Node): void {
removeNode(net, node)

for (const port of node.input) {
Expand Down
4 changes: 2 additions & 2 deletions src/lang/mod/defineOperator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Ctx } from "../ctx"
import * as Definitions from "../definition"
import { Net } from "../net"
import { Env } from "../env"
import { Mod } from "./Mod"
import { define } from "./define"

export function defineOperator(
mod: Mod,
name: string,
options: {
compose: (net: Net) => void
compose: (net: Env) => void
cut: (ctx: Ctx) => void
},
): void {
Expand Down
1 change: 0 additions & 1 deletion src/lang/net/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lang/run/closeFreePorts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { composeNode } from "../compose/composeNode"
import { Env } from "../env"
import { Node } from "../graph/Node"
import { createNode } from "../graph/createNode"
import { Net } from "../net"

/*
Expand All @@ -10,7 +10,7 @@ import { Net } from "../net"
*/

export function closeFreePorts(net: Net): Node | undefined {
export function closeFreePorts(net: Env): Node | undefined {
if (net.ports.length === 0) {
return undefined
}
Expand Down
6 changes: 3 additions & 3 deletions src/lang/run/interact.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { compose } from "../compose/compose"
import { Env } from "../env"
import { removeNodeAndEdges } from "../env/removeNodeAndEdges"
import { ActiveEdge } from "../graph"
import { Mod } from "../mod"
import { Net } from "../net"
import { removeNodeAndEdges } from "../net/removeNodeAndEdges"

export function interact(mod: Mod, net: Net, activeEdge: ActiveEdge): void {
export function interact(mod: Mod, net: Env, activeEdge: ActiveEdge): void {
removeNodeAndEdges(net, activeEdge.end.node)
removeNodeAndEdges(net, activeEdge.start.node)

Expand Down
8 changes: 4 additions & 4 deletions src/lang/run/releaseFreePorts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Env } from "../env"
import { disconnect } from "../env/disconnect"
import { removeNode } from "../env/removeNode"
import { Node } from "../graph/Node"
import { Net } from "../net"
import { disconnect } from "../net/disconnect"
import { removeNode } from "../net/removeNode"

export function releaseFreePorts(net: Net, closer: Node | undefined): void {
export function releaseFreePorts(net: Env, closer: Node | undefined): void {
if (closer === undefined) {
return
}
Expand Down
8 changes: 4 additions & 4 deletions src/lang/run/run.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Net } from "../net"
import { cleanUpWires } from "../net/cleanUpWires"
import { Env } from "../env"
import { cleanUpWires } from "../env/cleanUpWires"
import { closeFreePorts } from "./closeFreePorts"
import { interact } from "./interact"
import { releaseFreePorts } from "./releaseFreePorts"

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

while (net.activeEdges.length > 0) {
Expand All @@ -16,7 +16,7 @@ export function run(net: Net): void {
releaseFreePorts(net, closer)
}

function step(net: Net): void {
function step(net: Env): void {
const activeEdge = net.activeEdges.pop()
if (activeEdge !== undefined) {
interact(net.mod, net, activeEdge)
Expand Down
Loading

0 comments on commit f2f23eb

Please sign in to comment.