home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / bool-eval / eval_file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  5.4 KB  |  218 lines

  1. /* (c) copyright 1987 jim frost
  2.  * all rights reserved
  3.  *
  4.  * this program is copyrighted material.  the author gives permission
  5.  * to duplicate and redistribute this program provided the following
  6.  * conditions are met:
  7.  *   - this copyright notice is not removed.
  8.  *   - all duplicate copies or distributions contain full source
  9.  *     and documentation, including copyright notices.
  10.  *   - duplicate copies or distributions outside a single site are
  11.  *     original distributions without modifications.  (this is to keep
  12.  *     bastardized versions from showing up all over thie place.)
  13.  *
  14.  * this program source may be modified provided the following
  15.  * conditions are met:
  16.  *   - modified source is not distributed to other sites.
  17.  *   - modifications (including, but not limited to, bug fixes) are
  18.  *     sent to the author if the modifications are to be distributed.
  19.  *     no modified source is to be distributed unless done so by the
  20.  *     author.
  21.  *
  22.  * no warranty, either express or implied, is given for this program.
  23.  * the author makes no guarantees of fitness for any use of this
  24.  * program.  the author is not responsible for damages resulting from
  25.  * the use of this program for any purpose.
  26.  *
  27.  * 'site' refers to one or more computers under a single management.
  28.  * 'author' refers to the copyright holder, jim frost.
  29.  * 'source' refers to all files related to this program.
  30.  * 'documentation' refers to non-compilable files in the distribution.
  31.  *
  32.  * basically this notice is to keep me out of trouble should anything
  33.  * go wrong (i really *do* test these things though) and to make sure
  34.  * that the distribution of code is centralized.  makes bug fixes and
  35.  * enhancements much easier.
  36.  *
  37.  * thank you for your attention to this copyright notice.  if everyone
  38.  * follows this, you may find this a useful tool that is pretty well
  39.  * supported.
  40.  *
  41.  * author information:
  42.  *   jim frost                    permanent usnail address:
  43.  *   madd@bucsb.bu.edu            75 washington street
  44.  *   ..!harvard!bu-cs!bucsb!madd  laconia, nh  03246.
  45.  */
  46. /* eval_file.c:
  47.  *
  48.  * this function calls each of the necessary routines to build and
  49.  * evaluate a tree.
  50.  */
  51.  
  52. #include "bool.h"
  53.  
  54. int err,
  55.     paren,
  56.     max_token,
  57.     print_err;
  58.  
  59. extern int debug_mode,
  60.            no_print;
  61.  
  62. static char *line= "----------------------------------------------------";
  63.  
  64. TOKEN_LIST *t_list;
  65. FUNC_LIST  *f_list;
  66.  
  67. /*
  68.  * this function processes each boolean equation
  69.  */
  70.  
  71. void eval_file(f)
  72. FILE *f;
  73. { FUNC_LIST  *fn;
  74.   TOKEN_LIST *t;
  75.   char       func[MAXTOKEN],
  76.              c;
  77.   int        a;
  78.  
  79.   paren= 0;
  80.   max_token= 0;
  81.   t_list= NULL;
  82.   f_list= NULL;
  83.  
  84. /*
  85.  * loop through file to build all functions
  86.  */
  87.  
  88.   func[0]= '\0';
  89.   c= ' ';
  90.   while (!feof(f)) {
  91.     if (blank(c)) {
  92.       cget(f,c);
  93.     }
  94.     else if (begtok(c)) {
  95.       a= 0;
  96.       do {
  97.         func[a++]= c;
  98.         cget(f,c);
  99.       } while(intok(c));
  100.       func[a]= '\0';
  101.     }
  102.  
  103. /*
  104.  * when we hit an '=', start building a function
  105.  */
  106.  
  107.     else if ((c == '=') && strlen(func)) {
  108.       fn= new_func(func);
  109.       fn->bool_exp= build(f);
  110.  
  111. /*
  112.  * if we got a syntax error, explain that we're printing the function
  113.  * as evaluated up to the error.  otherwise, print out the header and
  114.  * do the tree evaluation.
  115.  */
  116.  
  117.       if (err) {
  118.         if (debug_mode) {
  119.           printf("Parsed function up to error:\n");
  120.           print_err= 0;
  121.           printf("%s= ",func);
  122.           print_tree(fn->bool_exp);
  123.           printf("\n");
  124.         }
  125.         exit(1);
  126.       }
  127.       if (debug_mode) {
  128.         printf("\n\nFunction parsed as:\n");
  129.         printf("%s= ",func);
  130.         print_tree(fn->bool_exp);
  131.         printf(";\n");
  132.       }
  133.       func[0]= '\0';    /* reset for next run */
  134.       cget(f,c);
  135.     }
  136.     else switch (c) {
  137.       case '[' :
  138.         do
  139.           cget(f,c);
  140.         while ((!feof(f)) && (c != ']'));
  141.         cget(f,c);
  142.         break;
  143.       case '{' :
  144.         do
  145.           cget(f,c);
  146.         while ((!feof(f)) && (c != '}'));
  147.         cget(f,c);
  148.         break;
  149.       case '=' :
  150.         printf("\nMissing equation declaration.\n");
  151.         exit(1);
  152.       case EOF :
  153.         printf("\nUnexpected end of file.\n");
  154.         exit(1);
  155.       default :
  156.         printf("\nIllegal character (possibly missing equation definition).\n");
  157.         exit(1);
  158.     }
  159.   }
  160.   printf("\n");
  161.   t= t_list;
  162.   while (t) {
  163.     printf("%s ",t->token);
  164.     t= t->next;
  165.   }
  166.   printf("| ");
  167.   fn= f_list;
  168.   while (fn) {
  169.     printf("%s ",fn->func);
  170.     fn= fn->next;
  171.   }
  172.   printf("\n");
  173.   t= t_list;
  174.   while (t) {
  175.     printf("%*.*s",strlen(t->token)+1,strlen(t->token)+1,line);
  176.     t= t->next;
  177.   }
  178.   printf("+-");
  179.   fn= f_list;
  180.   while (fn) {
  181.     printf("%*.*s",strlen(fn->func)+1,strlen(fn->func)+1,line);
  182.     fn= fn->next;
  183.   }
  184.   printf("\n");
  185.   eval_func(t_list); /* dump output */
  186.  
  187. /*
  188.  * because bool can be run on a lot of files each execution, we have
  189.  * to free up memory allocated for each file.  while we're at it, we
  190.  * print out lists of stuff used in the file if we're in debug mode.
  191.  * might be useful to somebody.
  192.  */
  193.  
  194.   if (debug_mode) {
  195.     printf("\n\nFunctions Defined\n");
  196.     printf("-----------------\n");
  197.   }
  198.   while (f_list) {
  199.     if (debug_mode)
  200.       printf("%s\n",f_list->func);
  201.     fn= f_list;
  202.     f_list= f_list->next;
  203.     free_tree(fn->bool_exp);
  204.     free((char *)fn);
  205.   }
  206.   if (debug_mode) {
  207.     printf("\n\nTokens Defined\n");
  208.     printf("--------------\n");
  209.   }
  210.   while (t_list) {
  211.     if (debug_mode)
  212.       printf("%s\n",t_list->token);
  213.     t= t_list;
  214.     t_list= t_list->next;
  215.     free((char *)t);
  216.   }
  217. }
  218.