Advertisement
Guest User

Untitled

a guest
May 26th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. unsigned char cells[30000] = {0};   //the tape
  4.  
  5. FILE *inFile = NULL;            //file to read brainfuck from
  6. int fileSize = 0;               //length of file text
  7.  
  8. int position = 0;           //where i am on the tape
  9. int loopcount = 0;          //how far i am nested in a loop
  10. int counter;
  11.  
  12. char *bf;                   //string containing brainfuck to be interpretted
  13.  
  14. int main()
  15. {
  16.     inFile = fopen("test.bf", "r");
  17.     while(fgetc(inFile) != EOF)
  18.     {
  19.         fileSize++;
  20.     }
  21.    
  22.     rewind(inFile);
  23.         bf = (char *)malloc(sizeof(char) * (fileSize + 1 + 1)); // one for space one for null
  24.     for(counter = 0; counter < fileSize; counter++)
  25.     {
  26.             *(bf + counter) = fgetc(inFile);
  27.     }
  28.     printf("\n%s", bf); //print the .bf source code
  29.     if (inFile)
  30.     {
  31.             for (counter = 0; counter < fileSize; counter++)
  32.         switch(bf[counter])
  33.         {
  34.             case '+':
  35.                     cells[position]++;
  36.             break;
  37.             case '-':
  38.                 cells[position]--;
  39.             break;
  40.             case '>':
  41.                 position++;
  42.                 if (position > 30000)
  43.                 {
  44.                     position = 0;
  45.                 }
  46.             break;
  47.             case '<':
  48.                 position--;
  49.                 if (position < 0)
  50.                 {
  51.                     position = 30000;
  52.                 }
  53.             break;
  54.             case '[':
  55.                 if(!cells[position])
  56.                 {
  57.                     loopcount++;
  58.                     while(loopcount)
  59.                     {
  60.                         loopcount++;
  61.                         if(bf[counter] == ']')
  62.                         {
  63.                                 loopcount--;
  64.                         }
  65.                         else if(bf[counter] == '[')
  66.                         {
  67.                                 loopcount++;
  68.                         }
  69.                     }
  70.                 }
  71.             break;
  72.             case ']':
  73.                 if(cells[position])
  74.                 {
  75.                     loopcount++;
  76.                     while(loopcount)
  77.                     {
  78.                             counter--;
  79.                         if(bf[counter] == '[')
  80.                         {
  81.                             loopcount--;
  82.                         }
  83.                         else if(bf[counter] == ']')
  84.                         {
  85.                             loopcount++;
  86.                         }
  87.                     }
  88.                 }
  89.             break;
  90.             case '.': // output pointer value
  91.                 putchar(cells[position]);
  92.             break;
  93.             case ',': // read in value
  94.                 cells[position] = getchar();
  95.             break;
  96.         }
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement