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 readline = require('readline'); var express = require('express'); var web = express(); var transitionReader = readline.createInterface({ input: fs.createReadStream('transitions') }); var tapeReader = readline.createInterface({ input: fs.createReadStream('tape') }); transitionReader.on('line', function(line) { if (verbose) console.log(line); var file_transitions = line.split(/[\s,]/); if (!transitions[file_transitions[0]]) transitions[file_transitions[0]] = {}; transitions[file_transitions[0]][file_transitions[1]] = { to:file_transitions[2], write:file_transitions[3], move:file_transitions[4] }; }); transitionReader.on('close', function() { console.log("Done reading TRANSITIONS!"); trans_done = 1; }); tapeReader.on('line', function(line) { var symbols = line; for (var i = 0; i < symbols.length; i++) { if (symbols[i] == " ") { tape[i+1] = "_"; } else { tape[i+1] = symbols[i]; } } }); tapeReader.on('close', function() { console.log("Done reading TAPE!"); tape_done = 1; }); var step = function() { steps++; if (verbose) console.log("START step"); if (tape[head_loc] == undefined) { 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]; if (verbose) console.log("Symbol: %s | State: %s",cur_symbol,state); // Get the transition based on the current state and symbol var transition = transitions[state][cur_symbol]; // Write to tape tape[head_loc] = transition.write; // Change state state = transition.to; // Move head if (transition.move == 'R') { head_loc++; } else if (transition.move == 'L') { 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) { throw "head_loc error"; } if (printtape) console.log("Tape %s",tape); if (verbose) console.log("END step!"); setTimeout(step, step_delay); } var prestep = function() { // Check if the transitions and tape have been initialised if (trans_done == 1 && tape_done == 1) { if (verbose) console.log(transitions); if (verbose) console.log(tape); console.log("Ready! Stepping!"); step(); } else { console.log("Not ready! Waiting 100 ms..."); setTimeout(prestep, 100); } } 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 + "

" + "Steps: " + steps); }); web.listen(port, function () { console.log("Server listening on port %s",port); });