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

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

unix.superglobalmegacorp.com

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