Talk:BrainFlow

From Esolang
Jump to navigation Jump to search

C++ interpreter

   #include <iostream>
   // Variables
   /* Code: Change this */ unsigned char code[] = "";

   unsigned char tape[255];   // Tape
   unsigned char pointer = 0; // Pointer of the tape
   unsigned short loops[50];  // Brackets positions
   unsigned char loop = 0;    // Pointer of the brackets
   unsigned char iloop;
   bool runCode = true; 

   // Coded by: DmilkaSTD
   int main()
   {
       // Read avery character
       for(unsigned short index = 0; index < sizeof(code); ++index) {
           switch(code[index]) {
               case 43: // +
                    // Incremets the current cell
                   if(runCode) {++tape[pointer];}
                   break;
               case 45: // -
                   // Decrements the current cell
                   if(runCode) {--tape[pointer];}
                   break;
               case 60: // >
                    // Move pointer to right
                   if(runCode) {++pointer;}
                   break;
               case 62: // <
                    // Move pointer to left
                   if(runCode) {--pointer;}
                   break;
               case 46:  // .
                   // Outputs the current cell value (is a char so)
                   if(runCode) {std::cout<<tape[pointer];}
                   break;
               case 44:  // ,
                   // Get inputs and stores it in the current cell
                   if(runCode) {std::cin>>tape[pointer];}
                   break;
               case 91: // [
                   // Stores actual index position
                   if(runCode) {
                       ++loop;
                       loops[loop] = index;
                       if(tape[pointer] == 0) {
                           runCode = false;
                           iloop = loop;
                       }
                   } else {++loop;}
                   break;
               case 93: // ]
                   // Jumps to the last saved index position if the current cell is not 0
                   if(runCode) {
                       if(tape[pointer] == 0) {--loop;}
                       else {index = loops[loop];}
                   } else {if(loop > iloop) {--loop;}
                           else {runCode = true; --loop;}}
                   break;
               // New commands
               case 94: // ^
                   // Sets the pointer to the value of the current cell
                   pointer = tape[pointer];
                   break;
               case 61: // ^
                   // Sets the current cell to the pointer's value
                   tape[pointer] = pointer;
                   break;
               case 38: // &
                   // Im not going to explain this.
                   tape[pointer] = tape[tape[pointer]];
                   break;
           }
       }
   }

DmilkaSTD (talk) 15:32, 31 January 2020 (UTC)