📄 winapp.cpp
字号:
m=0;n=0;
while(tempset2[m]!=NULL)
temp2->pa[n++]=tempset2[m++];
temp2->pa[n]=NULL;
m=0;n=0;
while(tempset3[m]!=NULL)
temp3->pa[n++]=tempset3[m++];
temp3->pa[n]=NULL;
m=0;n=0;
while(tempset4[m]!=NULL)
temp4->pa[n++]=tempset4[m++];
temp4->pa[n]=NULL;
m=0;n=0;
while(tempset5[m]!=NULL)
temp5->pa[n++]=tempset5[m++];
temp5->pa[n]=NULL;
m=0;n=0;
while(tempset6[m]!=NULL)
temp6->pa[n++]=tempset6[m++];
temp6->pa[n]=NULL;
m=0;n=0;
if(strcmp(sym,"ident")==0)
{ i=position(id);
if(i==0) error(11);
else
{ if(table[i].kind!=variable)
{error(12);
i=0;}
}
getsym();
if(strcmp(sym,"becomes")==0) getsym();
else error(13);
expression(fsys);
if(i!=0) gen(sto,plev-table[i].level,table[i].adr+2);
}
else if(strcmp(sym,"readsym")==0)
{ getsym();
if(strcmp(sym,"lparen")!=0) error(24);
else
{ do
{getsym();
if(strcmp(sym,"ident")==0) i=position(id);
else i=0;
if(i==0) error(35);
else
{ gen(opr,0,16);
gen(sto,plev-table[i].level,table[i].adr+2);
}
getsym();
}while(strcmp(sym,"comma")==0);
}
if(strcmp(sym,"rparen")!=0)
{ error(22);
while(in(sym,fsys)==0) getsym();
}
else getsym();
}
else if(strcmp(sym,"writesym")==0)
{ getsym();
if(strcmp(sym,"lparen")==0)
{ do
{ getsym();
expression(add(temp1,fsys));
gen(opr,0,14);
}while(strcmp(sym,"comma")==0);
if(strcmp(sym,"rparen")!=0) error(33);
else getsym();
}
gen(opr,0,15);
}
/* 297 */
else if(strcmp(sym,"callsym")==0)
{ getsym();
if(strcmp(sym,"ident")!=0) error(14);
else
{ i=position(id);
if(i==0)error(11);
else
{ if(table[i].kind==procedur)
gen(cal,plev-table[i].level,table[i].adr+2);
else error(15);
}
getsym();
}
}
else if(strcmp(sym,"ifsym")==0)
{ getsym();
condition(add(temp2,fsys));
if(strcmp(sym,"thensym")==0) getsym();
else error(16);
cx1=cx;
gen(jpc,0,0);
statement(fsys,plev);
code[cx1].a=cx;
}
else if(strcmp(sym,"beginsym")==0)
{ getsym();
statement(add(temp3,fsys),plev);
while(in(sym,add(temp4,statbegsys))==1)
{ if(strcmp(sym,"semicolon")==0) getsym();
else error(10);
statement(add(temp3,fsys),plev);
}
if(strcmp(sym,"endsym")==0) getsym();
else error(17);
}
else
{ if(strcmp(sym,"whilesym")==0)
{ cx1=cx;
getsym();
condition(add(temp5,fsys));
cx2=cx;
gen(jpc,0,0);
if(strcmp(sym,"dosym")==0) getsym();
else error(18);
statement(fsys,plev);
gen(jmp,0,cx1);
code[cx2].a=cx;
}
}
test(fsys,temp6,19);
}
void block(int plev,int tx,struct node *fsys)
{int m=0,n=0;
int dx=3;
int tx0;
int cx0;
char *tempset1[]={"semicolon","endsym",NULL};
char *tempset2[]={"ident","procsym",NULL};
char *tempset3[]={"semicolon",NULL};
char *tempset4[]={"ident",NULL};
char *tempset5[]={NULL};
struct node *temp1,*temp2,*temp3,*temp4,*temp5;
temp1=(struct node *)malloc(sizeof(struct node));
temp2=(struct node *)malloc(sizeof(struct node));
temp3=(struct node *)malloc(sizeof(struct node));
temp4=(struct node *)malloc(sizeof(struct node));
temp5=(struct node *)malloc(sizeof(struct node));
while(tempset1[m]!=NULL)
temp1->pa[n++]=tempset1[m++];
temp1->pa[n]=NULL;
m=0;n=0;
while(tempset2[m]!=NULL)
temp2->pa[n++]=tempset2[m++];
temp2->pa[n]=NULL;
m=0;n=0;
while(tempset3[m]!=NULL)
temp3->pa[n++]=tempset3[m++];
temp3->pa[n]=NULL;
m=0;n=0;
while(tempset4[m]!=NULL)
temp4->pa[n++]=tempset4[m++];
temp4->pa[n]=NULL;
m=0;n=0;
while(tempset5[m]!=NULL)
temp5->pa[n++]=tempset5[m++];
temp5->pa[n]=NULL;
m=0;n=0;
lev=plev;
dx=3;
tx0=tx;
table[tx].adr=cx;
gen(jmp,0,1);
if(plev>levmax) error(32);
do
{ if(strcmp(sym,"constsym")==0)
{ getsym();
do
{ constdeclaration();
while(strcmp(sym,"comma")==0)
{ getsym();
constdeclaration();
}
if(strcmp(sym,"semicolon")==0) getsym();
else error(5);
}while(strcmp(sym,"ident")==0);
}
if(strcmp(sym,"varsym")==0)
{ getsym();
do { vardeclaration();
while(strcmp(sym,"comma")==0)
{ getsym();
vardeclaration();
}
if(strcmp(sym,"semicolon")==0) getsym();
else error(5);
}while(strcmp(sym,"ident")==0);
}
while(strcmp(sym,"procsym")==0)
{ getsym();
if(strcmp(sym,"ident")==0)
{ enter(procedur);
getsym();
}
else error(4);
if(strcmp(sym,"semicolon")==0) getsym();
else error(5);
block(plev+1,tx,add(temp3,fsys));
lev=lev-1;
if(strcmp(sym,"semicolon")==0)
{ getsym();
test(add(statbegsys,temp2),fsys,6);
}
else error(5);
}
test(add(statbegsys,temp4),declbegsys,7);
}while(in(sym,declbegsys)==1);
code[table[tx0].adr].a=cx;
table[tx0].adr=cx;
table[tx0].size=dx;
cx0=cx;
gen(ini,0,dx);
statement(add(temp1,fsys),plev);
gen(opr,0,0);
test(fsys,temp5,8);
listcode(&cx0);
}
/* 300 */
int base(int l,int *b,int s[stacksize])
{ int b1;
b1=*b;
while(l>0)
{ b1=s[b1];
l=l-1;
}
return b1;
}
void interpret()
{ int p=0;
int b=1;
int t=0;
struct instruction i;
int s[stacksize];
s[0]=0;
s[1]=0;
s[2]=0;
s[3]=0;
do {
i=code[p];
p=p+1;
switch(i.f)
{ case lit:t=t+1;s[t]=i.a;break;
case opr:
switch(i.a)
{ case 0:t=b-1;p=s[t+3];b=s[t+2];break;
case 1:s[t]=-s[t];break;
case 2:t=t-1;s[t]=s[t]+s[t+1];break;
case 3:t=t-1;s[t]=s[t]-s[t+1];break;
case 4:t=t-1;s[t]=s[t]*s[t+1];break;
case 5:t=t-1;s[t]=s[t]/s[t+1];break;
case 6:if(s[t]%2==0) s[t]=0; else s[t]=1;break;
case 8:t=t-1;if(s[t]==s[t+1])s[t]=1;else s[t]=0;break;
case 9:t=t-1;if(s[t]==s[t+1])s[t]=0;else s[t]=1;break;
case 10:t=t-1;if(s[t]<s[t+1])s[t]=1;else s[t]=0;break;
case 11:t=t-1;if(s[t]>=s[t+1])s[t]=1;else s[t]=0;break;
case 12:t=t-1;if(s[t]>s[t+1])s[t]=1;else s[t]=0;break;
case 13:t=t-1;if(s[t]<=s[t+1])s[t]=1;else s[t]=0;break;
case 14:printf("%d",s[t]);
fprintf(fa2,"%d",s[t]);t=t-1;break;
case 15:printf("\n");fprintf(fa2,"\n");break;
case 16:t=t+1;printf("?");fprintf(fa2,"?");
scanf("%d",&s[t]);fprintf(fa2,"%d",s[t]);break;
}
break;
case lod:t=t+1;s[t]=s[base(1,&b,s)+i.a];break;
case sto:s[base(1,&b,s)+i.a]=s[t];t=t-1;break;
case cal:s[t+1]=base(1,&b,s);s[t+2]=b;s[t+3]=p;b=t+1;p=i.a;break;
case ini:t=t+i.a;break;
case jpc:if(s[t]==0)p=i.a;t=t-1;break;
}
}while(p!=0);
fclose(fa2);
}
void compile()
{int m=0,n=0;
char *declbeg[]={"constsym","varsym","procsym",NULL};
char *statbeg[]={"beginsym","callsym","ifsym","whilesym",NULL};
char *facbeg[]={"ident","number","lparen",NULL};
char *tempset[]={"period","constsym","varsym","procsym",NULL};
declbegsys=(struct node *)malloc(sizeof(struct node));
statbegsys=(struct node *)malloc(sizeof(struct node));
facbegsys=(struct node *)malloc(sizeof(struct node));
tempsetsys=(struct node *)malloc(sizeof(struct node));
while(declbeg[m]!=NULL)
declbegsys->pa[n++]=declbeg[m++];
declbegsys->pa[n]=NULL;
m=0;n=0;
while(statbeg[m]!=NULL)
statbegsys->pa[n++]=statbeg[m++];
statbegsys->pa[n]=NULL;
m=0;n=0;
while(facbeg[m]!=NULL)
facbegsys->pa[n++]=facbeg[m++];
facbegsys->pa[n]=NULL;
m=0;n=0;
while(tempset[m]!=NULL)
tempsetsys->pa[n++]=tempset[m++];
tempsetsys->pa[n]=NULL;
if((fa1=fopen("fa1.txt","w"))==NULL)
{ MessageBox(hwnd,"Cannot open file",NULL,MB_OK); exit(0); }
if((fin=fopen(filename,"r"))==NULL)
{ MessageBox(hwnd,"Cannot open file according to given filename",NULL,MB_OK);exit(0); }
listswitch=TRUE;
err=0;
cc=1;cx=0;ll=0;ch=' ';kk=al;
getsym();
if((fa=fopen("fa.txt","w"))==NULL)
{ MessageBox(hwnd,"Cannot open file according to given filename",NULL,MB_OK);exit(0);}
if((fa2=fopen("fa2.txt","w"))==NULL)
{ MessageBox(hwnd,"Cannot open file according to given filename",NULL,MB_OK);exit(0);}
block(0,0,add(statbegsys,tempsetsys));
fclose(fa);
fclose(fa1);
if(strcmp(sym,"period")!=0)
error(9);
if(err==0)
interpret();
else
{
char temp[255];
wsprintf(temp,"%d error in PASCAL program\n",err);
strcat(output,temp);
}
fclose(fin);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
hinst=hInstance;
return(DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINDLG),NULL,(DLGPROC)dlgproc));
}
BOOL CALLBACK dlgproc(HWND hdlg,UINT msg,WPARAM wparam,LPARAM lparam)
{
hwnd = hdlg;
switch(msg)
{
case WM_INITDIALOG:
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hdlg;
ofn.hInstance=hinst;
ofn.lpstrFilter="所有文件(*.*)\0*.*\0";
ofn.nMaxFile=255;
ofn.lpstrFile=str;
return 1;
case WM_COMMAND:
switch(LOWORD(wparam))
{
case ID_START:
compile();
MessageBox(hwnd,output,"object",MB_OK);
output[0] = 0;
break;
case ID_OPEN:
ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
ofn.lpstrTitle="打开源程序文件";
str[0]=0;
if(!GetOpenFileName(&ofn)) break;
str[255]=0;
filename = ofn.lpstrFile;
fp=fopen(ofn.lpstrFile,"rb");
if(!fp)
MessageBox(hdlg,str,"无法打开文件!",MB_ICONERROR);
else
{
fseek(fp,0,SEEK_END);
int i=ftell(fp);
fseek(fp,0,SEEK_SET);
if(i>65535)
MessageBox(hdlg,"文件太长(>=64K)!","错误",MB_ICONERROR);
else
{
fread(input,1,i,fp);
input[i]=0;
SetDlgItemText(hdlg,IDC_INPUT,input);
}
fclose(fp);
}
break;
}
return 1;
case WM_CLOSE:
EndDialog(hdlg,0);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -