@turing-machine-js/machine 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,705 @@
1
+ const uniquePredicate = (v, i, a) => a.indexOf(v) === i;
2
+ const idKey = Symbol('idCurrentKey');
3
+ const idWeakMapKey = Symbol('idWeakMapKey');
4
+ function id(object) {
5
+ if (!id[idWeakMapKey].has(object)) {
6
+ id[idWeakMapKey].set(object, id[idKey]);
7
+ id[idKey] += 1;
8
+ }
9
+ return id[idWeakMapKey].get(object);
10
+ }
11
+ id[idKey] = 0;
12
+ id[idWeakMapKey] = new WeakMap();
13
+
14
+ var __classPrivateFieldSet$8 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
15
+ if (kind === "m") throw new TypeError("Private method is not writable");
16
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
17
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
18
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
19
+ };
20
+ var __classPrivateFieldGet$8 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
21
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
22
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
23
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
24
+ };
25
+ var _Alphabet_symbols;
26
+ class Alphabet {
27
+ constructor(symbols) {
28
+ _Alphabet_symbols.set(this, void 0);
29
+ if (symbols instanceof Alphabet) {
30
+ symbols = symbols.symbols;
31
+ }
32
+ const uniqueSymbols = symbols.filter(uniquePredicate);
33
+ if (uniqueSymbols.length < 2) {
34
+ throw new Error('Invalid symbols length');
35
+ }
36
+ const isSymbolsValid = uniqueSymbols.every((symbol) => symbol.length === 1);
37
+ if (!isSymbolsValid) {
38
+ throw new Error('symbols contains invalid symbol');
39
+ }
40
+ __classPrivateFieldSet$8(this, _Alphabet_symbols, Array.from(uniqueSymbols), "f");
41
+ }
42
+ get symbols() {
43
+ return Array.from(__classPrivateFieldGet$8(this, _Alphabet_symbols, "f"));
44
+ }
45
+ get blankSymbol() {
46
+ return __classPrivateFieldGet$8(this, _Alphabet_symbols, "f")[0];
47
+ }
48
+ has(symbol) {
49
+ return __classPrivateFieldGet$8(this, _Alphabet_symbols, "f").indexOf(symbol) >= 0;
50
+ }
51
+ get(index) {
52
+ if (index < 0 || index >= __classPrivateFieldGet$8(this, _Alphabet_symbols, "f").length) {
53
+ throw new Error('Invalid index');
54
+ }
55
+ return __classPrivateFieldGet$8(this, _Alphabet_symbols, "f")[index];
56
+ }
57
+ index(symbol) {
58
+ return __classPrivateFieldGet$8(this, _Alphabet_symbols, "f").indexOf(symbol);
59
+ }
60
+ }
61
+ _Alphabet_symbols = new WeakMap();
62
+
63
+ var __classPrivateFieldSet$7 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
64
+ if (kind === "m") throw new TypeError("Private method is not writable");
65
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
66
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
67
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
68
+ };
69
+ var __classPrivateFieldGet$7 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
70
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
71
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
72
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
73
+ };
74
+ var _TapeCommand_movement, _TapeCommand_symbol;
75
+ const movements = {
76
+ left: Symbol('move caret left command'),
77
+ right: Symbol('move caret right command'),
78
+ stay: Symbol('do not move carer'),
79
+ };
80
+ const symbolCommands = {
81
+ erase: Symbol('erase symbol command'),
82
+ keep: Symbol('keep symbol command'),
83
+ };
84
+ class TapeCommand {
85
+ constructor({ movement = movements.stay, symbol = symbolCommands.keep, }) {
86
+ _TapeCommand_movement.set(this, void 0);
87
+ _TapeCommand_symbol.set(this, void 0);
88
+ const isValidMovement = [movements.left, movements.stay, movements.right].includes(movement);
89
+ if (!isValidMovement) {
90
+ throw new Error('invalid movement');
91
+ }
92
+ __classPrivateFieldSet$7(this, _TapeCommand_movement, movement, "f");
93
+ const isValidSymbol = ((typeof symbol === 'string' && symbol.length === 1)
94
+ || [symbolCommands.keep, symbolCommands.erase].includes(symbol));
95
+ if (!isValidSymbol) {
96
+ throw new Error('invalid symbol');
97
+ }
98
+ __classPrivateFieldSet$7(this, _TapeCommand_symbol, symbol, "f");
99
+ }
100
+ get symbol() {
101
+ return __classPrivateFieldGet$7(this, _TapeCommand_symbol, "f");
102
+ }
103
+ get movement() {
104
+ return __classPrivateFieldGet$7(this, _TapeCommand_movement, "f");
105
+ }
106
+ }
107
+ _TapeCommand_movement = new WeakMap(), _TapeCommand_symbol = new WeakMap();
108
+
109
+ var __classPrivateFieldSet$6 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
110
+ if (kind === "m") throw new TypeError("Private method is not writable");
111
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
112
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
113
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
114
+ };
115
+ var __classPrivateFieldGet$6 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
116
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
117
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
118
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
119
+ };
120
+ var _Command_tapesCommands;
121
+ class Command {
122
+ constructor(tapesCommands) {
123
+ _Command_tapesCommands.set(this, void 0);
124
+ if (tapesCommands.length === 0) {
125
+ throw new Error('invalid parameter');
126
+ }
127
+ try {
128
+ __classPrivateFieldSet$6(this, _Command_tapesCommands, tapesCommands.map((tapeCommand) => {
129
+ if (tapeCommand instanceof TapeCommand) {
130
+ return tapeCommand;
131
+ }
132
+ return new TapeCommand(tapeCommand);
133
+ }), "f");
134
+ }
135
+ catch (error) {
136
+ throw new Error('invalid tapeCommand');
137
+ }
138
+ }
139
+ get tapesCommands() {
140
+ return [...__classPrivateFieldGet$6(this, _Command_tapesCommands, "f")];
141
+ }
142
+ }
143
+ _Command_tapesCommands = new WeakMap();
144
+
145
+ var __classPrivateFieldGet$5 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
146
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
147
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
148
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
149
+ };
150
+ var __classPrivateFieldSet$5 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
151
+ if (kind === "m") throw new TypeError("Private method is not writable");
152
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
153
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
154
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
155
+ };
156
+ var _Reference_referenceBinding;
157
+ class Reference {
158
+ constructor() {
159
+ _Reference_referenceBinding.set(this, null);
160
+ }
161
+ get ref() {
162
+ if (!__classPrivateFieldGet$5(this, _Reference_referenceBinding, "f")) {
163
+ throw new Error('unbounded reference');
164
+ }
165
+ return __classPrivateFieldGet$5(this, _Reference_referenceBinding, "f");
166
+ }
167
+ bind(state) {
168
+ if (__classPrivateFieldGet$5(this, _Reference_referenceBinding, "f") == null) {
169
+ __classPrivateFieldSet$5(this, _Reference_referenceBinding, state, "f");
170
+ }
171
+ return __classPrivateFieldGet$5(this, _Reference_referenceBinding, "f");
172
+ }
173
+ }
174
+ _Reference_referenceBinding = new WeakMap();
175
+
176
+ var __classPrivateFieldGet$4 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
177
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
178
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
179
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
180
+ };
181
+ var __classPrivateFieldSet$4 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
182
+ if (kind === "m") throw new TypeError("Private method is not writable");
183
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
184
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
185
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
186
+ };
187
+ var _State_id, _State_name, _State_overrodeHaltState, _State_symbolToDataMap;
188
+ const ifOtherSymbol = Symbol('other symbol');
189
+ class State {
190
+ constructor(stateDefinition = null, name) {
191
+ _State_id.set(this, id(this));
192
+ _State_name.set(this, void 0);
193
+ _State_overrodeHaltState.set(this, null);
194
+ _State_symbolToDataMap.set(this, new Map());
195
+ if (stateDefinition) {
196
+ const keys = Object.getOwnPropertyNames(stateDefinition);
197
+ if (keys.length) {
198
+ throw new Error(`invalid state definition while constructing state #${__classPrivateFieldGet$4(this, _State_id, "f")}`);
199
+ }
200
+ const symbols = Object.getOwnPropertySymbols(stateDefinition);
201
+ if (symbols.length === 0) {
202
+ throw new Error(`invalid state definition while constructing state #${__classPrivateFieldGet$4(this, _State_id, "f")}`);
203
+ }
204
+ symbols.forEach((symbol) => {
205
+ const { nextState } = stateDefinition[symbol];
206
+ const nextStateLocal = nextState !== null && nextState !== void 0 ? nextState : this;
207
+ if (!(nextStateLocal instanceof State) && !(nextStateLocal instanceof Reference)) {
208
+ throw new Error('invalid nextState');
209
+ }
210
+ let { command } = stateDefinition[symbol];
211
+ if (command == null) {
212
+ command = new Command([
213
+ new TapeCommand({}),
214
+ ]);
215
+ }
216
+ if (!(command instanceof Command) && !Array.isArray(command)) {
217
+ command = [command];
218
+ }
219
+ let commandLocal = command;
220
+ if (Array.isArray(command)) {
221
+ try {
222
+ commandLocal = new Command(command);
223
+ }
224
+ catch (error) {
225
+ }
226
+ }
227
+ if (!(commandLocal instanceof Command)) {
228
+ throw new Error('invalid command');
229
+ }
230
+ __classPrivateFieldGet$4(this, _State_symbolToDataMap, "f").set(symbol, {
231
+ command: commandLocal,
232
+ nextState: nextStateLocal,
233
+ });
234
+ });
235
+ }
236
+ __classPrivateFieldSet$4(this, _State_name, name !== null && name !== void 0 ? name : `id:${__classPrivateFieldGet$4(this, _State_id, "f")}`, "f");
237
+ }
238
+ get id() {
239
+ return __classPrivateFieldGet$4(this, _State_id, "f");
240
+ }
241
+ get name() {
242
+ return __classPrivateFieldGet$4(this, _State_name, "f");
243
+ }
244
+ get isHalt() {
245
+ return __classPrivateFieldGet$4(this, _State_id, "f") === 0;
246
+ }
247
+ get overrodeHaltState() {
248
+ return __classPrivateFieldGet$4(this, _State_overrodeHaltState, "f");
249
+ }
250
+ get ref() {
251
+ return this;
252
+ }
253
+ getSymbol(tapeBlock) {
254
+ const symbol = [...__classPrivateFieldGet$4(this, _State_symbolToDataMap, "f").keys()].find((currentSymbol) => tapeBlock.isMatched({
255
+ symbol: currentSymbol,
256
+ }));
257
+ if (symbol) {
258
+ return symbol;
259
+ }
260
+ return ifOtherSymbol;
261
+ }
262
+ getCommand(symbol) {
263
+ if (__classPrivateFieldGet$4(this, _State_symbolToDataMap, "f").has(symbol)) {
264
+ return __classPrivateFieldGet$4(this, _State_symbolToDataMap, "f").get(symbol).command;
265
+ }
266
+ throw new Error(`No command for symbol at state named ${__classPrivateFieldGet$4(this, _State_name, "f")}`);
267
+ }
268
+ getNextState(symbol) {
269
+ if (__classPrivateFieldGet$4(this, _State_symbolToDataMap, "f").has(symbol)) {
270
+ return __classPrivateFieldGet$4(this, _State_symbolToDataMap, "f").get(symbol).nextState;
271
+ }
272
+ throw new Error(`No nextState for symbol at state named ${__classPrivateFieldGet$4(this, _State_id, "f")}`);
273
+ }
274
+ withOverrodeHaltState(overrodeHaltState) {
275
+ const state = new State(null, `${this.name}>${overrodeHaltState.name}`);
276
+ __classPrivateFieldSet$4(state, _State_symbolToDataMap, __classPrivateFieldGet$4(this, _State_symbolToDataMap, "f"), "f");
277
+ __classPrivateFieldSet$4(state, _State_overrodeHaltState, overrodeHaltState, "f");
278
+ return state;
279
+ }
280
+ }
281
+ _State_id = new WeakMap(), _State_name = new WeakMap(), _State_overrodeHaltState = new WeakMap(), _State_symbolToDataMap = new WeakMap();
282
+ const haltState = new State(null);
283
+
284
+ var __classPrivateFieldSet$3 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
285
+ if (kind === "m") throw new TypeError("Private method is not writable");
286
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
287
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
288
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
289
+ };
290
+ var __classPrivateFieldGet$3 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
291
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
292
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
293
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
294
+ };
295
+ var _Tape_alphabet, _Tape_symbols, _Tape_position, _Tape_viewportWidth;
296
+ class Tape {
297
+ constructor({ alphabet, symbols = [], position = 0, viewportWidth = 1, }) {
298
+ _Tape_alphabet.set(this, void 0);
299
+ _Tape_symbols.set(this, void 0);
300
+ _Tape_position.set(this, void 0);
301
+ _Tape_viewportWidth.set(this, void 0);
302
+ const isSymbolsValid = symbols.every((symbol) => alphabet.has(symbol));
303
+ if (!isSymbolsValid) {
304
+ throw new Error('symbolList contains invalid symbol');
305
+ }
306
+ __classPrivateFieldSet$3(this, _Tape_alphabet, new Alphabet(alphabet), "f");
307
+ __classPrivateFieldSet$3(this, _Tape_position, position, "f");
308
+ __classPrivateFieldSet$3(this, _Tape_viewportWidth, viewportWidth, "f");
309
+ const symbolsCopy = Array.from(symbols);
310
+ if (symbolsCopy.length === 0) {
311
+ symbolsCopy.push(__classPrivateFieldGet$3(this, _Tape_alphabet, "f").blankSymbol);
312
+ }
313
+ __classPrivateFieldSet$3(this, _Tape_symbols, symbolsCopy.map((symbol) => __classPrivateFieldGet$3(this, _Tape_alphabet, "f").index(symbol)), "f");
314
+ }
315
+ get alphabet() {
316
+ return __classPrivateFieldGet$3(this, _Tape_alphabet, "f");
317
+ }
318
+ get extraCellsCount() {
319
+ return (__classPrivateFieldGet$3(this, _Tape_viewportWidth, "f") - 1) / 2;
320
+ }
321
+ get position() {
322
+ return __classPrivateFieldGet$3(this, _Tape_position, "f");
323
+ }
324
+ get symbol() {
325
+ return __classPrivateFieldGet$3(this, _Tape_alphabet, "f").get(__classPrivateFieldGet$3(this, _Tape_symbols, "f")[__classPrivateFieldGet$3(this, _Tape_position, "f")]);
326
+ }
327
+ set symbol(symbol) {
328
+ if (!__classPrivateFieldGet$3(this, _Tape_alphabet, "f").has(symbol)) {
329
+ throw new Error('Invalid symbol');
330
+ }
331
+ __classPrivateFieldGet$3(this, _Tape_symbols, "f")[__classPrivateFieldGet$3(this, _Tape_position, "f")] = __classPrivateFieldGet$3(this, _Tape_alphabet, "f").index(symbol);
332
+ }
333
+ get symbols() {
334
+ return __classPrivateFieldGet$3(this, _Tape_symbols, "f")
335
+ .map((index) => __classPrivateFieldGet$3(this, _Tape_alphabet, "f").get(index));
336
+ }
337
+ get viewport() {
338
+ const startIx = __classPrivateFieldGet$3(this, _Tape_position, "f") - this.extraCellsCount;
339
+ const endIx = __classPrivateFieldGet$3(this, _Tape_position, "f") + this.extraCellsCount + 1;
340
+ return __classPrivateFieldGet$3(this, _Tape_symbols, "f")
341
+ .slice(startIx, endIx)
342
+ .map((index) => __classPrivateFieldGet$3(this, _Tape_alphabet, "f").get(index));
343
+ }
344
+ get viewportWidth() {
345
+ return __classPrivateFieldGet$3(this, _Tape_viewportWidth, "f");
346
+ }
347
+ set viewportWidth(width) {
348
+ let finalWidth = width;
349
+ if (finalWidth < 1) {
350
+ throw new Error('Invalid viewportWidth');
351
+ }
352
+ if (finalWidth % 2 === 0) {
353
+ finalWidth += 1;
354
+ }
355
+ __classPrivateFieldSet$3(this, _Tape_viewportWidth, finalWidth, "f");
356
+ this.normalise();
357
+ }
358
+ left() {
359
+ __classPrivateFieldSet$3(this, _Tape_position, __classPrivateFieldGet$3(this, _Tape_position, "f") - 1, "f");
360
+ this.normalise();
361
+ }
362
+ normalise() {
363
+ while (__classPrivateFieldGet$3(this, _Tape_position, "f") - this.extraCellsCount < 0) {
364
+ __classPrivateFieldGet$3(this, _Tape_symbols, "f").unshift(0);
365
+ __classPrivateFieldSet$3(this, _Tape_position, __classPrivateFieldGet$3(this, _Tape_position, "f") + 1, "f");
366
+ }
367
+ while (__classPrivateFieldGet$3(this, _Tape_position, "f") + this.extraCellsCount >= __classPrivateFieldGet$3(this, _Tape_symbols, "f").length) {
368
+ __classPrivateFieldGet$3(this, _Tape_symbols, "f").push(0);
369
+ }
370
+ }
371
+ right() {
372
+ __classPrivateFieldSet$3(this, _Tape_position, __classPrivateFieldGet$3(this, _Tape_position, "f") + 1, "f");
373
+ this.normalise();
374
+ }
375
+ }
376
+ _Tape_alphabet = new WeakMap(), _Tape_symbols = new WeakMap(), _Tape_position = new WeakMap(), _Tape_viewportWidth = new WeakMap();
377
+
378
+ var __classPrivateFieldGet$2 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
379
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
380
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
381
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
382
+ };
383
+ var __classPrivateFieldSet$2 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
384
+ if (kind === "m") throw new TypeError("Private method is not writable");
385
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
386
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
387
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
388
+ };
389
+ var _Lock_lockSymbol;
390
+ class Lock {
391
+ constructor() {
392
+ _Lock_lockSymbol.set(this, null);
393
+ }
394
+ lock(symbol) {
395
+ if (__classPrivateFieldGet$2(this, _Lock_lockSymbol, "f") === null) {
396
+ __classPrivateFieldSet$2(this, _Lock_lockSymbol, symbol, "f");
397
+ }
398
+ }
399
+ unlock(symbol) {
400
+ if (__classPrivateFieldGet$2(this, _Lock_lockSymbol, "f") === symbol) {
401
+ __classPrivateFieldSet$2(this, _Lock_lockSymbol, null, "f");
402
+ }
403
+ }
404
+ check(symbol) {
405
+ if (__classPrivateFieldGet$2(this, _Lock_lockSymbol, "f") && __classPrivateFieldGet$2(this, _Lock_lockSymbol, "f") !== symbol) {
406
+ throw new Error('Lock check failed');
407
+ }
408
+ }
409
+ }
410
+ _Lock_lockSymbol = new WeakMap();
411
+
412
+ var __classPrivateFieldSet$1 = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
413
+ if (kind === "m") throw new TypeError("Private method is not writable");
414
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
415
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
416
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
417
+ };
418
+ var __classPrivateFieldGet$1 = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
419
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
420
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
421
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
422
+ };
423
+ var _a, _TapeBlock_symbolToPatternListMap, _TapeBlock_lock, _TapeBlock_tapes, _TapeBlock_generateSymbolHint, _TapeBlock_buildPatternList, _TapeBlock_getSymbolForPatternList, _TapeBlock_symbol;
424
+ const symbolToPatternListMapSymbol = Symbol('symbol for symbolToPatternListMap setter');
425
+ const lockSymbol = Symbol('capture symbol');
426
+ class TapeBlock {
427
+ constructor(argument) {
428
+ _TapeBlock_symbolToPatternListMap.set(this, new Map());
429
+ _TapeBlock_lock.set(this, new Lock());
430
+ _TapeBlock_tapes.set(this, void 0);
431
+ _TapeBlock_buildPatternList.set(this, (symbolList) => symbolList.reduce((result, symbol, ix) => {
432
+ const row = Math.floor(ix / __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").length);
433
+ if (!Array.isArray(result[row])) {
434
+ result[row] = [];
435
+ }
436
+ result[row].push(symbol);
437
+ return result;
438
+ }, [])
439
+ .filter((pattern, ix, patternList) => {
440
+ const samePatternIx = patternList.findIndex((otherPattern) => (pattern
441
+ .every((symbol, symbolIx) => symbol === otherPattern[symbolIx])));
442
+ return samePatternIx === ix;
443
+ }));
444
+ _TapeBlock_getSymbolForPatternList.set(this, (patternList) => {
445
+ if (patternList.some((pattern) => pattern.every((symbol) => symbol === ifOtherSymbol))) {
446
+ return ifOtherSymbol;
447
+ }
448
+ const [storedPatternListSymbol] = [...__classPrivateFieldGet$1(this, _TapeBlock_symbolToPatternListMap, "f").entries()]
449
+ .find(([, storedPatternList]) => {
450
+ if (storedPatternList.length !== patternList.length) {
451
+ return false;
452
+ }
453
+ return patternList
454
+ .every((pattern, patternIx) => pattern
455
+ .every((symbol, symbolIx) => symbol === storedPatternList[patternIx][symbolIx]));
456
+ }) || [null, null];
457
+ let symbol;
458
+ if (storedPatternListSymbol) {
459
+ symbol = storedPatternListSymbol;
460
+ }
461
+ else {
462
+ symbol = Symbol(__classPrivateFieldGet$1(_a, _a, "f", _TapeBlock_generateSymbolHint).call(_a, patternList));
463
+ __classPrivateFieldGet$1(this, _TapeBlock_symbolToPatternListMap, "f").set(symbol, patternList);
464
+ }
465
+ return symbol;
466
+ });
467
+ _TapeBlock_symbol.set(this, (symbols) => {
468
+ let localSymbols = [];
469
+ if (symbols === ifOtherSymbol) {
470
+ return ifOtherSymbol;
471
+ }
472
+ if (typeof symbols === 'string') {
473
+ localSymbols = symbols.split('');
474
+ }
475
+ else if (Array.isArray(symbols)) {
476
+ localSymbols = [...symbols];
477
+ }
478
+ if (localSymbols.length === 0 || localSymbols.length % __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").length > 0) {
479
+ throw new Error('invalid symbol parameter');
480
+ }
481
+ const invalidSymbolIndex = localSymbols.findIndex((symbol, ix) => (symbol !== ifOtherSymbol
482
+ && !__classPrivateFieldGet$1(this, _TapeBlock_tapes, "f")[ix % __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").length].alphabet.has(symbol)));
483
+ if (invalidSymbolIndex >= 0) {
484
+ throw new Error('invalid symbol parameter');
485
+ }
486
+ if (localSymbols.every((symbol) => symbol === ifOtherSymbol)) {
487
+ return ifOtherSymbol;
488
+ }
489
+ return __classPrivateFieldGet$1(this, _TapeBlock_getSymbolForPatternList, "f").call(this, __classPrivateFieldGet$1(this, _TapeBlock_buildPatternList, "f").call(this, localSymbols));
490
+ });
491
+ __classPrivateFieldSet$1(this, _TapeBlock_tapes, [], "f");
492
+ if ('alphabets' in argument) {
493
+ const { alphabets } = argument;
494
+ if (alphabets.length === 0) {
495
+ throw new Error('empty alphabet list');
496
+ }
497
+ __classPrivateFieldSet$1(this, _TapeBlock_tapes, alphabets.map((alphabet) => new Tape({
498
+ alphabet,
499
+ })), "f");
500
+ }
501
+ else if ('tapes' in argument) {
502
+ __classPrivateFieldSet$1(this, _TapeBlock_tapes, argument.tapes, "f");
503
+ }
504
+ if (__classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").length === 0) {
505
+ throw new Error('empty tape list');
506
+ }
507
+ }
508
+ get alphabets() {
509
+ return [...__classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").map((tape) => tape.alphabet)];
510
+ }
511
+ get currentSymbols() {
512
+ return __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").map((tape) => tape.symbol);
513
+ }
514
+ get [(_TapeBlock_symbolToPatternListMap = new WeakMap(), _TapeBlock_lock = new WeakMap(), _TapeBlock_tapes = new WeakMap(), _TapeBlock_buildPatternList = new WeakMap(), _TapeBlock_getSymbolForPatternList = new WeakMap(), _TapeBlock_symbol = new WeakMap(), lockSymbol)]() {
515
+ return __classPrivateFieldGet$1(this, _TapeBlock_lock, "f");
516
+ }
517
+ get symbol() {
518
+ return __classPrivateFieldGet$1(this, _TapeBlock_symbol, "f").bind(this);
519
+ }
520
+ get tapes() {
521
+ return [...__classPrivateFieldGet$1(this, _TapeBlock_tapes, "f")];
522
+ }
523
+ set [symbolToPatternListMapSymbol](symbolToPatternListMap) {
524
+ __classPrivateFieldSet$1(this, _TapeBlock_symbolToPatternListMap, new Map(symbolToPatternListMap), "f");
525
+ }
526
+ applyCommand(command, executionSymbol = null) {
527
+ __classPrivateFieldGet$1(this, _TapeBlock_lock, "f").check(executionSymbol);
528
+ if (__classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").length !== command.tapesCommands.length) {
529
+ throw new Error('invalid command');
530
+ }
531
+ __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f").forEach((tape, ix) => {
532
+ const { movement, symbol } = command.tapesCommands[ix];
533
+ if (typeof symbol === 'string') {
534
+ tape.symbol = symbol;
535
+ }
536
+ if (typeof symbol === 'symbol') {
537
+ switch (symbol) {
538
+ case symbolCommands.keep:
539
+ break;
540
+ case symbolCommands.erase:
541
+ tape.symbol = tape.alphabet.blankSymbol;
542
+ break;
543
+ // no default
544
+ }
545
+ }
546
+ switch (movement) {
547
+ case movements.left:
548
+ tape.left();
549
+ break;
550
+ case movements.stay:
551
+ break;
552
+ case movements.right:
553
+ tape.right();
554
+ break;
555
+ // no default
556
+ }
557
+ });
558
+ }
559
+ clone(cloneTapes = false) {
560
+ let tapeBlock;
561
+ if (cloneTapes) {
562
+ tapeBlock = _a.fromTapes(this.tapes.map((tape) => new Tape(tape)));
563
+ }
564
+ else {
565
+ tapeBlock = _a.fromAlphabets(this.alphabets);
566
+ }
567
+ tapeBlock[symbolToPatternListMapSymbol] = __classPrivateFieldGet$1(this, _TapeBlock_symbolToPatternListMap, "f");
568
+ return tapeBlock;
569
+ }
570
+ isMatched({ currentSymbols = this.currentSymbols, symbol }) {
571
+ var _b;
572
+ if (symbol === ifOtherSymbol) {
573
+ return true;
574
+ }
575
+ if (!__classPrivateFieldGet$1(this, _TapeBlock_symbolToPatternListMap, "f").has(symbol)) {
576
+ throw new Error('invalid symbol');
577
+ }
578
+ const patternList = __classPrivateFieldGet$1(this, _TapeBlock_symbolToPatternListMap, "f").get(symbol);
579
+ return (_b = patternList === null || patternList === void 0 ? void 0 : patternList.some((pattern) => (pattern
580
+ .every((everySymbol, ix) => (everySymbol === ifOtherSymbol
581
+ || everySymbol === currentSymbols[ix]))))) !== null && _b !== void 0 ? _b : false;
582
+ }
583
+ replaceTape(tape, tapeIx = 0) {
584
+ if (__classPrivateFieldGet$1(this, _TapeBlock_tapes, "f")[tapeIx] == null) {
585
+ throw new Error('invalid tapeIx');
586
+ }
587
+ if (tape.alphabet.symbols.join('') === __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f")[tapeIx].alphabet.symbols.join('')) {
588
+ __classPrivateFieldGet$1(this, _TapeBlock_tapes, "f")[tapeIx] = tape;
589
+ }
590
+ else {
591
+ throw new Error('invalid tape');
592
+ }
593
+ }
594
+ }
595
+ _a = TapeBlock;
596
+ TapeBlock.fromAlphabets = (alphabets) => {
597
+ return new _a({ alphabets });
598
+ };
599
+ TapeBlock.fromTapes = (tapes) => {
600
+ return new _a({ tapes });
601
+ };
602
+ _TapeBlock_generateSymbolHint = { value: (patternList) => JSON.stringify(patternList
603
+ .map((pattern) => pattern
604
+ .map((symbol) => (symbol === ifOtherSymbol ? null : symbol)))) };
605
+
606
+ var __classPrivateFieldSet = (undefined && undefined.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
607
+ if (kind === "m") throw new TypeError("Private method is not writable");
608
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
609
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
610
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
611
+ };
612
+ var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
613
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
614
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
615
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
616
+ };
617
+ var _TuringMachine_tapeBlock, _TuringMachine_stack;
618
+ class TuringMachine {
619
+ constructor({ tapeBlock, } = {}) {
620
+ _TuringMachine_tapeBlock.set(this, void 0);
621
+ _TuringMachine_stack.set(this, []);
622
+ if (!tapeBlock) {
623
+ throw new Error('invalid tapeBlock');
624
+ }
625
+ __classPrivateFieldSet(this, _TuringMachine_tapeBlock, tapeBlock, "f");
626
+ }
627
+ get tapeBlock() {
628
+ return __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f");
629
+ }
630
+ run({ initialState, stepsLimit = 1e5, onStep }) {
631
+ const generator = this.runStepByStep({ initialState, stepsLimit });
632
+ for (const machineState of generator) {
633
+ if (onStep instanceof Function) {
634
+ onStep(machineState);
635
+ }
636
+ }
637
+ }
638
+ *runStepByStep({ initialState, stepsLimit = 1e5 }) {
639
+ const executionSymbol = Symbol('execution');
640
+ try {
641
+ __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f")[lockSymbol].check(executionSymbol);
642
+ __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f")[lockSymbol].lock(executionSymbol);
643
+ const stack = __classPrivateFieldGet(this, _TuringMachine_stack, "f");
644
+ let state = initialState;
645
+ if (state.overrodeHaltState) {
646
+ stack.push(state.overrodeHaltState);
647
+ }
648
+ let i = 0;
649
+ while (!state.isHalt) {
650
+ if (i === stepsLimit) {
651
+ throw new Error('Long execution');
652
+ }
653
+ i += 1;
654
+ const symbol = state.getSymbol(__classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f"));
655
+ const command = state.getCommand(symbol);
656
+ let nextState = state.getNextState(symbol).ref;
657
+ try {
658
+ const nextStateForYield = nextState.isHalt && stack.length
659
+ ? stack.slice(-1)[0]
660
+ : nextState;
661
+ yield {
662
+ step: i,
663
+ state,
664
+ currentSymbols: __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").currentSymbols,
665
+ nextSymbols: command.tapesCommands.map((tapeCommand, ix) => {
666
+ if (typeof tapeCommand.symbol === 'symbol') {
667
+ switch (tapeCommand.symbol) {
668
+ case symbolCommands.erase:
669
+ return __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").tapes[ix].alphabet.blankSymbol;
670
+ case symbolCommands.keep:
671
+ return __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").tapes[ix].symbol;
672
+ default:
673
+ throw new Error('invalid symbol command');
674
+ }
675
+ }
676
+ return tapeCommand.symbol;
677
+ }),
678
+ movements: command.tapesCommands.map((tapeCommand) => tapeCommand.movement),
679
+ nextState: nextStateForYield,
680
+ };
681
+ __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").applyCommand(command, executionSymbol);
682
+ if (nextState.isHalt && stack.length) {
683
+ nextState = stack.pop();
684
+ }
685
+ if (state !== nextState && nextState.overrodeHaltState) {
686
+ stack.push(nextState.overrodeHaltState);
687
+ }
688
+ state = nextState;
689
+ }
690
+ catch (error) {
691
+ if (error !== haltState) {
692
+ throw error;
693
+ }
694
+ break;
695
+ }
696
+ }
697
+ }
698
+ finally {
699
+ __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f")[lockSymbol].unlock(executionSymbol);
700
+ }
701
+ }
702
+ }
703
+ _TuringMachine_tapeBlock = new WeakMap(), _TuringMachine_stack = new WeakMap();
704
+
705
+ export { Alphabet, Command, Reference, State, Tape, TapeBlock, TapeCommand, TuringMachine, haltState, ifOtherSymbol, movements, symbolCommands };