URI:
       tsymtab.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tsymtab.c (2189B)
       ---
            1 #include        <stdio.h>
            2 #include        <ctype.h>
            3 #include        "pic.h"
            4 #include        "y.tab.h"
            5 
            6 YYSTYPE
            7 getvar(char *s)        /* return value of variable s (usually pointer) */
            8 {
            9         struct symtab *p;
           10         static YYSTYPE bug;
           11 
           12         p = lookup(s);
           13         if (p == NULL) {
           14                 if (islower(s[0]))
           15                         ERROR "no such variable as %s", s WARNING;
           16                 else
           17                         ERROR "no such place as %s", s WARNING;
           18                 return(bug);
           19         }
           20         return(p->s_val);
           21 }
           22 
           23 double
           24 getfval(char *s)        /* return float value of variable s */
           25 {
           26         YYSTYPE y;
           27 
           28         y = getvar(s);
           29         return y.f;
           30 }
           31 
           32 void
           33 setfval(char *s, double f)        /* set variable s to f */
           34 {
           35         struct symtab *p;
           36 
           37         if ((p = lookup(s)) != NULL)
           38                 p->s_val.f = f;
           39 }
           40 
           41 struct symtab*
           42 makevar(char *s, int t, YYSTYPE v)        /* make variable named s in table */
           43                 /* assumes s is static or from tostring */
           44 {
           45         struct symtab *p;
           46 
           47         for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next)
           48                 if (strcmp(s, p->s_name) == 0)
           49                         break;
           50         if (p == NULL) {        /* it's a new one */
           51                 p = (struct symtab *) malloc(sizeof(struct symtab));
           52                 if (p == NULL)
           53                         ERROR "out of symtab space with %s", s FATAL;
           54                 p->s_next = stack[nstack].p_symtab;
           55                 stack[nstack].p_symtab = p;        /* stick it at front */
           56         }
           57         p->s_name = s;
           58         p->s_type = t;
           59         p->s_val = v;
           60         return(p);
           61 }
           62 
           63 struct symtab*
           64 lookup(char *s)        /* find s in symtab */
           65 {
           66         int i;
           67         struct symtab *p;
           68 
           69         for (i = nstack; i >= 0; i--)        /* look in each active symtab */
           70                 for (p = stack[i].p_symtab; p != NULL; p = p->s_next)
           71                         if (strcmp(s, p->s_name) == 0)
           72                                 return(p);
           73         return(NULL);
           74 }
           75 
           76 void
           77 freesymtab(struct symtab *p)        /* free space used by symtab at p */
           78 {
           79         struct symtab *q;
           80 
           81         for ( ; p != NULL; p = q) {
           82                 q = p->s_next;
           83                 free(p->s_name);        /* assumes done with tostring */
           84                 free((char *)p);
           85         }
           86 }
           87 
           88 void
           89 freedef(char *s)        /* free definition for string s */
           90 {
           91         struct symtab *p, *q, *op;
           92 
           93         for (p = op = q = stack[nstack].p_symtab; p != NULL; p = p->s_next) {
           94                 if (strcmp(s, p->s_name) == 0) {         /* got it */
           95                         if (p->s_type != DEFNAME)
           96                                 break;
           97                         if (p == op)        /* 1st elem */
           98                                 stack[nstack].p_symtab = p->s_next;
           99                         else
          100                                 q->s_next = p->s_next;
          101                         free(p->s_name);
          102                         free(p->s_val.p);
          103                         free((char *)p);
          104                         return;
          105                 }
          106                 q = p;
          107         }
          108         /* ERROR "%s is not defined at this point", s WARNING; */
          109 }