@turing-machine-js/builder 2.0.1 → 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.
- package/CHANGELOG.md +42 -0
- package/README.md +62 -0
- package/dist/index.cjs +76 -0
- package/dist/index.d.ts +1 -1
- package/dist/{index.js → index.mjs} +15 -7
- package/package.json +11 -10
- package/tsconfig.tsbuildinfo +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [3.0.1] - 2026-04-30
|
|
8
|
+
|
|
9
|
+
Released in lockstep with `@turing-machine-js/machine` 3.0.1. No source or behavior changes in this package.
|
|
10
|
+
|
|
11
|
+
## [3.0.0] - 2026-04-30
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- **Status: not actively developed by the author.** The package still works and existing tests pass; the same state-table construction pattern is now shown as an inline example in [`@turing-machine-js/machine`'s README](../machine/README.md). Most users won't need this package as a separate dependency. Contributions are welcome — see the README for areas a contributor could pick up.
|
|
16
|
+
- **BREAKING** — `@turing-machine-js/machine` is now a `peerDependency` (was a regular `dependency`). npm 7+ auto-installs peers; older npm may need explicit installation.
|
|
17
|
+
- Peer-dep range bumped to `^3.0.0` to require the v3 line of the engine.
|
|
18
|
+
- Fixed `repository.directory` in `package.json` (was `packages/machine`, now correct `packages/builder`).
|
|
19
|
+
- Internal source files now import the bare `@turing-machine-js/machine` (was `@turing-machine-js/machine/src`).
|
|
20
|
+
|
|
21
|
+
### Migration
|
|
22
|
+
|
|
23
|
+
If you previously had:
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
{ "dependencies": { "@turing-machine-js/builder": "^2.0.2" } }
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
You should now have both as direct dependencies (npm 7+ does this automatically):
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@turing-machine-js/machine": "^3.0.0",
|
|
35
|
+
"@turing-machine-js/builder": "^3.0.0"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## [2.0.2] - earlier
|
|
41
|
+
|
|
42
|
+
Initial public 2.x release.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @turing-machine-js/builder
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@turing-machine-js/builder)
|
|
4
|
+
|
|
5
|
+
> **Status: not actively developed by the author.** The package still works and existing tests pass — but no new features are planned. The same state-table construction pattern is shown as an inline example in [`@turing-machine-js/machine`'s README](../machine/README.md), so most users won't need this package as a separate dependency. **Contributions are welcome** if you'd like to extend it (e.g. multi-tape support, OR-patterns, a string-DSL parser shipped with the package itself).
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
Constructs a Turing machine from a declarative state-table object. Every transition is a single `(state, currentSymbol) → (nextState, nextSymbol, movement)` row — the simplest possible API surface, matching how state machines are typically presented in textbooks.
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
import { Tape } from '@turing-machine-js/machine';
|
|
13
|
+
import buildMachine from '@turing-machine-js/builder';
|
|
14
|
+
|
|
15
|
+
const [machine, initialState] = buildMachine({
|
|
16
|
+
alphabetString: '_01XY#',
|
|
17
|
+
initialState: 'Q8',
|
|
18
|
+
finalStateList: ['Q5'],
|
|
19
|
+
states: {
|
|
20
|
+
Q8: { '#': { state: 'Q6', symbol: '#', movement: 'R' } },
|
|
21
|
+
Q6: {
|
|
22
|
+
'0': { state: 'Q0', symbol: 'X', movement: 'R' },
|
|
23
|
+
'1': { state: 'Q1', symbol: 'Y', movement: 'R' },
|
|
24
|
+
'#': { state: 'Q2', symbol: '#', movement: 'R' },
|
|
25
|
+
},
|
|
26
|
+
// ... more states ...
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
machine.tapeBlock.replaceTape(new Tape({
|
|
31
|
+
alphabet: machine.tapeBlock.alphabets[0],
|
|
32
|
+
symbols: '#011#'.split(''),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
machine.run({ initialState, stepsLimit: 100 });
|
|
36
|
+
// tape now contains: #011#011# (the input duplicated)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
See [`builder.spec.ts`](src/builder.spec.ts) for a complete worked example, including a small parser that reads the `(state,symbol)→(state,symbol,movement);` textual notation often used in textbooks.
|
|
40
|
+
|
|
41
|
+
## Limitations
|
|
42
|
+
|
|
43
|
+
The state-table format is intentionally minimal. It does **not** support:
|
|
44
|
+
|
|
45
|
+
- **OR-patterns** (matching multiple current symbols with one transition row). For `tapeBlock.symbol('^10$')` style patterns, use the raw `@turing-machine-js/machine` API.
|
|
46
|
+
- **Multi-tape machines** (`buildMachine` is single-tape only).
|
|
47
|
+
- **`withOverrodeHaltState` composition** (the subroutine-call mechanism). For composed machines like `library-binary-numbers`'s `minusOne`, use the raw API.
|
|
48
|
+
|
|
49
|
+
If you need any of the above, the inline state-table example in [`@turing-machine-js/machine`'s README](../machine/README.md) shows how to write your own `buildMachine`-equivalent in ~30 lines, and you can extend it to fit your case.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm install @turing-machine-js/machine @turing-machine-js/builder
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`@turing-machine-js/machine` is a peer dependency (so consumer and library share the same singleton sentinels — `haltState`, `ifOtherSymbol`, etc.).
|
|
58
|
+
|
|
59
|
+
## Links
|
|
60
|
+
|
|
61
|
+
- [Turing Machine](https://en.wikipedia.org/wiki/Turing_machine) on Wikipedia
|
|
62
|
+
- [`@turing-machine-js/machine`](https://github.com/mellonis/turing-machine-js/tree/master/packages/machine) — the core engine, sufficient on its own for most use cases
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var machine = require('@turing-machine-js/machine');
|
|
4
|
+
|
|
5
|
+
const movementsMap = {
|
|
6
|
+
L: machine.movements.left,
|
|
7
|
+
R: machine.movements.right,
|
|
8
|
+
S: machine.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 machine.Alphabet(alphabetString.split(''));
|
|
14
|
+
const machine$1 = new machine.TuringMachine({
|
|
15
|
+
tapeBlock: machine.TapeBlock.fromAlphabets([alphabet]),
|
|
16
|
+
});
|
|
17
|
+
const { symbol: getSymbol } = machine$1.tapeBlock;
|
|
18
|
+
const stateNameToStateOrReferenceMap = finalStateList.reduce((result, finalState) => ({
|
|
19
|
+
...result,
|
|
20
|
+
[finalState]: {
|
|
21
|
+
[stateKey]: machine.haltState,
|
|
22
|
+
[referenceKey]: machine.haltState,
|
|
23
|
+
},
|
|
24
|
+
}), {});
|
|
25
|
+
Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
|
|
26
|
+
if (stateNameToStateDeclarationMap[stateName] != null) {
|
|
27
|
+
stateNameToStateOrReferenceMap[stateName] = {
|
|
28
|
+
[referenceKey]: new machine.Reference(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
|
|
33
|
+
const stateDefinition = {};
|
|
34
|
+
Object.entries(stateNameToStateDeclarationMap[stateName]).forEach(([symbol, stateDeclaration]) => {
|
|
35
|
+
if (!alphabet.has(symbol)) {
|
|
36
|
+
throw new Error('invalid state declaration');
|
|
37
|
+
}
|
|
38
|
+
let nextSymbol = stateDeclaration.symbol;
|
|
39
|
+
if (nextSymbol === symbol) {
|
|
40
|
+
nextSymbol = machine.symbolCommands.keep;
|
|
41
|
+
}
|
|
42
|
+
if (nextSymbol === alphabet.blankSymbol) {
|
|
43
|
+
nextSymbol = machine.symbolCommands.erase;
|
|
44
|
+
}
|
|
45
|
+
const nextMovement = movementsMap[stateDeclaration.movement];
|
|
46
|
+
if (!nextMovement) {
|
|
47
|
+
throw new Error('invalid state declaration');
|
|
48
|
+
}
|
|
49
|
+
const nextState = stateNameToStateOrReferenceMap[stateDeclaration.state][referenceKey];
|
|
50
|
+
stateDefinition[getSymbol([symbol])] = {
|
|
51
|
+
command: {
|
|
52
|
+
symbol: nextSymbol,
|
|
53
|
+
movement: nextMovement,
|
|
54
|
+
},
|
|
55
|
+
nextState,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
const state = new machine.State(stateDefinition, stateName);
|
|
59
|
+
Object.assign(stateNameToStateOrReferenceMap[stateName], {
|
|
60
|
+
[stateKey]: state,
|
|
61
|
+
[referenceKey]: stateNameToStateOrReferenceMap[stateName][referenceKey]?.bind(state),
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
const resultStates = Object.entries(stateNameToStateOrReferenceMap)
|
|
65
|
+
.reduce((result, [stateName, stateOrReference]) => ({
|
|
66
|
+
...result,
|
|
67
|
+
[stateName]: stateOrReference[stateKey],
|
|
68
|
+
}), {});
|
|
69
|
+
return [
|
|
70
|
+
machine$1,
|
|
71
|
+
resultStates[initialState],
|
|
72
|
+
resultStates,
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = buildMachine;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { State, TuringMachine } from '@turing-machine-js/machine
|
|
1
|
+
import { State, TuringMachine } from '@turing-machine-js/machine';
|
|
2
2
|
declare const movementsMap: Record<'L' | 'R' | 'S', symbol>;
|
|
3
3
|
export type States = Record<string, Record<string, {
|
|
4
4
|
symbol: string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Alphabet,
|
|
1
|
+
import { movements, Alphabet, TuringMachine, TapeBlock, haltState, Reference, symbolCommands, State } from '@turing-machine-js/machine';
|
|
2
|
+
|
|
2
3
|
const movementsMap = {
|
|
3
4
|
L: movements.left,
|
|
4
5
|
R: movements.right,
|
|
@@ -6,16 +7,19 @@ const movementsMap = {
|
|
|
6
7
|
};
|
|
7
8
|
const referenceKey = Symbol('reference');
|
|
8
9
|
const stateKey = Symbol('state');
|
|
9
|
-
|
|
10
|
+
function buildMachine({ alphabetString, initialState, finalStateList, states: stateNameToStateDeclarationMap, }) {
|
|
10
11
|
const alphabet = new Alphabet(alphabetString.split(''));
|
|
11
12
|
const machine = new TuringMachine({
|
|
12
13
|
tapeBlock: TapeBlock.fromAlphabets([alphabet]),
|
|
13
14
|
});
|
|
14
15
|
const { symbol: getSymbol } = machine.tapeBlock;
|
|
15
|
-
const stateNameToStateOrReferenceMap = finalStateList.reduce((result, finalState) => (
|
|
16
|
+
const stateNameToStateOrReferenceMap = finalStateList.reduce((result, finalState) => ({
|
|
17
|
+
...result,
|
|
18
|
+
[finalState]: {
|
|
16
19
|
[stateKey]: haltState,
|
|
17
20
|
[referenceKey]: haltState,
|
|
18
|
-
}
|
|
21
|
+
},
|
|
22
|
+
}), {});
|
|
19
23
|
Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
|
|
20
24
|
if (stateNameToStateDeclarationMap[stateName] != null) {
|
|
21
25
|
stateNameToStateOrReferenceMap[stateName] = {
|
|
@@ -24,7 +28,6 @@ export default function buildMachine({ alphabetString, initialState, finalStateL
|
|
|
24
28
|
}
|
|
25
29
|
});
|
|
26
30
|
Object.keys(stateNameToStateDeclarationMap).forEach((stateName) => {
|
|
27
|
-
var _a;
|
|
28
31
|
const stateDefinition = {};
|
|
29
32
|
Object.entries(stateNameToStateDeclarationMap[stateName]).forEach(([symbol, stateDeclaration]) => {
|
|
30
33
|
if (!alphabet.has(symbol)) {
|
|
@@ -53,14 +56,19 @@ export default function buildMachine({ alphabetString, initialState, finalStateL
|
|
|
53
56
|
const state = new State(stateDefinition, stateName);
|
|
54
57
|
Object.assign(stateNameToStateOrReferenceMap[stateName], {
|
|
55
58
|
[stateKey]: state,
|
|
56
|
-
[referenceKey]:
|
|
59
|
+
[referenceKey]: stateNameToStateOrReferenceMap[stateName][referenceKey]?.bind(state),
|
|
57
60
|
});
|
|
58
61
|
});
|
|
59
62
|
const resultStates = Object.entries(stateNameToStateOrReferenceMap)
|
|
60
|
-
.reduce((result, [stateName, stateOrReference]) => (
|
|
63
|
+
.reduce((result, [stateName, stateOrReference]) => ({
|
|
64
|
+
...result,
|
|
65
|
+
[stateName]: stateOrReference[stateKey],
|
|
66
|
+
}), {});
|
|
61
67
|
return [
|
|
62
68
|
machine,
|
|
63
69
|
resultStates[initialState],
|
|
64
70
|
resultStates,
|
|
65
71
|
];
|
|
66
72
|
}
|
|
73
|
+
|
|
74
|
+
export { buildMachine as default };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turing-machine-js/builder",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A turing machine builder",
|
|
3
|
+
"version": "3.0.1",
|
|
4
|
+
"description": "A turing machine builder — declarative state-table construction. Not actively developed by the author; the same state-table pattern is also shown as an inline example in @turing-machine-js/machine's README. Contributions welcome.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"npm": ">=7.0.0"
|
|
7
7
|
},
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "https://github.com/mellonis/turing-machine-js.git",
|
|
17
|
-
"directory": "packages/
|
|
17
|
+
"directory": "packages/builder"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/mellonis/turing-machine-js/issues?utf8=✓&q=is%3Aissue+is%3Aopen+label%3A%22pkg%3A+builder%22+label%3Abug"
|
|
@@ -24,18 +24,19 @@
|
|
|
24
24
|
"machine",
|
|
25
25
|
"builder"
|
|
26
26
|
],
|
|
27
|
-
"
|
|
28
|
-
"@turing-machine-js/machine": "^
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@turing-machine-js/machine": "^3.0.0"
|
|
29
29
|
},
|
|
30
|
-
"main": "dist/index.
|
|
31
|
-
"module": "dist/index.
|
|
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.
|
|
37
|
-
"
|
|
36
|
+
"import": "./dist/index.mjs",
|
|
37
|
+
"require": "./dist/index.cjs",
|
|
38
|
+
"default": "./dist/index.mjs"
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "0a8d7ff87a3a73ea7ee1844f7f4602cbb23d20fe"
|
|
41
42
|
}
|
package/tsconfig.tsbuildinfo
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["./src/index.ts"],"version":"5.9.3"}
|