Skip to content

Commit

Permalink
extract netRun
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jul 28, 2023
1 parent e5bd9de commit 7a86b1c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extract `netStep`
extract `netRun`

command change render command to simple run command

quit using `def/` and `defs/`

- `Mod` should have namespaces for
Expand Down
3 changes: 2 additions & 1 deletion src/console/commands/render-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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 { Mod } from "../../lang/mod"
import { Parser } from "../../lang/syntax"
import { NetRenderer } from "../../renderers/NetRenderer"
Expand Down Expand Up @@ -77,7 +78,7 @@ export class RenderCommand extends Command<Args, Opts> {
async function renderNet(mod: Mod, file: string, name: string): Promise<void> {
const net = mod.buildNet(name)
renderFile(net, `${file}.${name}.initial.txt`)
net.run()
netRun(net)
renderFile(net, `${file}.${name}.finial.txt`)
}

Expand Down
21 changes: 0 additions & 21 deletions src/lang/graph/Net.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { InternalError } from "../errors"
import { Action, Edge, Node, Port } from "../graph"
import { Mod } from "../mod"
import { netCleanUpWires } from "./netCleanUpWires"
import { netCloseFreePorts } from "./netCloseFreePorts"
import { netReleaseFreePorts } from "./netReleaseFreePorts"

export class Net {
mod: Mod
Expand All @@ -18,21 +14,4 @@ export class Net {
constructor(mod: Mod) {
this.mod = mod
}

run(): void {
const closer = netCloseFreePorts(this)
while (this.actions.length > 0) this.step()
netCleanUpWires(this)
netReleaseFreePorts(this, closer)
}

private step(): void {
if (this.portStack.length !== 0) {
throw new InternalError("I can not handle free port during stepping.")
}

const action = this.actions.pop()
if (action === undefined) return
else action.act(this.mod, this)
}
}
26 changes: 26 additions & 0 deletions src/lang/graph/netRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { InternalError } from "../errors"
import { Net } from "./Net"
import { netCleanUpWires } from "./netCleanUpWires"
import { netCloseFreePorts } from "./netCloseFreePorts"
import { netReleaseFreePorts } from "./netReleaseFreePorts"

export function netRun(net: Net): void {
const closer = netCloseFreePorts(net)

while (net.actions.length > 0) {
netStep(net)
}

netCleanUpWires(net)
netReleaseFreePorts(net, closer)
}

function netStep(net: Net): void {
if (net.portStack.length !== 0) {
throw new InternalError("I can not handle free port during stepping.")
}

const action = net.actions.pop()
if (action === undefined) return
else action.act(net.mod, net)
}

0 comments on commit 7a86b1c

Please sign in to comment.