Created
May 30, 2012 07:54
-
-
Save amasad/2834435 to your computer and use it in GitHub Desktop.
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 bf (code) { | |
var codeSize = code.length; | |
var ip = 0, cp = 0, dp = 0, m = {}; | |
var loopIn = {} | |
, loopOut = {} | |
, tmp = []; | |
for (cp = 0; cp < codeSize ; cp++) | |
if (code[cp] == '[') tmp.push(cp); | |
else if (code[cp] == ']') loopOut[loopIn[cp] = tmp.pop()] = cp; | |
var ops = { | |
'>': function () { dp++; runNext();} | |
, '<': function () { dp--; runNext();} | |
, '+': function () {m[dp] = ((m[dp]||0)+1)&255; runNext();} | |
, '-': function () {m[dp] = ((m[dp]||0)-1)&255; runNext();} | |
, '.': function () {console.log(String.fromCharCode(m[dp])); runNext();} | |
, ',': function () { | |
console.input(function (data) { | |
m[dp] = data.charCodeAt(ip++) || 0; | |
runNext(); | |
}); | |
} | |
, '[': function () {m[dp]||(cp=loopOut[cp]); runNext();} | |
, ']': function () {cp = loopIn[cp]-1; runNext();} | |
}; | |
function noop () {} | |
function runNext () { | |
cp++; | |
if (cp < codeSize) { | |
(ops[code[cp]] || noop)(); | |
} | |
} | |
cp = -1; | |
runNext(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment