?? interpret.cpp
字號:
# include "common.h"
void INTERPRET() //翻譯的主過程
{
////////// 初始化部分 /////////
oldTop=0;
stop=0;
top=0;
bp=0;
pc=0;
DISPLAY[1]=0;
S[1]=0;
S[2]=0;
S[3]=0;
///////// 初始化結束 //////////
printf("翻譯開始\n");
do
{
instruction=CODE[pc]; //取指令
pc++; //PC加一
switch(instruction.func) //翻譯執行
{
case LIT:
case LIT1:
top++;
S[top]=instruction.address;
break;
case LOD:
top++;
S[top]=S[DISPLAY[instruction.level]+instruction.address];
break;
case LODA:
top++;
S[top]=DISPLAY[instruction.level]+instruction.address;
break;
case ILOD:
top++;
S[top]=S[S[DISPLAY[instruction.level]+instruction.address]];
break;
case LODT:
S[top]=S[S[top]];
break;
case LODB:
h=S[top];
top--;
hh=instruction.address+top;
while(top<hh)
{
top++;
S[top]=S[h];
h++;
}
break;
case CPYB:
h=S[top-1];
hh=S[top];
hhh=h+instruction.address;
while(h<hhh)
{
S[h]=S[hh];
h++;
hh++;
}
top-=2;
break;
case STO:
S[S[top-1]]=S[top];
top-=2;
break;
case OPAC:
oldTop=top;
top+=3;
break;
case CAL:
S[oldTop+1]=pc;
S[oldTop+2]=DISPLAY[instruction.level];
S[oldTop+3]=bp;
pc=instruction.address;
break;
case ENTP:
bp=oldTop+1;
DISPLAY[instruction.level]=bp;
top=oldTop+instruction.address;
break;
case UDIS:
h=instruction.address;
hh=instruction.level;
hhh=bp;
do
{
DISPLAY[h]=hhh;
h--;
hhh=S[hhh+1];
}while(h!=hh);
break;
case JMP:
pc=instruction.address;
break;
case JPC:
if(S[top]==0)
{
pc=instruction.address;
}
top--;//改正
break;
case RETP:
top=bp-1;
pc=S[top+1];
bp=S[top+3];
break;
case ENDP:
stop=1;
break;
case RED:
if(instruction.address==0)
{
printf("Your Input:");
scanf("%d",&temp);
}
else
getch();
S[S[top]]=temp;
break;
case WRT:
if(instruction.address==0)
printf("Your Output:%d\n",S[top]);
else
{
ch=(char)S[top];
printf("Your Output%c\n",ch);
}
top--;
break;
case MUS:
S[top]=-S[top];
case ADD:
case ADD1:
top--;
S[top]=S[top]+S[top+1];
break;
case SUB:
top--;
S[top]=S[top]-S[top+1];
break;
case MULT:
top--;
S[top]=S[top]*S[top+1];
break;
case IDIV:
top--;
S[top]=S[top]/S[top+1];
break;
case IMOD:
top--;
S[top]=S[top]%S[top+1];
break;
case ANDS:
top--;
S[top]=S[top]&S[top+1];
break;
case ORS:
top--;
S[top]=S[top]|S[top+1];
break;
case NOTS:
top--;
S[top]=~S[top];
break;
case EQ:
top--;
S[top]=(S[top]==S[top+1])?1:0;
break;
case NE:
top--;
S[top]=(S[top]!=S[top+1])?1:0;
break;
case LS:
top--;
S[top]=(S[top]<S[top+1])?1:0;
break;
case GE:
top--;
S[top]=(S[top]>=S[top+1])?1:0;
break;
case GT:
top--;
S[top]=(S[top]>S[top+1])?1:0;
break;
case LE:
top--;
S[top]=(S[top]<=S[top+1])?1:0;
break;
}
}while(!stop);
//printf("翻譯結束,請輸入任意字符退出。\n");
printf("翻譯結束。\n");
}
void main(int arg,char ** argv)
{
char objFileName[255];
FILE * objFile;
int objLength;
int dataunit;
//INSTRUCTION tempIns;
if(arg>1)
strcpy(objFileName,argv[1]);
else
{
printf("請輸入代碼源文件的名字:");
scanf("%s",objFileName);
}
if(!(objFile=fopen(objFileName,"rb")))
{
printf("錯誤產生:代碼源文件%s不能打開!\n",objFileName);
exit(1);
}
fseek(objFile,0,SEEK_END);
objLength=ftell(objFile); //獲得代碼文件的長度
rewind(objFile);
if(objLength%(3*sizeof(int))) //判斷代碼文件是否完整
{
printf("錯誤的代碼源文件!");
exit(2);
}
dataunit=2*sizeof(int)+sizeof(OPCOD);//一條指令代碼的長度
int codeSize=objLength/dataunit; //獲得代碼文件里面的指令條數
CODE=new INSTRUCTION[codeSize]; //生成代碼數組
if(!CODE)
{
printf("沒有足夠的堆可分配!");
exit(3);
}
int count=fread(CODE,dataunit,codeSize,objFile); //將代碼文件讀入代碼數組
printf("%d",count);
INTERPRET(); //開始翻譯執行
delete CODE; //翻譯完畢,刪除數組
fclose(objFile);
//int a; //小關卡,可以查看運行結果
//scanf("%d",a);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -