home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 309_01 / cc4.c < prev    next >
Text File  |  1990-03-20  |  5KB  |  337 lines

  1. /*
  2. HEADER:        Small-C source part 4;
  3. FILENAME:    CC4.C;
  4. AUTHORS:    Dieter H. Flunkert;
  5. COMPILERS:Turbo C V2.0 Medium Memory Model;
  6. SYSTEM: MSDOS (Brian Brown, Nov 1989)
  7. */
  8.  
  9. #include <stdio.h>
  10. #include "ccdef.c"
  11.  
  12. /* external functions */
  13. extern int addglb(), an(), ch(), comment(), findglb(), gch(), getint(),
  14.   illname(), inbyte(), inchar(), inline(), kill(), nch(), peephole(), symname();
  15.  
  16. /* external variables */
  17. extern int cmode, eof, errcnt, lptr, macptr, mptr, output, pause;
  18.  
  19. extern char *cptr, line[linesize], macq[macqsize], mline[linesize],
  20.   stage[stagesize], *stagelast, *stagenext;
  21.  
  22. keepch(c)
  23. char c;
  24. { mline[mptr]=c;
  25.  if(mptr<mpmax)mptr++;
  26.  return (c);
  27. }
  28.  
  29. preprocess()
  30. { int k;
  31.  char c,sname[namesize];
  32.  if(cmode==0)return;
  33.  mptr=lptr=0;
  34.  while(ch())
  35.   {if((ch()==' ')||(ch()==9))
  36.    {keepch(' ');
  37.    while((ch()==' ')|
  38.     (ch()==9))
  39.     gch();
  40.    }
  41.   else if(ch()=='"')
  42.    {keepch(ch());
  43.    gch();
  44.    while(ch()!='"')
  45.     {if(ch()==0)
  46.       {errrpt("missing quote");
  47.       break;
  48.       }
  49.     keepch(gch());
  50.     }
  51.    gch();
  52.    keepch('"');
  53.    }
  54.   else if(ch()==39)
  55.    {keepch(39);
  56.    gch();
  57.    while(ch()!=39)
  58.     {if(ch()==0)
  59.       {errrpt("missing apostrophe");
  60.       break;
  61.       }
  62.     keepch(gch());
  63.     }
  64.    gch();
  65.    keepch(39);
  66.    }
  67.   else if((ch()=='/')&&(nch()=='*'))
  68.    {inchar();inchar();
  69.    while(((ch()=='*')&
  70.     (nch()=='/'))==0)
  71.     {if(ch()) inchar();
  72.     else {
  73.      inline();
  74.      if(eof)break;
  75.      }
  76.     }
  77.    inchar();inchar();
  78.    }
  79.   else if(an(ch()))
  80.    {k=0;
  81.    while(an(ch()))
  82.     {if(k<namemax)sname[k++]=ch();
  83.     gch();
  84.     }
  85.    sname[k]=0;
  86.    if(k=findmac(sname))
  87.     while(c=macq[k++])
  88.      keepch(c);
  89.    else
  90.     {k=0;
  91.     while(c=sname[k++])
  92.      keepch(c);
  93.     }
  94.    }
  95.   else keepch(gch());
  96.   }
  97.  keepch(0);
  98.  if(mptr>=mpmax)errrpt("line too long");
  99.  lptr=mptr=0;
  100.  while(line[lptr++]=mline[mptr++]);
  101.  lptr=0;
  102.  }
  103.  
  104. addmac()
  105. { char sname[namesize];
  106.  if(symname(sname)==0)
  107.   {illname();
  108.   kill();
  109.   return;
  110.   }
  111.  
  112. addglb(sname,MACRO,0,macptr,MACRO);
  113.  while(ch()==' ' || ch()==9) gch();
  114.  while(putmac(gch()));
  115.  if(macptr>=macmax)errrpt("macro table full");
  116.  }
  117.  
  118. putmac(c)
  119.  char c;
  120. { macq[macptr]=c;
  121.  if(macptr<=macmax)++macptr;
  122.  return (c);
  123. }
  124.  
  125. findmac(sname) char *sname; {
  126.   if((findglb(sname)!=0)&&(cptr[ident]==MACRO))
  127.     return(getint(cptr+offset,offsize));
  128.   return (0);
  129. }
  130.  
  131. outbyte(c) char c; {
  132.   if(c==0) return(0);
  133.   if(stagenext) {
  134.     if(stagenext==stagelast) {
  135.       errrpt("staging buffer overflow");
  136.       return 0;
  137.       }
  138.     else *stagenext++ = c;
  139.     }
  140.   else cout(c,output);
  141.   return c;
  142.   }
  143.  
  144. /* modified for MSDOS, Brian Brown, Nov 1989 */
  145. cout(c, fd) char c; FILE *fd; {
  146.   if(putc(c, fd)==EOF) xout();
  147.   }
  148.  
  149. sout(string, fd) char *string; FILE *fd; {
  150.  if(fputs(string, fd)==EOF) xout();
  151.  }
  152.  
  153. xout() {
  154.  fputs("output error\n", stderr);
  155.  exit(ERRCODE);
  156.  }
  157.  
  158. outstr(ptr)
  159.  char ptr[];
  160.  {
  161.  int k;
  162.  k=0;
  163.  while(outbyte(ptr[k++]));
  164.  }
  165.  
  166. cnl() /* print CR to the console */
  167. {  putchar( '\n' );
  168. }
  169.  
  170. nl()
  171. {  outbyte( '\n' );
  172. }
  173.  
  174. tab()
  175. #ifdef TAB
  176.  {outbyte(TAB);}
  177. #else
  178.  {outbyte(' ');}
  179. #endif
  180.  
  181. errrpt(ptr)
  182.  char ptr[];
  183.  {
  184.  int k;
  185.  if(stdout!=output) {
  186.    putc('\n',stderr);
  187.    fputs(line,stderr);
  188.    fputs("\n*****  ",stderr);
  189.    fputs(ptr,stderr);
  190.    fputs("  *****\n",stderr);
  191.  }
  192.  comment();outstr(line);nl();comment();
  193.  k=0;
  194.  while(k<lptr)
  195.   {if(line[k]==9) tab();
  196.    else outbyte(' ');
  197.   ++k;
  198.   }
  199.  outbyte('^');
  200.  nl();comment();outstr("******  ");
  201.  outstr(ptr);
  202.  outstr("  ******");
  203.  nl();
  204.  ++errcnt;
  205.  }
  206.  
  207. ol(ptr)
  208. char ptr[];
  209. {
  210.  ot(ptr);
  211.  nl();
  212. }
  213.  
  214. ot(ptr)
  215.  char ptr[];
  216. {
  217.  tab();
  218.  outstr(ptr);
  219. }
  220.  
  221. streq(str1,str2)
  222.  char str1[],str2[];
  223.  {
  224.  int k;
  225.  k=0;
  226.  while (*str2)
  227.   {if ((*str1)!=(*str2)) return (0);
  228.   ++k; ++str1; ++str2;
  229.   }
  230.  return (k);
  231.  }
  232.  
  233. astreq(str1,str2,len)
  234.  char str1[],str2[];int len;
  235.  {
  236.  int k;
  237.  k=0;
  238.  while (k<len)
  239.   {if ((*str1)!=(*str2))break;
  240.   if(*str1==0)break;
  241.   if(*str2==0)break;
  242.   ++k; ++str1; ++str2;
  243.   }
  244.  if (an(*str1))return (0);
  245.  if (an(*str2))return (0);
  246.  return (k);
  247.  }
  248.  
  249. match(lit)
  250.  char *lit;
  251.  {
  252.  int k;
  253.  blanks();
  254.  if (k=streq(line+lptr,lit))
  255.   {lptr=lptr+k;
  256.   return (1);
  257.   }
  258.   return (0);
  259. }
  260.  
  261. amatch(lit,len)
  262. char *lit;int len;
  263. {
  264.  int k;
  265.  blanks();
  266.  if (k=astreq(line+lptr,lit,len))
  267.  { lptr=lptr+k;
  268.    while(an(ch())) inbyte();
  269.    return (1);
  270.  }
  271.  return (0);
  272. }
  273.  
  274. blanks()
  275. {
  276.  while(1)
  277.  {
  278.    while(ch()==0)
  279.    {
  280.      inline();
  281.      preprocess();
  282.      if(eof)break;
  283.    }
  284.    if(ch()==' ')gch();
  285.    else if(ch()==9)gch();
  286.    else return;
  287.  }
  288. }
  289.  
  290. PDBody( item)
  291. int item;
  292. {
  293.   char chtr;
  294.   if ( item != 0)
  295.   {
  296.     chtr = ( item % 10) + '0'; /*Calculate rightmost character*/
  297.     PDBody( item / 10);
  298.     outbyte(chtr);
  299.   }
  300. }
  301.  
  302. setstage(before, start)
  303. int *before, *start;
  304. {
  305.   if((*before=stagenext)==0) stagenext=stage;
  306.   *start=stagenext;
  307. }
  308.  
  309. clearstage(before, start)
  310. char *before, *start;
  311. {
  312.   *stagenext=0;
  313.   stagenext=before;
  314.   if( stagenext) return;
  315.   if(start) {
  316. #ifdef OPTIMIZE
  317.     peephole(start);
  318. #else
  319.     sout(start,output);
  320. #endif
  321.   }
  322. }
  323.  
  324. outdec(item)
  325. int item;
  326. {
  327.     if (item == 0) outbyte( '0');
  328.     else if (item == -32768) outstr( "-32768");
  329.     else if (item < 0)
  330.     {
  331.        outbyte( '-');
  332.        PDBody( -item);
  333.     }
  334.     else
  335.        PDBody( item);
  336. }
  337.