improve turing.js

This commit is contained in:
Christoffer Müller Madsen 2016-09-13 11:28:41 +02:00
parent 212f337688
commit 93425fd268
1 changed files with 66 additions and 66 deletions

132
turing.js
View File

@ -1,5 +1,29 @@
var step_delay = 0;
var port = 35209;
var verbose = false;
var printtape = false;
// TODO: Do better than this?
var trans_done = 0;
var tape_done = 0;
var head_loc = 0;
var state = 0;
var tape = ["_"];
var transitions = {};
var steps = 0;
var pretty_tape = "";
var fs = require('fs'); var fs = require('fs');
var readline = require('readline'); var readline = require('readline');
var express = require('express');
var web = express();
var transitionReader = readline.createInterface({ var transitionReader = readline.createInterface({
input: fs.createReadStream('transitions') input: fs.createReadStream('transitions')
}); });
@ -8,20 +32,16 @@ var tapeReader = readline.createInterface({
}); });
transitionReader.on('line', function(line) { transitionReader.on('line', function(line) {
console.log(line); if (verbose) console.log(line);
var file_transitions = line.split(/[\s,]/); var file_transitions = line.split(/[\s,]/);
if (!transitions[file_transitions[0]]) { if (!transitions[file_transitions[0]]) transitions[file_transitions[0]] = {};
transitions[file_transitions[0]] = {};
}
transitions[file_transitions[0]][file_transitions[1]] = { transitions[file_transitions[0]][file_transitions[1]] = {
to:file_transitions[2], to:file_transitions[2],
write:file_transitions[3], write:file_transitions[3],
move:file_transitions[4] move:file_transitions[4]
}; };
console.log(transitions);
}); });
@ -32,7 +52,6 @@ transitionReader.on('close', function() {
tapeReader.on('line', function(line) { tapeReader.on('line', function(line) {
console.log("Lol");
var symbols = line; var symbols = line;
for (var i = 0; i < symbols.length; i++) { for (var i = 0; i < symbols.length; i++) {
if (symbols[i] == " ") { if (symbols[i] == " ") {
@ -40,8 +59,6 @@ tapeReader.on('line', function(line) {
} else { } else {
tape[i+1] = symbols[i]; tape[i+1] = symbols[i];
} }
console.log(tape);
} }
}); });
@ -51,71 +68,32 @@ tapeReader.on('close', function() {
}); });
var step_time = 10;
// TEMP SOLUTION
var trans_done = 0;
var tape_done = 0;
// END TEMP
var head_loc = 0;
var state = 0;
var tape = ["_"];
var transitions = {};
/*
var readLines = function(input, func) {
var linesToRead = '';
input.on('data', function(data) {
linesToRead += data;
var index = linesToRead.indexOf('\n');
var last = 0;
while (index > -1) {
var line = linesToRead.substring(last, index);
last = index + 1;
linesToRead = linesToRead.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function() {
if (linesToRead.length > 0) {
func(remaining);
}
});
}
*/
var initialiseTransitions = function(callback) {
}
var initialiseTape = function(callback) {
}
var step = function() { var step = function() {
console.log("Step!"); steps++;
if (verbose) console.log("START step");
if (tape[head_loc] == undefined) { if (tape[head_loc] == undefined) {
tape[head_loc] = "_"; tape[head_loc] = "_";
} }
if (verbose) {
pretty_tape = "";
for (i = 0; i < tape.length; i++) {
if (i == head_loc) pretty_tape = pretty_tape + "[";
pretty_tape = pretty_tape + tape[i];
if (i == head_loc) pretty_tape = pretty_tape + "]";
}
}
if (verbose) console.log(pretty_tape);
var cur_symbol = tape[head_loc]; var cur_symbol = tape[head_loc];
console.log("Symbol: %s | State: %s",cur_symbol,state); if (verbose) console.log("Symbol: %s | State: %s",cur_symbol,state);
//console.log(tape[head_loc]); // Get the transition based on the current state and symbol
var transition = transitions[state][cur_symbol]; var transition = transitions[state][cur_symbol];
console.log(transition);
// Write to tape // Write to tape
tape[head_loc] = transition.write; tape[head_loc] = transition.write;
@ -130,19 +108,26 @@ var step = function() {
head_loc--; head_loc--;
} }
console.log("State: %s | Head: %s",state,head_loc) if (verbose) console.log("State: %s | Head: %s",state,head_loc)
// Throw error if head was moved over the left edge of the tape
if (head_loc == -1) { if (head_loc == -1) {
throw "head_loc error"; throw "head_loc error";
} }
console.log("Tape %s",tape); if (printtape) console.log("Tape %s",tape);
setTimeout(step, step_time);
if (verbose) console.log("END step!");
setTimeout(step, step_delay);
} }
var prestep = function() { var prestep = function() {
// Check if the transitions and tape have been initialised
if (trans_done == 1 && tape_done == 1) { if (trans_done == 1 && tape_done == 1) {
if (verbose) console.log(transitions);
if (verbose) console.log(tape);
console.log("Ready! Stepping!"); console.log("Ready! Stepping!");
step(); step();
} else { } else {
@ -153,3 +138,18 @@ var prestep = function() {
prestep(); prestep();
// Set up web server
web.get('', function(req, res) {
var webtape = "";
for (i = 0; i < tape.length; i++) {
if (i == head_loc) webtape = webtape + "[";
webtape = webtape + tape[i];
if (i == head_loc) webtape = webtape + "]";
}
res.send(webtape + "<br><br>" + "Steps: " + steps);
});
web.listen(port, function () {
console.log("Server listening on port %s",port);
});