Pella86

Brainfuck interpreter in C vs 1

Feb 12th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 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.     // fibonacci
  35.     //char codice[] = ">++++++++++>+>+[[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>]<<<]";
  36.  
  37.     char* istr_ptr = codice;
  38.     int nastro[DIM_NASTRO] = {0};
  39.     int* cursore = nastro;
  40.     //cursore += DIM_NASTRO / 2;
  41.  
  42.     char c_output[100] = {'\0'};
  43.     char* c_out_ptr = c_output;
  44.  
  45.     int par_count;
  46.     int istr_count = 0;
  47.  
  48.     while(*istr_ptr && istr_count < 5000){
  49.         //printf("istruzione: %c\n", *istr_ptr);
  50.         switch(*istr_ptr){
  51.             case '+':
  52.                 (*cursore)++;
  53.                 break;
  54.  
  55.             case '-':
  56.                 (*cursore)--;
  57.                 break;
  58.  
  59.             case '>':
  60.                 cursore++;
  61.                 break;
  62.  
  63.             case '<':
  64.                 cursore--;
  65.                 break;
  66.  
  67.             case '[':
  68.                 par_count = 1;
  69.                 if(*cursore == 0){
  70.                     istr_ptr++;
  71.                     while(par_count != 0){
  72.                         if(*istr_ptr == '[') par_count++;
  73.                         if(*istr_ptr == ']') par_count--;
  74.                         if(par_count == 0) break;
  75.                         istr_ptr++;
  76.                     }
  77.                 }
  78.                 break;
  79.             case ']':
  80.                 par_count = 1;
  81.                 if(*cursore != 0){
  82.                     istr_ptr--;
  83.                     while(par_count != 0){
  84.                         if(*istr_ptr == '[') par_count--;
  85.                         if(par_count == 0) break;
  86.                         if(*istr_ptr == ']') par_count++;
  87.  
  88.                         istr_ptr--;
  89.                     }
  90.                 }
  91.                 break;
  92.  
  93.             case '.':
  94.                 printf("STDOUT: %3d %c\n", *cursore, (char) *cursore);
  95.                 *c_out_ptr = (char) *cursore;
  96.                 c_out_ptr++;
  97.                 break;
  98.  
  99.             case ',':
  100.                 scanf("%d", cursore);
  101.                 while(getchar() != '\n');
  102.                 break;
  103.         }
  104.         istr_ptr++;
  105.         //stampa_nastro(nastro, DIM_NASTRO, nastro, cursore);
  106.         istr_count++;
  107.     }
  108.  
  109.     printf("The program run %d instructions\n\n", istr_count);
  110.  
  111.     printf("OUTPUT:\n");
  112.     int i;
  113.     for(i = 0; i < 100; i++){
  114.         if(c_output[i] == '\0') break;
  115.         printf("%c", c_output[i]);
  116.     }
  117.     printf("\n");
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment