@turing-machine-js/builder 2.0.0-alpha.0 → 2.0.0-alpha.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.
@@ -0,0 +1,14 @@
1
+ import { State, TuringMachine } from '@turing-machine-js/machine/src';
2
+ declare const movementsMap: Record<'L' | 'R' | 'S', symbol>;
3
+ export type States = Record<string, Record<string, {
4
+ symbol: string;
5
+ movement: keyof typeof movementsMap;
6
+ state: string;
7
+ }>>;
8
+ export default function buildMachine({ alphabetString, initialState, finalStateList, states: stateNameToStateDeclarationMap, }: {
9
+ alphabetString: string;
10
+ initialState: string;
11
+ finalStateList: string[];
12
+ states: States;
13
+ }): readonly [TuringMachine, State, Record<string, State>];
14
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,66 @@
1
+ import { Alphabet, haltState, movements, Reference, State, symbolCommands, TapeBlock, TuringMachine, } from '@turing-machine-js/machine/src';
2
+ const movementsMap = {
3
+ L: movements.left,
4
+ R: movements.right,
5
+ S: movements.stay,
6
+ };
7
+ const referenceKey = Symbol('reference');
8
+ const stateKey = Symbol('state');
9
+ export default function buildMachine({ alphabetString, initialState, finalStateList, states: stateNameToStateDeclarationMap, }) {
10
+ const alphabet = new Alphabet(alphabetString.split(''));
11
+ const machine = new TuringMachine({
12
+ tapeBlock: TapeBlock.fromAlphabets([alphabet]),
13
+ });
14
+ const { symbol: getSymbol } = machine.tapeBlock;
15
+ const stateNameToStateOrReferenceMap = finalStateList.reduce((result, finalState) => (Object.assign(Object.assign({}, result), { [finalState]: {
16
+ [stateKey]: haltState,
17
+ [referenceKey]: haltState,
18
+ } })), {});
19
+ Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
20
+ if (stateNameToStateDeclarationMap[stateName] != null) {
21
+ stateNameToStateOrReferenceMap[stateName] = {
22
+ [referenceKey]: new Reference(),
23
+ };
24
+ }
25
+ });
26
+ Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
27
+ var _a;
28
+ const stateDefinition = {};
29
+ Object.entries(stateNameToStateDeclarationMap[stateName]).forEach(([symbol, stateDeclaration]) => {
30
+ if (!alphabet.has(symbol)) {
31
+ throw new Error('invalid state declaration');
32
+ }
33
+ let nextSymbol = stateDeclaration.symbol;
34
+ if (nextSymbol === symbol) {
35
+ nextSymbol = symbolCommands.keep;
36
+ }
37
+ if (nextSymbol === alphabet.blankSymbol) {
38
+ nextSymbol = symbolCommands.erase;
39
+ }
40
+ const nextMovement = movementsMap[stateDeclaration.movement];
41
+ if (!nextMovement) {
42
+ throw new Error('invalid state declaration');
43
+ }
44
+ const nextState = stateNameToStateOrReferenceMap[stateDeclaration.state][referenceKey];
45
+ stateDefinition[getSymbol([symbol])] = {
46
+ command: {
47
+ symbol: nextSymbol,
48
+ movement: nextMovement,
49
+ },
50
+ nextState,
51
+ };
52
+ });
53
+ const state = new State(stateDefinition, stateName);
54
+ Object.assign(stateNameToStateOrReferenceMap[stateName], {
55
+ [stateKey]: state,
56
+ [referenceKey]: (_a = stateNameToStateOrReferenceMap[stateName][referenceKey]) === null || _a === void 0 ? void 0 : _a.bind(state),
57
+ });
58
+ });
59
+ const resultStates = Object.entries(stateNameToStateOrReferenceMap)
60
+ .reduce((result, [stateName, stateOrReference]) => (Object.assign(Object.assign({}, result), { [stateName]: stateOrReference[stateKey] })), {});
61
+ return [
62
+ machine,
63
+ resultStates[initialState],
64
+ resultStates,
65
+ ];
66
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turing-machine-js/builder",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "A turing machine builder",
5
5
  "author": "Ruslan Gilmullin <mellonis14@gmain.com>",
6
6
  "homepage": "https://github.com/mellonis/turing-machine-js#readme",
@@ -22,8 +22,8 @@
22
22
  "builder"
23
23
  ],
24
24
  "dependencies": {
25
- "@turing-machine-js/machine": "^2.0.0-alpha.0"
25
+ "@turing-machine-js/machine": "^2.0.0-alpha.2"
26
26
  },
27
27
  "module": "dist/index.js",
28
- "gitHead": "eac8ba0029090bdbd64f72566657400101fd587e"
28
+ "gitHead": "5f0e4eaf5c2671438590a479261481ad4bec58bd"
29
29
  }