I wrote this interpreter in C for my esoteric language Timef**k
Code:#include "stdio.h" #include "stdlib.h" char code[1000]; char data[10][100]; int ip[100]; int dx[100]; int dt[100]; char output[100]; int op[100]; int skip(int i){ while(code[i] != ')') if(code[i] == '(') i = skip(i+1); else i++; return i; } int exec(int ct,int ei){ printf("exec %d %d\n",ct,ei); int i = ip[ct]; int x = dx[ct]; int t = dt[ct]; int o = op[ct]; while(i<ei){ printf("%d %d %d %d : %c\n",ct,i,x,t,code[i]); if(code[i] == '>') if(x<9) x++; else x=0; if(code[i] == '<') if(x>0) x--; else x=9; if(code[i] == 'v'){ exec(t,i+1); if(t<99) t++; } if(code[i] == '^') if(t>0) t--; if(code[i] == '(') if(data[x][t] == 0) i = skip(i+1); if(code[i] == '|') { int j; for(j=0;j<10;j++) data[j][ct+1] = data[j][ct]; ip[ct]=i; dx[ct]=x; dt[ct]=t; op[ct]=o; ct++; t++; } if(code[i] == '+')data[x][t]++; if(code[i] == '-')data[x][t]--; if(code[i] == '.'){ output[o++] = data[x][t]; printf("%d was %d or '%c' at %d\n",x,data[x][t],data[x][t],t); } i++; } printf("end exec\n"); } int main(){ int i; for(i=0;i<1000;i++) if((code[i]=getchar())=='#') break; code[i]=0; puts(code); exec(0,i); puts(output); for(i=0;i<10;i++)printf("%d\n",output[i]); }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks