-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[definition]
Definition
as a type -- instead of class
- `composeDefinition`
- Loading branch information
Showing
18 changed files
with
175 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.