Skip to content

Instantly share code, notes, and snippets.

@beppe9000
Forked from azproduction/LICENSE.txt
Created August 19, 2020 12:25
Show Gist options
  • Save beppe9000/899aa661f2f25715d3bad71e78996e13 to your computer and use it in GitHub Desktop.
Save beppe9000/899aa661f2f25715d3bad71e78996e13 to your computer and use it in GitHub Desktop.
A turing machine in 79! bytes of javascript
// 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment