Skip to content

Commit

Permalink
[definition] Definition as a type -- instead of class
Browse files Browse the repository at this point in the history
- `composeDefinition`
  • Loading branch information
xieyuheng committed Aug 2, 2023
1 parent c1ffd3b commit 5dd0d20
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 113 deletions.
4 changes: 0 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# type

[definition] `Definition` as a type -- instead of class

`composeDefinition`

`cut` -- `Call`
`cut` -- `LocalSet`
`cut` -- `PortPush`
Expand Down
4 changes: 3 additions & 1 deletion src/lang/compose/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Net } from "../net"
import { connect } from "../net/connect"
import { disconnect } from "../net/disconnect"
import { Word } from "../word"
import { composeDefinition } from "./composeDefinition"

export interface ComposeOptions {
current?: { start: Node; end: Node }
Expand All @@ -25,7 +26,8 @@ export function compose(
net.localPorts.delete(word.name)
return
} else {
lookupDefinitionOrFail(mod, word.name).compose(net)
const definition = lookupDefinitionOrFail(mod, word.name)
composeDefinition(net, definition)
return
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/lang/compose/composeDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Definition } from "../definition"
import { Net } from "../net"
import { composeNodeDefinition } from "./composeNodeDefinition"
import { composeWords } from "./composeWords"

export function composeDefinition(net: Net, definition: Definition): void {
switch (definition.kind) {
case "NodeDefinition": {
composeNodeDefinition(net, definition)
return
}

case "NetDefinition": {
composeWords(definition.mod, net, definition.words)
return
}

case "OperatorDefinition": {
definition.compose(net)
return
}

case "TypeDefinition": {
throw new Error(
`[composeDefinition] Can not compose a type: ${definition.name}`,
)
}
}
}
37 changes: 37 additions & 0 deletions src/lang/compose/composeNodeDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NodeDefinition } from "../definition"
import { Node } from "../graph"
import { createNode } from "../graph/createNode"
import { Net } from "../net"
import { connect } from "../net/connect"

export function composeNodeDefinition(
net: Net,
definition: NodeDefinition,
): Node {
const node = createNode(
definition.mod,
definition.name,
definition.input,
definition.output,
)

// Be careful about the order:
// The first input port connects
// with the port on the top of the stack.

for (const port of node.input) {
const top = net.ports.pop()
if (top === undefined) {
throw new Error(
`[NodeDefinition.compose] I expect a port on top of the stack`,
)
}

connect(net, top, port)
}

net.ports.push(...node.output)
net.nodes.push(node)

return node
}
2 changes: 1 addition & 1 deletion src/lang/cut/cut.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ctx } from "../ctx"
import { NodeDefinition } from "../definitions"
import { NodeDefinition } from "../definition"
import { Mod } from "../mod"
import { Word } from "../word"

Expand Down
89 changes: 87 additions & 2 deletions src/lang/definition/Definition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,93 @@
import { PortExp } from "../graph/PortExp"
import { Mod } from "../mod"
import { Net } from "../net"
import { Word } from "../word"

export interface Definition {
export type Definition =
| NodeDefinition
| NetDefinition
| OperatorDefinition
| TypeDefinition

export type NodeDefinition = {
kind: "NodeDefinition"
mod: Mod
name: string
compose(net: Net): void
input: Array<PortExp>
output: Array<PortExp>
}

export function NodeDefinition(
mod: Mod,
name: string,
input: Array<PortExp>,
output: Array<PortExp>,
): NodeDefinition {
return {
kind: "NodeDefinition",
mod,
name,
input,
output,
}
}

export type NetDefinition = {
kind: "NetDefinition"
mod: Mod
name: string
words: Array<Word>
}

export function NetDefinition(
mod: Mod,
name: string,
words: Array<Word>,
): NetDefinition {
return {
kind: "NetDefinition",
mod,
name,
words,
}
}

export type OperatorDefinition = {
kind: "OperatorDefinition"
mod: Mod
name: string
compose: (net: Net) => void
}

export function OperatorDefinition(
mod: Mod,
name: string,
compose: (net: Net) => void,
): OperatorDefinition {
return {
kind: "OperatorDefinition",
mod,
name,
compose,
}
}

export type TypeDefinition = {
kind: "TypeDefinition"
mod: Mod
name: string
arity: number
}

export function TypeDefinition(
mod: Mod,
name: string,
arity: number,
): TypeDefinition {
return {
kind: "TypeDefinition",
mod,
name,
arity,
}
}
17 changes: 0 additions & 17 deletions src/lang/definitions/NetDefinition.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/lang/definitions/NodeDefinition.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/lang/definitions/OperatorDefinition.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/lang/definitions/TypeDefinition.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/lang/definitions/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lang/mod/defineOperator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Definitions from "../definitions"
import * as Definitions from "../definition"
import { Net } from "../net"
import { Mod } from "./Mod"
import { define } from "./define"
Expand All @@ -8,5 +8,5 @@ export function defineOperator(
name: string,
compose: (net: Net) => void,
): void {
define(mod, name, new Definitions.OperatorDefinition(mod, name, compose))
define(mod, name, Definitions.OperatorDefinition(mod, name, compose))
}
4 changes: 2 additions & 2 deletions src/lang/mod/lookupNetDefinitionOrFail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Definitions from "../definitions"
import * as Definitions from "../definition"
import { Mod } from "./Mod"
import { lookupDefinitionOrFail } from "./lookupDefinitionOrFail"

Expand All @@ -7,7 +7,7 @@ export function lookupNetDefinitionOrFail(
name: string,
): Definitions.NetDefinition {
const definition = lookupDefinitionOrFail(mod, name)
if (!(definition instanceof Definitions.NetDefinition)) {
if (!(definition.kind === "NetDefinition")) {
throw new Error(
`I expect a net definition, but ${name} is ${definition.constructor.name}`,
)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/mod/lookupNodeDefinitionOrFail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Definitions from "../definitions"
import * as Definitions from "../definition"
import { Mod } from "./Mod"
import { lookupDefinitionOrFail } from "./lookupDefinitionOrFail"

Expand All @@ -7,7 +7,7 @@ export function lookupNodeDefinitionOrFail(
name: string,
): Definitions.NodeDefinition {
const definition = lookupDefinitionOrFail(mod, name)
if (!(definition instanceof Definitions.NodeDefinition)) {
if (!(definition.kind === "NodeDefinition")) {
throw new Error(
`I expect a node definition, but ${name} is ${definition.constructor.name}`,
)
Expand Down
10 changes: 6 additions & 4 deletions src/lang/run/closeFreePorts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Definitions from "../definitions"
import { composeNodeDefinition } from "../compose/composeNodeDefinition"
import * as Definitions from "../definition"
import { Node } from "../graph/Node"
import { Net } from "../net"

Expand All @@ -13,11 +14,12 @@ export function closeFreePorts(net: Net): Node | undefined {
if (net.ports.length === 0) {
return undefined
}

return new Definitions.NodeDefinition(
const definition = Definitions.NodeDefinition(
net.mod,
"*root*",
[...net.ports],
[],
).compose(net)
)

return composeNodeDefinition(net, definition)
}
4 changes: 2 additions & 2 deletions src/lang/stmts/Defnet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Definitions from "../definitions"
import * as Definitions from "../definition"
import { Mod } from "../mod"
import { define } from "../mod/define"
import { Span } from "../span"
Expand All @@ -16,7 +16,7 @@ export class Defnet implements Stmt {
define(
mod,
this.name,
new Definitions.NetDefinition(mod, this.name, this.words),
Definitions.NetDefinition(mod, this.name, this.words),
)
}
}
4 changes: 2 additions & 2 deletions src/lang/stmts/Defnode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Definitions from "../definitions"
import * as Definitions from "../definition"
import { PortExp } from "../graph/PortExp"
import { Mod } from "../mod"
import { define } from "../mod/define"
Expand All @@ -17,7 +17,7 @@ export class Defnode implements Stmt {
define(
mod,
this.name,
new Definitions.NodeDefinition(mod, this.name, this.input, this.output),
Definitions.NodeDefinition(mod, this.name, this.input, this.output),
)
}
}
Loading

0 comments on commit 5dd0d20

Please sign in to comment.