www.pudn.com > 扩展后的pl0编译器源码.rar > Pl0Run.cpp
/////////////////////////////////////////////////////////////// // PL/0 ASM Interpreter 0.1 (2003.12.27) [Source File] // Author:Dwing // ccy借用。 /////////////////////////////////////////////////////////////// #include//For ... #include //For /////////////////////////////////////////////////////////////// typedef struct //汇编指令格式 { char opr; //指令操作码 char lvl; //级数 unsigned short adr; //地址码/操作数 }ASM; enum{OP_LIT,OP_LOD,OP_STO,OP_CAL,OP_INT,OP_JMP,OP_JPC,OP_OPR}; enum{ OPR_RET,OPR_NEG,OPR_ADD,OPR_SUB,OPR_MUL,OPR_DIV, OPR_ODD,OPR_NUL,OPR_EQL,OPR_NE ,OPR_LT ,OPR_LGE, OPR_LG ,OPR_LTE,OPR_WRT,OPR_WRN,OPR_RED }; /////////////////////////////////////////////////////////////// int getbase(short *s,int base,int lvl) //取基址 { while(lvl--) base=s[base]; return base; } /////////////////////////////////////////////////////////////// int main() //Program Entry { int i,num; char b[4]; ASM a[16384]; FILE *fp; fp=fopen("temp.pl0","rb"); if(!fp) { printf("PL/0 ASM Interpreter 0.1\n"); printf("Can't open \"temp.pl0\"!\n"); return 0; } fscanf(fp,"%u",&num); for(i=0;i =s[sp+1]); break; //是否次栈顶大于栈顶,结果入栈(0或1) case OPR_LG: sp--;s[sp]=(s[sp]>s[sp+1]); break; //是否次栈顶小于等于栈顶,结果入栈(0或1) case OPR_LTE:sp--;s[sp]=(s[sp]<=s[sp+1]); break; //打印栈顶元素 case OPR_WRT:printf("%u",s[sp--]); break; //打印回车 case OPR_WRN:putchar('\n'); break; //从键盘读取数 case OPR_RED:putchar('?');scanf("%u%*c",&s[++sp]); break; } } ip++; //得到下条指令 }while(ip); printf("\nProgram terminated!\nPress ENTER to return..."); getchar(); return 0; } ///////////////////////////////////////////////////////////////