@turing-machine-js/machine 0.3.0-alpha.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # @turing-machine-js/machine
2
2
 
3
- [![Build Status](https://travis-ci.com/mellonis/turing-machine-js.svg?branch=next)](https://travis-ci.com/mellonis/turing-machine-js)
4
- ![npm (tag)](https://img.shields.io/npm/v/@turing-machine-js/machine/next)
3
+ [![build](https://github.com/mellonis/turing-machine-js/actions/workflows/main.yml/badge.svg)](https://github.com/mellonis/turing-machine-js/actions/workflows/main.yml)
4
+ ![npm (tag)](https://img.shields.io/npm/v/@turing-machine-js/machine)
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/next/packages/library-binary-numbers)
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": "0.3.0-alpha.4",
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
- "main": "dist/index.cjs.js",
24
- "module": "dist/index.es.js",
25
- "scripts": {
26
- "build": "rollup -c ../../rollup.config.mjs",
27
- "test": "jest --colors"
28
- },
29
- "gitHead": "46f086aced8630b3c5f7d5e1585dacc6735d7666"
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){var r=e.get(t);if(!r)throw new TypeError("attempted to get private field on non-instance");return r.get?r.get.call(t):r.value}function e(t,e,r){var i=e.get(t);if(!i)throw new TypeError("attempted to set private field on non-instance");if(i.set)i.set.call(t,r);else{if(!i.writable)throw new TypeError("attempted to set read only private field");i.value=r}return r}Object.defineProperty(exports,"__esModule",{value:!0});const r=(t,e,r)=>r.indexOf(t)===e,i=Symbol("idCurrentKey"),s=Symbol("idWeakMapKey");function a(t){return a[s].has(t)||(a[s].set(t,a[i]),a[i]+=1),a[s].get(t)}a[i]=0,a[s]=new WeakMap;var n=new WeakMap;class o{constructor({symbolList:t=[]}={}){n.set(this,{writable:!0,value:void 0});const i=t.filter(r);if(i.length<2)throw new Error("Invalid symbolList length");if(!i.every(t=>"string"==typeof t&&1===t.length))throw new Error("symbolList contains invalid symbol");e(this,n,Array.from(i))}get symbolList(){return Array.from(t(this,n))}get blankSymbol(){return t(this,n)[0]}has(e){return t(this,n).indexOf(e)>=0}get(e){if(e<0||e>=t(this,n).length)throw new Error("Invalid index");return t(this,n)[e]}index(e){return t(this,n).indexOf(e)}}const l={left:Symbol("move caret left command"),right:Symbol("move caret right command"),stay:Symbol("do not move carer")},h={erase:Symbol("erase symbol command"),keep:Symbol("keep symbol command")};var m=new WeakMap,p=new WeakMap;class w{constructor({movement:t=l.stay,symbol:r=h.keep}){m.set(this,{writable:!0,value:void 0}),p.set(this,{writable:!0,value:void 0});if(!(t===l.left||t===l.stay||t===l.right))throw new Error("invalid movement");e(this,m,t);if(!("string"==typeof r&&1===r.length||r===h.keep||r===h.erase))throw new Error("invalid symbol");e(this,p,r)}get symbol(){return t(this,p)}get movement(){return t(this,m)}}var c=new WeakMap;class u{constructor(r){if(c.set(this,{writable:!0,value:void 0}),e(this,c,r),!Array.isArray(t(this,c)))throw new Error("invalid parameter");if(0===t(this,c).length)throw new Error("invalid parameter");try{e(this,c,t(this,c).map(t=>{let e;if(t instanceof w)e=t;else{if(!Object.prototype.hasOwnProperty.call(t,"movement")&&!Object.prototype.hasOwnProperty.call(t,"symbol"))throw new Error("invalid tapeCommand");e=new w(t)}return e}))}catch(t){throw new Error("invalid tapeCommand")}}get tapeCommandList(){return[...t(this,c)]}}var y=new WeakMap;class b{constructor(){y.set(this,{writable:!0,value:void 0})}get ref(){if(!t(this,y))throw new Error("unbounded reference");return t(this,y)}bind(r){t(this,y)||e(this,y,r)}}const d=Symbol("other symbol");var f=new WeakMap,v=new WeakMap,g=new WeakMap;class S{constructor(e=null){if(f.set(this,{writable:!0,value:a(this)}),v.set(this,{writable:!0,value:void 0}),g.set(this,{writable:!0,value:new Map}),e){if(Object.getOwnPropertyNames(e).length)throw new Error("invalid state definition while constructing state #"+t(this,f));const r=Object.getOwnPropertySymbols(e);if(0===r.length)throw new Error("invalid state definition while constructing state #"+t(this,f));r.forEach(r=>{let{command:i,nextState:s}=e[r];if(null==i&&(i=new u([new w({})])),i instanceof u||Array.isArray(i)||(i=[i]),Array.isArray(i))try{i=new u(i)}catch(t){}if(!(i instanceof u))throw new Error("invalid command");if(null==s&&(s=this),!(s instanceof S||s instanceof b))throw new Error("invalid nextState");t(this,g).set(r,{command:i,nextState:s})})}}get id(){return t(this,f)}get isHalt(){return 0===t(this,f)}get overrodeHaltState(){return t(this,v)}get ref(){return this}getSymbol(e){const r=[...t(this,g).keys()].find(t=>e.isMatched({symbol:t}));return r||d}getCommand(e){if(t(this,g).has(e))return t(this,g).get(e).command;throw new Error("No command for symbol at state named "+t(this,f))}getNextState(e){if(t(this,g).has(e))return t(this,g).get(e).nextState;throw new Error("No nextState for symbol at state named "+t(this,f))}withOverrodeHaltState(r){const i=new S(null);return e(i,g,t(this,g)),e(i,v,r),e(i,f,`${t(this,f)}>${t(r,f)}`),i}}const E=new S(null);var x=new WeakMap,k=new WeakMap,L=new WeakMap,M=new WeakMap;class W{constructor({alphabet:r,symbolList:i=[],position:s=0,viewportWidth:a=1}={}){if(x.set(this,{writable:!0,value:void 0}),k.set(this,{writable:!0,value:void 0}),L.set(this,{writable:!0,value:void 0}),M.set(this,{writable:!0,value:void 0}),!(r instanceof o))throw new Error("Invalid alphabet");if(!i.every(t=>r.has(t)))throw new Error("symbolList contains invalid symbol");e(this,x,new o(r)),e(this,L,Array.from(i)),e(this,k,s),0===t(this,L).length&&t(this,L).push(t(this,x).blankSymbol),e(this,L,t(this,L).map(e=>t(this,x).index(e))),this.viewportWidth=a}get alphabet(){return t(this,x)}get extraCellsCount(){return(t(this,M)-1)/2}get position(){return t(this,k)}get symbol(){return t(this,x).get(t(this,L)[t(this,k)])}set symbol(e){if(!t(this,x).has(e))throw new Error("Invalid symbol");t(this,L)[t(this,k)]=t(this,x).index(e)}get symbolList(){return t(this,L).map(e=>t(this,x).get(e))}get viewport(){const e=t(this,k)-this.extraCellsCount,r=t(this,k)+this.extraCellsCount+1;return t(this,L).slice(e,r).map(e=>t(this,x).get(e))}get viewportWidth(){return t(this,M)}set viewportWidth(t){let r=t;if(r<1)throw new Error("Invalid viewportWidth");r%2==0&&(r+=1),e(this,M,r),this.normalise()}left(){e(this,k,t(this,k)-1),this.normalise()}normalise(){for(;t(this,k)-this.extraCellsCount<0;)t(this,L).unshift(0),e(this,k,t(this,k)+1);for(;t(this,k)+this.extraCellsCount>=t(this,L).length;)t(this,L).push(0)}right(){e(this,k,t(this,k)+1),this.normalise()}}const C=Symbol("symbol for symbolToPatternListMap setter");var A=new WeakMap,O=new WeakMap,I=new WeakMap,H=new WeakMap,T=new WeakMap;class j{constructor({tapeList:r,alphabetList:i}){var s,a,n;if(A.set(this,{writable:!0,value:new Map}),O.set(this,{writable:!0,value:void 0}),n=t=>{e(this,A,new Map(t))},(a=C)in(s=this)?Object.defineProperty(s,a,{value:n,enumerable:!0,configurable:!0,writable:!0}):s[a]=n,I.set(this,{writable:!0,value:e=>e.reduce((e,r,i)=>{const s=Math.floor(i/t(this,O).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)}),H.set(this,{writable:!0,value:e=>{if(e.some(t=>t.every(t=>t===d)))return d;const[r]=[...t(this,A).entries()].find(([,t])=>t.length===e.length&&e.every((e,r)=>e.every((e,i)=>e===t[r][i])))||[null,null];let i;return r?i=r:(i=Symbol(function(t,e,r){if(t!==e)throw new TypeError("Private static access of wrong provenance");return r.get?r.get.call(t):r.value}(j,j,P).call(j,e)),t(this,A).set(i,e)),i}}),T.set(this,{writable:!0,value:e=>{let r;if(e===d)return d;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,O).length>0)throw new Error("invalid symbol parameter");if(r.findIndex((e,r)=>e!==d&&!t(this,O)[r%t(this,O).length].alphabet.has(e))>=0)throw new Error("invalid symbol parameter");return r.every(t=>t===d)?d:t(this,H).call(this,t(this,I).call(this,r))}}),!i&&!r)throw new Error("invalid parameter");if(i){if(!Array.isArray(i))throw new Error("alphabetList must be an array");if(0===i.length)throw new Error("empty alphabet list");if(i.findIndex(t=>!(t instanceof o))>=0)throw new Error("invalid alphabet");e(this,O,i.map(t=>new W({alphabet:t})))}else e(this,O,r);if(!Array.isArray(t(this,O)))throw new Error("tapeList must be an array");if(0===t(this,O).length)throw new Error("empty tape list");if(t(this,O).findIndex(t=>!(t instanceof W))>=0)throw new Error("invalid tape")}get alphabetList(){return[...t(this,O).map(t=>t.alphabet)]}get currentSymbolList(){return t(this,O).map(t=>t.symbol)}get symbol(){return t(this,T).bind(this)}get tapeList(){return[...t(this,O)]}applyCommand(e){if(!(e instanceof u))throw new Error("invalid command");if(t(this,O).length!==e.tapeCommandList.length)throw new Error("invalid command");t(this,O).forEach((t,r)=>{const{movement:i,symbol:s}=e.tapeCommandList[r];switch(s){case h.keep:break;case h.erase:t.symbol=t.alphabet.blankSymbol;break;default:t.symbol=s}switch(i){case l.left:t.left();break;case l.stay:break;case l.right:t.right()}})}clone(e=!1){let r;return r=new j(e?{tapeList:this.tapeList.map(t=>new W(t))}:{alphabetList:this.alphabetList}),r[C](t(this,A)),r}isMatched({currentSymbolList:e=this.currentSymbolList,symbol:r}){if(r===d)return!0;if("symbol"!=typeof r)throw new Error("invalid symbol");if(!t(this,A).has(r))throw new Error("invalid symbol");return t(this,A).get(r).some(t=>t.every((t,r)=>t===d||t===e[r]))}replaceTape(e,r=0){if(!(e instanceof W))throw new Error("invalid tape");if(null==t(this,O)[r])throw new Error("invalid tapeIx");if(e.alphabet.symbolList.join("")!==t(this,O)[r].alphabet.symbolList.join(""))throw new Error("invalid tape");t(this,O)[r]=e}}var P={writable:!0,value:t=>JSON.stringify(t.map(t=>t.map(t=>t===d?null:t)))},B=new WeakMap,N=new WeakMap;exports.Alphabet=o,exports.Command=u,exports.Reference=b,exports.State=S,exports.Tape=W,exports.TapeBlock=j,exports.TapeCommand=w,exports.default=class{constructor({tapeBlock:r}){if(B.set(this,{writable:!0,value:void 0}),N.set(this,{writable:!0,value:void 0}),e(this,B,r),!(t(this,B)instanceof j))throw new Error("invalid tapeBlock");e(this,N,[])}get tapeBlock(){return t(this,B)}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 S))throw new Error("Invalid parameters");const i=t(this,N);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,B)),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,B).currentSymbolList,nextSymbolList:n.tapeCommandList.map((e,r)=>{switch(e.symbol){case h.erase:return t(this,B).tapeList[r].alphabet.blankSymbol;case h.keep:return t(this,B).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,B).applyCommand(n),o.isHalt&&i.length&&(o=i.pop()),s!==o&&o.overrodeHaltState&&i.push(o.overrodeHaltState),s=o}}},exports.haltState=E,exports.ifOtherSymbol=d,exports.movements=l,exports.symbolCommands=h;
package/dist/index.es.js DELETED
@@ -1 +0,0 @@
1
- function t(t,e){var r=e.get(t);if(!r)throw new TypeError("attempted to get private field on non-instance");return r.get?r.get.call(t):r.value}function e(t,e,r){var i=e.get(t);if(!i)throw new TypeError("attempted to set private field on non-instance");if(i.set)i.set.call(t,r);else{if(!i.writable)throw new TypeError("attempted to set read only private field");i.value=r}return r}const r=(t,e,r)=>r.indexOf(t)===e,i=Symbol("idCurrentKey"),a=Symbol("idWeakMapKey");function s(t){return s[a].has(t)||(s[a].set(t,s[i]),s[i]+=1),s[a].get(t)}s[i]=0,s[a]=new WeakMap;var n=new WeakMap;class o{constructor({symbolList:t=[]}={}){n.set(this,{writable:!0,value:void 0});const i=t.filter(r);if(i.length<2)throw new Error("Invalid symbolList length");if(!i.every(t=>"string"==typeof t&&1===t.length))throw new Error("symbolList contains invalid symbol");e(this,n,Array.from(i))}get symbolList(){return Array.from(t(this,n))}get blankSymbol(){return t(this,n)[0]}has(e){return t(this,n).indexOf(e)>=0}get(e){if(e<0||e>=t(this,n).length)throw new Error("Invalid index");return t(this,n)[e]}index(e){return t(this,n).indexOf(e)}}const l={left:Symbol("move caret left command"),right:Symbol("move caret right command"),stay:Symbol("do not move carer")},h={erase:Symbol("erase symbol command"),keep:Symbol("keep symbol command")};var m=new WeakMap,w=new WeakMap;class p{constructor({movement:t=l.stay,symbol:r=h.keep}){m.set(this,{writable:!0,value:void 0}),w.set(this,{writable:!0,value:void 0});if(!(t===l.left||t===l.stay||t===l.right))throw new Error("invalid movement");e(this,m,t);if(!("string"==typeof r&&1===r.length||r===h.keep||r===h.erase))throw new Error("invalid symbol");e(this,w,r)}get symbol(){return t(this,w)}get movement(){return t(this,m)}}var c=new WeakMap;class u{constructor(r){if(c.set(this,{writable:!0,value:void 0}),e(this,c,r),!Array.isArray(t(this,c)))throw new Error("invalid parameter");if(0===t(this,c).length)throw new Error("invalid parameter");try{e(this,c,t(this,c).map(t=>{let e;if(t instanceof p)e=t;else{if(!Object.prototype.hasOwnProperty.call(t,"movement")&&!Object.prototype.hasOwnProperty.call(t,"symbol"))throw new Error("invalid tapeCommand");e=new p(t)}return e}))}catch(t){throw new Error("invalid tapeCommand")}}get tapeCommandList(){return[...t(this,c)]}}var y=new WeakMap;class b{constructor(){y.set(this,{writable:!0,value:void 0})}get ref(){if(!t(this,y))throw new Error("unbounded reference");return t(this,y)}bind(r){t(this,y)||e(this,y,r)}}const d=Symbol("other symbol");var f=new WeakMap,v=new WeakMap,g=new WeakMap;class E{constructor(e=null){if(f.set(this,{writable:!0,value:s(this)}),v.set(this,{writable:!0,value:void 0}),g.set(this,{writable:!0,value:new Map}),e){if(Object.getOwnPropertyNames(e).length)throw new Error("invalid state definition while constructing state #"+t(this,f));const r=Object.getOwnPropertySymbols(e);if(0===r.length)throw new Error("invalid state definition while constructing state #"+t(this,f));r.forEach(r=>{let{command:i,nextState:a}=e[r];if(null==i&&(i=new u([new p({})])),i instanceof u||Array.isArray(i)||(i=[i]),Array.isArray(i))try{i=new u(i)}catch(t){}if(!(i instanceof u))throw new Error("invalid command");if(null==a&&(a=this),!(a instanceof E||a instanceof b))throw new Error("invalid nextState");t(this,g).set(r,{command:i,nextState:a})})}}get id(){return t(this,f)}get isHalt(){return 0===t(this,f)}get overrodeHaltState(){return t(this,v)}get ref(){return this}getSymbol(e){const r=[...t(this,g).keys()].find(t=>e.isMatched({symbol:t}));return r||d}getCommand(e){if(t(this,g).has(e))return t(this,g).get(e).command;throw new Error("No command for symbol at state named "+t(this,f))}getNextState(e){if(t(this,g).has(e))return t(this,g).get(e).nextState;throw new Error("No nextState for symbol at state named "+t(this,f))}withOverrodeHaltState(r){const i=new E(null);return e(i,g,t(this,g)),e(i,v,r),e(i,f,`${t(this,f)}>${t(r,f)}`),i}}const S=new E(null);var k=new WeakMap,L=new WeakMap,x=new WeakMap,M=new WeakMap;class W{constructor({alphabet:r,symbolList:i=[],position:a=0,viewportWidth:s=1}={}){if(k.set(this,{writable:!0,value:void 0}),L.set(this,{writable:!0,value:void 0}),x.set(this,{writable:!0,value:void 0}),M.set(this,{writable:!0,value:void 0}),!(r instanceof o))throw new Error("Invalid alphabet");if(!i.every(t=>r.has(t)))throw new Error("symbolList contains invalid symbol");e(this,k,new o(r)),e(this,x,Array.from(i)),e(this,L,a),0===t(this,x).length&&t(this,x).push(t(this,k).blankSymbol),e(this,x,t(this,x).map(e=>t(this,k).index(e))),this.viewportWidth=s}get alphabet(){return t(this,k)}get extraCellsCount(){return(t(this,M)-1)/2}get position(){return t(this,L)}get symbol(){return t(this,k).get(t(this,x)[t(this,L)])}set symbol(e){if(!t(this,k).has(e))throw new Error("Invalid symbol");t(this,x)[t(this,L)]=t(this,k).index(e)}get symbolList(){return t(this,x).map(e=>t(this,k).get(e))}get viewport(){const e=t(this,L)-this.extraCellsCount,r=t(this,L)+this.extraCellsCount+1;return t(this,x).slice(e,r).map(e=>t(this,k).get(e))}get viewportWidth(){return t(this,M)}set viewportWidth(t){let r=t;if(r<1)throw new Error("Invalid viewportWidth");r%2==0&&(r+=1),e(this,M,r),this.normalise()}left(){e(this,L,t(this,L)-1),this.normalise()}normalise(){for(;t(this,L)-this.extraCellsCount<0;)t(this,x).unshift(0),e(this,L,t(this,L)+1);for(;t(this,L)+this.extraCellsCount>=t(this,x).length;)t(this,x).push(0)}right(){e(this,L,t(this,L)+1),this.normalise()}}const C=Symbol("symbol for symbolToPatternListMap setter");var A=new WeakMap,O=new WeakMap,I=new WeakMap,H=new WeakMap,j=new WeakMap;class P{constructor({tapeList:r,alphabetList:i}){var a,s,n;if(A.set(this,{writable:!0,value:new Map}),O.set(this,{writable:!0,value:void 0}),n=t=>{e(this,A,new Map(t))},(s=C)in(a=this)?Object.defineProperty(a,s,{value:n,enumerable:!0,configurable:!0,writable:!0}):a[s]=n,I.set(this,{writable:!0,value:e=>e.reduce((e,r,i)=>{const a=Math.floor(i/t(this,O).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)}),H.set(this,{writable:!0,value:e=>{if(e.some(t=>t.every(t=>t===d)))return d;const[r]=[...t(this,A).entries()].find(([,t])=>t.length===e.length&&e.every((e,r)=>e.every((e,i)=>e===t[r][i])))||[null,null];let i;return r?i=r:(i=Symbol(function(t,e,r){if(t!==e)throw new TypeError("Private static access of wrong provenance");return r.get?r.get.call(t):r.value}(P,P,N).call(P,e)),t(this,A).set(i,e)),i}}),j.set(this,{writable:!0,value:e=>{let r;if(e===d)return d;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,O).length>0)throw new Error("invalid symbol parameter");if(r.findIndex((e,r)=>e!==d&&!t(this,O)[r%t(this,O).length].alphabet.has(e))>=0)throw new Error("invalid symbol parameter");return r.every(t=>t===d)?d:t(this,H).call(this,t(this,I).call(this,r))}}),!i&&!r)throw new Error("invalid parameter");if(i){if(!Array.isArray(i))throw new Error("alphabetList must be an array");if(0===i.length)throw new Error("empty alphabet list");if(i.findIndex(t=>!(t instanceof o))>=0)throw new Error("invalid alphabet");e(this,O,i.map(t=>new W({alphabet:t})))}else e(this,O,r);if(!Array.isArray(t(this,O)))throw new Error("tapeList must be an array");if(0===t(this,O).length)throw new Error("empty tape list");if(t(this,O).findIndex(t=>!(t instanceof W))>=0)throw new Error("invalid tape")}get alphabetList(){return[...t(this,O).map(t=>t.alphabet)]}get currentSymbolList(){return t(this,O).map(t=>t.symbol)}get symbol(){return t(this,j).bind(this)}get tapeList(){return[...t(this,O)]}applyCommand(e){if(!(e instanceof u))throw new Error("invalid command");if(t(this,O).length!==e.tapeCommandList.length)throw new Error("invalid command");t(this,O).forEach((t,r)=>{const{movement:i,symbol:a}=e.tapeCommandList[r];switch(a){case h.keep:break;case h.erase:t.symbol=t.alphabet.blankSymbol;break;default:t.symbol=a}switch(i){case l.left:t.left();break;case l.stay:break;case l.right:t.right()}})}clone(e=!1){let r;return r=new P(e?{tapeList:this.tapeList.map(t=>new W(t))}:{alphabetList:this.alphabetList}),r[C](t(this,A)),r}isMatched({currentSymbolList:e=this.currentSymbolList,symbol:r}){if(r===d)return!0;if("symbol"!=typeof r)throw new Error("invalid symbol");if(!t(this,A).has(r))throw new Error("invalid symbol");return t(this,A).get(r).some(t=>t.every((t,r)=>t===d||t===e[r]))}replaceTape(e,r=0){if(!(e instanceof W))throw new Error("invalid tape");if(null==t(this,O)[r])throw new Error("invalid tapeIx");if(e.alphabet.symbolList.join("")!==t(this,O)[r].alphabet.symbolList.join(""))throw new Error("invalid tape");t(this,O)[r]=e}}var N={writable:!0,value:t=>JSON.stringify(t.map(t=>t.map(t=>t===d?null:t)))},T=new WeakMap,B=new WeakMap;export default class{constructor({tapeBlock:r}){if(T.set(this,{writable:!0,value:void 0}),B.set(this,{writable:!0,value:void 0}),e(this,T,r),!(t(this,T)instanceof P))throw new Error("invalid tapeBlock");e(this,B,[])}get tapeBlock(){return t(this,T)}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 E))throw new Error("Invalid parameters");const i=t(this,B);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,T)),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,T).currentSymbolList,nextSymbolList:n.tapeCommandList.map((e,r)=>{switch(e.symbol){case h.erase:return t(this,T).tapeList[r].alphabet.blankSymbol;case h.keep:return t(this,T).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,T).applyCommand(n),o.isHalt&&i.length&&(o=i.pop()),a!==o&&o.overrodeHaltState&&i.push(o.overrodeHaltState),a=o}}}export{o as Alphabet,u as Command,b as Reference,E as State,W as Tape,P as TapeBlock,p as TapeCommand,S as haltState,d as ifOtherSymbol,l as movements,h as symbolCommands};