1: # include "r.h" 2: 3: char *keyword []{ 4: "do", "DO", /* have to be first */ 5: "if", "IF", 6: "else", "ELSE", 7: "for", "FOR", 8: "repeat", "REPEAT", 9: "until", "UNTIL", 10: "while", "WHILE", 11: "break", "BREAK", 12: "next", "NEXT", 13: "define", "DEFINE", 14: "include", "INCLUDE", 15: 0}; 16: 17: #include "y.tab.c" 18: 19: int keytran[]{ 20: 0, 0, 21: XIF, XIF, 22: XELSE, XELSE, 23: XFOR, XFOR, 24: REPEAT, REPEAT, 25: UNTIL, UNTIL, 26: XWHILE, XWHILE, 27: XBREAK, XBREAK, 28: NEXT, NEXT, 29: XDEFINE, XDEFINE, 30: XINCLUDE, XINCLUDE, 31: 0}; 32: 33: int svargc; 34: char **svargv; 35: int infile 0; 36: int fd 0; 37: int ninclude 0; 38: int filestack[10]; 39: int linect[10]; 40: 41: main(argc,argv) int argc; char **argv; { 42: contfld = errorflag = 0; 43: if(argc>1 && argv[1][0]=='-'){ 44: if(argv[1][1]=='6') 45: contfld=6; 46: argc--; 47: argv++; 48: } 49: svargc = argc; 50: svargv = argv; 51: filestack[0] = infile = fd = ninclude = linect[0] = 0; 52: if(--svargc>0) 53: if( (fd = filestack[0] = copen(svargv[++infile],'r')) < 0 ) { 54: error("can't open %s", svargv[infile]); 55: cexit(1); 56: } 57: yyparse(); 58: cexit(errorflag); 59: } 60: 61: int peek -1; 62: int nextchar '\n'; 63: 64: getc(){ 65: nextchar = (peek<0) ? gchar(): peek; 66: peek = -1; 67: return(nextchar); 68: } 69: 70: int gcp 0; 71: char gcbuf[300]; 72: int apos -1; 73: 74: gchar(){ 75: extern int linect[], nnames; 76: extern char *names[], *nameptr[]; 77: int c,i,atype,t; 78: if( c=gcbuf[gcp++] ) 79: return(c); 80: loop: 81: for(gcp=0; (c=gcbuf[gcp]=cgetc(fd))!='\0' ; gcp++ ){ 82: if( gcbuf[0]== '%' ){ 83: while(putchar(cgetc(fd))!='\n'); 84: gcp = -1; 85: ++linect[ninclude]; 86: continue; 87: } 88: if( (atype=alphanum(c)) && apos < 0 ){ 89: apos = gcp; 90: continue; 91: } 92: if( !atype ) 93: if( apos >= 0 ){ 94: gcbuf[gcp] = '\0'; 95: if( nnames>0 && (t=lookup(&gcbuf[apos],names))>=0){ 96: for(i=0;gcbuf[apos++]=nameptr[t][i];i++); 97: gcp = apos-1; 98: } 99: apos = -1; 100: gcbuf[gcp] = c; 101: } 102: if( c < ' ' && (c!='\n' && c!='\t') ) /* strip crap */ 103: c = gcbuf[gcp] = ' '; 104: if( c=='#' ){ 105: gcbuf[gcp] = '\n'; 106: while( (c=cgetc(fd))!='\n' && c!='\0'); 107: } 108: if( c=='"' || c=='\'' ){ 109: while( (gcbuf[++gcp]=t=cgetc(fd)) != c ) 110: if( t=='\n' ) { 111: error("unbalanced quote"); 112: gcbuf[gcp] = c; 113: gcbuf[++gcp] = c = '\n'; 114: goto newline; 115: } 116: continue; 117: } 118: newline: 119: if( c=='\n' ){ 120: gcbuf[gcp+1] = '\0'; 121: gcp = 1; 122: ++linect[ninclude]; 123: return(gcbuf[0]); 124: } 125: } 126: if(ninclude){ 127: cclose(filestack[ninclude--]); 128: fd = filestack[ninclude]; 129: goto loop; 130: } 131: cclose(filestack[ninclude]); 132: if(--svargc>0){ 133: if( (fd = filestack[ninclude] = copen(svargv[++infile],'r')) < 0) { 134: error("can't open %s", svargv[infile]); 135: cexit(1); 136: } 137: linect[0] = 0; 138: goto loop; 139: } 140: return(0); 141: } 142: 143: inclstat(){ 144: int i,c; 145: char fname[100]; 146: while( (c=getc())==' ' || c=='\t' ); 147: peek = c; 148: for(i=0; (fname[i]=c=getc())!='\n' && c!=';' && c!=' ' && c!='\t'; i++); 149: fname[i] = '\0'; 150: if( (fd = copen(fname,'r')) < 0 ) { 151: error("can't open %s", fname); 152: cexit(1); 153: } 154: else { 155: filestack[++ninclude] = fd; 156: linect[ninclude] = 0; 157: } 158: } 159: 160: lookup(string,tbl) char *string; char *tbl[]; { 161: register i,j, r; 162: for(i=0; tbl[i]!=0; i++){ 163: for( j=0; (r=tbl[i][j])==string[j] && r!='\0'; j++); 164: if( r == string[j] ) 165: return(i); 166: } 167: return( -1 ); 168: } 169: 170: char str[200]; 171: int strp; 172: int nstr; 173: 174: yylex(){ 175: int c, type; 176: top: 177: while( (c=getc())==' ' || c=='\n' || c=='\t' ); 178: yylval = c; 179: switch(c){ 180: 181: case '\0': 182: return('\0'); 183: case ';': 184: return(SCOL); 185: case'{': 186: return(LCURL); 187: case '}': 188: return(RCURL); 189: } 190: peek = c; 191: nstr = getstr(str); 192: yylval = &str[0]; 193: if( alldigits(str) ) 194: return(DIGITS); 195: type = lookup(str,keyword); 196: if( keytran[type]==XDEFINE ) { 197: defstat(); 198: goto top; 199: } else if( keytran[type]==XINCLUDE ) { 200: inclstat(); 201: goto top; 202: } else if( type > 1 ) 203: return(keytran[type]); 204: else if( type < 0 ) 205: return(XGOK); 206: while( (c=getc())==' ' || c=='\t' || c=='\n' ); 207: peek = c; 208: if( c>='a' && c<='z' || c>='A' && c<='Z' ) 209: return(NEWDO); 210: else 211: return(OLDDO); 212: } 213: 214: getstr(s) char *s; { 215: int c, sp; 216: for (sp=0; (c=s[sp++]=getc())!=' ' && c!='\t' && c!='\n' && c!='{' && c!='}' 217: && c!=';' && c!='(' && c!=')' ; ) 218: if( c=='\'' || c=='"' ) 219: while( (s[sp++]=getc())!=c ); 220: peek = c; 221: s[--sp]='\0'; 222: return(sp); 223: } 224: 225: alldigits(s) char *s; { 226: int c; 227: if( *s == '\0' ) 228: return(0); 229: while( (c = *s++) != '\0' ) 230: if( c<'0' || c>'9' ) 231: return(0); 232: return(1); 233: } 234: 235: int dbg 0; 236: 237: yyerror(){;} 238: 239: alphanum(c) int c; { 240: if(c>='0' && c<='9') return(1); 241: if(c>='a' && c<='z') return(1); 242: if(c>='A' && c<='Z') return(1); 243: return(0); 244: } 245: 246: #define MAXNAMES 100 247: 248: char *names[MAXNAMES]; 249: char *nameptr[MAXNAMES]; 250: int nnames 0; 251: 252: defstat(){ 253: int c,i,index; 254: extern int peek,nstr; 255: extern char str[]; 256: char *getvec(); 257: while( (c=getc())==' ' || c=='\t' ); 258: peek = c; 259: for(nstr=0; c=getc(); nstr++ ){ 260: if(c==' ' || c=='\t' || c=='\n') break; 261: str[nstr] = c; 262: } 263: peek = c; 264: str[nstr] = '\0'; 265: if( (index=lookup(str,names)) >= 0 ) 266: nameptr[index] = 0; 267: else if( (index = nnames++)>=MAXNAMES-1 ){ 268: error("too many defined names"); 269: cexit(1); 270: } 271: names[index] = getvec(nstr+1); 272: for( i=0; names[index][i]=str[i]; i++ ); 273: while( (c=getc())==' ' || c=='\t' ); 274: peek = c; 275: for( i=0; (c=getc())!='\n' && c!='\0'; i++ ) 276: str[i] = c; 277: str[i] = '\0'; 278: nameptr[index] = getvec(i+1); 279: for( i=0; nameptr[index][i]=str[i]; i++ ); 280: }