Skip to content
Browse files

turingdone

  • Loading branch information...
1 parent 029a167 commit e97644129e4813b4b2be7690c71b3572cdb6c598 @parkerbossier committed Jul 9, 2012
Showing with 153 additions and 0 deletions.
  1. +20 −0 app/webroot/css/responsive-customizations.css
  2. +133 −0 app/webroot/js/turing.js
View
20 app/webroot/css/responsive-customizations.css
@@ -0,0 +1,20 @@
+@media (max-width: 979px) {
+ .navbar-fixed-top {
+ margin-bottom: 0px;
+ }
+}
+@media (max-width: 767px) {
+ .navbar-fixed-top {
+ margin-left: 0px;
+ margin-right: 0px;
+ }
+
+ body {
+ padding-left: 0px;
+ padding-right: 0px;
+ }
+
+ #profile-pic {
+ margin-bottom: 15px;
+ }
+}
View
133 app/webroot/js/turing.js
@@ -0,0 +1,133 @@
+// these can be global to avoid having to pass copies all the time
+var matrix;
+var speed;
+var stop = false;
+
+// replaceAt
+String.prototype.replaceAt = function(index, replace_char) {
+ return this.substr(0, index) + replace_char + this.substr(index+replace_char.length);
+}
+
+// DOM load
+$(function() {
+ // speed change events
+ $('#iters_per_sec').keyup(function() {
+ speed = 1000/$('#iters_per_sec').val();
+ });
+
+ // trigger the keyup function to pre-populate the speed
+ $('#iters_per_sec').keyup();
+
+ // run click handler
+ $('#run').click(function() {
+ var self = $(this);
+ if (!self.hasClass('disabled')) {
+ self.addClass('disabled');
+ $('#stop').removeClass('disabled');
+
+ // get the matrix (global)
+ matrix = matrix_from_transitions();
+
+ // zero out the iterations
+ $('#iterations').val('0');
+
+ // run!
+ setTimeout(function() {
+ // not that the blank value is set here so thet the user can't modify the blank that the code sees during execution
+ var tape = $('#tape').val();
+ var head_loc = tape.indexOf('|');
+ if (head_loc == -1) head_loc = 0;
+ tape = tape.replace('|', '');
+ run(matrix[0][0], head_loc, tape, $('#blank_symbol').val());
+ }, speed);
+ }
+ });
+
+ // stop click handler
+ $('#stop').click(function() {
+ stop = true;
+ });
+});
+
+// does the actual computation
+function run(state, head_loc, tape, blank) {
+ // get the current tape value
+ var cur_input = get_input(head_loc, tape, blank);
+
+ // fetch the transition
+ var transition = get_transition(state, cur_input);
+
+ // done
+ if (transition === false) {
+ $('#run').removeClass('disabled');
+ alert('Done. Finished in state ' + state + '.');
+ return;
+ }
+
+ // update the tape
+ tape = tape.replaceAt(head_loc, transition[3]);
+ $('#tape').val(tape);
+
+ // update the state
+ state = transition[2];
+
+ // move the head
+ if (transition[4] == 'r' || transition[4] == 'R') {
+ ++head_loc;
+ } else {
+ --head_loc;
+ }
+
+ // increment iterations
+ $('#iterations').val(Number($('#iterations').val()) + 1);
+
+ // start again
+ if (stop) {
+ $('#run').removeClass('disabled');
+ } else {
+ setTimeout(function() {
+ run(state, head_loc, tape, blank);
+ }, speed);
+ }
+}
+
+// determine if the tape index is blank
+function get_input(index, tape, blank) {
+ if (index < 0 || index >= tape.length) {
+ return blank;
+ } else {
+ return tape[index];
+ }
+}
+
+// returns a delta-matrix
+function matrix_from_transitions() {
+ // loop through the rows
+ return $.map($('#transition_input').val().split('\n'), function(row, line_num) {
+ row = row.trim();
+ if (row[0] != '#') {
+ // error
+ if (!/^.+( )+.( )+.+( )+.( )+(l|r)$/i.test(row)) {
+ alert('Malformed transition at line ' + line_num + '. Skipping...');
+ }
+
+ // process the row
+ return [$.map(row.split(' '), function(item) {
+ if (item != '') {
+ return item;
+ }
+ })];
+ }
+ });
+}
+
+// returns the correct delta transition
+function get_transition(state, input) {
+ for (index in matrix) {
+ if (matrix[index][0] == state && matrix[index][1] == input) {
+ return matrix[index];
+ }
+ }
+
+ return false;
+}

0 comments on commit e976441

Please sign in to comment.
Something went wrong with that request. Please try again.