@turing-machine-js/machine 1.0.0 → 2.0.0-alpha.0
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/README.md +3 -9
- package/package.json +3 -8
- package/dist/index.cjs.js +0 -1
- package/dist/index.es.js +0 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @turing-machine-js/machine
|
|
2
2
|
|
|
3
|
-
[](https://github.com/mellonis/turing-machine-js/actions/workflows/main.yml)
|
|
4
|
+

|
|
5
5
|
|
|
6
6
|
Some basic objects to build your own turing machine
|
|
7
7
|
|
|
@@ -13,12 +13,6 @@ Using npm:
|
|
|
13
13
|
npm install @turing-machine-js/machine
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
or using yarn:
|
|
17
|
-
|
|
18
|
-
```sh
|
|
19
|
-
yarn add @turing-machine-js/machine
|
|
20
|
-
```
|
|
21
|
-
|
|
22
16
|
## Classes
|
|
23
17
|
|
|
24
18
|
### Alphabet
|
|
@@ -60,7 +54,7 @@ A special symbol for representing the other symbols in `State` class definition
|
|
|
60
54
|
|
|
61
55
|
## Libraries
|
|
62
56
|
|
|
63
|
-
- [@turing-machine-js/library-binary-numbers](https://github.com/mellonis/turing-machine-js/tree/
|
|
57
|
+
- [@turing-machine-js/library-binary-numbers](https://github.com/mellonis/turing-machine-js/tree/master/packages/library-binary-numbers)
|
|
64
58
|
|
|
65
59
|
## Links
|
|
66
60
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turing-machine-js/machine",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.0",
|
|
4
4
|
"description": "A convenient Turing machine",
|
|
5
5
|
"author": "Ruslan Gilmullin <mellonis14@gmain.com>",
|
|
6
6
|
"homepage": "https://github.com/mellonis/turing-machine-js#readme",
|
|
@@ -20,11 +20,6 @@
|
|
|
20
20
|
"turing",
|
|
21
21
|
"machine"
|
|
22
22
|
],
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"scripts": {
|
|
26
|
-
"build": "rollup -c ../../rollup.config.mjs",
|
|
27
|
-
"test": "jest --colors"
|
|
28
|
-
},
|
|
29
|
-
"gitHead": "2a9ef21893e125282272aba41a747c7b638400db"
|
|
23
|
+
"module": "dist/index.js",
|
|
24
|
+
"gitHead": "eac8ba0029090bdbd64f72566657400101fd587e"
|
|
30
25
|
}
|
package/dist/index.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";function t(t,e){return s(t,r(t,e,"get"))}function e(t,e,i){return function(t,e,r){if(e.set)e.set.call(t,r);else{if(!e.writable)throw new TypeError("attempted to set read only private field");e.value=r}}(t,r(t,e,"set"),i),i}function r(t,e,r){if(!e.has(t))throw new TypeError("attempted to "+r+" private field on non-instance");return e.get(t)}function i(t,e,r){return function(t,e){if(t!==e)throw new TypeError("Private static access of wrong provenance")}(t,e),function(t,e){if(void 0===t)throw new TypeError("attempted to "+e+" private static field before its declaration")}(r,"get"),s(t,r)}function s(t,e){return e.get?e.get.call(t):e.value}Object.defineProperty(exports,"__esModule",{value:!0});const a=(t,e,r)=>r.indexOf(t)===e,n=Symbol("idCurrentKey"),o=Symbol("idWeakMapKey");function l(t){return l[o].has(t)||(l[o].set(t,l[n]),l[n]+=1),l[o].get(t)}l[n]=0,l[o]=new WeakMap;var h=new WeakMap;class m{constructor({symbolList:t=[]}={}){h.set(this,{writable:!0,value:void 0});const r=t.filter(a);if(r.length<2)throw new Error("Invalid symbolList length");if(!r.every((t=>"string"==typeof t&&1===t.length)))throw new Error("symbolList contains invalid symbol");e(this,h,Array.from(r))}get symbolList(){return Array.from(t(this,h))}get blankSymbol(){return t(this,h)[0]}has(e){return t(this,h).indexOf(e)>=0}get(e){if(e<0||e>=t(this,h).length)throw new Error("Invalid index");return t(this,h)[e]}index(e){return t(this,h).indexOf(e)}}const p={left:Symbol("move caret left command"),right:Symbol("move caret right command"),stay:Symbol("do not move carer")},w={erase:Symbol("erase symbol command"),keep:Symbol("keep symbol command")};var c=new WeakMap,u=new WeakMap;class d{constructor({movement:t=p.stay,symbol:r=w.keep}){c.set(this,{writable:!0,value:void 0}),u.set(this,{writable:!0,value:void 0});if(!(t===p.left||t===p.stay||t===p.right))throw new Error("invalid movement");e(this,c,t);if(!("string"==typeof r&&1===r.length||r===w.keep||r===w.erase))throw new Error("invalid symbol");e(this,u,r)}get symbol(){return t(this,u)}get movement(){return t(this,c)}}var f=new WeakMap;class y{constructor(r){if(f.set(this,{writable:!0,value:void 0}),e(this,f,r),!Array.isArray(t(this,f)))throw new Error("invalid parameter");if(0===t(this,f).length)throw new Error("invalid parameter");try{e(this,f,t(this,f).map((t=>{let e;if(t instanceof d)e=t;else{if(!Object.prototype.hasOwnProperty.call(t,"movement")&&!Object.prototype.hasOwnProperty.call(t,"symbol"))throw new Error("invalid tapeCommand");e=new d(t)}return e})))}catch(t){throw new Error("invalid tapeCommand")}}get tapeCommandList(){return[...t(this,f)]}}var b=new WeakMap;class v{constructor(){b.set(this,{writable:!0,value:void 0})}get ref(){if(!t(this,b))throw new Error("unbounded reference");return t(this,b)}bind(r){t(this,b)||e(this,b,r)}}const g=Symbol("other symbol");var S=new WeakMap,E=new WeakMap,x=new WeakMap;class k{constructor(e=null){if(S.set(this,{writable:!0,value:l(this)}),E.set(this,{writable:!0,value:void 0}),x.set(this,{writable:!0,value:new Map}),e){if(Object.getOwnPropertyNames(e).length)throw new Error(`invalid state definition while constructing state #${t(this,S)}`);const r=Object.getOwnPropertySymbols(e);if(0===r.length)throw new Error(`invalid state definition while constructing state #${t(this,S)}`);r.forEach((r=>{let{command:i,nextState:s}=e[r];if(null==i&&(i=new y([new d({})])),i instanceof y||Array.isArray(i)||(i=[i]),Array.isArray(i))try{i=new y(i)}catch(t){}if(!(i instanceof y))throw new Error("invalid command");if(null==s&&(s=this),!(s instanceof k||s instanceof v))throw new Error("invalid nextState");t(this,x).set(r,{command:i,nextState:s})}))}}get id(){return t(this,S)}get isHalt(){return 0===t(this,S)}get overrodeHaltState(){return t(this,E)}get ref(){return this}getSymbol(e){const r=[...t(this,x).keys()].find((t=>e.isMatched({symbol:t})));return r||g}getCommand(e){if(t(this,x).has(e))return t(this,x).get(e).command;throw new Error(`No command for symbol at state named ${t(this,S)}`)}getNextState(e){if(t(this,x).has(e))return t(this,x).get(e).nextState;throw new Error(`No nextState for symbol at state named ${t(this,S)}`)}withOverrodeHaltState(r){const i=new k(null);return e(i,x,t(this,x)),e(i,E,r),e(i,S,`${t(this,S)}>${t(r,S)}`),i}}const L=new k(null);var M=new WeakMap,W=new WeakMap,C=new WeakMap,A=new WeakMap;class O{constructor({alphabet:r,symbolList:i=[],position:s=0,viewportWidth:a=1}={}){if(M.set(this,{writable:!0,value:void 0}),W.set(this,{writable:!0,value:void 0}),C.set(this,{writable:!0,value:void 0}),A.set(this,{writable:!0,value:void 0}),!(r instanceof m))throw new Error("Invalid alphabet");if(!i.every((t=>r.has(t))))throw new Error("symbolList contains invalid symbol");e(this,M,new m(r)),e(this,C,Array.from(i)),e(this,W,s),0===t(this,C).length&&t(this,C).push(t(this,M).blankSymbol),e(this,C,t(this,C).map((e=>t(this,M).index(e)))),this.viewportWidth=a}get alphabet(){return t(this,M)}get extraCellsCount(){return(t(this,A)-1)/2}get position(){return t(this,W)}get symbol(){return t(this,M).get(t(this,C)[t(this,W)])}set symbol(e){if(!t(this,M).has(e))throw new Error("Invalid symbol");t(this,C)[t(this,W)]=t(this,M).index(e)}get symbolList(){return t(this,C).map((e=>t(this,M).get(e)))}get viewport(){const e=t(this,W)-this.extraCellsCount,r=t(this,W)+this.extraCellsCount+1;return t(this,C).slice(e,r).map((e=>t(this,M).get(e)))}get viewportWidth(){return t(this,A)}set viewportWidth(t){let r=t;if(r<1)throw new Error("Invalid viewportWidth");r%2==0&&(r+=1),e(this,A,r),this.normalise()}left(){e(this,W,t(this,W)-1),this.normalise()}normalise(){for(;t(this,W)-this.extraCellsCount<0;)t(this,C).unshift(0),e(this,W,t(this,W)+1);for(;t(this,W)+this.extraCellsCount>=t(this,C).length;)t(this,C).push(0)}right(){e(this,W,t(this,W)+1),this.normalise()}}const I=Symbol("symbol for symbolToPatternListMap setter");var H=new WeakMap,T=new WeakMap,j=new WeakMap,P=new WeakMap,$=new WeakMap;class B{constructor({tapeList:r,alphabetList:s}){var a,n,o;if(H.set(this,{writable:!0,value:new Map}),T.set(this,{writable:!0,value:void 0}),o=t=>{e(this,H,new Map(t))},(n=I)in(a=this)?Object.defineProperty(a,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):a[n]=o,j.set(this,{writable:!0,value:e=>e.reduce(((e,r,i)=>{const s=Math.floor(i/t(this,T).length);return Array.isArray(e[s])||(e[s]=[]),e[s].push(r),e}),[]).filter(((t,e,r)=>r.findIndex((e=>t.every(((t,r)=>t===e[r]))))===e))}),P.set(this,{writable:!0,value:e=>{if(e.some((t=>t.every((t=>t===g)))))return g;const[r]=[...t(this,H).entries()].find((([,t])=>t.length===e.length&&e.every(((e,r)=>e.every(((e,i)=>e===t[r][i]))))))||[null,null];let s;return r?s=r:(s=Symbol(i(B,B,N).call(B,e)),t(this,H).set(s,e)),s}}),$.set(this,{writable:!0,value:e=>{let r;if(e===g)return g;if("string"==typeof e?r=e.split(""):Array.isArray(e)&&(r=[...e]),!Array.isArray(r))throw new Error("invalid symbol parameter");if(0===r.length||r.length%t(this,T).length>0)throw new Error("invalid symbol parameter");if(r.findIndex(((e,r)=>e!==g&&!t(this,T)[r%t(this,T).length].alphabet.has(e)))>=0)throw new Error("invalid symbol parameter");return r.every((t=>t===g))?g:t(this,P).call(this,t(this,j).call(this,r))}}),!s&&!r)throw new Error("invalid parameter");if(s){if(!Array.isArray(s))throw new Error("alphabetList must be an array");if(0===s.length)throw new Error("empty alphabet list");if(s.findIndex((t=>!(t instanceof m)))>=0)throw new Error("invalid alphabet");e(this,T,s.map((t=>new O({alphabet:t}))))}else e(this,T,r);if(!Array.isArray(t(this,T)))throw new Error("tapeList must be an array");if(0===t(this,T).length)throw new Error("empty tape list");if(t(this,T).findIndex((t=>!(t instanceof O)))>=0)throw new Error("invalid tape")}get alphabetList(){return[...t(this,T).map((t=>t.alphabet))]}get currentSymbolList(){return t(this,T).map((t=>t.symbol))}get symbol(){return t(this,$).bind(this)}get tapeList(){return[...t(this,T)]}applyCommand(e){if(!(e instanceof y))throw new Error("invalid command");if(t(this,T).length!==e.tapeCommandList.length)throw new Error("invalid command");t(this,T).forEach(((t,r)=>{const{movement:i,symbol:s}=e.tapeCommandList[r];switch(s){case w.keep:break;case w.erase:t.symbol=t.alphabet.blankSymbol;break;default:t.symbol=s}switch(i){case p.left:t.left();break;case p.stay:break;case p.right:t.right()}}))}clone(e=!1){let r;return r=new B(e?{tapeList:this.tapeList.map((t=>new O(t)))}:{alphabetList:this.alphabetList}),r[I](t(this,H)),r}isMatched({currentSymbolList:e=this.currentSymbolList,symbol:r}){if(r===g)return!0;if("symbol"!=typeof r)throw new Error("invalid symbol");if(!t(this,H).has(r))throw new Error("invalid symbol");return t(this,H).get(r).some((t=>t.every(((t,r)=>t===g||t===e[r]))))}replaceTape(e,r=0){if(!(e instanceof O))throw new Error("invalid tape");if(null==t(this,T)[r])throw new Error("invalid tapeIx");if(e.alphabet.symbolList.join("")!==t(this,T)[r].alphabet.symbolList.join(""))throw new Error("invalid tape");t(this,T)[r]=e}}var N={writable:!0,value:t=>JSON.stringify(t.map((t=>t.map((t=>t===g?null:t)))))},K=new WeakMap,_=new WeakMap;exports.Alphabet=m,exports.Command=y,exports.Reference=v,exports.State=k,exports.Tape=O,exports.TapeBlock=B,exports.TapeCommand=d,exports.default=class{constructor({tapeBlock:r}){if(K.set(this,{writable:!0,value:void 0}),_.set(this,{writable:!0,value:void 0}),e(this,K,r),!(t(this,K)instanceof B))throw new Error("invalid tapeBlock");e(this,_,[])}get tapeBlock(){return t(this,K)}run({initialState:t,stepsLimit:e=1e5,onStep:r=null}={}){const i=this.runStepByStep({initialState:t,stepsLimit:e});for(const t of i)r instanceof Function&&r(t)}*runStepByStep({initialState:e,stepsLimit:r=1e5}={}){if(!(e instanceof k))throw new Error("Invalid parameters");const i=t(this,_);let s=e;s.overrodeHaltState&&i.push(s.overrodeHaltState);let a=0;for(;!s.isHalt;){if(a===r)throw new Error("Long execution");a+=1;const e=s.getSymbol(t(this,K)),n=s.getCommand(e);let o=s.getNextState(e).ref;try{const e=o.isHalt&&i.length?i.slice(-1)[0]:o;yield{step:a,state:s,currentSymbolList:t(this,K).currentSymbolList,nextSymbolList:n.tapeCommandList.map(((e,r)=>{switch(e.symbol){case w.erase:return t(this,K).tapeList[r].alphabet.blankSymbol;case w.keep:return t(this,K).tapeList[r].symbol;default:return e.symbol}})),movementList:n.tapeCommandList.map((t=>t.movement)),nextState:e}}catch(t){throw new Error(`Execution halted because of ${t.message}`)}t(this,K).applyCommand(n),o.isHalt&&i.length&&(o=i.pop()),s!==o&&o.overrodeHaltState&&i.push(o.overrodeHaltState),s=o}}},exports.haltState=L,exports.ifOtherSymbol=g,exports.movements=p,exports.symbolCommands=w;
|
package/dist/index.es.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function t(t,e){return a(t,r(t,e,"get"))}function e(t,e,i){return function(t,e,r){if(e.set)e.set.call(t,r);else{if(!e.writable)throw new TypeError("attempted to set read only private field");e.value=r}}(t,r(t,e,"set"),i),i}function r(t,e,r){if(!e.has(t))throw new TypeError("attempted to "+r+" private field on non-instance");return e.get(t)}function i(t,e,r){return function(t,e){if(t!==e)throw new TypeError("Private static access of wrong provenance")}(t,e),function(t,e){if(void 0===t)throw new TypeError("attempted to "+e+" private static field before its declaration")}(r,"get"),a(t,r)}function a(t,e){return e.get?e.get.call(t):e.value}const s=(t,e,r)=>r.indexOf(t)===e,n=Symbol("idCurrentKey"),o=Symbol("idWeakMapKey");function l(t){return l[o].has(t)||(l[o].set(t,l[n]),l[n]+=1),l[o].get(t)}l[n]=0,l[o]=new WeakMap;var h=new WeakMap;class m{constructor({symbolList:t=[]}={}){h.set(this,{writable:!0,value:void 0});const r=t.filter(s);if(r.length<2)throw new Error("Invalid symbolList length");if(!r.every((t=>"string"==typeof t&&1===t.length)))throw new Error("symbolList contains invalid symbol");e(this,h,Array.from(r))}get symbolList(){return Array.from(t(this,h))}get blankSymbol(){return t(this,h)[0]}has(e){return t(this,h).indexOf(e)>=0}get(e){if(e<0||e>=t(this,h).length)throw new Error("Invalid index");return t(this,h)[e]}index(e){return t(this,h).indexOf(e)}}const w={left:Symbol("move caret left command"),right:Symbol("move caret right command"),stay:Symbol("do not move carer")},p={erase:Symbol("erase symbol command"),keep:Symbol("keep symbol command")};var c=new WeakMap,u=new WeakMap;class f{constructor({movement:t=w.stay,symbol:r=p.keep}){c.set(this,{writable:!0,value:void 0}),u.set(this,{writable:!0,value:void 0});if(!(t===w.left||t===w.stay||t===w.right))throw new Error("invalid movement");e(this,c,t);if(!("string"==typeof r&&1===r.length||r===p.keep||r===p.erase))throw new Error("invalid symbol");e(this,u,r)}get symbol(){return t(this,u)}get movement(){return t(this,c)}}var y=new WeakMap;class b{constructor(r){if(y.set(this,{writable:!0,value:void 0}),e(this,y,r),!Array.isArray(t(this,y)))throw new Error("invalid parameter");if(0===t(this,y).length)throw new Error("invalid parameter");try{e(this,y,t(this,y).map((t=>{let e;if(t instanceof f)e=t;else{if(!Object.prototype.hasOwnProperty.call(t,"movement")&&!Object.prototype.hasOwnProperty.call(t,"symbol"))throw new Error("invalid tapeCommand");e=new f(t)}return e})))}catch(t){throw new Error("invalid tapeCommand")}}get tapeCommandList(){return[...t(this,y)]}}var d=new WeakMap;class v{constructor(){d.set(this,{writable:!0,value:void 0})}get ref(){if(!t(this,d))throw new Error("unbounded reference");return t(this,d)}bind(r){t(this,d)||e(this,d,r)}}const g=Symbol("other symbol");var E=new WeakMap,S=new WeakMap,k=new WeakMap;class L{constructor(e=null){if(E.set(this,{writable:!0,value:l(this)}),S.set(this,{writable:!0,value:void 0}),k.set(this,{writable:!0,value:new Map}),e){if(Object.getOwnPropertyNames(e).length)throw new Error(`invalid state definition while constructing state #${t(this,E)}`);const r=Object.getOwnPropertySymbols(e);if(0===r.length)throw new Error(`invalid state definition while constructing state #${t(this,E)}`);r.forEach((r=>{let{command:i,nextState:a}=e[r];if(null==i&&(i=new b([new f({})])),i instanceof b||Array.isArray(i)||(i=[i]),Array.isArray(i))try{i=new b(i)}catch(t){}if(!(i instanceof b))throw new Error("invalid command");if(null==a&&(a=this),!(a instanceof L||a instanceof v))throw new Error("invalid nextState");t(this,k).set(r,{command:i,nextState:a})}))}}get id(){return t(this,E)}get isHalt(){return 0===t(this,E)}get overrodeHaltState(){return t(this,S)}get ref(){return this}getSymbol(e){const r=[...t(this,k).keys()].find((t=>e.isMatched({symbol:t})));return r||g}getCommand(e){if(t(this,k).has(e))return t(this,k).get(e).command;throw new Error(`No command for symbol at state named ${t(this,E)}`)}getNextState(e){if(t(this,k).has(e))return t(this,k).get(e).nextState;throw new Error(`No nextState for symbol at state named ${t(this,E)}`)}withOverrodeHaltState(r){const i=new L(null);return e(i,k,t(this,k)),e(i,S,r),e(i,E,`${t(this,E)}>${t(r,E)}`),i}}const x=new L(null);var M=new WeakMap,W=new WeakMap,C=new WeakMap,A=new WeakMap;class O{constructor({alphabet:r,symbolList:i=[],position:a=0,viewportWidth:s=1}={}){if(M.set(this,{writable:!0,value:void 0}),W.set(this,{writable:!0,value:void 0}),C.set(this,{writable:!0,value:void 0}),A.set(this,{writable:!0,value:void 0}),!(r instanceof m))throw new Error("Invalid alphabet");if(!i.every((t=>r.has(t))))throw new Error("symbolList contains invalid symbol");e(this,M,new m(r)),e(this,C,Array.from(i)),e(this,W,a),0===t(this,C).length&&t(this,C).push(t(this,M).blankSymbol),e(this,C,t(this,C).map((e=>t(this,M).index(e)))),this.viewportWidth=s}get alphabet(){return t(this,M)}get extraCellsCount(){return(t(this,A)-1)/2}get position(){return t(this,W)}get symbol(){return t(this,M).get(t(this,C)[t(this,W)])}set symbol(e){if(!t(this,M).has(e))throw new Error("Invalid symbol");t(this,C)[t(this,W)]=t(this,M).index(e)}get symbolList(){return t(this,C).map((e=>t(this,M).get(e)))}get viewport(){const e=t(this,W)-this.extraCellsCount,r=t(this,W)+this.extraCellsCount+1;return t(this,C).slice(e,r).map((e=>t(this,M).get(e)))}get viewportWidth(){return t(this,A)}set viewportWidth(t){let r=t;if(r<1)throw new Error("Invalid viewportWidth");r%2==0&&(r+=1),e(this,A,r),this.normalise()}left(){e(this,W,t(this,W)-1),this.normalise()}normalise(){for(;t(this,W)-this.extraCellsCount<0;)t(this,C).unshift(0),e(this,W,t(this,W)+1);for(;t(this,W)+this.extraCellsCount>=t(this,C).length;)t(this,C).push(0)}right(){e(this,W,t(this,W)+1),this.normalise()}}const I=Symbol("symbol for symbolToPatternListMap setter");var H=new WeakMap,j=new WeakMap,P=new WeakMap,$=new WeakMap,N=new WeakMap;class T{constructor({tapeList:r,alphabetList:a}){var s,n,o;if(H.set(this,{writable:!0,value:new Map}),j.set(this,{writable:!0,value:void 0}),o=t=>{e(this,H,new Map(t))},(n=I)in(s=this)?Object.defineProperty(s,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):s[n]=o,P.set(this,{writable:!0,value:e=>e.reduce(((e,r,i)=>{const a=Math.floor(i/t(this,j).length);return Array.isArray(e[a])||(e[a]=[]),e[a].push(r),e}),[]).filter(((t,e,r)=>r.findIndex((e=>t.every(((t,r)=>t===e[r]))))===e))}),$.set(this,{writable:!0,value:e=>{if(e.some((t=>t.every((t=>t===g)))))return g;const[r]=[...t(this,H).entries()].find((([,t])=>t.length===e.length&&e.every(((e,r)=>e.every(((e,i)=>e===t[r][i]))))))||[null,null];let a;return r?a=r:(a=Symbol(i(T,T,B).call(T,e)),t(this,H).set(a,e)),a}}),N.set(this,{writable:!0,value:e=>{let r;if(e===g)return g;if("string"==typeof e?r=e.split(""):Array.isArray(e)&&(r=[...e]),!Array.isArray(r))throw new Error("invalid symbol parameter");if(0===r.length||r.length%t(this,j).length>0)throw new Error("invalid symbol parameter");if(r.findIndex(((e,r)=>e!==g&&!t(this,j)[r%t(this,j).length].alphabet.has(e)))>=0)throw new Error("invalid symbol parameter");return r.every((t=>t===g))?g:t(this,$).call(this,t(this,P).call(this,r))}}),!a&&!r)throw new Error("invalid parameter");if(a){if(!Array.isArray(a))throw new Error("alphabetList must be an array");if(0===a.length)throw new Error("empty alphabet list");if(a.findIndex((t=>!(t instanceof m)))>=0)throw new Error("invalid alphabet");e(this,j,a.map((t=>new O({alphabet:t}))))}else e(this,j,r);if(!Array.isArray(t(this,j)))throw new Error("tapeList must be an array");if(0===t(this,j).length)throw new Error("empty tape list");if(t(this,j).findIndex((t=>!(t instanceof O)))>=0)throw new Error("invalid tape")}get alphabetList(){return[...t(this,j).map((t=>t.alphabet))]}get currentSymbolList(){return t(this,j).map((t=>t.symbol))}get symbol(){return t(this,N).bind(this)}get tapeList(){return[...t(this,j)]}applyCommand(e){if(!(e instanceof b))throw new Error("invalid command");if(t(this,j).length!==e.tapeCommandList.length)throw new Error("invalid command");t(this,j).forEach(((t,r)=>{const{movement:i,symbol:a}=e.tapeCommandList[r];switch(a){case p.keep:break;case p.erase:t.symbol=t.alphabet.blankSymbol;break;default:t.symbol=a}switch(i){case w.left:t.left();break;case w.stay:break;case w.right:t.right()}}))}clone(e=!1){let r;return r=new T(e?{tapeList:this.tapeList.map((t=>new O(t)))}:{alphabetList:this.alphabetList}),r[I](t(this,H)),r}isMatched({currentSymbolList:e=this.currentSymbolList,symbol:r}){if(r===g)return!0;if("symbol"!=typeof r)throw new Error("invalid symbol");if(!t(this,H).has(r))throw new Error("invalid symbol");return t(this,H).get(r).some((t=>t.every(((t,r)=>t===g||t===e[r]))))}replaceTape(e,r=0){if(!(e instanceof O))throw new Error("invalid tape");if(null==t(this,j)[r])throw new Error("invalid tapeIx");if(e.alphabet.symbolList.join("")!==t(this,j)[r].alphabet.symbolList.join(""))throw new Error("invalid tape");t(this,j)[r]=e}}var B={writable:!0,value:t=>JSON.stringify(t.map((t=>t.map((t=>t===g?null:t)))))},K=new WeakMap,F=new WeakMap;export default class{constructor({tapeBlock:r}){if(K.set(this,{writable:!0,value:void 0}),F.set(this,{writable:!0,value:void 0}),e(this,K,r),!(t(this,K)instanceof T))throw new Error("invalid tapeBlock");e(this,F,[])}get tapeBlock(){return t(this,K)}run({initialState:t,stepsLimit:e=1e5,onStep:r=null}={}){const i=this.runStepByStep({initialState:t,stepsLimit:e});for(const t of i)r instanceof Function&&r(t)}*runStepByStep({initialState:e,stepsLimit:r=1e5}={}){if(!(e instanceof L))throw new Error("Invalid parameters");const i=t(this,F);let a=e;a.overrodeHaltState&&i.push(a.overrodeHaltState);let s=0;for(;!a.isHalt;){if(s===r)throw new Error("Long execution");s+=1;const e=a.getSymbol(t(this,K)),n=a.getCommand(e);let o=a.getNextState(e).ref;try{const e=o.isHalt&&i.length?i.slice(-1)[0]:o;yield{step:s,state:a,currentSymbolList:t(this,K).currentSymbolList,nextSymbolList:n.tapeCommandList.map(((e,r)=>{switch(e.symbol){case p.erase:return t(this,K).tapeList[r].alphabet.blankSymbol;case p.keep:return t(this,K).tapeList[r].symbol;default:return e.symbol}})),movementList:n.tapeCommandList.map((t=>t.movement)),nextState:e}}catch(t){throw new Error(`Execution halted because of ${t.message}`)}t(this,K).applyCommand(n),o.isHalt&&i.length&&(o=i.pop()),a!==o&&o.overrodeHaltState&&i.push(o.overrodeHaltState),a=o}}}export{m as Alphabet,b as Command,v as Reference,L as State,O as Tape,T as TapeBlock,f as TapeCommand,x as haltState,g as ifOtherSymbol,w as movements,p as symbolCommands};
|