|
1 | | -import { |
2 | | - assertIsNode, |
3 | | - GetNodeFromKind, |
4 | | - InstructionNode, |
5 | | - Node, |
6 | | - NodeKind, |
7 | | - ProgramNode, |
8 | | - REGISTERED_NODE_KINDS, |
9 | | -} from '@codama/nodes'; |
| 1 | +import { GetNodeFromKind, Node, NodeKind } from '@codama/nodes'; |
10 | 2 |
|
11 | | -import { findLastNodeFromPath, NodePath } from './NodePath'; |
| 3 | +import { assertIsNodePath, NodePath } from './NodePath'; |
| 4 | + |
| 5 | +type MutableNodePath = Node[]; |
12 | 6 |
|
13 | 7 | export class NodeStack { |
14 | 8 | /** |
15 | | - * Contains all the node stacks saved during the traversal. |
| 9 | + * Contains all the node paths saved during the traversal. |
16 | 10 | * |
17 | | - * - The very last stack is the current stack which is being |
| 11 | + * - The very last path is the current path which is being |
18 | 12 | * used during the traversal. |
19 | | - * - The other stacks can be used to save and restore the |
20 | | - * current stack when jumping to different parts of the tree. |
| 13 | + * - The other paths can be used to save and restore the |
| 14 | + * current path when jumping to different parts of the tree. |
21 | 15 | * |
22 | | - * There must at least be one stack in the heap at all times. |
| 16 | + * There must at least be one path in the stack at all times. |
23 | 17 | */ |
24 | | - private readonly heap: [...Node[][], Node[]]; |
| 18 | + private readonly stack: [...MutableNodePath[], MutableNodePath]; |
25 | 19 |
|
26 | | - constructor(...heap: readonly [...(readonly (readonly Node[])[]), readonly Node[]] | readonly []) { |
27 | | - this.heap = heap.length === 0 ? [[]] : ([...heap.map(nodes => [...nodes])] as [...Node[][], Node[]]); |
| 20 | + constructor(...stack: readonly [...(readonly NodePath[]), NodePath] | readonly []) { |
| 21 | + this.stack = |
| 22 | + stack.length === 0 |
| 23 | + ? [[]] |
| 24 | + : ([...stack.map(nodes => [...nodes])] as [...MutableNodePath[], MutableNodePath]); |
28 | 25 | } |
29 | 26 |
|
30 | | - public get stack(): Node[] { |
31 | | - return this.heap[this.heap.length - 1]; |
| 27 | + private get currentPath(): MutableNodePath { |
| 28 | + return this.stack[this.stack.length - 1]; |
32 | 29 | } |
33 | 30 |
|
34 | 31 | public push(node: Node): void { |
35 | | - this.stack.push(node); |
| 32 | + this.currentPath.push(node); |
36 | 33 | } |
37 | 34 |
|
38 | 35 | public pop(): Node | undefined { |
39 | | - return this.stack.pop(); |
| 36 | + return this.currentPath.pop(); |
40 | 37 | } |
41 | 38 |
|
42 | 39 | public peek(): Node | undefined { |
43 | | - return this.isEmpty() ? undefined : this.stack[this.stack.length - 1]; |
| 40 | + return this.isEmpty() ? undefined : this.currentPath[this.currentPath.length - 1]; |
44 | 41 | } |
45 | 42 |
|
46 | | - public pushStack(newStack: readonly Node[] = []): void { |
47 | | - this.heap.push([...newStack]); |
| 43 | + public pushPath(newPath: NodePath = []): void { |
| 44 | + this.stack.push([...newPath]); |
48 | 45 | } |
49 | 46 |
|
50 | | - public popStack(): readonly Node[] { |
51 | | - const oldStack = this.heap.pop() as Node[]; |
52 | | - if (this.heap.length === 0) { |
| 47 | + public popPath(): NodePath { |
| 48 | + if (this.stack.length === 0) { |
53 | 49 | // TODO: Coded error |
54 | | - throw new Error('The heap of stacks can never be empty.'); |
| 50 | + throw new Error('The stack of paths can never be empty.'); |
55 | 51 | } |
56 | | - return [...oldStack] as readonly Node[]; |
57 | | - } |
58 | | - |
59 | | - public find<TKind extends NodeKind>(kind: TKind | TKind[]): GetNodeFromKind<TKind> | undefined { |
60 | | - return findLastNodeFromPath([...this.stack] as unknown as NodePath<GetNodeFromKind<TKind>>, kind); |
61 | | - } |
62 | | - |
63 | | - public getProgram(): ProgramNode | undefined { |
64 | | - return this.find('programNode'); |
65 | | - } |
66 | | - |
67 | | - public getInstruction(): InstructionNode | undefined { |
68 | | - return this.find('instructionNode'); |
| 52 | + return [...this.stack.pop()!]; |
69 | 53 | } |
70 | 54 |
|
71 | | - public all(): readonly Node[] { |
72 | | - return [...this.stack]; |
73 | | - } |
74 | | - |
75 | | - public getPath<TKind extends NodeKind>(kind?: TKind | TKind[]): NodePath<GetNodeFromKind<TKind>> { |
76 | | - const node = this.peek(); |
77 | | - assertIsNode(node, kind ?? REGISTERED_NODE_KINDS); |
78 | | - return [...this.stack] as unknown as NodePath<GetNodeFromKind<TKind>>; |
| 55 | + public getPath(): NodePath; |
| 56 | + public getPath<TKind extends NodeKind>(kind: TKind | TKind[]): NodePath<GetNodeFromKind<TKind>>; |
| 57 | + public getPath<TKind extends NodeKind>(kind?: TKind | TKind[]): NodePath { |
| 58 | + const path = [...this.currentPath]; |
| 59 | + if (kind) { |
| 60 | + assertIsNodePath(path, kind); |
| 61 | + } |
| 62 | + return path; |
79 | 63 | } |
80 | 64 |
|
81 | 65 | public isEmpty(): boolean { |
82 | | - return this.stack.length === 0; |
| 66 | + return this.currentPath.length === 0; |
83 | 67 | } |
84 | 68 |
|
85 | 69 | public clone(): NodeStack { |
86 | | - return new NodeStack(...this.heap); |
87 | | - } |
88 | | - |
89 | | - public toString(): string { |
90 | | - return this.toStringArray().join(' > '); |
91 | | - } |
92 | | - |
93 | | - public toStringArray(): string[] { |
94 | | - return this.stack.map((node): string => { |
95 | | - return 'name' in node ? `[${node.kind}]${node.name}` : `[${node.kind}]`; |
96 | | - }); |
| 70 | + return new NodeStack(...this.stack); |
97 | 71 | } |
98 | 72 | } |
0 commit comments