version 1.3
Original idea by http://swizec.com/blog/a-turing-machine-in-133-bytes-of-javascript/swizec/3069#
Demo http://jsfiddle.net/WcjZ5/6/
Thx @subzey, @Kambfhase, @tsaniel for help!
version 1.3
Original idea by http://swizec.com/blog/a-turing-machine-in-133-bytes-of-javascript/swizec/3069#
Demo http://jsfiddle.net/WcjZ5/6/
Thx @subzey, @Kambfhase, @tsaniel for help!
// added some curly braces for readability | |
function( | |
a, // {Object} program @see test.html for details | |
b, // {Number[]} tape | |
c, // {String} end state | |
d, // {String} start state | |
e // [{Number} = 0] caret position | |
) { | |
while( | |
d < c // while ! eof program | |
) { | |
/* 'e |= 0' - if e is undefined - reset to 0 else leave as is */ | |
with (/* q = */a[d][b[e |= 0] || "B"]) { // push current program statement aka "q" to the top of current scope | |
b[e] = w, // chenge symbol under caret, w is the item of "q" | |
e += m, // move caret by ..., m is the item of "q" | |
d = n; // jump to next state, n is the item of "q" | |
} | |
} | |
return b | |
} |
function(a,b,c,d,e){while(d<c)with(a[d][b[e|=0]||"B"])b[e]=w,e+=m,d=n;return b} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "turing_machine", | |
"description": "A turing machine implementation", | |
"keywords": [ | |
"turing_machine" | |
] | |
} |
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>1,1,1,0,1,1,1</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var tm = function(a,b,c,d,e){while(d<c)with(a[d][b[e|=0]||"B"])b[e]=w,e+=m,d=n;return b}; | |
var program = {"q0": {"1": {"w": "B", "m": 1, "n": "q1"}}, | |
"q1": {"1": {"w": "1", "m": 1, "n": "q1"}, | |
"0": {"w": "0", "m": 1, "n": "q2"}, | |
"B": {"w": "0", "m": 1, "n": "q2"}}, | |
"q2": {"1": {"w": "1", "m": 1, "n": "q2"}, | |
"B": {"w": "1", "m": -1, "n": "q3"}}, | |
"q3": {"1": {"w": "1", "m": -1, "n": "q3"}, | |
"0": {"w": "0", "m": -1, "n": "q3"}, | |
"B": {"w": "1", "m": 1, "n": "q4"}}, | |
"q4": {"1": {"w": "B", "m": 1, "n": "q1"}, | |
"0": {"w": "0", "m": 1, "n": "q5"}}}; | |
var tape = [1,1,1]; | |
document.getElementById("ret").innerHTML = tm(program, tape, "q5", "q0", 0); | |
</script> |
Might well happen (b[e|=0]) = 0, then ||"B" will cause absolutely wrong course of actions. It's a pure bug. Sorry.