-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturing.html
More file actions
129 lines (127 loc) · 3.8 KB
/
turing.html
File metadata and controls
129 lines (127 loc) · 3.8 KB
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
<html><head>
<title>Turing</title>
<link rel="stylesheet" href="/css/khak.css" type="text/css" id="theme"></link>
<link rel="icon" href='/media/NPHotChoc.svg.png'></link>
</head>
<body >
<div class="gray notes">
...A turing machine...
<!-- <button height="35"> -->
<image src="/media/moon.svg" id="themech"
width="30" height="30" >
</image> <!-- </button> -->
<script type="text/javascript" src="/js/theme.js">
</script>
<br>
</div>
<div class="fg">
<blockquote style="text-align: left;">
<div id="test" hidden>
<pre id="out" style="max-height: 50%; overflow-y: auto;"><br>
Turing inputs
<br>
</pre>
=> <input type="search" autocomplete="on"
style="width: 500px; text-align: left;
font-family: 'SpiderWireMono'; font-size: 20pt;"
id="inp">
</input><br>
<div style="text-align: center;">
<button class="interactive switch">
Edit turing machine
</button><br>
</div>
</div>
<div id="write" style="text-align: center;"><br>
<pre>
<textarea id="machine" rows="20" cols="40" name="text"
placeholder="Custom Turing Machine:
A rule is:
[state] [ch] [newstate] [newch] [dir]
(should match
/^([^ ]*) (.) ([^ ]*) (.) ([<lrs>_])$/)
[dir] can be '<>_' or 'lrs' for left,
right, or stay, respectively.
The turing machine starts in state
begin and should end in end unless
there is an error
[ch] can be . to match any character
except one that's already matched
" class="interactive"
style="margin: auto; max-width: inherit;"
value>
</textarea></pre><br>
<button class="interactive switch">Try It!</button><br>
</write>
</blockquote></div>
<script src="/js/console.js" type="text/javascript"></script>
<script>
function turEval(tape, mach){
let state = "begin"
let indx = tape.lastIndexOf("|");
indx = indx >=0? indx : 0;
tape = tape.replace("|", "");
mach = mach.split("\n");
mach = mach.map( (str)=>{
return str.match(/^([^ ]*) (.) ([^ ]*) (.) ([<lrs>_])$/);
});
for (var i=0;i<mach.length;i++)
if(!(mach[i]))
console.log("malformed line #"+i);
while (state != "end"){
var i; var found = false;
found = false
while (indx<0){
indx++;
tape = "_"+tape;
}
while (indx > tape.length-1){
tape = tape+"_";
}
for (i=0;i<mach.length;i++){
if (!mach[i]) continue;
if (state==mach[i][1])
if ( tape[indx].match(new RegExp(mach[i][2])) ){
found = true;
break;
}
}
if (!found){
console.log("exited with "+state);
return tape;
}
tape = tape.substr(0,indx)+
mach[i][4]+tape.substr(indx+1);
switch(mach[i][5]){
case "<":
case "l":
indx--;
break;
case "r":
case ">":
indx++;
case "_":
case "s":
break;
default:
console.error("bad direction");
}
state = mach[i][3];
}
return tape;
}
myconsole.setinputhandler((str)=>{
console.log( turEval(str,
document.getElementById("machine").value) );
})
var wr = document.getElementById("write");
var ts = document.getElementById("test");
function toggle (){
wr.hidden = !wr.hidden;
ts.hidden = !wr.hidden;
}
buttons=document.getElementsByClassName("switch");
buttons[0].onclick = buttons[1].onclick = toggle;
document.getElementById("machine").value = "";
</script>
</body></html>