Vegetarianboy30

quick and dirty branflakes interpreter in C

Jan 17th, 2021 (edited)
768
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  qdb.c -- quick and dirty branflakes interpreter.
  2.     Version 1.2
  3.     Anonymous Pastebin User  */
  4.  
  5. /*  Make any use you like of this software. I can't stop you anyway. :)*/
  6.  
  7. /*  This interpreter uses byte cells, wraps around on overflow or
  8.     underflow, has unpredictable misbehavior if the pointer is moved
  9.     outside the array, and interprets '#' as a command to output the
  10.     first 64 cells of the array, plus a '^' for the pointer. It
  11.     should be fairly fast, but no match for a decent compiler or even
  12.     for a really clever interpreter.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. #define S 655360
  18.  
  19. int t[S];
  20. char a[16777216], p[S], bf[256], b;
  21. int c, g, i, j, m, x;
  22. FILE* prog;
  23.  
  24. int main(int argc, char** argv) {
  25.     if (!(prog=fopen(argv[1], "r"))){
  26.         fprintf(stderr, "can't open the file \"%s\".\n", argv[1]);
  27.         exit(1);
  28.     }
  29.     g=fread(p, 1, S, prog);
  30.     fclose(prog);
  31.     bf['<']=bf['>']=bf['+']=bf['-']=bf[',']=bf['.']=bf['[']=bf[']']=bf['#']=1;
  32.     m=S;
  33.     while (i<g){
  34.         for (b=0; i<g && (!bf[p[i]] || (b?b==p[i]?++c:0:(b=p[i],c=1)));) i++;
  35.         switch (b) {
  36.         case '+': if (c <= 4) p[j++]='a'-1+c; else p[j]='+', t[j++]=c; break;
  37.         case '-': if (c<=4) p[j++]='d'+c; else p[j]='-', t[j++]=c; break;
  38.         case '>': if (c<=4) p[j++]='h'+c; else p[j]='>', t[j++]=c; break;
  39.         case '<': if (c<=4) p[j++]='l'+c; else p[j]='<', t[j++]=c; break;
  40.         case ',': case '.': case '#': while (c--) p[j++]=b; break;
  41.         case '[': while (c--) t[--m]=j; p[j++]='['; break;
  42.         case ']': if (m+c>S) fprintf(stderr, "unbalanced ].\n"), exit(1);
  43.             t[j]=t[m]+1; while (c--) t[t[m++]]=j+1; p[j++]=']'; break;
  44.         }
  45.     }
  46.     p[j]=0;
  47.     if (m!=S) fprintf(stderr, "unbalanced [.\n"), exit(1);
  48.     j=0;
  49.     while (1) {
  50.         switch (p[j]) {
  51.         case '+': a[x]+=t[j++]; break;
  52.         case '-': a[x]-=t[j++]; break;
  53.         case '>': x+=t[j++]; break;
  54.         case '<': x-=t[j++]; break;
  55.         case '[': if (!a[x]) j=t[j]; else j++; break;
  56.         case ']': if (a[x]) j=t[j]; else j++; break;
  57. #if '\n' == 10
  58.         case ',': if ((c=getchar())!=EOF) a[x]=c; j++; break;
  59.         case '.': putchar(a[x]); fflush(stdout); j++; break;
  60. #else
  61.         case ',': if ((c=getchar())!=EOF) a[x]=c=='\n'?10:c; j++; break;
  62.         case '.': putchar(a[x]==10?'\n':a[x]); fflush(stdout); j++; break;
  63. #endif
  64.         case 'a': a[x]++; j++; break;
  65.         case 'b': a[x]+=2; j++; break;
  66.         case 'c': a[x]+=3; j++; break;
  67.         case 'd': a[x]+=4; j++; break;
  68.         case 'e': a[x]--; j++; break;
  69.         case 'f': a[x]-=2; j++; break;
  70.         case 'g': a[x]-=3; j++; break;
  71.         case 'h': a[x]-=4; j++; break;
  72.         case 'i': x++; j++; break;
  73.         case 'j': x+=2; j++; break;
  74.         case 'k': x+=3; j++; break;
  75.         case 'l': x+=4; j++; break;
  76.         case 'm': x--; j++; break;
  77.         case 'n': x-=2; j++; break;
  78.         case 'o': x-=3; j++; break;
  79.         case 'p': x-=4; j++; break;
  80.         case '#': printf("\n"); for(i=0;i<64;i++) printf("%4d",(signed char)a[i]);
  81.             printf("\n%*s\n", x*4+4, "^"); j++; break;
  82.         case 0: exit(0); break;
  83.         }
  84.     }
  85. }
RAW Paste Data