📄 execute.cpp
字号:
// execute.cpp : implementation file
//
#include "stdafx.h"
#include "MFCpl0.h"
#include "execute.h"
#include "prop2.h"
#include "propersheet1.h"
#include "propshxiugai.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Cexecute
IMPLEMENT_DYNCREATE(Cexecute, CDocument)
Cexecute::Cexecute()
{
errline=0;
err=0;
}
BOOL Cexecute::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
Cexecute::~Cexecute()
{
}
BEGIN_MESSAGE_MAP(Cexecute, CDocument)
//{{AFX_MSG_MAP(Cexecute)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cexecute diagnostics
#ifdef _DEBUG
void Cexecute::AssertValid() const
{
CDocument::AssertValid();
}
void Cexecute::error(int n,int line)
{
char space[81];
memset(space ,32,81);
space[cc-1]=0;
CString msg;
switch(n)
{
case 1:
msg="常数说明中的=写成了:=";
break;
case 2:
msg="常数说明中的=后应是数字";
break;
case 3:
msg="常数说明中的标识符后应是=";
break;
case 4:
msg="const,var,procedure后应是标识符";
break;
case 5:
msg="漏掉了,或;";
break;
case 6:
msg="过程说明后的符号不正确";
break;
case 7:
msg="应是语句开始符";
break;
case 8:
msg="程序体内语句部分的后跟符不正确";
break;
case 9:
msg="程序结尾丢了句号";
break;
case 10:
msg="语句之间漏了;";
break;
case 11:
msg="标识符未说明";
break;
case 12:
msg="赋值语句中,赋值号左边标识符应该是变量";
break;
case 13:
msg="赋值语句左部标识符应该是赋值符号:=";
break;
case 14:
msg="call后应为标识符";
break;
case 15:
msg="call后标识符属性应为过程";
break;
case 16:
msg="条件语句中丢了then";
break;
case 17:
msg="丢了end或;";
break;
case 18:
msg="while型循环语句中丢了do";
break;
case 19:
msg="语句后的符号不正确";
break;
case 20:
msg="应为关系运算符";
break;
case 21:
msg="表达式内标识符属性不能是过程";
break;
case 22:
msg="表达式中漏掉右括号";
break;
case 23:
msg="因子后的非法符号";
break;
case 24:
msg="表达式的开始符不能是此符号";
break;
case 31:
msg="数越界";
break;
case 32:
msg="read语句括号中的标识符不是变量";
break;
}
CString errmsg;
errmsg.Format("%s%d%s%s","第",line-1,"行,",msg);
AfxMessageBox(errmsg);
allerrmsg[err].line=line-1;
allerrmsg[err].type=n;
fprintf(fa1,"*****%s%d\n",space,n);
err++;
}
int Cexecute::getsym()
{
int i,j,k;
while(ch==' '||ch==10||ch==9)
{
getchdo;
}
if(ch>='a'&&ch<='z')
{
k=0;
do
{
if(k<al)
{
a[k]=ch;
k++;
}
getchdo;
}while(ch>='a'&&ch<='z'||ch>='0'&&ch<='9');
a[k]=0;
strcpy(id,a);
i=0;
j=norw-1;
do
{
k=(i+j)/2;
if(strcmp(id,word[k])<=0)
{
j=k-1;
}
if(strcmp(id,word[k])>=0)
{
i=k+1;
}
}while(i<=j);
if(i-1>j)
{
sym=wsym[k];
}
else
{
sym=ident;
}
}
else
{
if(ch>='0'&&ch<='9')
{
k=0;
num=0;
sym=number;
do
{
num=10*num+ch-'0';
k++;
getchdo;
}while(ch>='0'&&ch<='9');
k--;
if(k>nmax)
{
error(30,errline);
}
}
else
{
if(ch==':')
{
getchdo;
if(ch=='=')
{
sym=becomes;
getchdo;
}
else
{
sym=nul;
}
}
else
{
if(ch=='<')
{
getchdo;
if(ch=='=')
{
sym=leq;
getchdo;
}
else
{
sym=lss;
}
}
else
{
if(ch=='>')
{
getchdo;
if(ch=='=')
{
sym=geq;
getchdo;
}
else
{
sym=gtr;
}
}
else
{
sym=ssym[ch];
if(sym!=period)
{
getchdo;
}
}
}
}
}
}
return 0;
}
int Cexecute::getch()
{
if(cc==ll)
{
if(feof(fin))
{
printf("program incomplete");
return -1;
}
ll=0;
cc=0;
printf("%d",cx);
fprintf(fa1,"%d",cx);
ch=' ';
while(ch!=10)
{
if(EOF==fscanf(fin,"%c",&ch))
{
line[ll]=0;
break;
}
if(ch==10)
{
errline++;
}
printf("%c",ch);
fprintf(fa1,"%c",ch);
line[ll]=ch;
ll++;
}
printf("\n");
fprintf(fa1,"\n");
}
ch=line[cc];
cc++;
return 0;
}
void Cexecute::init()
{
int i;
for(i=0;i<=255;i++)
{
ssym[i]=nul;
}
ssym['+']=plus;
ssym['-']=minus;
ssym['*']=times;
ssym['/']=slash;
ssym['(']=lparen;
ssym[')']=rparen;
ssym['=']=eql;
ssym[',']=comma;
ssym['.']=period;
ssym['#']=neq;
ssym[';']=semicolon;
strcpy(&(word[0][0]),"begin");
strcpy(&(word[1][0]),"call");
strcpy(&(word[2][0]),"const");
strcpy(&(word[3][0]),"do");
strcpy(&(word[4][0]),"end");
strcpy(&(word[5][0]),"if");
strcpy(&(word[6][0]),"odd");
strcpy(&(word[7][0]),"procedure");
strcpy(&(word[8][0]),"read");
strcpy(&(word[9][0]),"then");
strcpy(&(word[10][0]),"var");
strcpy(&(word[11][0]),"while");
strcpy(&(word[12][0]),"write");
wsym[0]=beginsym;
wsym[1]=callsym;
wsym[2]=constsym;
wsym[3]=dosym;
wsym[4]=endsym;
wsym[5]=ifsym;
wsym[6]=oddsym;
wsym[7]=procsym;
wsym[8]=readsym;
wsym[9]=thensym;
wsym[10]=varsym;
wsym[11]=whilesym;
wsym[12]=writesym;
strcpy(&(mnemonic[lit][0]),"lit");
strcpy(&(mnemonic[opr][0]),"opr");
strcpy(&(mnemonic[lod][0]),"lod");
strcpy(&(mnemonic[sto][0]),"sto");
strcpy(&(mnemonic[cal][0]),"cal");
strcpy(&(mnemonic[inte][0]),"int");
strcpy(&(mnemonic[jmp][0]),"jmp");
strcpy(&(mnemonic[jpc][0]),"jpc");
for(i=0;i<symnum;i++)
{
declbegsys[i]=false;
statbegsys[i]=false;
facbegsys[i]=false;
}
declbegsys[constsym]=true;
declbegsys[varsym]=true;
declbegsys[procsym]=true;
statbegsys[beginsym]=true;
statbegsys[callsym]=true;
statbegsys[whilesym]=true;
facbegsys[ident]=true;
facbegsys[number]=true;
facbegsys[lparen]=true;
}
int Cexecute::gen(enum fct x,int y,int z)
{
if(cx>=cxmax)
{
printf("Program too long!");
return -1;
}
code[cx].f=x;
code[cx].l=y;
code[cx].a=z;
cx++;
return 0;
}
int Cexecute::test(bool* s1,bool* s2,int n)
{
if(!inset(sym,s1))
{
error(n,errline);
while((!inset(sym,s1))&&(!inset(sym,s2)))
{
getsymdo;
}
}
return 0;
}
int Cexecute::inset(int e,bool *s)
{
return s[e];
}
int Cexecute::addset(bool* sr,bool* s1,bool* s2,int n)
{
int i ;
for(i=0;i<n;i++)
{
sr[i]=s1[i]||s2[i];
}
return 0;
}
int Cexecute::subset(bool* sr,bool* s1,bool* s2,int n)
{
int i ;
for(i=0;i<n;i++)
{
sr[i]=s1[i]&&(!s2[i]);
}
return 0;
}
int Cexecute::mulset(bool* sr,bool* s1,bool* s2,int n)
{
int i ;
for(i=0;i<n;i++)
{
sr[i]=s1[i]&&s2[i];
}
return 0;
}
int Cexecute::block(int lev,int tx,bool *fsys)
{
int i;
int dx;
int tx0;
int cx0;
bool nxtlev[symnum];
dx=3;
tx0=tx;
table[tx].adr=cx;
gendo(jmp,0,0);
if(lev>levmax)
{
error(32,errline);
}
do
{
if(sym==constsym)
{
getsymdo;
do
{
constdeclarationdo(&tx,lev,&dx);
while(sym==comma)
{
constdeclarationdo(&tx,lev,&dx);
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5,errline);
}
}while(sym==ident);
}
if(sym==varsym)
{
getsymdo;
do
{
vardeclarationdo(&tx,lev,&dx);
while(sym==comma)
{
getsymdo;
vardeclarationdo(&tx,lev,&dx);
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5,errline);
}
}while(sym==ident);
}
while(sym==procsym)
{
getsymdo;
if(sym==ident)
{
enter(procedur,&tx,lev,&dx);
getsymdo;
}
else
{
error(4,errline);
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5,errline);
}
memcpy(nxtlev,fsys,sizeof(bool)*symnum);
nxtlev[semicolon]=true;
if(-1==block(lev+1,tx,nxtlev))
{
return -1;
}
if(sym==semicolon)
{
getsymdo;
memcpy(nxtlev,statbegsys,sizeof(bool)*symnum);
nxtlev[ident]=true;
nxtlev[procsym]=true;
testdo(nxtlev,fsys,6);
}
else
{
error(5,errline);
}
}
memcpy(nxtlev,statbegsys,sizeof(bool)*symnum);
nxtlev[ident]=true;
nxtlev[period]=true;
testdo(nxtlev,declbegsys,7);
}while(inset(sym,declbegsys));
code[table[tx0].adr].a=cx;
table[tx0].adr=cx;
table[tx0].size=dx;
cx0=cx;
gendo(inte,0,dx);
// Cprop2 prop2;
// prop2.Addmsg(table);
if(tableswitch)
{
Cpropersheet1 propsheet2("table 表");
propsheet2.inputtable(table);
propsheet2.SetWizardMode();
propsheet2.DoModal();
}
/*
if(tableswitch)
{
printf("TABLE:\n");
if(tx0+1>tx)
{
printf("NULL\n");
}
for(i=tx0+1;i<tx;i++)
{
switch(table[i].kind)
{
case constant:
printf("%d const%s",i,table[i].name);
printf("val=%d\n",table[i].val);
fprintf(fas,"%d const%s",i,table[i].name);
fprintf(fas,"val=%d\n",table[i].val);
break;
case variable:
printf("%d var%s",i,table[i].name);
printf("lev=%d addr=%d\n",table[i].level,table[i].adr);
fprintf(fas,"%d var%s",i,table[i].name);
fprintf(fas,"lev=%d addr=%d\n",table[i].level,table[i].adr);
break;
case procedur:
printf("%d proc%s",i,table[i].name);
printf("lev=%d addr=%d size=%d\n",table[i].level,table[i].adr,table[i].size);
fprintf(fas,"%d proc%s",i,table[i].name);
fprintf(fas,"lev=%d addr=%d size=%d\n",table[i].level,table[i].adr,table[i].size);
break;
}
}
printf("\n");
}*/
memcpy(nxtlev,fsys,sizeof(bool)*symnum);
nxtlev[semicolon]=true;
nxtlev[endsym]=true;
statementdo(nxtlev,&tx,lev);
gendo(opr,0,0);
memset(nxtlev,0,sizeof(bool)*symnum);
testdo(fsys,nxtlev,8);
listcode(cx0);
return 0;
}
void Cexecute::interpret(char** table)
{
hang=lie=0;
int p,b,t;
struct instruction i;
int s[stacksize];
printf("start pl0\n");
t=0;
b=0;
p=0;
s[0]=s[1]=s[2]=0;
do
{
i=code[p];
p++;
switch(i.f)
{
case lit:
s[t]=i.a;
t++;
break;
case opr:
switch(i.a)
{
case 0:
t=b;
p=s[t+2];
b=s[t+1];
break;
case 1:
s[t-1]=-s[t-1];
break;
case 2:
t--;
s[t-1]=s[t-1]+s[t];
break;
case 3:
t--;
s[t-1]=s[t-1]-s[t];
break;
case 4:
t--;
s[t-1]=s[t-1]*s[t];
break;
case 5:
t--;
s[t-1]=s[t-1]/s[t];
break;
case 6:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -