-
Notifications
You must be signed in to change notification settings - Fork 6
/
Turing.js
executable file
·175 lines (143 loc) · 4.02 KB
/
Turing.js
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
// Simulates a Turing Machine
// ---------- VARS -----------
var Turing = true;
// List of states
var Qstates = [];
// Tape
var Tape = [];
var TapeIndex = 0;
// Start State
var Qzero = null;
// Accept States
var FStates = [];
// Alphabet
var Alphabet = new Array('X');
var currentState = null;
var nextState = null;
document.getElementById('RWS').style.visibility = "hidden";
function setup(){
Tape = document.getElementById('input').value.split("");
currentState = Qzero;
if( currentState == null ){
alert("No start state");
}
TapeIndex = 0;
nextState = null;
displayInputs(Tape.toString(),true);
}
function step(){
//DEBUG
//alert("Current State: " + currentState.label);
//alert("Input: " + Tape);
// Check the character on the tape using TapeIndex
var tempChar = Tape[TapeIndex];
// Get the transition attached to that character
var tempTran = currentState.transitions[tempChar];
// If there is no matching transition - Failure
if( tempTran == null ){
return false;
}
// Write the character attached to the transition
Tape[TapeIndex] = tempTran.writeCharacter;
// Move the TapeIndex according to the transition
TapeIndex += tempTran.tapeShift;
if( TapeIndex < 0 ){
Tape.unshift(0);
}
if( TapeIndex > Tape.length ){
Tape.push(0);
}
// Advance to the transitions "endState"
currentState = tempTran.endState;
// Update the input string
var tempstring = "";
for( i in Tape ){
tempstring += Tape[i];
}
document.getElementById('input').value = tempstring;
return true;
}
function readInputAnimated(){
// set up recursive loop
setTimeout(function(){
ctx.fillStyle="#B7AA86";
ctx.fillRect(0,0,c.width,c.height);
// draws the machine, needs to be done because we stopped the update
//drawMachine();
for(var i=0; i<Qstates.length; i++){
Qstates[i].display();
}
drawReadingCharacters(TapeIndex);
//drawHighlighted(currentState.x,currentState.y,currentState.radius+3);
// step with current input
foundAState = step();
if(!foundAState){ // at the end of the input list
if( $.inArray(currentState, FStates) != -1 ){
alert("Machine completed in accept State");
setAcceptedForInput(AcceptedForInput.ACCEPTED);
animating = false; // updates can start drawing again
return;
}
else{
alert("Not accepted, \n finished in state" + currentState.label);
setAcceptedForInput(AcceptedForInput.NOTACCEPTED);
animating = false; // updates can start drawing again
return;
}
}
// calls readInputAnimated, replaces for loop
requestAnimationFrame(readInputAnimated); // basic recursion
},1000); // updates every once every 1 second
}
function checkInput(){
//
}
function debugInput(){
setup();
readInputAnimated();
}
function setSelectedAsStart(){
if( selectedState != null ){
Qzero = selectedState;
//toggle this for display
}
}
// toggles the if a state is an accepted state or not
function setSelectedAsAccept(){
if( selectedState != null ){
if( $.inArray(selectedState, FStates) != -1){
i = FStates.indexOf(selectedState);
// remove it from the array
if(i != -1){
FStates.splice(i,1);
}
}
else{
FStates.push(selectedState);
//toggle this for display
selectedState.isAccept = !selectedState.isAccept;
console.log(selectedState.label + " is accept? " +selectedState.isAccept);
}
}
}
//Updates alphabets based on input box
function RefreshAlphabet(){
Alphabet = new Array();
var ab = document.getElementById('Alphabet').value;
for( var c in ab.split("") ){
Alphabet.push(ab.split("")[c]);
}
for(var i=0; i<Qstates.length; i++ ){
st = Qstates[i];
st.refreshTrans();
}
}
// Populates the transition property text boxes when a transition is selected
function populateRWS(){
if( selectedTran != null ){
document.getElementById('RWS').style.visibility='visible';
document.getElementById('Read').value = selectedTran.character;
document.getElementById('Write').value = selectedTran.writeCharacter;
document.getElementById('Shift').value = selectedTran.tapeShift;
}
}