-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdeclare.d.ts
273 lines (234 loc) · 6.05 KB
/
declare.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//======//
// Enum //
//======//
declare type CellType = Cell["type"]
declare type PulseType = Pulse["type"]
declare type PulseColour = "blue" | "green" | "red"
declare type WireColour = "any" | "blue" | "green" | "red"
declare type Timing = 0 | -1 | 1
declare type OperationType = Operation["type"]
//=========//
// Utility //
//=========//
declare type Vector2D = [number, number]
//====//
// Id //
//====//
declare type CellId = number
declare type WireId = number
//======//
// Cell //
//======//
declare type Cell = BaseCell & CellTemplate
declare type BaseCell = {
id: CellId
parent: CellId
position: Vector2D
cells: CellId[]
inputs: WireId[]
outputs: WireId[]
fire: Fire
tag: { [key: string]: Seralisable }
}
//======//
// Wire //
//======//
declare type Wire = {
id: WireId
colour: WireColour
timing: Timing
source: CellId
target: CellId
cells: CellId[]
}
//=======//
// Nogan //
//=======//
declare type Nogan = {
json: string | null
nextCell: CellId
nextWire: WireId
archivedCells: CellId[]
archivedWires: WireId[]
deletedCells: CellId[]
deletedWires: WireId[]
items: { [id: number]: Cell | Wire | null }
}
//======//
// Peak //
//======//
declare type FailPeak = {
result: false
operations: Operation[]
pulse: null
final: boolean
colour: PulseColour
}
declare type SuccessPeak = {
result: true
operations: Operation[]
pulse: Pulse
final: boolean
colour: PulseColour
}
declare type Peak = FailPeak | SuccessPeak
declare type Behave<T extends Pulse> = ({
nogan,
source,
target,
previous,
next,
}: {
nogan: Nogan
source: CellId
target: CellId
previous: Peak
peak: SuccessPeak & { pulse: T }
}) => Peak
declare type PulseMap<key extends PulseType = PulseType> = {
[P in key]: { type: P } & Omit<Extract<Pulse, { type: P }>, "type">
}[key]
declare type BehaviourMap<key extends PulseType = PulseType> = {
[P in key]: Behave<PulseMap<P>>
}
//===========//
// Operation //
//===========//
// Based on: https://github.com/microsoft/TypeScript/pull/47109
declare type Operation = ReportOperation | InstructionOperation
declare type OperationMap<key extends OperationType = OperationType> = {
[P in key]: { type: P } & Omit<Extract<Operation, { type: P }>, "type">
}[key]
declare type Operate<key extends OperationType> = (nogan: Nogan, operation: OperationMap<key>) => Operation[]
declare type OperateMap<key extends OperationType = OperationType> = {
[P in key]: Operate<P>
}
declare type TunnelFunction<key extends OperationType = OperationType> = (operation: OperationMap<key>) => void
declare type TunnelMap<key extends OperationType = OperationType> = {
[P in key]: TunnelFunction<P>
}
//======//
// Fire //
//======//
// declare type Fire = {
// red: Pulse | null
// green: Pulse | null
// blue: Pulse | null
// }
declare type Fire = {
[key in PulseColour]: Pulse | null
}
//=======//
// Cache //
//=======//
declare class Memo<Value, Key, Args> {
static RESERVED: unique symbol
static NEW: unique symbol
entries: Map<string, Value | typeof Memo.RESERVED>
encode(args: Args): Key
query(key: Key): Value | typeof Memo.RESERVED
store(key: Key, value: Value): void
}
//======//
// Type //
//======//
declare type Primitive = string | number | boolean | null | undefined
declare type Seralisable = Primitive | [...Serialisable] | Record<string, Serialisable>
declare function asConst<
V extends Primitive,
T extends V | Record<string, T> | [...V],
R extends T,
>(v: R): R {
return v
}
declare function asTuple<V extends any, T extends [...V], R extends T>(v: R): R {
return v
}
declare type AsConst = typeof asConst
declare type AsTuple = typeof asTuple
//------- Customisable stuff below this line -------//
//================//
// Cell templates //
//================//
type CellTemplate =
| RootCell
| DummyCell
| SlotCell
| CreationCell
| DestructionCell
| RecordingCell
| StopperCell
| ConnectionCell
| TimingCell
| ColourCell
| RealityCell
| DefinitionCell
// Required
declare type RootCell = { type: "root" }
// In built in some way
declare type RealityCell = { type: "reality" }
// For debugging
declare type DummyCell = { type: "dummy" }
declare type StopperCell = { type: "stopper" }
// For arroost
declare type SlotCell = { type: "slot" }
declare type RecordingCell = { type: "recording"; key: number | null }
declare type CreationCell = { type: "creation" }
declare type DestructionCell = { type: "destruction" }
declare type TimingCell = { type: "timing"; wire: WireId }
declare type ColourCell = { type: "colour"; wire: WireId }
declare type ConnectionCell = { type: "connection" }
declare type DefinitionCell = { type: "definition" }
//========//
// Pulses //
//========//
type Pulse = RawPulse | CreationPulse | DestructionPulse | PingPulse
// Required
declare type RawPulse = { type: "raw" }
// For debugging
declare type PingPulse = { type: "ping" }
// For real
declare type CreationPulse = { type: "creation"; template: CellTemplate | null }
declare type DestructionPulse = { type: "destruction" }
//===================//
// Report operations //
//===================//
declare type ReportOperation = FiredOperation | UnfiredOperation | BinnedOperation | MovedOperation
// Required
declare type FiredOperation = { type: "fired"; id: CellId }
declare type UnfiredOperation = { type: "unfired"; id: CellId }
declare type BinnedOperation = { type: "binned"; id: CellId | WireId }
declare type MovedOperation = { type: "moved"; id: CellId; position: Vector2D }
//========================//
// Instruction operations //
//========================//
type InstructionOperation =
| ModifyCellOperation
| PongOperation
| TagOperation
| ModifyWireOperation
| UnfireOperation
// For debugging
declare type PongOperation = { type: "pong" }
// For real
declare type ModifyCellOperation = {
type: "modifyCell"
id: CellId
template: Partial<CellTemplate>
}
declare type ModifyWireOperation = {
type: "modifyWire"
id: WireId
template: Partial<{ colour: WireColour; timing: Timing }>
}
declare type TagOperation = {
type: "tag"
id: CellId
key: string
value?: Serialisable
}
declare type UnfireOperation = {
type: "unfire"
id: CellId
colour: PulseColour
}