Skip to content

Commit f6a3a05

Browse files
committed
move id/ to node/
1 parent eb02ba9 commit f6a3a05

21 files changed

+140
-14
lines changed

TODO.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# module system
22

3+
node/Node
4+
5+
Mod -- has loader
6+
37
Place and Transition -- has modId
48

59
Mod has definitions: Map<string, Definition>

src/lang/id/createId.ts

-12
This file was deleted.

src/lang/id/globalIdCounters.ts

-1
This file was deleted.

src/lang/id/index.ts

-1
This file was deleted.

src/lang/mod/Mod.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Definition } from "../definition"
22

33
export type Mod = {
4+
url: URL
45
definitions: Map<string, Definition>
56
}

src/lang/node/Node.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export type Node = {
2+
modId: string
3+
id: string
4+
name: string
5+
}
6+
7+
export type NodeWithoutId = {
8+
modId: string
9+
name: string
10+
}

src/lang/node/createNodeId.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { globalNodeCounters } from "./globalNodeCounters"
2+
3+
export function createNodeId(name: string): string {
4+
const foundCounter = globalNodeCounters.get(name)
5+
if (foundCounter === undefined) {
6+
globalNodeCounters.set(name, 0)
7+
return String(0)
8+
} else {
9+
globalNodeCounters.set(name, foundCounter + 1)
10+
return String(foundCounter + 1)
11+
}
12+
}

src/lang/node/formatNode.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { stringToSubscript } from "../../utils/stringToSubscript"
2+
import { Net } from "../net"
3+
import { Node } from "../node"
4+
5+
export function formatNode(net: Net, node: Node): string {
6+
const subscript = stringToSubscript(node.id.toString())
7+
return `${node.name}${subscript}`
8+
}

src/lang/node/globalNodeCounters.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const globalNodeCounters: Map<string, number> = new Map()

src/lang/node/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from "./Node"
2+
export * from "./formatNode"
3+
export * from "./nodeEqual"
4+
export * from "./nodeKey"
5+
export * from "./nodeKeyWithoutId"

src/lang/node/nodeEqual.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Node } from "./Node"
2+
3+
export function nodeEqual(x: Node, y: Node): boolean {
4+
return x.name === y.name && x.id === y.id
5+
}

src/lang/node/nodeKey.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Node } from "./Node"
2+
3+
export function nodeKey(node: Node): string {
4+
return `${node.modId}/${node.name}#${node.id}`
5+
}

src/lang/node/nodeKeyWithoutId.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { NodeWithoutId } from "./Node"
2+
3+
export function nodeKeyWithoutId(node: NodeWithoutId): string {
4+
return `${node.modId}/${node.name}`
5+
}

src/utils/arrayPickOut.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function arrayPickOut<A>(array: Array<A>, i: number): A {
2+
const x = array[i]
3+
array.splice(i, 1)
4+
return x
5+
}

src/utils/arrayPopMany.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function arrayPopMany<A>(array: Array<A>, n: number): Array<A> {
2+
const results: Array<A> = []
3+
while (n > 0) {
4+
const value = array.pop()
5+
if (value === undefined) {
6+
break
7+
} else {
8+
results.push(value)
9+
}
10+
11+
n--
12+
}
13+
14+
return results
15+
}

src/utils/colors.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import picocolors from "picocolors"
2+
3+
export const colors = picocolors

src/utils/countStringOccurrences.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function countStringOccurrences(
2+
words: Array<string>,
3+
): Map<string, number> {
4+
const counts = new Map()
5+
6+
for (const word of words) {
7+
const count = counts.get(word)
8+
if (count === undefined) {
9+
counts.set(word, 1)
10+
} else {
11+
counts.set(word, count + 1)
12+
}
13+
}
14+
15+
return counts
16+
}

src/utils/createURL.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { resolve } from "node:path"
2+
3+
export function createURL(path: string): URL {
4+
if (
5+
path.startsWith("http://") ||
6+
path.startsWith("https://") ||
7+
path.startsWith("https://")
8+
) {
9+
return new URL(path)
10+
}
11+
12+
const absolutePath = resolve(path)
13+
return new URL(`file://${absolutePath}`)
14+
}

src/utils/indent.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function indent(text: string, indentation: string = " "): string {
2+
return text
3+
.split("\n")
4+
.map((line) => indentation + line)
5+
.join("\n")
6+
}

src/utils/manyTimes.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function manyTimes<A>(n: number, f: () => A): Array<A> {
2+
const results: Array<A> = []
3+
while (n > 0) {
4+
n--
5+
results.push(f())
6+
}
7+
8+
return results
9+
}

src/utils/stringToSubscript.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function stringToSubscript(s: string): string {
2+
return [...s].map((c) => numberSubscripts[c] || c).join("")
3+
}
4+
5+
const numberSubscripts: Record<string, string> = {
6+
"0": "₀",
7+
"1": "₁",
8+
"2": "₂",
9+
"3": "₃",
10+
"4": "₄",
11+
"5": "₅",
12+
"6": "₆",
13+
"7": "₇",
14+
"8": "₈",
15+
"9": "₉",
16+
}

0 commit comments

Comments
 (0)