home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / bool-eval / token.c < prev   
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. /* token.c:
  47.  *
  48.  * this file contains token creation, insertion, print, and value funcs.
  49.  */
  50.  
  51. #include "bool.h"
  52.  
  53. extern TOKEN_LIST *t_list;
  54.  
  55. /*
  56.  * these functions return a boolean value dependent on whether or
  57.  * not a char is a legal char inside a token.
  58.  */
  59.  
  60. int begtok(c)
  61. char c;
  62. {
  63.   return(((c >='@') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) ||
  64.          (c == '_'));
  65. }
  66.  
  67. int intok(c)
  68. char c;
  69. {
  70.   return(begtok(c) || ((c >='0') && (c <='9')));
  71. }
  72.  
  73. /*
  74.  * this looks through the token list and returns token values.  if no
  75.  * token exists, it is added.
  76.  */
  77.  
  78. int get_token(s)
  79. char *s;
  80. { TOKEN_LIST *t, *u;
  81.  
  82.   if (!t_list)
  83.     return((t_list= new_token(s))->number); /* null list so make one */
  84.  
  85. /*
  86.  * try to find token in list
  87.  */
  88.  
  89.   t= t_list;
  90.   for (;;) {                 /* we should always return() out of this */
  91.     if (!strcmp(t->token,s)) /* found it so return its number */
  92.       return(t->number);
  93.     else if (t->next)        /* if not end of list, keep looking */
  94.       t= t->next;
  95.     else {                   /* end of list so make new token */
  96.       if (strcmp(s,t_list->token) < 0) {
  97.         t= new_token(s);
  98.         t->next= t_list;
  99.         t_list= t;
  100.         return(t_list->number);
  101.       }
  102.       else {
  103.         for (t= t_list; (t->next) && (strcmp(s,t->next->token) > 0); t= t->next);
  104.         u= t->next;
  105.         t->next= new_token(s);
  106.         t->next->next= u;
  107.         return(t->next->number);
  108.       }
  109.     }
  110.   }
  111. }
  112.  
  113. void print_token(tok)
  114. int tok;
  115. { TOKEN_LIST *t;
  116.  
  117.   t= t_list;
  118.   while (t) {
  119.     if (t->number == tok) {
  120.       printf("%s",t->token);
  121.       return;
  122.     }
  123.     t= t->next;
  124.   }
  125.  
  126. /*
  127.  * if we get down this far, it's screwed.
  128.  */
  129.  
  130.   panic("print_token");
  131. }
  132.  
  133. int token_val(tok)
  134. int tok;
  135. { TOKEN_LIST *t;
  136.  
  137.   t= t_list;
  138.   while (t) {
  139.     if (t->number == tok)
  140.       return(t->value);
  141.     t= t->next;
  142.   }
  143.   panic("token_val");
  144. }
  145.