@turing-machine-js/machine 2.0.2 → 3.0.1

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.
@@ -1,198 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- 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");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _a, _TapeBlock_symbolToPatternListMap, _TapeBlock_lock, _TapeBlock_tapes, _TapeBlock_generateSymbolHint, _TapeBlock_buildPatternList, _TapeBlock_getSymbolForPatternList, _TapeBlock_symbol;
13
- import Tape from './Tape';
14
- import { ifOtherSymbol } from './State';
15
- import { movements, symbolCommands } from './TapeCommand';
16
- import Lock from './Lock';
17
- const symbolToPatternListMapSymbol = Symbol('symbol for symbolToPatternListMap setter');
18
- export const lockSymbol = Symbol('capture symbol');
19
- class TapeBlock {
20
- constructor(argument) {
21
- _TapeBlock_symbolToPatternListMap.set(this, new Map());
22
- _TapeBlock_lock.set(this, new Lock());
23
- _TapeBlock_tapes.set(this, void 0);
24
- _TapeBlock_buildPatternList.set(this, (symbolList) => symbolList.reduce((result, symbol, ix) => {
25
- const row = Math.floor(ix / __classPrivateFieldGet(this, _TapeBlock_tapes, "f").length);
26
- if (!Array.isArray(result[row])) {
27
- result[row] = [];
28
- }
29
- result[row].push(symbol);
30
- return result;
31
- }, [])
32
- .filter((pattern, ix, patternList) => {
33
- const samePatternIx = patternList.findIndex((otherPattern) => (pattern
34
- .every((symbol, symbolIx) => symbol === otherPattern[symbolIx])));
35
- return samePatternIx === ix;
36
- }));
37
- _TapeBlock_getSymbolForPatternList.set(this, (patternList) => {
38
- if (patternList.some((pattern) => pattern.every((symbol) => symbol === ifOtherSymbol))) {
39
- return ifOtherSymbol;
40
- }
41
- const [storedPatternListSymbol] = [...__classPrivateFieldGet(this, _TapeBlock_symbolToPatternListMap, "f").entries()]
42
- .find(([, storedPatternList]) => {
43
- if (storedPatternList.length !== patternList.length) {
44
- return false;
45
- }
46
- return patternList
47
- .every((pattern, patternIx) => pattern
48
- .every((symbol, symbolIx) => symbol === storedPatternList[patternIx][symbolIx]));
49
- }) || [null, null];
50
- let symbol;
51
- if (storedPatternListSymbol) {
52
- symbol = storedPatternListSymbol;
53
- }
54
- else {
55
- symbol = Symbol(__classPrivateFieldGet(_a, _a, "f", _TapeBlock_generateSymbolHint).call(_a, patternList));
56
- __classPrivateFieldGet(this, _TapeBlock_symbolToPatternListMap, "f").set(symbol, patternList);
57
- }
58
- return symbol;
59
- });
60
- _TapeBlock_symbol.set(this, (symbols) => {
61
- let localSymbols = [];
62
- if (symbols === ifOtherSymbol) {
63
- return ifOtherSymbol;
64
- }
65
- if (typeof symbols === 'string') {
66
- localSymbols = symbols.split('');
67
- }
68
- else if (Array.isArray(symbols)) {
69
- localSymbols = [...symbols];
70
- }
71
- if (localSymbols.length === 0 || localSymbols.length % __classPrivateFieldGet(this, _TapeBlock_tapes, "f").length > 0) {
72
- throw new Error('invalid symbol parameter');
73
- }
74
- const invalidSymbolIndex = localSymbols.findIndex((symbol, ix) => (symbol !== ifOtherSymbol
75
- && !__classPrivateFieldGet(this, _TapeBlock_tapes, "f")[ix % __classPrivateFieldGet(this, _TapeBlock_tapes, "f").length].alphabet.has(symbol)));
76
- if (invalidSymbolIndex >= 0) {
77
- throw new Error('invalid symbol parameter');
78
- }
79
- if (localSymbols.every((symbol) => symbol === ifOtherSymbol)) {
80
- return ifOtherSymbol;
81
- }
82
- return __classPrivateFieldGet(this, _TapeBlock_getSymbolForPatternList, "f").call(this, __classPrivateFieldGet(this, _TapeBlock_buildPatternList, "f").call(this, localSymbols));
83
- });
84
- __classPrivateFieldSet(this, _TapeBlock_tapes, [], "f");
85
- if ('alphabets' in argument) {
86
- const { alphabets } = argument;
87
- if (alphabets.length === 0) {
88
- throw new Error('empty alphabet list');
89
- }
90
- __classPrivateFieldSet(this, _TapeBlock_tapes, alphabets.map((alphabet) => new Tape({
91
- alphabet,
92
- })), "f");
93
- }
94
- else if ('tapes' in argument) {
95
- __classPrivateFieldSet(this, _TapeBlock_tapes, argument.tapes, "f");
96
- }
97
- if (__classPrivateFieldGet(this, _TapeBlock_tapes, "f").length === 0) {
98
- throw new Error('empty tape list');
99
- }
100
- }
101
- get alphabets() {
102
- return [...__classPrivateFieldGet(this, _TapeBlock_tapes, "f").map((tape) => tape.alphabet)];
103
- }
104
- get currentSymbols() {
105
- return __classPrivateFieldGet(this, _TapeBlock_tapes, "f").map((tape) => tape.symbol);
106
- }
107
- 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)]() {
108
- return __classPrivateFieldGet(this, _TapeBlock_lock, "f");
109
- }
110
- get symbol() {
111
- return __classPrivateFieldGet(this, _TapeBlock_symbol, "f").bind(this);
112
- }
113
- get tapes() {
114
- return [...__classPrivateFieldGet(this, _TapeBlock_tapes, "f")];
115
- }
116
- set [symbolToPatternListMapSymbol](symbolToPatternListMap) {
117
- __classPrivateFieldSet(this, _TapeBlock_symbolToPatternListMap, new Map(symbolToPatternListMap), "f");
118
- }
119
- applyCommand(command, executionSymbol = null) {
120
- __classPrivateFieldGet(this, _TapeBlock_lock, "f").check(executionSymbol);
121
- if (__classPrivateFieldGet(this, _TapeBlock_tapes, "f").length !== command.tapesCommands.length) {
122
- throw new Error('invalid command');
123
- }
124
- __classPrivateFieldGet(this, _TapeBlock_tapes, "f").forEach((tape, ix) => {
125
- const { movement, symbol } = command.tapesCommands[ix];
126
- if (typeof symbol === 'string') {
127
- tape.symbol = symbol;
128
- }
129
- if (typeof symbol === 'symbol') {
130
- switch (symbol) {
131
- case symbolCommands.keep:
132
- break;
133
- case symbolCommands.erase:
134
- tape.symbol = tape.alphabet.blankSymbol;
135
- break;
136
- // no default
137
- }
138
- }
139
- switch (movement) {
140
- case movements.left:
141
- tape.left();
142
- break;
143
- case movements.stay:
144
- break;
145
- case movements.right:
146
- tape.right();
147
- break;
148
- // no default
149
- }
150
- });
151
- }
152
- clone(cloneTapes = false) {
153
- let tapeBlock;
154
- if (cloneTapes) {
155
- tapeBlock = _a.fromTapes(this.tapes.map((tape) => new Tape(tape)));
156
- }
157
- else {
158
- tapeBlock = _a.fromAlphabets(this.alphabets);
159
- }
160
- tapeBlock[symbolToPatternListMapSymbol] = __classPrivateFieldGet(this, _TapeBlock_symbolToPatternListMap, "f");
161
- return tapeBlock;
162
- }
163
- isMatched({ currentSymbols = this.currentSymbols, symbol }) {
164
- var _b;
165
- if (symbol === ifOtherSymbol) {
166
- return true;
167
- }
168
- if (!__classPrivateFieldGet(this, _TapeBlock_symbolToPatternListMap, "f").has(symbol)) {
169
- throw new Error('invalid symbol');
170
- }
171
- const patternList = __classPrivateFieldGet(this, _TapeBlock_symbolToPatternListMap, "f").get(symbol);
172
- return (_b = patternList === null || patternList === void 0 ? void 0 : patternList.some((pattern) => (pattern
173
- .every((everySymbol, ix) => (everySymbol === ifOtherSymbol
174
- || everySymbol === currentSymbols[ix]))))) !== null && _b !== void 0 ? _b : false;
175
- }
176
- replaceTape(tape, tapeIx = 0) {
177
- if (__classPrivateFieldGet(this, _TapeBlock_tapes, "f")[tapeIx] == null) {
178
- throw new Error('invalid tapeIx');
179
- }
180
- if (tape.alphabet.symbols.join('') === __classPrivateFieldGet(this, _TapeBlock_tapes, "f")[tapeIx].alphabet.symbols.join('')) {
181
- __classPrivateFieldGet(this, _TapeBlock_tapes, "f")[tapeIx] = tape;
182
- }
183
- else {
184
- throw new Error('invalid tape');
185
- }
186
- }
187
- }
188
- _a = TapeBlock;
189
- TapeBlock.fromAlphabets = (alphabets) => {
190
- return new _a({ alphabets });
191
- };
192
- TapeBlock.fromTapes = (tapes) => {
193
- return new _a({ tapes });
194
- };
195
- _TapeBlock_generateSymbolHint = { value: (patternList) => JSON.stringify(patternList
196
- .map((pattern) => pattern
197
- .map((symbol) => (symbol === ifOtherSymbol ? null : symbol)))) };
198
- export default TapeBlock;
@@ -1,46 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- 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");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _TapeCommand_movement, _TapeCommand_symbol;
13
- export const movements = {
14
- left: Symbol('move caret left command'),
15
- right: Symbol('move caret right command'),
16
- stay: Symbol('do not move carer'),
17
- };
18
- export const symbolCommands = {
19
- erase: Symbol('erase symbol command'),
20
- keep: Symbol('keep symbol command'),
21
- };
22
- class TapeCommand {
23
- constructor({ movement = movements.stay, symbol = symbolCommands.keep, }) {
24
- _TapeCommand_movement.set(this, void 0);
25
- _TapeCommand_symbol.set(this, void 0);
26
- const isValidMovement = [movements.left, movements.stay, movements.right].includes(movement);
27
- if (!isValidMovement) {
28
- throw new Error('invalid movement');
29
- }
30
- __classPrivateFieldSet(this, _TapeCommand_movement, movement, "f");
31
- const isValidSymbol = ((typeof symbol === 'string' && symbol.length === 1)
32
- || [symbolCommands.keep, symbolCommands.erase].includes(symbol));
33
- if (!isValidSymbol) {
34
- throw new Error('invalid symbol');
35
- }
36
- __classPrivateFieldSet(this, _TapeCommand_symbol, symbol, "f");
37
- }
38
- get symbol() {
39
- return __classPrivateFieldGet(this, _TapeCommand_symbol, "f");
40
- }
41
- get movement() {
42
- return __classPrivateFieldGet(this, _TapeCommand_movement, "f");
43
- }
44
- }
45
- _TapeCommand_movement = new WeakMap(), _TapeCommand_symbol = new WeakMap();
46
- export default TapeCommand;
@@ -1,102 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- 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");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _TuringMachine_tapeBlock, _TuringMachine_stack;
13
- import { haltState } from './State';
14
- import { lockSymbol } from './TapeBlock';
15
- import { symbolCommands } from './TapeCommand';
16
- class TuringMachine {
17
- constructor({ tapeBlock, } = {}) {
18
- _TuringMachine_tapeBlock.set(this, void 0);
19
- _TuringMachine_stack.set(this, []);
20
- if (!tapeBlock) {
21
- throw new Error('invalid tapeBlock');
22
- }
23
- __classPrivateFieldSet(this, _TuringMachine_tapeBlock, tapeBlock, "f");
24
- }
25
- get tapeBlock() {
26
- return __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f");
27
- }
28
- run({ initialState, stepsLimit = 1e5, onStep }) {
29
- const generator = this.runStepByStep({ initialState, stepsLimit });
30
- for (const machineState of generator) {
31
- if (onStep instanceof Function) {
32
- onStep(machineState);
33
- }
34
- }
35
- }
36
- *runStepByStep({ initialState, stepsLimit = 1e5 }) {
37
- const executionSymbol = Symbol('execution');
38
- try {
39
- __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f")[lockSymbol].check(executionSymbol);
40
- __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f")[lockSymbol].lock(executionSymbol);
41
- const stack = __classPrivateFieldGet(this, _TuringMachine_stack, "f");
42
- let state = initialState;
43
- if (state.overrodeHaltState) {
44
- stack.push(state.overrodeHaltState);
45
- }
46
- let i = 0;
47
- while (!state.isHalt) {
48
- if (i === stepsLimit) {
49
- throw new Error('Long execution');
50
- }
51
- i += 1;
52
- const symbol = state.getSymbol(__classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f"));
53
- const command = state.getCommand(symbol);
54
- let nextState = state.getNextState(symbol).ref;
55
- try {
56
- const nextStateForYield = nextState.isHalt && stack.length
57
- ? stack.slice(-1)[0]
58
- : nextState;
59
- yield {
60
- step: i,
61
- state,
62
- currentSymbols: __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").currentSymbols,
63
- nextSymbols: command.tapesCommands.map((tapeCommand, ix) => {
64
- if (typeof tapeCommand.symbol === 'symbol') {
65
- switch (tapeCommand.symbol) {
66
- case symbolCommands.erase:
67
- return __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").tapes[ix].alphabet.blankSymbol;
68
- case symbolCommands.keep:
69
- return __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").tapes[ix].symbol;
70
- default:
71
- throw new Error('invalid symbol command');
72
- }
73
- }
74
- return tapeCommand.symbol;
75
- }),
76
- movements: command.tapesCommands.map((tapeCommand) => tapeCommand.movement),
77
- nextState: nextStateForYield,
78
- };
79
- __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f").applyCommand(command, executionSymbol);
80
- if (nextState.isHalt && stack.length) {
81
- nextState = stack.pop();
82
- }
83
- if (state !== nextState && nextState.overrodeHaltState) {
84
- stack.push(nextState.overrodeHaltState);
85
- }
86
- state = nextState;
87
- }
88
- catch (error) {
89
- if (error !== haltState) {
90
- throw error;
91
- }
92
- break;
93
- }
94
- }
95
- }
96
- finally {
97
- __classPrivateFieldGet(this, _TuringMachine_tapeBlock, "f")[lockSymbol].unlock(executionSymbol);
98
- }
99
- }
100
- }
101
- _TuringMachine_tapeBlock = new WeakMap(), _TuringMachine_stack = new WeakMap();
102
- export default TuringMachine;
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
- export { default as Alphabet } from './classes/Alphabet';
2
- export { default as Command } from './classes/Command';
3
- export { default as Reference } from './classes/Reference';
4
- export { default as State, haltState, ifOtherSymbol } from './classes/State';
5
- export { default as Tape } from './classes/Tape';
6
- export { default as TapeBlock } from './classes/TapeBlock';
7
- export { default as TapeCommand, movements, symbolCommands } from './classes/TapeCommand';
8
- export { default as TuringMachine } from './classes/TuringMachine';
@@ -1,13 +0,0 @@
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
- export { id, uniquePredicate, };
@@ -1 +0,0 @@
1
- {"root":["./src/index.ts","./src/classes/alphabet.ts","./src/classes/command.ts","./src/classes/lock.ts","./src/classes/reference.ts","./src/classes/state.ts","./src/classes/tape.ts","./src/classes/tapeblock.ts","./src/classes/tapecommand.ts","./src/classes/turingmachine.ts","./src/utilities/functions.ts"],"version":"5.9.3"}