Task: Execute Brain____





Java: import java.io.IOException;

public class Interpreter {

    public final static int MEMORY_SIZE = 65536;

    private final char[] memory = new char[MEMORY_SIZE];
    private int dp;
    private int ip;
    private int border;

    private void reset() {

        for (int i = 0; i < MEMORY_SIZE; i++) {
            memory[i] = 0;
        }
        ip = 0;
        dp = 0;
    }

    private void load(String program) {

        if (program.length() > MEMORY_SIZE - 2) {
            throw new RuntimeException("Not enough memory.");
        }

        reset();

        for (; dp < program.length(); dp++) {
            memory[dp] = program.charAt(dp);
        }

        
        
        border = dp;

        dp += 1;
    }

    public void execute(String program) {

        load(program);
        char instruction = memory[ip];

        while (instruction != 0) {

            switch (instruction) {
                case '>':
                    dp++;
                    if (dp == MEMORY_SIZE) {
                        throw new RuntimeException("Out of memory.");
                    }
                    break;
                case '<':
                    dp--;
                    if (dp == border) {
                        throw new RuntimeException("Invalid data pointer.");
                    }
                    break;
                case '+':
                    memory[dp]++;
                    break;
                case '-':
                    memory[dp]--;
                    break;
                case '.':
                    System.out.print(memory[dp]);
                    break;
                case ',':
                    try {
                        
                        memory[dp] = (char) System.in.read();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case '[':
                    if (memory[dp] == 0) {
                        skipLoop();
                    }
                    break;
                case ']':
                    if (memory[dp] != 0) {
                        loop();
                    }
                    break;
                default:
                    throw new RuntimeException("Unknown instruction.");
            }

            instruction = memory[++ip];
        }
    }

    private void skipLoop() {

        int loopCount = 0;

        while (memory[ip] != 0) {
            if (memory[ip] == '[') {
                loopCount++;
            } else if (memory[ip] == ']') {
                loopCount--;
                if (loopCount == 0) {
                    return;
                }
            }
            ip++;
        }

        if (memory[ip] == 0) {
            throw new RuntimeException("Unable to find a matching ']'.");
        }
    }

    private void loop() {

        int loopCount = 0;

        while (ip >= 0) {
            if (memory[ip] == ']') {
                loopCount++;
            } else if (memory[ip] == '[') {
                loopCount--;
                if (loopCount == 0) {
                    return;
                }
            }
            ip--;
        }

        if (ip == -1) {
            throw new RuntimeException("Unable to find a matching '['.");
        }
    }

    public static void main(String[] args) {

        Interpreter interpreter = new Interpreter();
        interpreter.execute(">++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>->+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.");
    }
}






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