/*PLA V.88*/ #include ".\INCLUDE\CONST1.C" #include ".\INCLUDE\VAR1.C" #include ".\INCLUDE\DOS1.C" int BCC=1; /*is Borland C, dann kein BSS */ unsigned char testa[]={148,153,129,154,0}; /*"öÖüÜ";*/ int test3( int i, unsigned int u){ _AX=0xB800; } char Nametemp[16]; int doglob() { int symlen; int j; int isstringarray; isstringarray=0; if (GTop >= GMAX) error1("Globale Tabelle voll"); symlen = strlen1(symbol); if (symlen > 15 ) error1("Variable ist laenger als 15 Zeichen"); if (istoken('[')) { istype='&'; if (istoken(T_CONST)) { if (BCC == FALSE) {prs("\nAData = $\norg "); prnum(orgData);} prs("\n"); prs(symbol); if (iswidth=='B') prs(" db "); else prs(" dw "); prnum(lexval); prs(" dup (?)"); if (BCC == FALSE) prs("\norg AData"); orgData=orgData+lexval; if (iswidth=='W') orgData=orgData+lexval; GData[GTop]=lexval; expect(']'); }else { expect(']'); if (iswidth=='W') error1("Nur ByteArray erlaubt"); prs("\n"); prs(symbol); prs(" db "); isstringarray=1; strcpy1(Nametemp, symbol); expect('='); if (istoken(T_STRING)) { prc(34); prscomment(symbol); prc(34); prs(",0"); symlen=strlen1(symbol); GData[GTop]=symlen; } else if (istoken('{' )) { narg=0; do { if(narg) prc(','); expect(T_CONST); prnum(lexval); narg++; } while (istoken(',')); expect('}'); } else error1("String oder Zahlenarray erwartet"); }; }else { expect('='); prs("\n"); prs(symbol); if (istype=='*') prs(" dw "); else { if(iswidth=='B') prs(" db "); else prs(" dw "); } if(istoken('-')) prc('-'); expect(T_CONST); prnum(lexval); } GSign [GTop]=issign; GWidth[GTop]=iswidth; GType [GTop]=istype; j=GTop*16; pt=&GNameField + j; if (isstringarray) strcpy1(pt, Nametemp); else strcpy1(pt, symbol); GTop++; expect(';'); } int addlocal() { int j; if(LTop >= LMAX) error1("Lokale Tabelle voll"); GSign [LTop]=issign; GWidth[LTop]=iswidth; GType [LTop]=istype; j=LTop*16; pt=&GNameField + j; strcpy1(pt, symbol); LTop++; } int gettypes(int i) {int j; char c; c=GSign [i]; if (c=='S') signi =1; else signi =0; c=GWidth[i]; if (c=='W') widthi=wi=1; else widthi=wi=0; c=GType [i]; typei=0; if (c=='*') typei=wi=1; if (c=='&') typei=2; return i; } int adrofname(int i) {int j; int k; j=i*16; k=&GNameField + j; return k; } int printName(int i) { i=adrofname(i); prs(i); } int searchname() { int i; int j; i=LStart;while(i<LTop) {j=adrofname(i);if(eqstr(symbol,j))return i; i++;} i=0; while(i<GTop) {j=adrofname(i);if(eqstr(symbol,j))return i; i++;} error1("Variable unbekannt"); } int typeName() { int m; /*0=V,1=*,2=&*/ issign='S'; if(istoken(T_SIGNED)) issign='S'; if(istoken(T_UNSIGNED)) issign='U'; if(istoken(T_VOID)) error1("Void ist unbekannt"); iswidth='W'; if(istoken(T_CHAR)) iswidth='B'; if(istoken(T_INT)) iswidth='W'; istype='V'; m=0; if(istoken('*')) { istype='*'; m=1; } if(istoken('&')) { istype='&'; m=2; } name1(); return m; } int name1() {if (token!=T_NAME) error1("Name erwartet"); token=getlex(); } int dofunc() { cloc=&co; strcpy1(fname, symbol); prs("\n\n"); prs(symbol); prs(" PROC"); expect('('); narg=0; LTop=LStart; if (istoken(')')==0) { prs("\narg "); do { typeName(); addlocal(); if (narg) prc(','); prs(symbol); if (istype!='*') {if (iswidth=='B') prs(":byte ");} narg++; } while (istoken(',')); expect(')'); } expect('{'); /*body*/ nlocal=0; nreturn=0; nconst=0; if (isvariable()) prs("\nlocal "); while(isvariable()) { do { typeName(); addlocal(); if (nlocal) prc(','); prs(symbol); if (istype!='*') {if (iswidth=='B') prs(":byte ");} nlocal++; if (istoken('[')){istype='&';GType[LTop]='&';expect(T_CONST);expect(']'); prs(":BYTE:"); prnum(lexval); } } while (istoken(',')); expect(';'); } while(istoken('}')==0) stmt(); if (nreturn) prs("\n@@retn:"); prs("\n ret\nENDP"); listproc(); } int pexpr() {expect('('); iscmp=0; if (token==T_NAME) {if (eqstr(symbol, "_")) {constantexpr(); return;}} expr(); if (iscmp==0) prs("\n or al, al\n je @@"); expect(')'); } int constantexpr() { int mode; int id1;int ids; token=getlex(); mode=typeName(); id1=searchname(); gettypes(id1); ids=signi; if (isrelational() ==0) error1("Vergleich erwartet"); expect(T_CONST); prs("; constant expression"); prs("\ncmp "); printName(id1); prs(", "); prnum(lexval); cmpneg(ids); expect(')'); } int expr() { int mode; int id1; int ixarr; int ids; if (istoken(T_CONST)) {doconst(); return 1; } mode=typeName(); /*0=V,1=*,2=&*/ if (token=='(') {docall1(); goto e1; } if (isreg()) goto e1; id1=searchname(); gettypes(id1); ids=signi; ixarr=0; if (istoken('[')) { ixarr=searchname(); expect(T_NAME); expect(']'); gettypes(ixarr); if (widthi==0) error1("Arrayindex muss int sein"); } if (istoken(T_PLUSPLUS )) {if(mode)error1("Nur var erlaubt");doinc();goto e1;} if (istoken(T_MINUSMINUS)) {if(mode)error1("Nur var erlaubt");dodec();goto e1;} if (istoken(T_PLUSASS )) {compoundass("add", mode); goto e1;} if (istoken(T_MINUSASS )) {compoundass("sub", mode); goto e1;} if (istoken(T_ANDASS )) {compoundass("and", mode); goto e1;} if (istoken(T_ORASS )) {compoundass("or" , mode); goto e1;} if (istoken(T_MULASS )) {error1("nicht implementiert");} if (istoken(T_DIVASS )) {error1("nicht implementiert");} if (istoken('=')) { isconst=expr(); if (isconst) { if(mode==0) {prs("\n;++++ mov "); printName(id1); prs(", "); prnum(lexval); } } doassign(mode, id1, ixarr); goto e1;} dovar1(mode, "mov", ixarr, id1); e1: if (istoken('+')) rterm("add"); else if (istoken('-')) rterm("sub" ); else if (istoken('&')) rterm("and" ); else if (istoken('|')) rterm("or" ); else if (istoken(T_LESSLESS)) rterm("shl"); else if (istoken(T_GREATGREAT)) rterm("shr"); else if (istoken('*')) domul (ids); else if (istoken('/')) doidiv(ids); else if (istoken('%')) domod (ids); if (isrelational()) { rterm("cmp"); cmpneg(ids);} return 0; } int compoundass(char *op, int mode) { if(mode) error1("Nur scalar Var erlaubt"); prnl(); prs(op); prs(" "); prs(symbol); prs(", "); expect(T_CONST); prnum(lexval); } int dovar1(int mode, int op, int ixarr, int id1) { gettypes(id1); if (mode==1) {prs("\n mov bx, "); printName(id1); prnl(); prs(op); if(widthi) prs(" ax, [bx]"); else prs(" al, [bx]\n mov ah, 0"); return; } if (mode==2){prnl();prs(op);prs(" ax, offset ");printName(id1); return; } if (ixarr) { prs("\n mov bx, "); printName(ixarr); if (wi) prs("\n shl bx, 1"); prs("\n "); prs(op); if (wi) prs(" ax, "); else prs(" al, "); printName(id1); prs(" [bx]"); return; } prnl();prs(op); if(wi) prs(" ax, "); else prs(" al, "); printName(id1); } int rterm(char *op) {int mode; int opint; int ixarr; int id1; if (istoken(T_CONST)) { prnl(); prs(op); if (wi) prs(" ax, "); else prs(" al, "); prnum(lexval); return;} mode=typeName(); id1=searchname(); ixarr=0; if (istoken('[')) { ixarr=searchname(); expect(T_NAME); expect(']'); gettypes(ixarr); if (widthi==0) error1("Arrayindex muss int sein"); } if (eqstr(symbol,"_AX")) return; opint=op; dovar1(mode, opint, ixarr, id1); } int isreg() { if (eqstr(symbol,"_AH")) {doreg("ah"); goto r1;} if (eqstr(symbol,"_AL")) {doreg("al"); goto r1;} if (eqstr(symbol,"_AX")) {doreg("ax"); goto r1;} if (eqstr(symbol,"_BH")) {doreg("bh"); goto r1;} if (eqstr(symbol,"_BL")) {doreg("bl"); goto r1;} if (eqstr(symbol,"_BX")) {doreg("bx"); goto r1;} if (eqstr(symbol,"_CH")) {doreg("ch"); goto r1;} if (eqstr(symbol,"_CL")) {doreg("cl"); goto r1;} if (eqstr(symbol,"_CX")) {doreg("cx"); goto r1;} if (eqstr(symbol,"_DH")) {doreg("dh"); goto r1;} if (eqstr(symbol,"_DL")) {doreg("dl"); goto r1;} if (eqstr(symbol,"_DX")) {doreg("dx"); goto r1;} if (eqstr(symbol,"_FLAGS")) {doreg("flags"); goto r1;} return 0; r1: return 1; } int doreg(char *dr) { expect('='); prs("\n mov "); prs(dr); prs(", "); if (istoken(T_CONST)) prunsign(lexval); else if (istoken(T_NAME )) { if (eqstr(symbol,"_DX")) prs("dx"); else if (eqstr(symbol,"_CX")) prs("cx"); else prs(symbol); } else error1("Nur Zahl oder Var erlaubt"); } int doinc() { prs("\n inc "); prs(symbol); } int dodec() { prs("\n dec "); prs(symbol); } int doassign(int mode, int i, int ixarr) { gettypes(i); if (mode==1) {prs("\n mov bx, ");printName(i); if (widthi) prs("\n mov [bx], ax"); else prs("\n mov [bx], al"); return;} if (mode==2) {prs("\n mov offset ");printName(i); prs(", ax"); return;} if (ixarr) { prs("\n mov bx, "); printName(ixarr); if (wi) prs("\n shl bx, 1"); prs("\n mov "); printName(i); if (wi) prs(" [bx], ax"); else prs(" [bx], al"); return; } prs("\n mov "); printName(i); if (wi) prs(", ax"); else prs(", al"); } int domul(int ids) { if (ids) rterm("imul"); else { if (istoken(T_CONST)) {prs("\n mov bx, "); prnum(lexval); prs("\n mul bx"); } else error1("Bei MUL nur Zahl als Multiplikator erlaubt"); } } int doidiv(int ids) { if (istoken(T_CONST)) { prs("\n mov bx, "); prnum(lexval); if (ids) prs("\n cwd\n idiv bx"); else prs("\n mov dx, 0\n div bx"); } else error1("Nur Zahl als Divisor erlaubt"); } int domod(int ids) { doidiv(ids); prs("\n mov ax, dx"); } int doconst() { prs("\n mov ax, "); prnum(lexval); } int part1=0;int part2=0;int part3=0;int part4=0;int part5=0;int part6=0; int parn1=0;int parn2=0;int parn3=0;int parn4=0;int parn5=0;int parn6=0; char procname[20]; /*1=CONST,2=String,3=&,4=Name*/ int docall1() {int i; int nargs; int t0; int n0; nargs=0; strcpy1(&procname, symbol);expect('('); if (istoken(')') ==0 ) { do { nargs++; if (nargs >6 ) error1("Max. 6 Parameter"); t0=0; if(istoken(T_CONST)) {t0=1; n0=lexval; } if(istoken(T_STRING)){t0=2; n0=nconst; eprs("\n"); eprs(fname); eprc(95);eprnum(nconst);eprs(" db "); eprc(34);eprs(symbol);eprc(34);eprs(",0"); nconst++; } if(istoken('&')) {t0=3; name1(); n0=searchname();} if(istoken(T_NAME)) {t0=4; n0=searchname(); p1=>ype; p1=p1+n0; if (*p1=='&') t0=3; } if (t0==0) error1("Parameter nicht erkannt (kein * erlaubt)"); if (nargs==1) {part1=t0; parn1=n0; } if (nargs==2) {part2=t0; parn2=n0; } if (nargs==3) {part3=t0; parn3=n0; } if (nargs==4) {part4=t0; parn4=n0; } if (nargs==5) {part5=t0; parn5=n0; } if (nargs==6) {part6=t0; parn6=n0; } } while (istoken(',')); expect(')'); i=nargs; do { if (i==1) {t0=part1; n0=parn1; } if (i==2) {t0=part2; n0=parn2; } if (i==3) {t0=part3; n0=parn3; } if (i==4) {t0=part4; n0=parn4; } if (i==5) {t0=part5; n0=parn5; } if (i==6) {t0=part6; n0=parn6; } if(t0==1){ prs("\n push "); prnum(n0);} if(t0==2){ prs("\n push offset "); prs(fname);prc(95);prnum(n0);} if(t0==3){ prs("\n lea ax, "); printName(n0); prs("\n push ax");} if(t0==4){ gettypes(n0); if(wi) { prs("\n push "); printName(n0);} else { prs("\n mov al, byte ptr "); printName(n0); prs("\n mov ah, 0\n push ax"); } } i--; } while (i > 0); } prs("\n call "); prs(&procname); if (nargs>0) {prs("\n add sp, "); nargs=nargs+nargs; prnum(nargs); } }
int expr() { int mode; int id1; int ixarr; int ids; if (istoken(T_CONST)) {doconst(); return 1; } mode=typeName(); /*0=V,1=*,2=&*/ if (token=='(') {docall1(); goto e1; } if (isreg()) goto e1; id1=searchname(); gettypes(id1); ids=signi; ixarr=0; if (istoken('[')) { ixarr=searchname(); expect(T_NAME); expect(']'); gettypes(ixarr); if (widthi==0) error1("Arrayindex muss int sein"); } if (istoken(T_PLUSPLUS )) {if(mode)error1("Nur var erlaubt");doinc();goto e1;} if (istoken(T_MINUSMINUS)) {if(mode)error1("Nur var erlaubt");dodec();goto e1;} if (istoken(T_PLUSASS )) {compoundass("add", mode); goto e1;} if (istoken(T_MINUSASS )) {compoundass("sub", mode); goto e1;} if (istoken(T_ANDASS )) {compoundass("and", mode); goto e1;} if (istoken(T_ORASS )) {compoundass("or" , mode); goto e1;} if (istoken(T_MULASS )) {error1("nicht implementiert");} if (istoken(T_DIVASS )) {error1("nicht implementiert");} if (istoken('=')) { isconst=expr(); if (isconst) { if(mode==0) {prs("\n;++++ mov "); printName(id1); prs(", "); prnum(lexval); } } doassign(mode, id1, ixarr); goto e1;} dovar1(mode, "mov", ixarr, id1); e1: if (istoken('+')) rterm("add"); else if (istoken('-')) rterm("sub" ); else if (istoken('&')) rterm("and" ); else if (istoken('|')) rterm("or" ); else if (istoken(T_LESSLESS)) rterm("shl"); else if (istoken(T_GREATGREAT)) rterm("shr"); else if (istoken('*')) domul (ids); else if (istoken('/')) doidiv(ids); else if (istoken('%')) domod (ids); if (isrelational()) { rterm("cmp"); cmpneg(ids);} return 0; }
int rterm(char *op) {int mode; int opint; int ixarr; int id1; if (istoken(T_CONST)) { prnl(); prs(op); if (wi) prs(" ax, "); else prs(" al, "); prnum(lexval); return;} mode=typeName(); id1=searchname(); ixarr=0; if (istoken('[')) { ixarr=searchname(); expect(T_NAME); expect(']'); gettypes(ixarr); if (widthi==0) error1("Arrayindex muss int sein"); } if (eqstr(symbol,"_AX")) return; opint=op; dovar1(mode, opint, ixarr, id1); }
int main() { struct total ex; //将文件内容导入 FILE *fp; int j; fp=fopen("bus.txt","r"); if(fp==NULL) { printf("FILE OPEN ERROR"); return -1; } fscanf(fp,"%d",&ex.totalline); int i; for(i=0; i<ex.totalline; i++) { fscanf(fp,"%s%d",ex.line[i].linenumber,&ex.line[i].stopcount); } for(i=0; i<ex.totalline; i++) { for(j=0; j<ex.line[i].stopcount; j++) { fscanf(fp,"%s",ex.line[i].station[j].stopname); } } fclose(fp); //search(ex); searchname(ex); return 0; }
int constantexpr() { int mode; int id1;int ids; token=getlex(); mode=typeName(); id1=searchname(); gettypes(id1); ids=signi; if (isrelational() ==0) error1("Vergleich erwartet"); expect(T_CONST); prs("; constant expression"); prs("\ncmp "); printName(id1); prs(", "); prnum(lexval); cmpneg(ids); expect(')'); }
void searchentry() { system("clear"); printw("#######"); FILE *fp; Book b; int g, id, found = 0; char c, ch, option = 'y'; char bname[20]; init_pair(2, COLOR_YELLOW, COLOR_BLACK); attron(COLOR_PAIR(2)); border(0,0,0,0, 0,0,0,0); refresh(); attroff(COLOR_PAIR(2)); refresh(); mvaddstr(1,60," ~~~~~~ SEARCH BOOKS ~~~~~~"); refresh(); while(option == 'y' || option == 'Y') { mvaddstr(3,32,"A. Search by ID"); mvaddstr(5,32,"B. Search by name"); mvaddstr(7,32,"Enter your choice"); fflush(stdin); scanw(" %c",&c); switch(c) { case 'A' : printgen(); addstr("(Genre to be searched) "); scanw("%d", &g); switch(g) { case FICTION : fp = fopen("Fiction.dat","rb+"); mvaddstr(13,32,"Enter book id:"); scanw("%d",&id); while(fread(&b, sizeof(b), 1, fp) == 1) { searchid(&b, fp, id, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case NONFICTION : fp = fopen("Nonfiction.dat","rb+"); mvaddstr(13,32,"Enter book id:"); scanw("%d",&id); while(fread(&b, sizeof(b), 1, fp) == 1) { searchid(&b, fp, id, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case REFERENCE : fp = fopen("Reference.dat","rb+"); mvaddstr(13,32,"Enter book id:"); scanw("%d",&id); while(fread(&b, sizeof(b), 1, fp) == 1) { searchid(&b, fp, id, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case SELFHELP : fp = fopen("Selfhelp.dat","rb+"); mvaddstr(13,32,"Enter book id:"); scanw("%d",&id); while(fread(&b, sizeof(b), 1, fp) == 1) { searchid(&b, fp, id, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case MENU : mainmenu(); return; break; default : mvaddstr(13,32,"Wrong option. Press enter to search again. "); getch(); refresh(); searchentry(); return; } break; case 'B' : printgen(); scanw("%d",&g); switch(g) { case FICTION : fp = fopen("Fiction.dat","rb+"); mvaddstr(13,32,"Enter book name:"); getstr(bname); while(fread(&b, sizeof(b), 1, fp) == 1) { searchname(&b, fp, bname, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case NONFICTION : fp = fopen("Nonfiction.dat","rb+"); mvaddstr(13,32,"Enter book name:"); getstr(bname); while(fread(&b, sizeof(b), 1, fp) == 1) { searchname(&b, fp, bname, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case REFERENCE : fp = fopen("Reference.dat","rb+"); mvaddstr(13,32,"Enter book name:"); getstr(bname); while(fread(&b, sizeof(b), 1, fp) == 1) { searchname(&b, fp, bname, &found); } fclose(fp); break; case SELFHELP : fp = fopen("Selfhelp.dat","rb+"); mvaddstr(13,32,"Enter book name:"); getstr(bname); while(fread(&b, sizeof(b), 1, fp) == 1) { searchname(&b, fp, bname, &found); } fclose(fp); if(!found) mvaddstr(14,32,"Record not found."); break; case MENU : mainmenu(); return; break; default : mvaddstr(13,32,"Wrong option. Press enter to search again "); getch(); clear(); refresh(); searchentry(); return; } break; default : mvaddstr(13,32,"Wrong option. Press enter to search again"); getch(); refresh(); clear(); refresh(); searchentry(); return; } mvaddstr(21,16,"Search again ? (y/n) "); scanw("%c", &option); clear(); refresh(); } mvaddstr(22,16,"Press enter to return to main menu"); getch(); mainmenu(); return; }