Annotation of researchv10no/cmd/pic/symtab.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.