|
6 | 6 |
|
7 | 7 | import { Tape } from "./tape.js"; |
8 | 8 |
|
9 | | -/** |
10 | | - * Transition rule for the Turing machine |
11 | | - */ |
| 9 | + |
12 | 10 | export interface Transition { |
13 | 11 | currentState: string; |
14 | 12 | readSymbol: string; |
@@ -44,59 +42,50 @@ export class TuringMachine { |
44 | 42 | this.finalStates = new Set(finalStates); |
45 | 43 | } |
46 | 44 |
|
47 | | - /** |
48 | | - * Gets the current state |
49 | | - */ |
| 45 | + |
50 | 46 | getCurrentState(): string { |
51 | 47 | return this.currentState; |
52 | 48 | } |
53 | 49 |
|
54 | | - /** |
55 | | - * Gets the tape |
56 | | - */ |
| 50 | + |
57 | 51 | getTape(): Tape { |
58 | 52 | return this.tape; |
59 | 53 | } |
60 | 54 |
|
61 | | - /** |
62 | | - * Checks if the machine is in a final state |
63 | | - */ |
| 55 | + |
64 | 56 | isInFinalState(): boolean { |
65 | 57 | return this.finalStates.has(this.currentState); |
66 | 58 | } |
67 | 59 |
|
68 | | - /** |
69 | | - * Executes a single step of the machine |
70 | | - * @returns true if step was executed, false if machine is halted |
71 | | - */ |
| 60 | + |
72 | 61 | executeStep(): boolean { |
73 | | - // Check if already in final state |
| 62 | + |
74 | 63 | if (this.isInFinalState()) { |
75 | 64 | return false; |
76 | 65 | } |
77 | 66 |
|
78 | | - // Read current symbol |
| 67 | + |
79 | 68 | const readSymbol = this.tape.read(); |
80 | 69 |
|
81 | | - // Find matching transition |
| 70 | + |
82 | 71 | const transition = this.transitions.find( |
83 | 72 | (t) => t.currentState === this.currentState && t.readSymbol === readSymbol |
84 | 73 | ); |
85 | 74 |
|
86 | 75 | if (!transition) { |
87 | | - // No transition found - machine halts |
| 76 | + |
88 | 77 | return false; |
89 | 78 | } |
90 | 79 |
|
91 | | - // Execute transition: write, move, change state |
| 80 | + |
92 | 81 | this.tape.write(transition.writeSymbol); |
93 | 82 |
|
94 | 83 | if (transition.moveDirection === "L") { |
95 | 84 | this.tape.moveLeft(); |
96 | 85 | } else if (transition.moveDirection === "R") { |
97 | 86 | this.tape.moveRight(); |
98 | 87 | } |
99 | | - // If direction is neither L nor R, don't move (for "no move" behavior) |
| 88 | + |
100 | 89 |
|
101 | 90 | this.currentState = transition.nextState; |
102 | 91 |
|
@@ -126,23 +115,19 @@ export class TuringMachine { |
126 | 115 | this.currentState = this.initialState; |
127 | 116 | } |
128 | 117 |
|
129 | | - /** |
130 | | - * Gets all transitions |
131 | | - */ |
| 118 | + |
| 119 | + |
| 120 | + |
132 | 121 | getTransitions(): Transition[] { |
133 | | - return [...this.transitions]; // Return defensive copy |
| 122 | + return [...this.transitions]; |
134 | 123 | } |
135 | 124 |
|
136 | | - /** |
137 | | - * Gets initial state |
138 | | - */ |
| 125 | + |
139 | 126 | getInitialState(): string { |
140 | 127 | return this.initialState; |
141 | 128 | } |
142 | 129 |
|
143 | | - /** |
144 | | - * Gets final states |
145 | | - */ |
| 130 | + |
146 | 131 | getFinalStates(): string[] { |
147 | 132 | return Array.from(this.finalStates); |
148 | 133 | } |
|
0 commit comments