-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.js
130 lines (116 loc) · 3.81 KB
/
machine.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
"use strict"
var placeholder = 'Enter your program in following format:\n[State][Value] > [State][Value][Go]\n\nWhere State = {\'A\'..\'Z\'}\nGo = {\'L\', \'R\', Nothing}\nValue is any Symbol you want.\n\nProgram Example:\nA1 > B1R\nB1 > B1R\nB0 > Z0\n\nAlso you can use multiline comments.\n[This is a comment]\n\n[\nThis is a comment too\n]';
var textarea; var output; var TM; var countMax = 100;
function executeCode() {
output.innerHTML = '';
// Don't ever do smth like that
var code = '-' + textarea.value.replace(/ /g,'').replace(/\r?\n|\r/g, '-').replace(/\[[^\[]*?\]/g, '');
var command = ''; var count = 0;
TM = new TuringMachine(document.getElementById('machine').value, true);
TM.print(output);
while (command[0] != 'Z') {
var block = TM.getFullState();
if (code.indexOf(block) != -1) {
block = '-' + block;
command = code.substring(code.indexOf(block) + 4, code.indexOf(block) + 7)
TM.setState(command[0]);
TM.setValue(command[1]);
TM.go(command[2]);
} else {
command = 'Z0';
alert('No commands left. Did you forget to add Z?');
}
console.log('code: ' + code);
console.log('block: ' + block);
console.log('command: ' + command);
if (code != '-') TM.print(output);
if (count > countMax) {
alert('Error. More than ' + countMax + ' operations. \nLooks like infinite loop.');
break;
}
count++;
}
output.scrollTop = output.scrollHeight;
}
window.onload = function () {
textarea = document.getElementsByTagName('textarea')[0]
output = document.getElementById('output');
textarea.placeholder = placeholder;
}
window.onkeypress = function(e){
var keyCode = e.keyCode || e.which;
if (keyCode == '13' && e.shiftKey){
executeCode();
}
}
/*TuringMachine Class*/
function TuringMachine(rawdata, rawcustomAlphabet) {
var customAlphabet = rawcustomAlphabet == null? false : rawcustomAlphabet;
var rawdata = !(rawdata == null || rawdata.length == 0)? rawdata.split(' ') : [0];
var data;
var pointer = 0;
var maxPointer;
var currentState = 'A';
var initializeData = function () {
data = new Array();
for (var i = 0; i < rawdata.length; i++)
if (rawdata[i].length == 3) {
for (var j = parseInt(rawdata[i][2]); j > 0; j--) {
data.push(customAlphabet? rawdata[i][0] : Number(rawdata[i][0]));
}
}
else {
data.push(customAlphabet? rawdata[i] : Number(rawdata[i]));
}
maxPointer = data.length;
}
var goLeft = function () {
pointer--;
if (pointer < 0) {
data.unshift(0);
pointer = 0;
}
}
var goRight = function () {
pointer++;
if (pointer > (maxPointer - 1)) {
data.push(0);
maxPointer++;
}
}
this.go = function (where) {
switch (where) {
case 'R': goRight(); break;
case 'L': goLeft(); break;
}
}
this.setState = function (state) { currentState = state }
this.setValue = function (value) {
if (!customAlphabet) value = (value <= 0) ? 0 : 1;
data[pointer] = value;
}
this.getFullState = function () {
return currentState + data[pointer];
console.log(data[pointer]);
}
this.print = function (output) {
var machineBody= '';
var spaces = ' ';
var line = '';
for (var i = 0; i < data.length; i++) {
machineBody += '| ' + data[i] + ' ';
line += '----';
}
machineBody += '|'; line += '-';
for (var i = 0; i < pointer; i++) {
spaces += ' ';
}
output.innerHTML += spaces + currentState + '<br>' + spaces + '∨' + '<br>' + line + '<br>' + machineBody + '<br>' + line + '<br>';
}
this.toggleCustomAlphabet = function () {
customAlphabet = !customAlphabet;
customAlphabet? console.log('Custom Alphabet On') : console.log('Custom Alphabet Off');
initializeData();
}
initializeData();
}