A Pen by Kyah Rindlisbacher on CodePen.
Created
January 19, 2024 00:04
-
-
Save L-four/b9a6f939e467f2193eb44f3de8f9fba6 to your computer and use it in GitHub Desktop.
brainFuck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h3> status: <span id='info'></span></h3><br/> | |
<label for="input">input</label><br/> | |
<input id="input" type="textfield" /><br/> | |
<label for="output">output</label><br/> | |
<textarea id="output" rows="4" cols="50"></textarea><br/> | |
<label for="code">your code</label><br/> | |
<textarea id="code" rows="10" cols="50" >++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.</textarea> | |
<button id="run"> Run </button></br> | |
<textarea id="mem" rows="10" cols="50" ></textarea> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var p = 0, // memory pointer | |
m = [0], // memory | |
lcp = 0, // last code pointer | |
cp = 0, // code pointer | |
stack = [], // loop stack | |
codes = '', // code | |
opc = '', // op code | |
o = document.querySelector('#output'), | |
i = document.querySelector('#input'), | |
info = document.querySelector('#info'), | |
code = document.querySelector('#code'), | |
run = document.querySelector('#run'), | |
mem = document.querySelector('#mem'), | |
read = function() { | |
if (i.value.length != 0) { | |
m[p] = i.value.charCodeAt(0); i.value = ''; | |
i.removeEventListener('change', read); | |
info.textContent = 'running'; | |
step(); | |
} | |
}, | |
opcodes = { | |
'+': function () { | |
m[p]++; | |
cp++; | |
}, | |
'-': function () { | |
m[p]--; | |
cp++; | |
}, | |
'>': function () { | |
if (m.length - 1 == p) { | |
m.push(0) | |
} | |
p++; | |
cp++; | |
}, | |
'<': function () { | |
if (p == 0) {console.error('negative mems')} else {p--;} | |
cp++; | |
}, | |
'.': function () { | |
o.value = o.value + String.fromCharCode(m[p]); | |
cp++; | |
}, | |
',': function () { | |
info.textContent = 'Waiting for input'; | |
i.addEventListener('change', read); | |
cp++; | |
return 1; | |
}, | |
'[': function () { | |
if (m[p] == 0) { | |
var opens = 0; | |
cp++; | |
while (opens != 0 || codes[cp] != ']') { | |
if (codes[cp] == '[') { | |
opens++; | |
} | |
else if (codes[cp] == ']' && opens > 0) { | |
opens--; | |
} | |
cp++; | |
} | |
cp++; | |
} else { | |
stack.push(cp); | |
cp++; | |
}}, | |
']': function () { | |
if (m[p] == 0) { | |
stack.pop(); | |
cp++; | |
} else { | |
cp = stack.pop(); | |
}} | |
}, | |
reset = function() { | |
info.textContent = 'Finished'; | |
if (isNaN(cp)) { | |
var lcp_less_5 = lcp - 5; | |
var str = ''; | |
for (var j = lcp_less_5; j < lcp + 5 && j < codes.length; j++) { | |
str = str + ' ' + ((j == lcp)? '*': '') + j + ':' + codes[j]; | |
} | |
info.textContent = 'Error: ' + codes.substr(lcp - 5, 10) + '(' + str + ')'; | |
} | |
p = 0; | |
m = [0]; | |
cp = 0; | |
stack = []; | |
run.disabled = false; | |
run.addEventListener('click', interpret) | |
}, | |
step = function() { | |
var counter = 0; | |
var t0 = performance.now(); | |
while (true) { | |
if (counter > 50000 && performance.now() - t0 > 1000) { | |
mem.value = "" + | |
" p :" + p + "\n" + | |
" cp :" + cp + "\n" + | |
" lcp :" + lcp + "\n" + | |
" opc :" + opc + "\n" + | |
" stack :" + JSON.stringify(stack) + "\n" + | |
" m :" + JSON.stringify(m) + "\n"; | |
setTimeout(step,0); | |
break; | |
} | |
if (codes.length - 1 < cp || isNaN(cp)) { | |
reset(); | |
break; | |
} | |
var opc = codes[cp]; | |
if (opcodes.hasOwnProperty(opc)) { | |
lcp = cp; | |
if (opcodes[opc]()) { | |
break; | |
} | |
} | |
else { | |
cp++; | |
} | |
counter++; | |
} | |
}, | |
interpret = function() { | |
o.value = ''; | |
codes = code.value; | |
run.removeEventListener('click', interpret) | |
run.disabled = true; | |
step(); | |
}; | |
info.textContent = 'Waiting'; | |
run.addEventListener('click', interpret) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment