@turing-machine-js/builder 2.0.1 → 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.cjs ADDED
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ var src = require('@turing-machine-js/machine/src');
4
+
5
+ const movementsMap = {
6
+ L: src.movements.left,
7
+ R: src.movements.right,
8
+ S: src.movements.stay,
9
+ };
10
+ const referenceKey = Symbol('reference');
11
+ const stateKey = Symbol('state');
12
+ function buildMachine({ alphabetString, initialState, finalStateList, states: stateNameToStateDeclarationMap, }) {
13
+ const alphabet = new src.Alphabet(alphabetString.split(''));
14
+ const machine = new src.TuringMachine({
15
+ tapeBlock: src.TapeBlock.fromAlphabets([alphabet]),
16
+ });
17
+ const { symbol: getSymbol } = machine.tapeBlock;
18
+ const stateNameToStateOrReferenceMap = finalStateList.reduce((result, finalState) => (Object.assign(Object.assign({}, result), { [finalState]: {
19
+ [stateKey]: src.haltState,
20
+ [referenceKey]: src.haltState,
21
+ } })), {});
22
+ Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
23
+ if (stateNameToStateDeclarationMap[stateName] != null) {
24
+ stateNameToStateOrReferenceMap[stateName] = {
25
+ [referenceKey]: new src.Reference(),
26
+ };
27
+ }
28
+ });
29
+ Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
30
+ var _a;
31
+ const stateDefinition = {};
32
+ Object.entries(stateNameToStateDeclarationMap[stateName]).forEach(([symbol, stateDeclaration]) => {
33
+ if (!alphabet.has(symbol)) {
34
+ throw new Error('invalid state declaration');
35
+ }
36
+ let nextSymbol = stateDeclaration.symbol;
37
+ if (nextSymbol === symbol) {
38
+ nextSymbol = src.symbolCommands.keep;
39
+ }
40
+ if (nextSymbol === alphabet.blankSymbol) {
41
+ nextSymbol = src.symbolCommands.erase;
42
+ }
43
+ const nextMovement = movementsMap[stateDeclaration.movement];
44
+ if (!nextMovement) {
45
+ throw new Error('invalid state declaration');
46
+ }
47
+ const nextState = stateNameToStateOrReferenceMap[stateDeclaration.state][referenceKey];
48
+ stateDefinition[getSymbol([symbol])] = {
49
+ command: {
50
+ symbol: nextSymbol,
51
+ movement: nextMovement,
52
+ },
53
+ nextState,
54
+ };
55
+ });
56
+ const state = new src.State(stateDefinition, stateName);
57
+ Object.assign(stateNameToStateOrReferenceMap[stateName], {
58
+ [stateKey]: state,
59
+ [referenceKey]: (_a = stateNameToStateOrReferenceMap[stateName][referenceKey]) === null || _a === void 0 ? void 0 : _a.bind(state),
60
+ });
61
+ });
62
+ const resultStates = Object.entries(stateNameToStateOrReferenceMap)
63
+ .reduce((result, [stateName, stateOrReference]) => (Object.assign(Object.assign({}, result), { [stateName]: stateOrReference[stateKey] })), {});
64
+ return [
65
+ machine,
66
+ resultStates[initialState],
67
+ resultStates,
68
+ ];
69
+ }
70
+
71
+ module.exports = buildMachine;
package/dist/index.mjs ADDED
@@ -0,0 +1,69 @@
1
+ import { movements, Alphabet, TuringMachine, TapeBlock, haltState, Reference, symbolCommands, State } from '@turing-machine-js/machine/src';
2
+
3
+ const movementsMap = {
4
+ L: movements.left,
5
+ R: movements.right,
6
+ S: movements.stay,
7
+ };
8
+ const referenceKey = Symbol('reference');
9
+ const stateKey = Symbol('state');
10
+ function buildMachine({ alphabetString, initialState, finalStateList, states: stateNameToStateDeclarationMap, }) {
11
+ const alphabet = new Alphabet(alphabetString.split(''));
12
+ const machine = new TuringMachine({
13
+ tapeBlock: TapeBlock.fromAlphabets([alphabet]),
14
+ });
15
+ const { symbol: getSymbol } = machine.tapeBlock;
16
+ const stateNameToStateOrReferenceMap = finalStateList.reduce((result, finalState) => (Object.assign(Object.assign({}, result), { [finalState]: {
17
+ [stateKey]: haltState,
18
+ [referenceKey]: haltState,
19
+ } })), {});
20
+ Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
21
+ if (stateNameToStateDeclarationMap[stateName] != null) {
22
+ stateNameToStateOrReferenceMap[stateName] = {
23
+ [referenceKey]: new Reference(),
24
+ };
25
+ }
26
+ });
27
+ Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
28
+ var _a;
29
+ const stateDefinition = {};
30
+ Object.entries(stateNameToStateDeclarationMap[stateName]).forEach(([symbol, stateDeclaration]) => {
31
+ if (!alphabet.has(symbol)) {
32
+ throw new Error('invalid state declaration');
33
+ }
34
+ let nextSymbol = stateDeclaration.symbol;
35
+ if (nextSymbol === symbol) {
36
+ nextSymbol = symbolCommands.keep;
37
+ }
38
+ if (nextSymbol === alphabet.blankSymbol) {
39
+ nextSymbol = symbolCommands.erase;
40
+ }
41
+ const nextMovement = movementsMap[stateDeclaration.movement];
42
+ if (!nextMovement) {
43
+ throw new Error('invalid state declaration');
44
+ }
45
+ const nextState = stateNameToStateOrReferenceMap[stateDeclaration.state][referenceKey];
46
+ stateDefinition[getSymbol([symbol])] = {
47
+ command: {
48
+ symbol: nextSymbol,
49
+ movement: nextMovement,
50
+ },
51
+ nextState,
52
+ };
53
+ });
54
+ const state = new State(stateDefinition, stateName);
55
+ Object.assign(stateNameToStateOrReferenceMap[stateName], {
56
+ [stateKey]: state,
57
+ [referenceKey]: (_a = stateNameToStateOrReferenceMap[stateName][referenceKey]) === null || _a === void 0 ? void 0 : _a.bind(state),
58
+ });
59
+ });
60
+ const resultStates = Object.entries(stateNameToStateOrReferenceMap)
61
+ .reduce((result, [stateName, stateOrReference]) => (Object.assign(Object.assign({}, result), { [stateName]: stateOrReference[stateKey] })), {});
62
+ return [
63
+ machine,
64
+ resultStates[initialState],
65
+ resultStates,
66
+ ];
67
+ }
68
+
69
+ export { buildMachine as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turing-machine-js/builder",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A turing machine builder",
5
5
  "engines": {
6
6
  "npm": ">=7.0.0"
@@ -25,17 +25,18 @@
25
25
  "builder"
26
26
  ],
27
27
  "dependencies": {
28
- "@turing-machine-js/machine": "^2.0.1"
28
+ "@turing-machine-js/machine": "^2.0.2"
29
29
  },
30
- "main": "dist/index.js",
31
- "module": "dist/index.js",
30
+ "main": "dist/index.cjs",
31
+ "module": "dist/index.mjs",
32
32
  "types": "dist/index.d.ts",
33
33
  "exports": {
34
34
  ".": {
35
35
  "types": "./dist/index.d.ts",
36
- "import": "./dist/index.js",
37
- "default": "./dist/index.js"
36
+ "import": "./dist/index.mjs",
37
+ "require": "./dist/index.cjs",
38
+ "default": "./dist/index.mjs"
38
39
  }
39
40
  },
40
- "gitHead": "18d8a7388cea7372a9e262452797699cea7b61dd"
41
+ "gitHead": "54da1870a45fd78496c6ba56ce1de728d766470b"
41
42
  }