|
|
1.1 root 1: #include <stdio.h>
2: #include <ctype.h>
3: #include "pic.h"
4: #include "y.tab.h"
5: extern char *tostring();
6:
7: getvar(s) /* return value of variable s */
8: char *s;
9: {
10: struct symtab *p;
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(0);
19: }
20: return(p->s_val);
21: }
22:
23: struct symtab *makevar(s, t, v) /* make variable named s in table */
24: char *s; /* assumes s is static or from tostring */
25: int t;
26: int v;
27: {
28: int i;
29: struct symtab *p;
30:
31: for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next)
32: if (strcmp(s, p->s_name) == 0)
33: break;
34: if (p == NULL) { /* it's a new one */
35: p = (struct symtab *) malloc(sizeof(struct symtab));
36: if (p == NULL) {
37: yyerror("out of symtab space with %s", s);
38: exit(1);
39: }
40: p->s_next = stack[nstack].p_symtab;
41: stack[nstack].p_symtab = p; /* stick it at front */
42: }
43: p->s_name = s;
44: p->s_type = t;
45: p->s_val = v;
46: return(p);
47: }
48:
49: struct symtab *lookup(s) /* find s in symtab */
50: char *s;
51: {
52: int i;
53: struct symtab *p;
54:
55: for (i = nstack; i >= 0; i--) /* look in each active symtab */
56: for (p = stack[i].p_symtab; p != NULL; p = p->s_next)
57: if (strcmp(s, p->s_name) == 0)
58: return(p);
59: return(NULL);
60: }
61:
62: freesymtab(p) /* free space used by symtab at p */
63: struct symtab *p;
64: {
65: for ( ; p != NULL; p = p->s_next) {
66: free(p->s_name); /* assumes done with tostring */
67: free(p);
68: }
69: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.