Execute Brain**** is an implementation of Brainf***.
Other implementations of Brainf***.
RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages.

Below are links to each of the versions of RCBF.

An implementation need only properly implement the following instructions:



Command

Description


 > 
 Move the pointer to the right


 < 
 Move the pointer to the left


 + 
 Increment the memory cell under the pointer


 - 
 Decrement the memory cell under the pointer


 . 
 Output the character signified by the cell at the pointer


 , 
 Input a character and store it in the cell at the pointer


 [ 
 Jump past the matching ] if the cell under the pointer is 0


 ] 
 Jump back to the matching [ if the cell under the pointer is nonzero

Any cell size is allowed,   EOF   (End-O-File)   support is optional, as is whether you have bounded or unbounded memory.




fcn bf(pgm,input=""){ pgm=pgm.text;  // handle both String and Data
   const CELLS=0d30_000;
   if(Void==pgm.span("[","]")){ println("Mismatched brackets"); return(); }
   fcn(code,z,jmpTable){ // build jump table (for [ & ])
      if(span:=code.span("[","]")){
	 a,b:=span; b+=a-1; jmpTable[a+z]=b+z; jmpTable[b+z]=a+z;
	 self.fcn(code[a+1,b-a-1],z+a+1,jmpTable);
	 self.fcn(code[b+1,*],z+b+1,jmpTable);
      }
   }(pgm,0,jmpTable:=Dictionary());
 
   tape:=CELLS.pump(Data(CELLS,Int),0);
   ip:=dp:=0; input=input.walker();
   try{
      while(1){
	 switch(pgm[ip]){
	    case(">"){ dp+=1 }
	    case("<"){ dp-=1 }
	    case("+"){ tape[dp]=tape[dp]+1 }
	    case("-"){ tape[dp]=tape[dp]-1 }
	    case("."){ tape[dp].toChar().print() }
	    case(","){ c:=input._next(); tape[dp]=(c and input.value or 0); }
	    case("["){ if(0==tape[dp]){ ip=jmpTable[ip] }}
	    case("]"){ if(tape[dp])   { ip=jmpTable[ip] }}
	 }
	 ip+=1;
      } // while
   }catch(IndexError){}  // read past end of tape == end of program
}