home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / bool-eval / alloc.c next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  3.6 KB  |  141 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. /* alloc.c:
  47.  *
  48.  * this file contains memory allocation and freeing routines
  49.  */
  50.  
  51. #include "bool.h"
  52.  
  53. extern FUNC_LIST  *f_list;
  54. extern TOKEN_LIST *t_list;
  55. extern int        max_token,
  56.                   err;
  57.  
  58. FUNC_LIST *new_func(name)
  59. char *name;
  60. { FUNC_LIST *f,*g;
  61.  
  62.   if ((f= (FUNC_LIST *)malloc(sizeof(FUNC_LIST))) == NULL) {
  63.     perror("\nMemory allocation error");
  64.     exit(1);
  65.   }
  66.   if (!f_list)
  67.     f_list= f;
  68.   else {        /* append to list and check for duplicates */
  69.     if (!strcmp(name,f_list->func)) {
  70.       printf("\nDuplicate function name.\n");
  71.       exit(1);
  72.     }
  73.     g= f_list;
  74.     while (g->next) {
  75.       if (!strcmp(name,g->func)) {
  76.         printf("\nDuplicate function name.\n");
  77.         exit(1);
  78.       }
  79.       g= g->next;
  80.     }
  81.     g->next= f;
  82.   }
  83.   strcpy(f->func,name);
  84.   f->bool_exp= NULL;
  85.   f->next= NULL;
  86.   return(f);
  87. }
  88.  
  89. /*
  90.  * this function creates a new token
  91.  */
  92.  
  93. TOKEN_LIST *new_token(s)
  94. char *s;
  95. { TOKEN_LIST *t;
  96.  
  97.   if ((t= (TOKEN_LIST *)malloc(sizeof(TOKEN_LIST))) == NULL) {
  98.     perror("\nMemory allocation error");
  99.     exit(1);
  100.   }
  101.   strcpy(t->token,s);
  102.   t->number= max_token++;
  103.   t->next= NULL;
  104.   return(t);
  105. }
  106.  
  107. /*
  108.  * this function creates a new BOOL_EXP node
  109.  */
  110.  
  111. BOOL_EXP *newbnode()
  112. { BOOL_EXP *t;
  113.  
  114.     if ((t= (BOOL_EXP *)malloc(sizeof(BOOL_EXP))) == NULL) {
  115.       err= 1;
  116.       perror("\nMemory allocation error");
  117.       return(NULL);
  118.     }
  119.     t->opcode= UNDEF;
  120.     t->value= UNDEF;
  121.     t->b1=
  122.     t->b2= NULL;
  123.     t->n1=
  124.     t->n2= 0;
  125.     return(t);
  126. }
  127.  
  128. /*
  129.  * this function free()s up a boolean tree.
  130.  */
  131.  
  132. void free_tree(b)
  133. BOOL_EXP *b;
  134. {
  135.   if (!b)
  136.     return;
  137.   free_tree(b->b1);
  138.   free_tree(b->b2);
  139.   free((char *)b);
  140. }
  141.