home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / bool-eval / eval_func.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  3.8 KB  |  145 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_func.c:
  47.  *
  48.  * this contains boolean expression evaluation routines.
  49.  */
  50.  
  51. #include "bool.h"
  52.  
  53. extern FUNC_LIST  *f_list;
  54. extern TOKEN_LIST *t_list;
  55. extern int        line;
  56. /*
  57.  * this function evaluates a boolean expression tree
  58.  */
  59.  
  60. int eval_exp(b)
  61. BOOL_EXP *b;
  62. { int e1, e2;
  63.  
  64.   if (b->opcode > TOKEN) {
  65.     e1= eval_exp(b->b1);
  66.     e2= eval_exp(b->b2);
  67.     if (b->n1)
  68.       e1= !e1;
  69.     if (b->n2)
  70.       e2= !e2;
  71.   }
  72.   switch(b->opcode) {
  73.     case UNDEF : /* single expressions have undefined opcodes */
  74.       if (b->n1)
  75.         return(!eval_exp(b->b1));
  76.       else
  77.         return(eval_exp(b->b1));
  78.     case TRUE :
  79.       return(1);
  80.     case FALSE :
  81.       return(0);
  82.     case TOKEN :
  83.       return(token_val(b->value));
  84.     case OR :
  85.       return(e1 || e2);
  86.     case AND :
  87.       return(e1 && e2);
  88.     case XOR :
  89.       return((e1 || e2) && (!(e1 && e2)));
  90.   }
  91. }
  92.  
  93. /*
  94.  * this routine evaluates a function for all possible values of all tokens
  95.  */
  96.  
  97. void eval_func(t)
  98. TOKEN_LIST   *t;
  99. { FUNC_LIST  *f;
  100.   TOKEN_LIST *u;
  101.   int        l1,l2;
  102.  
  103.   if (t == NULL) { /* end of list so do evaluations */
  104.  
  105. /*
  106.  * print out values of each token
  107.  */
  108.  
  109.     u= t_list;
  110.     while (u) {
  111.       l1= l2= strlen(u->token)/2;
  112.       if (!(strlen(u->token) % 2))
  113.         l1--;
  114.       printf("%*.*s%d%*.*s ",l1,l1,"",u->value,l2,l2,"");
  115.       u= u->next;
  116.     }
  117.     printf("| ");
  118.  
  119. /*
  120.  * print out values of each tree
  121.  */
  122.  
  123.     f= f_list;
  124.     while(f) {
  125.       l1= l2= strlen(f->func)/2;
  126.       if (!(strlen(f->func) % 2))
  127.         l1--;
  128.       printf("%*.*s%d%*.*s ",l1,l1,"",eval_exp(f->bool_exp),l2,l2,"");
  129.       f= f->next;
  130.     }
  131.     printf("\n");
  132.     return;
  133.   }
  134.  
  135. /*
  136.  * this section handles value setting to evaluate for all possible
  137.  * token values.
  138.  */
  139.  
  140.   t->value= 0;
  141.   eval_func(t->next);
  142.   t->value= 1;
  143.   eval_func(t->next);
  144. }
  145.