home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / misc / icalc.lzh / icalc / src / symbol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-26  |  2.7 KB  |  145 lines

  1. /*
  2. *    icalc - complex-expression parser
  3. *
  4. *    Symbol-table management routines for complex-number expression parser.
  5. *
  6. *    (C) Martin W Scott, 1991.
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "memory.h"
  12. #include "complex.h"
  13. #include "complex.tab.h"
  14.  
  15.  
  16. /* Prototypes for static functions defined in symbol.c */
  17. static void insert(Symbol **tree, Symbol *item);
  18. static void traverse(Symbol *tree, short type, void (*visit)(Symbol *));
  19. static void printval(Symbol *item);
  20. static void printname(Symbol *item);
  21. static void printufunc(Symbol *item);
  22. static void printargs(SymList *sl);
  23.  
  24. static Symbol    *symtree = NULL;    /* symbol table: binary tree */
  25.  
  26. Symbol *lookup(s)            /* find s in symbol table */
  27.     char *s;
  28. {
  29.     Symbol *sp = symtree;
  30.     int cmp;
  31.  
  32.     while (sp)
  33.     {
  34.         cmp = strcmp(s, sp->name);
  35.         if (cmp == 0)
  36.             break;
  37.         else if (cmp < 0)
  38.             sp = sp->left;
  39.         else  /* cmp > 0 */
  40.             sp = sp->right;
  41.     }
  42.  
  43.     return sp;
  44. }
  45.  
  46. static void insert(tree, item)        /* no duplicate names permitted... */
  47.     Symbol **tree, *item;
  48. {
  49.     if (*tree == NULL)
  50.     {
  51.         *tree = item;
  52.         item->left = item->right = NULL;
  53.     }
  54.     else if (strcmp(item->name, (*tree)->name) < 0)
  55.         insert(&(*tree)->left, item);
  56.     else
  57.         insert(&(*tree)->right, item);
  58. }
  59.  
  60. Symbol *allocsym(s, t)            /* allocate a symbol */
  61.     char *s;
  62.     int t;
  63. {
  64.     Symbol *sp;
  65.     
  66.     sp = (Symbol *) emalloc(sizeof(Symbol));
  67.     sp->name = emalloc(strlen(s)+1);
  68.     strcpy(sp->name, s);
  69.     sp->type = t;
  70.  
  71.     return sp;
  72. }
  73.  
  74. Symbol *install(s, t, cval)        /* install s in symbol table */
  75.     char *s;
  76.     int t;
  77.     Complex cval;
  78. {
  79.     Symbol *sp = allocsym(s, t);
  80.     sp->u.val = cval;
  81.  
  82.     insert(&symtree, sp);
  83.     return sp;
  84. }
  85.  
  86. static void traverse(tree, type, visit)    /* inorder traversal of tree */
  87.     Symbol *tree;
  88.     short type;
  89.     void (*visit)(struct Symbol *);
  90. {
  91.     if (tree)
  92.     {
  93.         traverse(tree->left, type, visit);
  94.         if (tree->type == type)
  95.             (*visit)(tree);
  96.         traverse(tree->right, type, visit);
  97.     }
  98. }
  99.  
  100. static void printval(item)
  101.     Symbol *item;
  102. {
  103.     fprintf(stdout, "\t%s\t= ", item->name);
  104.     cprin(stdout, NULL, "\n", item->u.val);
  105. }
  106.  
  107. static void printargs(sl)    /* of course, list gives params in */
  108.     SymList *sl;        /* REVERSE order...sigh */
  109. {
  110.     if (sl)            /* there are arguments */
  111.     {
  112.         if (sl->next)
  113.         {
  114.             printargs(sl->next);
  115.             fputs(",", stdout);
  116.         }
  117.         fputs(sl->sym->name, stdout);
  118.     }
  119. }
  120.  
  121. static void printufunc(item)
  122.     Symbol *item;
  123. {
  124.     fprintf(stdout, "\t%s(", item->name);
  125.     printargs(item->u.ufunc.params);
  126.     fprintf(stdout, ") = \n");
  127. }
  128.  
  129. static void printname(item)
  130.     Symbol *item;
  131. {
  132.     fprintf(stdout,"\t%s\n", item->name);
  133. }
  134.  
  135. void printlist(type)        /* print names of symbols with given type */
  136.     int type;        /* simple-minded at moment */
  137. {
  138.     if (type == CONST || type == VAR)
  139.         traverse(symtree, type, printval);
  140.     else if (type == UFUNC)
  141.         traverse(symtree, type, printufunc);
  142.     else
  143.         traverse(symtree, type, printname);
  144. }
  145.