Task: Execute Brain____






PHP: <?php
function brainfuck_interpret(&$s, &$_s, &$d, &$_d, &$i, &$_i, &$o) {
   do {
     switch($s[$_s]) {
       case '+': $d[$_d] = chr(ord($d[$_d]) + 1); break;
       case '-': $d[$_d] = chr(ord($d[$_d]) - 1); break;
       case '>': $_d++; if(!isset($d[$_d])) $d[$_d] = chr(0); break;
       case '<': $_d--; break;
       case '.': $o .= $d[$_d]; break;
       case ',': $d[$_d] = $_i==strlen($i) ? chr(0) : $i[$_i++]; break;
       case '[':
         if((int)ord($d[$_d]) == 0) {
           $brackets = 1;
           while($brackets && $_s++ < strlen($s)) {
             if($s[$_s] == '[')
               $brackets++;
             else if($s[$_s] == ']')
               $brackets--;
           }
         }
         else {
             $pos = $_s++-1;
           if(brainfuck_interpret($s, $_s, $d, $_d, $i, $_i, $o))
             $_s = $pos;
         }
         break;
       case ']': return ((int)ord($d[$_d]) != 0);
    }
  } while(++$_s < strlen($s));
}

function brainfuck($source, $input='') {
  $data         = array();
  $data[0]      = chr(0);
  $data_index   = 0;
  $source_index = 0;
  $input_index  = 0;
  $output       = '';
  
  brainfuck_interpret($source, $source_index,
                      $data,   $data_index,
                      $input,  $input_index,
                      $output);
  return $output;
}

$code = "
    >++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>
    >+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++.
";
$inp = '123';
print brainfuck( $code, $inp );





Fortran:       MODULE BRAIN	
       INTEGER MSG,KBD
       CONTAINS		
        SUBROUTINE RUN(PROG,STORE)	
         CHARACTER*(*) PROG	
         CHARACTER*(1) STORE(:)	
         CHARACTER*1 C		
         INTEGER I,D		
          D = 1		
          I = 1		

          DO WHILE(I.LE.LEN(PROG))	
            C = PROG(I:I)			
            I = I + 1				
            SELECT CASE(C)			
             CASE(">"); D = D + 1				
             CASE("<"); D = D - 1				
             CASE("+"); STORE(D) = CHAR(ICHAR(STORE(D)) + 1)	
             CASE("-"); STORE(D) = CHAR(ICHAR(STORE(D)) - 1)	
             CASE("."); WRITE (MSG,1) STORE(D)			
             CASE(","); READ (KBD,1) STORE(D)			
             CASE("["); IF (ICHAR(STORE(D)).EQ.0) CALL SEEK(+1)	
             CASE("]"); IF (ICHAR(STORE(D)).NE.0) CALL SEEK(-1)	
             CASE DEFAULT				
		  						
            END SELECT				
          END DO			

    1     FORMAT (A1,$)	
         CONTAINS	
          SUBROUTINE SEEK(WAY)	
           INTEGER WAY		
           CHARACTER*1 AB,BA	
           INTEGER INDEEP	
            INDEEP = 0		
            I = I - 1		
            AB = PROG(I:I)	
            BA = "[ ]"(WAY + 2:WAY + 2)	
    1       IF (I.GT.LEN(PROG)) STOP "Out of code
            IF (PROG(I:I).EQ.AB) THEN		
              INDEEP = INDEEP + 1			
            ELSE IF (PROG(I:I).EQ.BA) THEN	
              INDEEP = INDEEP - 1			
            END IF				
            IF (INDEEP.GT.0) THEN	
              I = I + WAY			
              IF (I.GT.0) GO TO 1		
              STOP "Back to 0
            END IF			
            I = I + 1			
          END SUBROUTINE SEEK	
        END SUBROUTINE RUN	
      END MODULE BRAIN	

      PROGRAM POKE	
      USE BRAIN		
      CHARACTER*1 STORE(30000)	
      CHARACTER*(*) HELLOWORLD	
      PARAMETER (HELLOWORLD = "++++++++[>++++[>++>+++>+++>+<<<<-]"
     1 //" >+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------"
     2 //".--------.>>+.>++.")
      KBD = 5		
      MSG = 6		
      STORE = CHAR(0)	

      CALL RUN(HELLOWORLD,STORE)	

      END