1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
const MESSAGES = {
SET_CELL: "SET_CELL",
PAUSE: "PAUSE",
SIMULATE: "SIMULATE",
SET_READER: "SET_READER",
NEXT_STEP: "NEXT_STEP",
COMPILE: "COMPILE",
COMPILE_STATUS: "COMPILE_STATUS",
NEW_TURING_STATE: "NEW_TURING_STATE",
};
const state = new Observable();
const inputCellId = (cellId) => `${cellId}-input`;
const setCellFromInput = (cellId, inputId) => {
const input = document.getElementById(inputId);
tape[cellId] = input.value;
};
const cell = (cellId, initValue = "NULL") => {
const cellDiv = document.createElement("div");
cellDiv.classList.add("cell");
cellDiv.id = cellId;
const readingHead = document.createElement("div");
readingHead.classList.add("circle");
const input = document.createElement("input");
const inputId = inputCellId(cellId);
input.classList.add("cell-input");
input.id = inputId;
input.value = initValue;
input.addEventListener("focusin", () =>
state.notify({ type: MESSAGES.PAUSE })
);
input.addEventListener("focusout", () =>
state.notify({ type: MESSAGES.SET_CELL, cell: cellId, value: input.value })
);
state.subscribe((msg) => {
if (msg.type == MESSAGES.SET_CELL && msg.cell == cellId) {
input.value = msg.value;
}
if (msg.type == MESSAGES.SET_READER) {
if (msg.cell == cellId) {
cellDiv.classList.add("reading");
cellDiv.scrollIntoView({
behavior: "smooth",
});
} else cellDiv.classList.remove("reading");
}
});
cellDiv.appendChild(input);
cellDiv.appendChild(readingHead);
return cellDiv;
};
const parseRules = (rules, beginState) => {
const quadruples = rules.split("\n");
const instructions = [];
quadruples.forEach((quadruple, line) => {
const commentLess = quadruple.replaceAll(/\/\/.*/g, "").trim();
if (!commentLess) {
return;
}
const items = commentLess
.split(" ")
.map((x) => x.trim())
.filter((x) => x);
if (items.length != 4) {
throw new Error(`Invalid instruction on line ${line}`);
}
instructions.push(items);
});
if (!instructions.some(([fromState]) => fromState == beginState)) {
throw new Error(
`At least one instruction must begin from state: ${beginState}`
);
}
return instructions;
};
// --
const tapeEl = document.getElementById("tape");
const compileButton = document.getElementById("compile");
const instructionsEl = document.getElementById("instructions");
const controlsEl = document.getElementById("controls");
const nextStepButton = document.getElementById("next_step");
const togglePlayButton = document.getElementById("toggle_play");
const compileStatusEl = document.getElementById("compile_status");
const turingMachineStateEl = document.getElementById("turing_state");
const resetButton = document.getElementById("reset");
resetButton.addEventListener("click", () => {
state.notify({ type: MESSAGES.PAUSE });
state.notify({ type: MESSAGES.COMPILE, value: instructionsEl.value });
});
compileButton.addEventListener("click", () => {
state.notify({ type: MESSAGES.COMPILE, value: instructionsEl.value });
});
nextStepButton.addEventListener("click", () => {
state.notify({ type: MESSAGES.PAUSE });
state.notify({ type: MESSAGES.NEXT_STEP });
});
let playButton_simulationStatus = "paused";
togglePlayButton.addEventListener("click", function () {
if (playButton_simulationStatus == "paused") {
state.notify({ type: MESSAGES.SIMULATE });
} else if (playButton_simulationStatus == "simulate") {
state.notify({ type: MESSAGES.PAUSE });
}
});
state.subscribe((msg) => {
if (msg.type == MESSAGES.COMPILE_STATUS) {
const { error, success } = msg;
if (error) {
compileStatusEl.classList.remove("success");
compileStatusEl.classList.add("error");
compileStatusEl.innerHTML = error;
}
if (success) {
compileStatusEl.classList.add("success");
compileStatusEl.classList.remove("error");
compileStatusEl.innerHTML = `Successful compile at ${new Date().toLocaleString()}!`;
}
}
});
state.subscribe((msg) => {
if (msg.type == MESSAGES.COMPILE_STATUS) {
const { error, success } = msg;
if (error) {
controlsEl.style.display = "none";
} else if (success) {
controlsEl.style.display = "block";
}
}
});
state.subscribe((msg) => {
if (msg.type == MESSAGES.PAUSE) {
togglePlayButton.innerHTML = "🔁 Begin";
playButton_simulationStatus = "paused";
}
if (msg.type == MESSAGES.SIMULATE) {
togglePlayButton.innerHTML = "⏸️ Pause";
playButton_simulationStatus = "simulate";
}
});
state.subscribe((msg) => {
if (msg.type == MESSAGES.NEW_TURING_STATE) {
turingMachineStateEl.innerHTML = msg.value;
}
});
// -
const main = () => {
const blank = "B";
const beginState = "q0";
const acceptState = "f";
const tape = Array(200).fill(blank);
const cells = tape.map((_, cellId) => cell(cellId, blank));
for (const cell of cells) {
tapeEl.appendChild(cell);
}
let turingMachine;
state.subscribe((msg) => {
if (msg.type == MESSAGES.SET_CELL) {
const { value, cell } = msg;
tape[cell] = value;
if (turingMachine) turingMachine.setTapeAtCell(cell, value);
}
if (msg.type == MESSAGES.COMPILE) {
const { value } = msg;
try {
const rules = parseRules(value, beginState, acceptState);
turingMachine = new TuringMachine(
[...tape],
rules,
beginState,
blank,
acceptState
);
state.notify({ type: MESSAGES.SET_READER, cell: 0 });
state.notify({
type: MESSAGES.NEW_TURING_STATE,
value: turingMachine.getStateStatus(),
});
state.notify({ type: MESSAGES.COMPILE_STATUS, success: true });
} catch (e) {
state.notify({ type: MESSAGES.COMPILE_STATUS, error: e.toString() });
}
}
if (msg.type == MESSAGES.SIMULATE) {
const interval = setInterval(() => {
state.notify({ type: MESSAGES.NEXT_STEP });
}, 300);
const subscriptionFn = (msg) => {
if (msg.type == MESSAGES.PAUSE) {
clearInterval(interval);
state.unsubscribe(subscriptionFn);
}
};
state.subscribe(subscriptionFn);
}
if (msg.type == MESSAGES.NEXT_STEP && turingMachine) {
const step = turingMachine.step();
const status = turingMachine.getStateStatus();
const cell = turingMachine.getHead();
if (!step) {
const accepting = turingMachine.isAccepting();
state.notify({
type: MESSAGES.NEW_TURING_STATE,
value: accepting
? `<span class='success'>Accept(${status})</span>`
: `<span class='error'>Fail(${status}}</span>`,
});
} else {
state.notify({
type: MESSAGES.NEW_TURING_STATE,
value: status,
});
}
state.notify({
type: MESSAGES.SET_READER,
cell,
});
state.notify({
type: MESSAGES.SET_CELL,
cell,
value: turingMachine.getTapeAtCell(cell),
});
if (!step)
state.notify({
type: MESSAGES.PAUSE,
});
}
});
};
main();
|