home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-2 / iconc.sit / trans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  4.5 KB  |  204 lines  |  [TEXT/MPS ]

  1. /*
  2.  * trans.c - main control of the translation process.
  3.  */
  4.  
  5. #include "::h:gsupport.h"
  6. #include "globals.h"
  7. #include "trans.h"
  8. #include "tsym.h"
  9. #include "tree.h"
  10. #include "token.h"
  11. #include "tcode.h"
  12. #include "tproto.h"
  13.  
  14. /*
  15.  * Prototypes.
  16.  */
  17.  
  18. hidden FILE    *preprocess    Params((char *filename));
  19. hidden novalue    trans1        Params((char *filename));
  20.  
  21. int tfatals = 0;        /* total number of fatal errors */
  22. int twarns = 0;            /* total number of warnings */
  23. int in_line;            /* current input line number */
  24. int incol;            /* current input column number */
  25. int peekc;            /* one-character look ahead */
  26.  
  27. FILE *srcfile;            /* current input file */
  28.  
  29. /*
  30.  * translate a number of files, returning an error count
  31.  */
  32. int trans()
  33.    {
  34.    register struct pentry *proc;
  35.    struct srcfile *sf;
  36.  
  37.    for (sf = srclst; sf != NULL; sf = sf->next)
  38.       trans1(sf->name);    /* translate each file in turn */
  39.  
  40.    /*
  41.     * Resolve undeclared references.
  42.     */
  43.    for (proc = proc_lst; proc != NULL; proc = proc->next)
  44.       resolve(proc);
  45.  
  46. #ifdef DeBug
  47.    symdump();
  48. #endif                    /* DeBug */
  49.  
  50.    if (tfatals == 0) {
  51.       chkstrinv();  /* see what needs to be available for string invocation */
  52.       chkinv();     /* perform "naive" optimizations */
  53.       }
  54.  
  55.    if (tfatals == 0)
  56.       typeinfer();        /* perform type inference */
  57.  
  58.    if (just_type_trace)
  59.       return tfatals;     /* stop without generating code */
  60.  
  61.    if (tfatals == 0) {
  62.       var_dcls();         /* output declarations for globals and statics */
  63.       const_blks();       /* output blocks for cset and real literals */
  64.       for (proc = proc_lst; proc != NULL; proc = proc->next)
  65.          proccode(proc);  /* output code for a procedure */
  66.       recconstr(rec_lst); /* output code for record constructors */
  67.       }
  68.  
  69.    /*
  70.     * Report information about errors and warnings and be correct about it.
  71.     */
  72.    if (tfatals == 1)
  73.       fprintf(stderr, "1 error; ");
  74.    else if (tfatals > 1)
  75.       fprintf(stderr, "%d errors; ", tfatals);
  76.    else if (verbose > 0)
  77.       fprintf(stderr, "No errors; ");
  78.  
  79.    if (twarns == 1)
  80.       fprintf(stderr, "1 warning\n");
  81.    else if (twarns > 1)
  82.       fprintf(stderr, "%d warnings\n", twarns);
  83.    else if (verbose > 0)
  84.       fprintf(stderr, "no warnings\n");
  85.    else if (tfatals > 0)
  86.       fprintf(stderr, "\n");
  87.  
  88. #ifdef TranStats
  89.    tokdump();
  90. #endif                    /* TranStats */
  91.  
  92.    return tfatals;
  93.    }
  94.  
  95. /*
  96.  * translate one file.
  97.  */
  98. static novalue trans1(filename)
  99. char *filename;
  100.    {
  101.    in_line = 1;            /* start with line 1, column 0 */
  102.    incol = 0;
  103.    peekc = 0;            /* clear character lookahead */
  104.  
  105.    if (m4pre)
  106.       srcfile = preprocess(filename);
  107.    else if (strcmp(filename,"-") == 0) {
  108.       srcfile = stdin;
  109.       filename = "stdin";
  110.       }
  111.    else
  112.       srcfile = fopen(filename,"r");
  113.    if (srcfile == NULL) {
  114.       tfatal(filename, "cannot open source file");
  115.       return;
  116.       }
  117.    if (verbose > 0)
  118.       fprintf(stderr, "%s:\n",filename);
  119.  
  120.    tok_loc.n_file = filename;
  121.    in_line = 1;
  122.  
  123.    yyparse();                /* Parse the input */
  124.  
  125.    /*
  126.     * Close the the input file.
  127.     */
  128.  
  129.    if (!m4pre) {
  130.       if (srcfile != stdin) fclose(srcfile);
  131.       }
  132.    /* "else" is below in conditional */
  133.  
  134. /*
  135.  * The following code is operating-system dependent [@trans.01].  This code
  136.  *  closes the pipe for the preprocessor if processing was done.
  137.  */
  138.  
  139. #if PORT
  140. /* nothing to do */
  141. Deliberate Syntax Error
  142. #endif                    /* PORT */
  143.  
  144. #if AMIGA || ATARI_ST || MACINTOSH || MSDOS || MVS || VM || VMS
  145. /* nothing to do */
  146. #endif                    /* AMIGA || ATARI_ST || ... */
  147.  
  148. #if UNIX
  149.    else if (pclose(srcfile) != 0)
  150.       quit("m4 terminated abnormally");
  151. #endif                    /* UNIX */
  152.  
  153. /*
  154.  * End of operating-system specific code.
  155.  */
  156.    }
  157.  
  158. /*
  159.  * writecheck - check the return code from a stdio output operation
  160.  */
  161. novalue writecheck(rc)
  162.    int rc;
  163.  
  164.    {
  165.    if (rc < 0)
  166.       quit("unable to write to icode file");
  167.    }
  168.  
  169. /*
  170.  * preprocess - open a pipe to the m4 preprocessor.
  171.  */
  172. static FILE *preprocess(filename)
  173. char *filename;
  174. {
  175. /*
  176.  * The following code is operating-system dependent [@trans.02]. This code opens
  177.  *  a pipe to m4(1) on systems where the "-m" flag is accepted.
  178.  */
  179.  
  180. #if PORT
  181.    /* can't get here */
  182. Deliberate Syntax Error
  183. #endif                    /* PORT */
  184.  
  185. #if AMIGA || ATARI_ST || MACINTOSH || MSDOS || MVS || VM || VMS
  186.    /* can't get here */
  187. #endif                    /* AMIGA || ATARI_ST || ... */
  188.  
  189. #if UNIX
  190.       {
  191.       FILE *f, *popen();
  192.       char *s = alloc((unsigned int)(4+strlen(filename)));
  193.       sprintf(s,"m4 %s",filename);
  194.       f = popen(s,"r");
  195.       free(s);
  196.       return f;
  197.       }
  198. #endif                    /* UNIX */
  199.  
  200. /*
  201.  * End of operating-system specific code.
  202.  */
  203. }
  204.