Advertisement
a3f

BrainFuck Interpreter

a3f
Sep 20th, 2012
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. /*
  2.  * bfun.c ; BrainFuck UNleashed! (or BrainFuck fUN w/e)
  3.  */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. static char cell[3000];
  10. static char *ptr = cell;
  11. static FILE *input;
  12.  
  13. static char pBck, pAdv, cInc, cDec, tInp, tOut, jInz, jOiz;
  14. static char flow = 0, numpad = 0, wysiwyg = 0;
  15.  
  16. void process(char c, FILE *f);
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.     switch(argc)
  21.     {
  22.         int argn;
  23.         char *split;
  24.         case 1:
  25.             printf("bfun -- a C interpreter for Brainfuck ver 1.0\n"
  26.             "\n"
  27.             "Usage:\n"
  28.             "\tbfun filename [options]\n"
  29.             "Options:\n"
  30.             "\t-f \t Dump workflow to STDOUT\n"
  31.             "\t-n \t Use alternative charset for language (numpad keys)\n"
  32.             "\t-w \t WYSIWYG - I/O integers by real value (not ASCII)\n");
  33.             return 0;
  34.         default:
  35.            
  36.             for(argn=2;argn < argc;++argn)
  37.             {
  38.                 if (strlen(argv[argn]) == 2)
  39.                 {
  40.                     if((split = strstr(argv[argn], "-")) == NULL)
  41.                         goto Fail;
  42.                        
  43.                     switch(split[strlen(split) -1])
  44.                     {
  45.                         case 'f':
  46.                             flow = 1;
  47.                         break;
  48.                         case 'n':
  49.                             numpad = 1;
  50.                         break;
  51.                         case 'w':
  52.                             wysiwyg = 1;
  53.                         break;
  54.                         default:
  55.                             goto Fail;
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     Fail:
  61.                     printf("bfun: invalid option '%s'\n"
  62.                     "Usage: bfun filename [-f] [-n] [-w]\n", argv[argn]);
  63.                     return 0;
  64.                 }
  65.             }
  66.         case 2:
  67.             input = fopen(argv[1], "r");
  68.             if (!input) {
  69.                 fprintf(stderr, "Error: No such file %s\n", argv[1]);
  70.                 return 0;
  71.             }
  72.     }
  73.             ptr = &cell[0];
  74.    
  75.             if (numpad)
  76.                 pBck = '4',pAdv = '6',cInc = '+',cDec = '-',tInp = '1',tOut = '2',jInz = '9',jOiz = '3';
  77.             else
  78.                 pBck = '<',pAdv = '>',cInc = '+',cDec = '-',tInp = ',',tOut = '.',jInz = '[',jOiz = ']';
  79.  
  80.             while(!feof(input))
  81.                 process(getc(input), input);
  82.        
  83.             fclose(input);
  84.    
  85.     return 1;
  86. }
  87. void process(char cur, FILE * input)
  88. {
  89.     long pos;
  90.     char loopcom;
  91.    
  92.             if (cur == pBck)
  93.                 --ptr;
  94.             else if (cur == pAdv)
  95.                 ++ptr;
  96.             else if (cur == cInc)
  97.                 ++*ptr;
  98.             else if (cur == cDec)
  99.                 --*ptr;
  100.             else if (cur == tInp)
  101.                 *ptr = getchar();
  102.             else if (cur == tOut)
  103.                 putchar(*ptr);
  104.             else if (cur == jInz)
  105.             {
  106.                 pos = ftell(input);
  107.                 while (*ptr != 0)
  108.                 {
  109.                     fseek (input, pos, SEEK_SET);
  110.                     loopcom = getc(input);
  111.                     while(loopcom != jOiz && loopcom != EOF)
  112.                     {
  113.                         process(loopcom, input);
  114.                         loopcom = getc(input);
  115.                     }
  116.                    
  117.                 }
  118.             }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement