Pella86

Brainfuck interpreter in C

Feb 11th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define DIM_NASTRO 15
  6.  
  7. void stampa_nastro(int* nastro, int dim_nastro, int* cur_start, int* cursore){
  8.     int i;
  9.     for(i = 0; i < dim_nastro; i++)
  10.         printf("|%3d", nastro[i]);
  11.     printf("\n");
  12.     int cur_pos = ((int) (cursore - cur_start) )*4;
  13.     for(i = 0; i < cur_pos; i++)
  14.         printf(" ");
  15.     printf("  ^");
  16.     printf("\n");
  17. }
  18.  
  19.  
  20. int main()
  21. {
  22.     // adder
  23.     //char codice[] = "++>+++++[->+<]>.";
  24.  
  25.     // multiplier
  26.     //char codice[] = "+++++[->+++<]>.";
  27.  
  28.     // hello world
  29.     char codice[] = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
  30.  
  31.     // hello world but shorter (doesnt work yet)
  32.     //char codice[] = "--<-<<+[+[<+>--->->->-<<<]>]<<--.<++++++.<<-..<<.<+.>>.>>.<<<.+++.>>.>>-.<<<+.";
  33.  
  34.     char* istr_ptr = codice;
  35.     int nastro[DIM_NASTRO] = {0};
  36.     int* cursore = nastro;
  37.     //cursore += DIM_NASTRO / 2;
  38.  
  39.     int par_count;
  40.  
  41.     while(*istr_ptr){
  42.         //printf("istruzione: %c\n", *istr_ptr);
  43.         switch(*istr_ptr){
  44.             case '+':
  45.                 (*cursore)++;
  46.                 break;
  47.  
  48.             case '-':
  49.                 (*cursore)--;
  50.                 break;
  51.  
  52.             case '>':
  53.                 cursore++;
  54.                 break;
  55.  
  56.             case '<':
  57.                 cursore--;
  58.                 break;
  59.  
  60.             case '[':
  61.                 par_count = 1;
  62.                 if(*cursore == 0){
  63.                     istr_ptr++;
  64.                     while(par_count != 0){
  65.                         if(*istr_ptr == '[') par_count++;
  66.                         if(*istr_ptr == ']') par_count--;
  67.                         if(par_count == 0) break;
  68.                         istr_ptr++;
  69.                     }
  70.                 }
  71.                 break;
  72.             case ']':
  73.                 par_count = 1;
  74.                 if(*cursore != 0){
  75.                     istr_ptr--;
  76.                     while(par_count != 0){
  77.                         if(*istr_ptr == '[') par_count--;
  78.                         if(par_count == 0) break;
  79.                         if(*istr_ptr == ']') par_count++;
  80.  
  81.                         istr_ptr--;
  82.                     }
  83.                 }
  84.  
  85.                 break;
  86.  
  87.             case '.':
  88.                 printf("STDOUT: %3d %c\n", *cursore, (char) *cursore);
  89.                 break;
  90.  
  91.             case ',':
  92.                 scanf("%d", cursore);
  93.                 while(getchar() != '\n');
  94.                 break;
  95.         }
  96.         istr_ptr++;
  97.         //stampa_nastro(nastro, DIM_NASTRO, nastro, cursore);
  98.     }
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment