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

📄 run.cpp

📁 一个basic语言的编译工具。
💻 CPP
字号:
#include"type.cpp"
#include"bastype.cpp"

/*-------------DATA--------------*/
extern Stack basstack;

/*-------------FUNC--------------*/
uchar huge*NEXTLINE();
void PRINT();
void LET(uchar far*varn,uchar far*num);
void INPUT();
void IF();
void GOTO();
void FOR();
void NEXT();
void DIM();
/*-------------CODE--------------*/
uchar huge*NEXTLINE()
{uchar huge*p;
 p=BP;
 while(*p!='\n'&&*p!=0)p++;
 return p;
}

void LET(uchar far*varn,uchar far*num)
{uchar far*p,a;
 p=varn;
 switch(*p)
 {
  case '%':var.SETIVAR(varn,CALC(num));return;
  case '$':var.SETSVAR(varn,CALCSTR(num));return;
  case '!':
  default:var.SETFVAR(varn,CALC(num));return;
 }
}

void PRINT()
{uchar a,x,t;
 for(;;)
 {
  t=THISIS;
  getapara(BP+0xffff);
  if(CALCERR)return;
  switch(THISIS)
  {
   case 0:if(t<4)putchar('\n');return;
   case 1:printf("%d",THISISAINT);break;          /* A int */
   case 2:printf("%g",THISISAFLOAT);break;        /* a float*/
   case 3:printf("%s",THISISASTR);break;          /* a string*/
   case 4:putchar('\n');break;                    /* a \n  */
   case 5:CALCERR=1;return;                            /* a '.'  */
   case 6:break;                                  /*  a  ';'  */
   case 7:x=wherex()-1;
	  if(x>64)putchar('\n');
	  else gotoxy((x/16+1)*16+1,wherey());
	  break;                                  /*  ','  */
   default:CALCERR=1;return;
  }
 }
}

void INPUT()
{uchar datn[16][16],i=0,j=0,prompted=0;
 uchar buf[255],far*p;
 p=buf;
 for(;;)
 {BP=getaword(BP);
  if(lineend||end)break;
  if(aword[0]==0){getch();return;}
  if(aword[0]=='"'){printf("%s",CALCSTR(aword));prompted=1;}
  if(isdigit(aword[0])){ERR(11);return;}
  if(isalpha(aword[0]))strcpy(datn[i++],aword);
 }
 if(!prompted)putchar('?');
redofromstart:
 gets(buf);p=buf;
 for(j=0;;j++)
 {
  if(j>i)break;
  p=getaword(p);
  if(aword[0]==','||aword[0]==';'){j--;continue;}
  if(end)break;
  switch(thelastchar(datn[j]))
  {
   case '%':var.SETIVAR(datn[j],CALC(aword));break;
   case '$':if(aword[0]=='"')strcpy(aword,CALCSTR(aword));
	    var.SETSVAR(datn[j],aword);break;
   case '!':
   default :var.SETFVAR(datn[j],CALC(aword));break;
  }
 }
 lineend=end=0;
 if(i!=j){puts("Redo from start:");goto redofromstart;}
}

void IF()
{uchar huge*bpz,huge*tbp,huge*bpz2;
 bpz=GETBPZ("THEN","","","");
 if(bpz==(uchar far*)0xF000FFFF){ERR(14);return;}
 BPZ=(uchar far*)0xF000FFFF;
 bpz2=GETBPZ("ELSE","","","");
 lineend=end=0;
 getapara(bpz);

 switch(THISIS)
 {
  case 1:THISISAFLOAT=THISISAINT;
  case 2:if(THISISAFLOAT)
	 {
	  BP=bpz+4;
	  BPZ=(uchar far*)0XF000FFFF;
	  lineend=end=0;
	  return;
	 }
	 else
	  if(bpz2!=(uchar far*)0XF000FFFF)
	  {BP=bpz2+4;
	   BPZ=(uchar far*)0XF000FFFF;
	   return;
	  }
	 if(CALCERR)return;
	 BP=bpz;
	 BP=NEXTLINE();
	 return;
  default:ERR(11);return;
 }
}

void GOTO()
{uint i;
 getaword(BP);
 if(end||lineend){ERR(15);return;}
 for(i=0;i<LN;i++)
  if(strcmp(linen.p+(i<<4),aword)==0)
  {BP=*(((uint huge*)linep.p)+i)+buf;return;}
 ERR(16);
}

void FOR()
{uchar huge*bpz,far*p,v;
 double onum,tonum,step;uint nextline;
 end=0;
 bpz=GETBPZ("TO","","","");
 if(bpz==(uchar huge*)0xF000FFFF){ERR(17);return;}
 BP=getaword(BP);nextline=NEXTLINE()-buf;
 p=aword;while(*p!='='&&*p)p++;
 if(*p==0){ERR(11);return;}
 *p=0;p++;
 BPZ=(uchar huge*)0XF000FFFF;
 switch(*(p-2))
 {
  case '$':ERR(11);return;
  case '%':dellastchar(aword);
	   v=var.SETIVAR(aword,onum=CALC(p));
	   BP=getaword(BP);BP=getaword(BP);
	   tonum=CALC(aword);
	   bpz=getaword(BP);
	   if(strcmp(aword,"STEP"))step=1;
	   else {BP=bpz;BP=getaword(BP);step=CALC(aword);}
	   if(onum>tonum&&step>0)step=(-step);
	   if(onum<=tonum&&step<0)step=(-step);
	   basstack.pushaint(step);
	   basstack.pushaint(tonum);
	   basstack.pushaint(nextline);
	   basstack.pushachar(v);
	   if(onum<=tonum)basstack.pushachar(1);else basstack.pushachar(2);

	   break;
  case '!':dellastchar(aword);
  default :v=var.SETFVAR(aword,onum=CALC(p));
	   BP=getaword(BP);BP=getaword(BP);
	   tonum=CALC(aword);
	   bpz=getaword(BP);
	   if(strcmp(aword,"STEP"))step=1;
	   else {BP=bpz;BP=getaword(BP);step=CALC(aword);}
	   if(onum>tonum&&step>0)step=(-step);
	   if(onum<=tonum&&step<0)step=(-step);
	   basstack.pushafloat(step);
	   basstack.pushafloat(tonum);
	   basstack.pushaint(nextline);
	   basstack.pushachar(v);
	   if(onum<=tonum)basstack.pushachar(3);else basstack.pushachar(4);
	   break;
 }
 if(CALCERR)return;
 BP=nextline+buf;
}

void NEXT()
{uchar opnum,varnum,nexttype;
 uint nextline;double tonum,step,onum;
 BP=getaword(BP);
 if(lineend||end)nexttype=0;
 else
 {switch(thelastchar(aword))
  {case '$':ERR(11);return;
   case '%':nexttype=1;break;
   default :nexttype=2;break;
  }
  varnum=var.VARNUM(aword);
 }
nextone:
 opnum=basstack.popachar();
 switch(opnum)
 {case 1:
  case 2:if(nexttype==2){ERR(18);return;}onum=var.GETIVAR(aword);break;
  case 3:
  case 4:if(nexttype==1){ERR(18);return;}onum=var.GETFVAR(aword);break;
  default:ERR(18);return;
 }
 if(nexttype==0)varnum=basstack.popaint();
  else
   if(varnum!=basstack.popachar())
   {
    switch(opnum)
    {case 1:
     case 2:basstack.sp+=6;goto nextone;
     case 3:
     case 4:basstack.sp+=10;goto nextone;
    }
   }
 nextline=basstack.popaint();
 switch(opnum)
 {case 1:
  case 2:tonum=(int)basstack.popaint();step=(int)basstack.popaint();break;
  case 3:
  case 4:tonum=basstack.popafloat();step=basstack.popafloat();break;
  default:ERR(18);return;
 }
 onum+=step;
 switch(opnum)
 {case 1:
  case 3:if(onum>tonum)return;break;
  case 2:
  case 4:if(onum<tonum)return;break;
 }
 switch(opnum)
 {case 1:
  case 2:basstack.pushaint(step);basstack.pushaint(tonum);var.SETIVAR(aword,onum);break;
  case 3:
  case 4:basstack.pushafloat(step);basstack.pushafloat(tonum);var.SETFVAR(aword,onum);break;
 }
 basstack.pushaint(nextline);
 basstack.pushachar(varnum);
 basstack.pushachar(opnum);
 BP=nextline+buf;
 lineend=end=0;
}

void DIM()
{uchar huge*p;
 for(;;)
 {
  BP=getaword(BP);
  if(end||lineend)return;
  if(strcmp(aword,",")==0)continue;
  p=strchr(aword,'(');
  *p=0;
  p++;dellastchar(p);
  creatarray(aword,CALC(p));
  if(CALCERR)return;
 }
}

void ERASE()
{
 for(;;)
 {
  BP=getaword(BP);
  if(end||lineend)return;
  if(strcmp(aword,",")==0)continue;
  delarray(aword);
  if(CALCERR)return;
 }
}
void GOSUB()
{uint i;
 getaword(BP);
 if(end||lineend){ERR(15);return;}
 for(i=0;i<LN;i++)
  if(strcmp(linen.p+(i<<4),aword)==0)
  {basstack.pushaint(NEXTLINE()-buf);
   basstack.pushachar(5);
   BP=*(((uint huge*)linep.p)+i)+buf;
   return;}
 ERR(16);
}

void RETURN()
{
 if(basstack.popachar()!=5){ERR(22);return;}
 BP=basstack.popaint()+buf;
}

⌨️ 快捷键说明

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