Created
          December 4, 2014 06:38 
        
      - 
      
 - 
        
Save ccbikai/772f3d6faff3dce58bad to your computer and use it in GitHub Desktop.  
Revisions
- 
        
不给力的面条 created this gist
Dec 4, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ function getMatchingBra(code) { var stack = []; var bra = []; for(var i = 0; i < code.length; i++) bra.push(-1); for(var i = 0; i < code.length; i++) { if(code[i] === '[') { stack.push(i); } else if(code[i] === ']') { bra[i] = stack.pop(); bra[bra[i]] = i; } } return bra; } function brainFuck(code, input) { var inputPos = 0; var commandPos = 0; var pointerPos = 0; var bytes = [ 0 ]; var output = ""; var matching = getMatchingBra(code); while(commandPos < code.length) { switch(code[commandPos]) { case '>': { pointerPos++; if(undefined === bytes[pointerPos]) { bytes.push(0); } break; } case '<': { pointerPos--; if(0 > pointerPos) { bytes.unshift(0); pointerPos = 0; } break; } case '+': { bytes[pointerPos] = (bytes[pointerPos] + 1) % 256; break; } case '-': { bytes[pointerPos]--; if(bytes[pointerPos] < 0) bytes[pointerPos] = 0; break; } case '.': { output += String.fromCharCode(bytes[pointerPos]); break; } case ',': { var temp = input.charCodeAt(inputPos++); bytes[pointerPos] = temp; break; } case '[': { if(!bytes[pointerPos]) { commandPos = matching[commandPos]; } break; } case ']': { if(bytes[pointerPos]) { commandPos = matching[commandPos]; } break; } } commandPos++; } return output; } var out = brainFuck('+++++++[>+++++[>++>+<<-]>>>+>++>++<<<<<<-]>>+++.[->+<]>.+++.[->+<]>.>[-<->]<---.>>[-<<->>]<<--.'); console.log(out);