Created
October 10, 2012 23:01
-
-
Save bigomega/3869102 to your computer and use it in GitHub Desktop.
BrainFuck Javascript Compiler
This file contains 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
var strip=[0]; | |
var index=0; | |
var loopStack=new Array(); | |
var loopFlag=0; | |
var innerLoopCount=0; | |
input=document.getElementById('inp').value; | |
for(i=0;i<input.length;i++){ | |
var element=input[i]; | |
if(loopFlag==1){ | |
if(element=='[') | |
innerLoopCount++; | |
else if(element==']'){ | |
if(innerLoopCount==0) | |
loopFlag=0; | |
else | |
innerLoopCount--; | |
} | |
continue; | |
} | |
switch(element){ | |
case '+': | |
strip[index]++; | |
break; | |
case '-': | |
strip[index]--; | |
break; | |
case ',': | |
temp=prompt(); | |
strip[index]=temp && temp.charCodeAt(0)||0; | |
console.log(strip[index]);//its ascii value | |
break; | |
case '.': | |
document.getElementById("out").innerHTML+=String.fromCharCode(strip[index])||0; | |
break; | |
case '>': | |
index+=1; | |
strip[index]=strip[index]||0; | |
break; | |
case '<': | |
index-=1; | |
strip[index]=strip[index]||0; | |
//if (index<0) {alert("Array-Out-Of-Bound-Exception:\ncrossed the left end of the strip");return false;};//Finite start | |
break; | |
case '[': | |
if(strip[index]==0) | |
loopFlag=1; | |
else | |
loopStack.push(i); | |
break; | |
case ']': | |
if(strip[index]!=0) | |
i=loopStack[loopStack.length-1]; | |
else | |
loopStack.pop(); | |
break; | |
default: | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints what you type
,[.,]
Hello World
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
bugs