Advertisement
Guest User

Brainfuck Interpreter

a guest
May 12th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #define BUF_SIZE 30000
  2. #define CODE_SIZE 4096
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct linked_list
  8. {
  9.     int index;
  10.     struct linked_list *next;
  11. };
  12.  
  13. struct linked_list *stack_add(struct linked_list *list, int data)
  14. {
  15.     struct linked_list *ptr = malloc(sizeof(struct linked_list));
  16.     ptr->next = list;
  17.     ptr->index = data;
  18.     return ptr;
  19. }
  20.  
  21. struct linked_list *stack_remove(struct linked_list *list)
  22. {
  23.     struct linked_list *ptr = list->next;
  24.     free(list);
  25.     return ptr;
  26. }
  27.  
  28. int interpret(char *code, unsigned char *buf, int index)
  29. {
  30.     struct linked_list *brackets = NULL;
  31.     int c_index = 0;
  32.    
  33.     while(code[c_index])
  34.     {
  35.         //printf("c_index = %d\tcode = %c\tindex = %d\tbuf = %d\n",c_index,code[c_index],index,buf[index]);
  36.         switch(code[c_index++])
  37.         {
  38.             case '>':
  39.                 index++;
  40.                 break;
  41.             case '<':
  42.                 index--;
  43.                 break;
  44.             case '+':
  45.                 buf[index]++;
  46.                 break;
  47.             case '-':
  48.                 buf[index]--;
  49.                 break;
  50.             case '.':
  51.                 putchar(buf[index]);
  52.                 break;
  53.             case ',':
  54.                 buf[index] = getchar();
  55.                 break;
  56.             case '[':
  57.                 if(!buf[index])
  58.                 {
  59.                     while(code[c_index++]!=']');
  60.                     if((brackets!=NULL) && (brackets->index == index))
  61.                         brackets = stack_remove(brackets);
  62.                 }  
  63.                 else
  64.                     if((brackets==NULL) || (brackets->index != index))
  65.                         brackets = stack_add(brackets, index);
  66.                 break;
  67.             case ']':
  68.                 while(code[--c_index]!='[');
  69.                 break;
  70.         }
  71.     }
  72.     return index;
  73. }
  74.  
  75. void execute(char *code)
  76. {
  77.     unsigned char buf[BUF_SIZE] = {0};
  78.     interpret(code, buf, 0);
  79. }
  80.  
  81. int main(int argc, char **argv)
  82. {
  83.     if(argc>1)
  84.     {
  85.         int size;
  86.         FILE *file = fopen(argv[1], "r");
  87.         char *code;
  88.         if(file == NULL)
  89.         {
  90.             printf("Error, could not open file (%s)\n", argv[1]);
  91.             return 0;
  92.         }
  93.         fseek(file, 0, SEEK_END);
  94.         size = ftell(file);
  95.         rewind(file);
  96.         code = malloc(size);
  97.         fread(code, size, 1, file);
  98.         printf("loaded program: (%d bytes)\n%s\nStarting...\n", size, code);
  99.         execute(code);
  100.         return 0;
  101.     }
  102.     else
  103.     {
  104.         unsigned char buffer[BUF_SIZE] = {0};
  105.         char code[CODE_SIZE];
  106.         int buf_index = 0;
  107.         printf("Brainfuck Interpreter Shell\n");
  108.         while(1)
  109.         {
  110.             char open_loop = 0;
  111.             int index = 0;
  112.             printf("\n>> ");
  113.             do
  114.             {
  115.                 fgets(&code[index], CODE_SIZE, stdin);
  116.                 for(;code[index]!=0;index++)
  117.                     if(code[index]=='[')
  118.                         open_loop++;
  119.                     else
  120.                         if(code[index]==']')
  121.                             open_loop--;
  122.                 if(open_loop)
  123.                     printf(">>\t");
  124.             } while(open_loop | !index);
  125.             buf_index = interpret(code, buffer, buf_index);
  126.         }
  127.     }
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement