home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 338_02 / preproc.c < prev    next >
Text File  |  1979-12-31  |  3KB  |  86 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. FILE            *inclfile[10];
  26. int             incldepth = 0;
  27. int             inclline[10];
  28. char            *lptr;
  29.  
  30. preprocess()
  31. {       ++lptr;
  32.         lastch = ' ';
  33.         getsym();               /* get first word on line */
  34.         if( lastst != id ) {
  35.                 error(ERR_PREPROC);
  36.                 return getline(incldepth == 0);
  37.                 }
  38.         if( strcmp(lastid,"include") == 0 )
  39.                 return doinclude();
  40.         else if( strcmp(lastid,"define") == 0 )
  41.                 return dodefine();
  42.         else    {
  43.                 error(ERR_PREPROC);
  44.                 return getline(incldepth == 0);
  45.                 }
  46. }
  47.  
  48. doinclude()
  49. {       int     rv;
  50.         getsym();               /* get file to include */
  51.         if( lastst != sconst ) {
  52.                 error(ERR_INCLFILE);
  53.                 return getline(incldepth == 0);
  54.                 }
  55.         inclline[incldepth] = lineno;
  56.         inclfile[incldepth++] = input;  /* push current input file */
  57.         input = fopen(laststr,"r");
  58.         if( input == 0 ) {
  59.                 input = inclfile[--incldepth];
  60.                 error(ERR_CANTOPEN);
  61.                 rv = getline(incldepth == 0);
  62.                 }
  63.         else    {
  64.                 rv = getline(incldepth == 1);
  65.                 lineno = -32768;        /* dont list include files */
  66.                 }
  67.         return rv;
  68. }
  69.  
  70. dodefine()
  71. {       SYM     *sp;
  72.         getsym();               /* get past #define */
  73.         if( lastst != id ) {
  74.                 error(ERR_DEFINE);
  75.                 return getline(incldepth == 0);
  76.                 }
  77.         ++global_flag;          /* always do #define as globals */
  78.         sp = xalloc(sizeof(SYM));
  79.         sp->name = litlate(lastid);
  80.         sp->value.s = litlate(lptr);
  81.         insert(sp,&defsyms);
  82.         --global_flag;
  83.         return getline(incldepth == 0);
  84. }
  85.  
  86.