summaryrefslogtreecommitdiff
path: root/src/toys/turing/js/machine.js
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-07-05 12:42:44 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-07-05 12:42:44 -0700
commit94b912b649150b9863a62096ba4740b2cc8ad21a (patch)
tree1882236fc980fa5946d3089a2f2e4670f3a70580 /src/toys/turing/js/machine.js
parentab0ddcaeb1fc37351d14b89100e07d3a343ba9e1 (diff)
downloadlizdotcoffee-94b912b649150b9863a62096ba4740b2cc8ad21a.tar.gz
lizdotcoffee-94b912b649150b9863a62096ba4740b2cc8ad21a.zip
Factor
Diffstat (limited to 'src/toys/turing/js/machine.js')
-rw-r--r--src/toys/turing/js/machine.js75
1 files changed, 0 insertions, 75 deletions
diff --git a/src/toys/turing/js/machine.js b/src/toys/turing/js/machine.js
deleted file mode 100644
index 6af4be6..0000000
--- a/src/toys/turing/js/machine.js
+++ /dev/null
@@ -1,75 +0,0 @@
-export class TuringMachine {
- constructor({
- tape,
- rules,
- startState,
- acceptStates = [],
- rejectStates = []
- }) {
- this.tape = tape;
- this.rules = rules;
- this.state = startState;
- this.acceptStates = new Set(acceptStates);
- this.rejectStates = new Set(rejectStates);
- this.iteration = 0;
- }
-
- step() {
- if (this.isHalted()) {
- return false;
- }
-
- const currentSymbol = this.tape.readHead();
- const ruleKey = this.getRuleKey(this.state, currentSymbol);
- if (!this.rules.has(ruleKey)) {
- return false;
- }
-
- const { nextState, writeSymbol, direction } = this.rules.get(ruleKey);
- this.tape.writeHead(writeSymbol);
-
- if (direction === "R") {
- this.tape.moveRight();
- } else if (direction === "L") {
- this.tape.moveLeft();
- }
-
- this.state = nextState;
- this.iteration += 1;
- return !this.isHalted();
- }
-
- canStep() {
- if (this.isHalted()) {
- return false;
- }
-
- const currentSymbol = this.tape.readHead();
- const ruleKey = this.getRuleKey(this.state, currentSymbol);
- return this.rules.has(ruleKey);
- }
-
- getRuleKey(state, symbol) {
- return `${state}:${symbol}`;
- }
-
- isAccepting() {
- return this.acceptStates.has(this.state);
- }
-
- isRejecting() {
- return this.rejectStates.has(this.state);
- }
-
- isHalted() {
- return this.isAccepting() || this.isRejecting();
- }
-
- getStateStatus() {
- return `State: ${this.state}, Step: ${this.iteration}`;
- }
-
- getState() {
- return this.state;
- }
-}