https://github.com/mellonis/post-machine-js
🧘🏼♂️ A convenient Post-Turing machine (based on @turing-machine-js/machine)
https://github.com/mellonis/post-machine-js
post-machine post-machine-simulator simulator turing-machine
Last synced: about 1 month ago
JSON representation
🧘🏼♂️ A convenient Post-Turing machine (based on @turing-machine-js/machine)
- Host: GitHub
- URL: https://github.com/mellonis/post-machine-js
- Owner: mellonis
- License: gpl-3.0
- Created: 2019-08-15T18:50:49.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2026-03-06T17:05:20.000Z (4 months ago)
- Last Synced: 2026-03-06T19:57:12.050Z (4 months ago)
- Topics: post-machine, post-machine-simulator, simulator, turing-machine
- Language: TypeScript
- Homepage:
- Size: 2.27 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# post-machine-js
[](https://github.com/mellonis/post-machine-js/actions/workflows/main.yml)
[](https://coveralls.io/github/mellonis/post-machine-js?branch=master)
[](https://github.com/users/mellonis/projects/5)
A convenient Post machine.
Under the hood, the `PostMachine` class builds some `State`s for `TuringMachine` from provided instructions. When you run it, it runs the built TuringMachine.
This repository contains following packages:
* [@post-machine-js/machine](https://github.com/mellonis/post-machine-js/tree/master/packages/machine)
## Installation
`@post-machine-js/machine` declares [@turing-machine-js/machine](https://github.com/mellonis/turing-machine-js) as a **peer dependency**, so both use the same Turing machine implementation (one instance in the bundle). Install:
```bash
npm install @turing-machine-js/machine @post-machine-js/machine
```
# An example
A tape contains two marked sections divided by the blank symbol(s). The issue is to move the first section close to the second. In other words, to remove blank symbols between these sections.
This example demonstrates an issue solving.
```javascript
import {
PostMachine, check, erase, left, mark, right, stop, Tape,
} from '@post-machine-js/machine';
const machine = new PostMachine({
10: erase,
20: right,
30: check(20, 40),
40: mark,
50: right,
60: check(70, 90),
70: left,
80: stop,
90: left,
100: check(90, 110),
110: right(10),
});
machine.replaceTapeWith(new Tape({
alphabet: machine.tape.alphabet,
symbols: ['*', '*', '*', ' ', ' ', ' ', '*'],
}));
console.log(machine.tape.symbols.join('').trim()); // *** *
await machine.run();
console.log(machine.tape.symbols.join('').trim()); // ****
```
# An example with subroutines
A tape contains a marked section. The issue is to duplicate it.
This example demonstrates an issue solving with subroutines. A subroutine is a peace of code which can be reused multiple times. The issue could be solved without subroutines at all, but with them the algorithm looks more readable.
```javascript
import {
PostMachine, call, check, erase, left, mark, right, stop, Tape,
} from '@post-machine-js/machine';
const machine = new PostMachine({
leftAndGoToBlank: {
1: left,
2: check(1, 3),
3: stop,
},
rightAndGoToBlank: {
1: right,
2: check(1, 3),
3: stop,
},
markTwoCells: {
1: [mark, right, mark],
},
1: call('leftAndGoToBlank'),
2: [right, erase],
3: call('rightAndGoToBlank'),
4: call('rightAndGoToBlank'),
5: call('markTwoCells'),
6: call('leftAndGoToBlank'),
7: left,
8: check(1, 9),
9: stop,
});
// the first run
machine.replaceTapeWith(new Tape({
alphabet: machine.tape.alphabet,
symbols: ['*'],
}));
console.log(machine.tape.symbols.join('').trim()); // *
await machine.run();
console.log(machine.tape.symbols.join('').trim()); // **
// the second run
machine.replaceTapeWith(new Tape({
alphabet: machine.tape.alphabet,
symbols: ['*', '*', '*'],
}));
console.log(machine.tape.symbols.join('').trim()); // ***
await machine.run();
console.log(machine.tape.symbols.join('').trim()); // ******
```