⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ver4.bak

📁 一个小型的C解释器
💻 BAK
📖 第 1 页 / 共 2 页
字号:
  brktable[brkptr].cases[ii]=' ';
 for(ii=start;ii<=end;ii++)
  brktable[brkptr].cases[ii-start]=str[ii];
}
//breaktable

void chkstyle(char str[50])
{
 int i=0;
 style=-2;
 while(1)
  {
   if((str[i]==' ')||(str[i]=='\n')) { i++; if(i==50) break; continue; }
   if((str[i]=='i')&&(str[i+1]=='n')&&(str[i+2]=='t')&&(str[i+3]==' '))
    {  style=INT;  break; }
   if((str[i]=='s')&&(str[i+1]=='c')&&(str[i+2]=='a')&&(str[i+3]=='n')&&(str[i+4]=='f'))
    {  style=SCANF; break; }
   if((str[i]=='p')&&(str[i+1]=='r')&&(str[i+2]=='i')&&(str[i+3]=='n')&&(str[i+4]=='t')&&(str[i+5]=='f'))
    {  style=PRINTF; break; }
   if((str[i]=='i')&&(str[i+1]=='f')&&(str[i+2]=='('))
    {  style=IF; break; }
   if((str[i]=='w')&&(str[i+1]=='h')&&(str[i+2]=='i')&&(str[i+3]=='l')&&(str[i+4]=='e'))
    {  style=WHILE; break; }
   if(str[i]=='=')
    {  style=TRANSVALUE; break; }
   if(str[i]=='{')
    {  style=-1; break;}
   i++;
  }
 if(style==-2) error(SYNTAXERROR,temp_line);
}
//check style of string

void dovalue1(char str[51])
{
 int i,j,k=0,n,nop,t,eq;
 char ss[30]={""};
 char s[30]={""};
 int op[100]={0};
 for(i=1;i<=60;i++) op[i]=0;
 nop=0;
 for(i=0;i<=strlen(str);i++)
  {
   if(str[i]=='=') eq=i;
   if(str[i]==';')
    {
     str[i]='#';
     break;
    }
  }
 i=0;
 s[0]=' ';
 while(1)
  {
   i++;
   eq++;
   s[i]=str[eq];
   if((s[i]>=48)&&(s[i-1]<=47)&&(i>2)&&(s[i-1]!=46))
    {
     i++;
     s[i]=s[i-1];
     s[i-1]='A';
    }
   if(s[i]=='#') break;
  }
  //
  for(j=1;j<=i;j++)
   if(s[j]!='A')
    {
     if((s[j]==46)||(s[j]>=48))
      {
       if(s[j-1]=='A')
	{
	 ss[k]=' ';
	 k++;
	}
       ss[k]=s[j];
       k++;
      }
     else
      if((nop==0)&&(s[j]!=10)) {nop++; op[nop]=s[j]-40;}
      else {
	    while(1)
	     {
	      if((s[j]=='#')||(s[j]==10)) break;
	      if((nop==0)&&(s[j]!=10)) {nop++; op[nop]=s[j]-40; break;}
	      if((f[op[nop]][s[j]-40]==2)&&(nop!=0))
	       {
		ss[k]=' ';
		k++;
		ss[k]=op[nop]+40;
		k++;
		nop--;
	       }
	      if((f[op[nop]][s[j]-40]==-1)&&(nop!=0))
	       {
		nop++;
		op[nop]=s[j]-40;
		break;
	       }
	      if(f[op[nop]][s[j]-40]==1)
	       {
		nop--;
		break;
	       }
	     }
	   }
     }
   if(nop!=0) for(t=nop;t>=1;t--)
    {
     ss[k]=' ';
     k++;
     ss[k]=op[t]+40;
     k++;
    }
  for(i=0;i<=k;i++) strexp[i]=ss[i];
}

int dovalue2(char str[30])
{
 int stack[10]={0};
 int ii,jj,n=0,l,last;
 char temp[10];
 l=strlen(str);
 last=0;
 for(ii=0;ii<=l;ii++)
  {
   if((str[ii]==' ')||(str[ii]=='\x0'))
    {
     for(jj=last;jj<=ii-1;jj++)
      temp[jj-last]=str[jj];
     temp[ii-last]='\x0';
     last=ii+1;
     if(temp[0]<=47)
      {
       if(temp[0]=='+')
	{
	 stack[n-1]=stack[n-1]+stack[n];
	 n--;
	}
       if(temp[0]=='-')
	{
	 stack[n-1]=stack[n-1]-stack[n];
	 n--;
	}
       if(temp[0]=='*')
	{
	 stack[n-1]=stack[n-1]*stack[n];
	 n--;
	}
       if(temp[0]=='/')
	{
	 stack[n-1]=stack[n-1]/stack[n];
	 n--;
	}
      }
     if((temp[0]<60)&&(temp[0]>=48))
      {
       n++;
       stack[n]=0;
       for(jj=0;jj<=strlen(temp)-1;jj++)
	{
	 stack[n]=stack[n]*10+temp[jj]-48;
	}
      }
     if(temp[0]>60)
      {
       n++;
       stack[n]=chkvar(temp);
      }
    }
  }
 if(n==1) return stack[1];
 if(n!=1) error(EXPERROR,temp_line);
}
//caculate strexp

void dovalue3(char str[30])
{
 char temp[6]={""};
 int ii,jj,bool=0,start,eq;
 for(ii=0;ii<=strlen(str);ii++)
  {
   if(str[ii]==' ') start=ii;
   if(str[ii]=='=')
    {
     eq=ii;
     break;
    }
  }
 for(ii=start+1;ii<=eq-1;ii++)
  {
   temp[ii-start-1]=str[ii];
  }
 temp[ii-start]='\x0';
 for(ii=0;ii<=varnum-1;ii++)
  {
   bool=1;
   for(jj=0;jj<=strlen(varible[ii].name);jj++)
    if(temp[jj]!=' ')
    if(temp[jj]!=varible[ii].name[jj])
     {
      bool=0;
      break;
     }
    if(bool) break;
  }
 varible[ii].val=value;
}
//transvalue

void mainfun()
{
 while(1)
  {
   if(text[temp_line][temp_row]=='$')
    {
     if(flag==0) break;
     if(flag>=1) error(RIGHTMISSINGB,temp_line);
    }
   if((text[temp_line][temp_row]==' ')||(text[temp_line][temp_row]=='\n')||(text[temp_line][temp_row]=='\x0'))
    {
     mvfwd();
     continue;
    }
    //jump over ' '
   if(text[temp_line][temp_row]=='{')
    {
     copy(text_line,text_row,temp_line,temp_row);
     chkstyle(part);
     if(style==-1)  //normal part
      {
       flagtable[flag+1]=0;
       flag++;
       mvfwd();
       text_row=temp_row;
       text_line=temp_line;
       continue;
      }
    if(style==IF) //"if" part
     {
      if(ccltbool(part))
       {
	flagtable[flag+1]=0;
	flag++;
	mvfwd();
	text_line=temp_line;
	text_row=temp_row;
	continue;
       }
      if(ccltbool(part)==0)
       {
	jumpover();
	continue;
       }
     }
    if(style==WHILE)
     {
      if(ccltbool(part)==1)
       {
	brkptr++;
	dobrkpt(part);
	mvfwd();
	brktable[brkptr].line=temp_line;
	brktable[brkptr].row=temp_row;
	flagtable[flag+1]=1;
	flag++;
	text_line=temp_line;
	text_row=temp_row;
	continue;
       }
      if(ccltbool(part)==0)
       {
	jumpover();
	continue;
       }
     }
    }
   //'{'
   if(text[temp_line][temp_row]=='}')
    {
     if(flagtable[flag]==0)
      {
       flag--;
       if(flag<0) error(LEFTMISSINGB,temp_line);//'{'missing
       mvfwd();
       text_row=temp_row;
       text_line=temp_line;
       continue;
      }
     //normal or if part
     if(flagtable[flag]==1)
      {
       if(ccltbool(brktable[brkptr].cases)==1)
	{
	 temp_row=brktable[brkptr].row;
	 temp_line=brktable[brkptr].line;
	 text_row=temp_row;
	 text_line=temp_line;
	 continue;
	}
       if(ccltbool(brktable[brkptr].cases)==0)
	{
	 brkptr--;
	 flag--;
	 if(flag<0) error(LEFTMISSINGB,temp_line);
	 mvfwd();
	 text_row=temp_row;
	 text_line=temp_line;
	 continue;
	}
      }
     //while part
    }
   //'}'
   if(text[temp_line][temp_row]==';')
    {
     copy(text_line,text_row,temp_line,temp_row);
     chkstyle(part);
     if(style==PRINTF)
      {
       doprintf(part);
       mvfwd();
       text_line=temp_line;
       text_row=temp_row;
       continue;
      }
     if(style==INT)
      {
       doint(part);
       mvfwd();
       text_line=temp_line;
       text_row=temp_row;
       continue;
      }
     if(style==SCANF)
      {
       doscanf(part);
       mvfwd();
       text_line=temp_line;
       text_row=temp_row;
      }
     if(style==TRANSVALUE)
      {
       dovalue1(part);
       value=dovalue2(strexp);
       dovalue3(part);
       mvfwd();
       text_line=temp_line;
       text_row=temp_row;
      }
    }
   mvfwd();
  }
}

void filein()
{
 FILE *fp;
 fp=fopen("E:\\test.txt","rt");
 while((c=fgetc(fp))!=EOF)
  {
   text[text_line][text_row++]=c;
   if(text[text_line][text_row-1]=='\n')
    {
     text_line++;
     text_row=0;
    }
  }
 text[text_line+1][0]='$';
}
//read file in

main()
{
 filein();
 text_line=1;
 text_row=0;
 temp_line=1;
 temp_row=0;
//jump over "Main" (always at line 0)

 mainfun();
 return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -