home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / SMALL_C / CC22.C < prev    next >
Text File  |  1987-10-04  |  8KB  |  388 lines

  1. /*
  2. ** Small C - 8088/8086 version - modified by R. Grehan, BYTE Magazine
  3. */
  4.  
  5. ifline() {
  6.   while(1) {
  7.     inline();
  8.     if(eof) return;
  9.     if(match("#ifdef")) {
  10.       ++iflevel;
  11.       if(skiplevel) continue;
  12.       symname(msname, NO);
  13.       if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0)==0)
  14.         skiplevel=iflevel;
  15.       continue;
  16.       }
  17.     if(match("#ifndef")) {
  18.       ++iflevel;
  19.       if(skiplevel) continue;
  20.       symname(msname, NO);
  21.       if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0))
  22.         skiplevel=iflevel;
  23.       continue;
  24.       }
  25.     if(match("#else")) {
  26.       if(iflevel) {
  27.         if(skiplevel==iflevel) skiplevel=0;
  28.         else if(skiplevel==0)  skiplevel=iflevel;
  29.         }
  30.       else noiferr();
  31.       continue;
  32.       }
  33.     if(match("#endif")) {
  34.       if(iflevel) {
  35.         if(skiplevel==iflevel) skiplevel=0;
  36.         --iflevel;
  37.         }
  38.       else noiferr();
  39.       continue;
  40.       }
  41.     if(skiplevel) continue;
  42.     if(cch==0) continue;
  43.     break;
  44.     }
  45.   }
  46.  
  47. keepch(c)  char c; {
  48.   if(pptr<LINEMAX) pline[++pptr]=c;
  49.   }
  50.  
  51. preprocess() {
  52.   int k;
  53.   char c;
  54.   if(ccode) {
  55.     line=mline;
  56.     ifline();
  57.     if(eof) return;
  58.     }
  59.   else {
  60.     line=pline;
  61.     inline();
  62.     return;
  63.     }
  64.   pptr = -1;
  65.   while(cch != NEWLINE && cch) {
  66.     if(white()) {
  67.       keepch(' ');
  68.       while(white()) gch();
  69.       }
  70.     else if(cch=='"') {
  71.       keepch(cch);
  72.       gch();
  73.       while((cch!='"')|((*(lptr-1)==92)&(*(lptr-2)!=92))) {
  74.         if(cch==0) {
  75.           error("no quote");
  76.           break;
  77.           }
  78.         keepch(gch());
  79.         }
  80.       gch();
  81.       keepch('"');
  82.       }
  83.     else if(cch==39) {
  84.       keepch(39);
  85.       gch();
  86.       while((cch!=39)|((*(lptr-1)==92)&(*(lptr-2)!=92))) {
  87.         if(cch==0) {
  88.           error("no apostrophe");
  89.           break;
  90.           }
  91.         keepch(gch());
  92.         }
  93.       gch();
  94.       keepch(39);
  95.       }
  96.     else if((cch=='/')&(nch=='*')) {
  97.       bump(2);
  98.       while(((cch=='*')&(nch=='/'))==0) {
  99.         if(cch) bump(1);
  100.         else {
  101.           ifline();
  102.           if(eof) break;
  103.           }
  104.         }
  105.       bump(2);
  106.       }
  107.     else if(an(cch)) {
  108.       k=0;
  109.       while((an(cch)) & (k<NAMEMAX)) {
  110.         msname[k++]=cch;
  111.         gch();
  112.         }
  113.       msname[k]=0;
  114.       if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0)) {
  115.         k=getint(cptr+NAMESIZE, 2);
  116.         while(c=macq[k++]) keepch(c);
  117.         while(an(cch)) gch();
  118.         }
  119.       else {
  120.         k=0;
  121.         while(c=msname[k++]) keepch(c);
  122.         }
  123.       }
  124.     else keepch(gch());
  125.     }
  126.   if(pptr>=LINEMAX) error("line too long");
  127.   keepch(0);
  128.   line=pline;
  129.   bump(0);
  130.   }
  131.  
  132. noiferr() {
  133.   error("no matching #if...");
  134.   errflag=0;
  135.   }
  136.  
  137. addmac() {
  138.   int k;
  139.   if(symname(msname, NO)==0) {
  140.     illname();
  141.     kill();
  142.     return;
  143.     }
  144.   k=0;
  145.   if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0)==0) {
  146.     if(cptr2=cptr) while(*cptr2++ = msname[k++]);
  147.     else {
  148.       error("macro name table full");
  149.       return;
  150.       }
  151.     }
  152.   putint(macptr, cptr+NAMESIZE, 2);
  153.   while(white()) gch();
  154.   while(putmac(gch()));
  155.   if(macptr>=MACMAX) {
  156.     error("macro string queue full"); abort(ERRCODE);
  157.     }
  158.   }
  159.  
  160. putmac(c)  char c; {
  161.   macq[macptr]=c;
  162.   if(macptr<MACMAX) ++macptr;
  163.   return c;
  164.   }
  165.  
  166. /*
  167. ** search for symbol match
  168. ** on return cptr points to slot found or empty slot
  169. */
  170. search(sname, buf, len, end, max, off)
  171.   char *sname, *buf, *end;  int len, max, off; {
  172.   cptr=cptr2=buf+((hash(sname)%(max-1))*len);
  173.   while(*cptr != 0) {
  174.     if(astreq(sname, cptr+off, NAMEMAX)) return 1;
  175.     if((cptr=cptr+len) >= end) cptr=buf;
  176.     if(cptr == cptr2) return (cptr=0);
  177.     }
  178.   return 0;
  179.   }
  180.  
  181. hash(sname) char *sname; {
  182.   int i, c;
  183.   i=0;
  184.   while(c = *sname++) i=(i<<1)+c;
  185.   return i;
  186.   }
  187.  
  188. setstage(before, start) int *before, *start; {
  189.   if((*before=stagenext)==0) stagenext=stage;
  190.   *start=stagenext;
  191.   }
  192.  
  193. clearstage(before, start) char *before, *start; {
  194.   *stagenext=0;
  195.   if(stagenext=before) return;
  196.   if(start) {
  197. #ifdef OPTIMIZE
  198.     peephole(start);
  199. #else
  200.     sout(start, output);
  201. #endif
  202.     }
  203.   }
  204.  
  205. outdec(number)  int number; {
  206.   int k,zs;
  207.   char c, *q, *r;
  208.   zs = 0;
  209.   k=10000;
  210.   if (number<0) {
  211.     number=(-number);
  212.     outbyte('-');
  213.     }
  214.   while (k>=1) {
  215.     q=0; r=number;
  216.     while(r >= k) {++q; r -= k;}
  217.     c = q + '0';
  218.     if ((c!='0')|(k==1)|(zs)) {
  219.       zs=1;
  220.       outbyte(c);
  221.       }
  222.     number=r;
  223.     k=k/10;
  224.     }
  225.   }
  226.  
  227. ol(ptr)  char ptr[];  {
  228.   ot(ptr);
  229.   nl();
  230.   }
  231.  
  232. ot(ptr) char ptr[]; {
  233.   outstr(ptr);
  234.   }
  235.  
  236. /* Act like a real C compiler...append all labels */
  237. /* with underscores.       */
  238.  
  239. outlab(ptr) char ptr[]; {
  240.  ot("_");
  241.  outstr(ptr);
  242. }
  243.  
  244. outstr(ptr) char ptr[]; {
  245.   poll(1); /* allow program interruption */
  246.   /* must work with symbol table names terminated by length */
  247.   while(*ptr >= ' ') outbyte(*ptr++);
  248.   }
  249.  
  250. outbyte(c) char c; {
  251.   if(stagenext) {
  252.     if(stagenext==stagelast) {
  253.       error("staging buffer overflow");
  254.       return 0;
  255.       }
  256.     else *stagenext++ = c;
  257.     }
  258.   else cout(c,output);
  259.   return c;
  260.   }
  261.  
  262. cout(c, fd) char c; int fd; {
  263.     if(fputc(c,fd)==EOF) xout();
  264.  }
  265.  
  266. sout(string, fd) char *string; int fd; {
  267.   while(*string)
  268.     cout(*string++,fd);
  269.   }
  270.  
  271. lout(line, fd) char *line; int fd; {
  272.   sout(line, fd);
  273.   cout(NEWLINE, fd);
  274.   }
  275.  
  276. xout() {
  277.   fputs("output error", stderr);
  278.   abort(ERRCODE);
  279.   }
  280.  
  281. nl() {
  282.   outbyte(NEWLINE);
  283.   }
  284.  
  285. col() {
  286. #ifdef COL
  287.   outbyte(':');
  288. #endif
  289.   }
  290.  
  291. error(msg) char msg[]; {
  292.   if(errflag) return; else errflag=1;
  293.   lout(line, stderr);
  294.   errout(msg, stderr);
  295.   if(alarm) fputc(7, stderr);
  296.   if(pause) while(fgetc(stderr)!=NEWLINE);
  297.   if(listfp>0) errout(msg, listfp);
  298.   }
  299.  
  300. errout(msg, fp) char msg[]; int fp; {
  301.   int k; k=line+2;
  302.   while(k++ <= lptr) cout(' ', fp);
  303.   lout("/\\", fp);
  304.   sout("**** ", fp); lout(msg, fp);
  305.   }
  306.  
  307. streq(str1,str2)  char str1[],str2[]; {
  308.   int k;
  309.   k=0;
  310.   while (str2[k]) {
  311.     if ((str1[k])!=(str2[k])) return 0;
  312.     ++k;
  313.     }
  314.   return k;
  315.  }
  316.  
  317. astreq(str1,str2,len)  char str1[],str2[];int len; {
  318.   int k;
  319.   k=0;
  320.   while (k<len) {
  321.     if ((str1[k])!=(str2[k]))break;
  322.     /*
  323.     ** must detect end of symbol table names terminated by
  324.     ** symbol length in binary
  325.     */
  326.     if(str1[k] < ' ') break;
  327.     if(str2[k] < ' ') break;
  328.     ++k;
  329.     }
  330.   if (an(str1[k]))return 0;
  331.   if (an(str2[k]))return 0;
  332.   return k;
  333.  }
  334.  
  335. match(lit)  char *lit; {
  336.   int k;
  337.   blanks();
  338.   if (k=streq(lptr,lit)) {
  339.     bump(k);
  340.     return 1;
  341.     }
  342.   return 0;
  343.   }
  344.  
  345. amatch(lit,len)  char *lit;int len; {
  346.   int k;
  347.   blanks();
  348.   if (k=astreq(lptr,lit,len)) {
  349.     bump(k);
  350.     while(an(cch)) inbyte();
  351.     return 1;
  352.     }
  353.   return 0;
  354.  }
  355.  
  356. nextop(list) char *list; {
  357.   char op[4];
  358.   opindex=0;
  359.   blanks();
  360.   while(1) {
  361.     opsize=0;
  362.     while(*list > ' ') op[opsize++] = *list++;
  363.     op[opsize]=0;
  364.     if(opsize=streq(lptr, op))
  365.       if((*(lptr+opsize) != '=')&
  366.          (*(lptr+opsize) != *(lptr+opsize-1)))
  367.          return 1;
  368.     if(*list) {
  369.       ++list;
  370.       ++opindex;
  371.       }
  372.     else return 0;
  373.     }
  374.   }
  375.  
  376. blanks() {
  377.   while(1) {
  378.     while(cch) {
  379.       if(white()) gch();
  380.       else return;
  381.       }
  382.     if(line==mline) return;
  383.     preprocess();
  384.     if(eof)break;
  385.     }
  386.   }
  387.  
  388.