带调试功能的BrainF**k编译器

啊这......最近忽然想试试BrainF**k(BF)语言,于是写一个编译器在此暂存

  1 #include <cstdio>
  2 #include <cctype>
  3 #include <cstring>
  4 #include <iostream>
  5 
  6 using namespace std;
  7 
  8 const int MAXS = 194910;
  9 const int MIDS = 97455;
 10 
 11 int len, top, pos;
 12 int s[MAXS], *pc = s + MIDS;
 13 char code[MAXS], *p1 = code, *p2;
 14 int stack[MAXS];
 15 
 16 namespace Debugger{ // 调试器,可以在gdb内调用相关函数或设置断点
 17     struct Jumper{
 18         bool flg;
 19         int tarpos, maxprint;
 20         Jumper(){flg = false, tarpos = 0, maxprint = 0;}
 21         Jumper(int tar, int mp) : flg(false), tarpos(tar), maxprint(mp){}
 22         void doSomething(){
 23             flg = true;
 24         }
 25         void printStates(){
 26             static char tmps[50];
 27             memcpy(tmps, code + pos, 50);
 28             cerr << tmps << endl;
 29             cerr << "{";
 30             for(int i = 0; i < maxprint; i++)
 31                 cerr << *(s + MIDS + i) << ", ";
 32             cerr << "} : " << (pc - s - MIDS) << endl;
 33         }
 34         void stopCaller(){
 35             if(pos == tarpos)
 36                 doSomething();
 37             if(flg) printStates();
 38         }
 39         void jumpToPos(int tar){
 40             tarpos = tar;
 41         }
 42         void jumpNextLine(){
 43             tarpos = strchr(code + pos, '\n') - code;
 44         }
 45     };
 46 }
 47 
 48 int main(){
 49 
 50     FILE *CODE = fopen("code.bf", "r");
 51     if(CODE == NULL){
 52         cerr << "Where is your Code ?" << endl;
 53         return 3;
 54     }
 55     p2 = p1 + fread(code, 1, MAXS, CODE);
 56     fclose(CODE);
 57     if(p1 == p2){
 58         cerr << "Where is your Code ?" << endl;
 59         return 3;
 60     }
 61     len = p2 - p1;//获得纸带
 62 
 63     Debugger::Jumper jumper(-1, 10);
 64 
 65     while(pos < len){// 读取纸带
 66         jumper.stopCaller();
 67         if(code[pos] == '+') (*pc) ++;
 68         else if(code[pos] == '-') (*pc) --;
 69         else if(code[pos] == '>') pc ++;
 70         else if(code[pos] == '<') pc --;
 71         else if(code[pos] == '.') putchar((char)*pc);
 72         else if(code[pos] == ',') *pc = getchar();
 73         else if(code[pos] == '['){
 74             if(*pc) stack[top ++] = pos;// 不跳转
 75             else {// 跳转
 76                 int i, j;
 77                 for(j = pos, i = 0; j < len; j++){
 78                     if(code[j] == '[') i ++;
 79                     else if(code[j] == ']') i --;
 80                     if(i == 0) break;
 81                 }
 82                 if(i == 0) pos = j;
 83                 else {
 84                     cerr << "No enough \']\' was found at " << pos << endl;
 85                     return 3;
 86                 }
 87             }
 88         } else if(code[pos] == ']') pos = stack[-- top] - 1;
 89         else if(code[pos] == '/'){// 允许注释
 90             if(code[++ pos] == '/') while(code[++ pos] != '\n');
 91             else if(code[pos] == '*'){
 92                 while(code[++ pos] != '*' || code[pos + 1] != '/');
 93                 pos += 2;
 94             }
 95         } else if(!isspace(code[pos])) cerr << "What's That? It's in " << pos << endl;// 允许换行
 96         pos ++;
 97     }
 98     puts("");
 99     return 0;
100 }

 

  

posted @ 2020-09-22 11:15  Creeper_LKF  阅读(262)  评论(0编辑  收藏  举报