1 #include <stdio.h>
2 #include <ctype.h>
3 #include "pic.h"
4 #include "picy.h"
5
getvar(s)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
getfval(s)23 float getfval(s) /* return float value of variable s */
24 char *s;
25 {
26 YYSTYPE *y;
27 y = getvar(s);
28 return y->f;
29 }
30
setfval(s,f)31 setfval(s, f) /* set variable s to f */
32 char *s;
33 float f;
34 {
35 struct symtab *p;
36
37 if ((p = lookup(s)) != NULL)
38 p->s_val.f = f;
39 }
40
makevar(s,t,v)41 struct symtab *makevar(s, t, v) /* make variable named s in table */
42 char *s; /* assumes s is static or from tostring */
43 int t;
44 YYSTYPE v;
45 {
46 struct symtab *p;
47
48 for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next)
49 if (strcmp(s, p->s_name) == 0)
50 break;
51 if (p == NULL) { /* it's a new one */
52 p = (struct symtab *) malloc(sizeof(struct symtab));
53 if (p == NULL) {
54 yyerror("out of symtab space with %s", s);
55 exit(1);
56 }
57 p->s_next = stack[nstack].p_symtab;
58 stack[nstack].p_symtab = p; /* stick it at front */
59 }
60 p->s_name = s;
61 p->s_type = t;
62 p->s_val = v;
63 return(p);
64 }
65
lookup(s)66 struct symtab *lookup(s) /* find s in symtab */
67 char *s;
68 {
69 int i;
70 struct symtab *p;
71
72 for (i = nstack; i >= 0; i--) /* look in each active symtab */
73 for (p = stack[i].p_symtab; p != NULL; p = p->s_next)
74 if (strcmp(s, p->s_name) == 0)
75 return(p);
76 return(NULL);
77 }
78
79 freesymtab(p) /* free space used by symtab at p */
80 struct symtab *p;
81 {
82 for ( ; p != NULL; p = p->s_next) {
83 free(p->s_name); /* assumes done with tostring */
84 free((char *)p);
85 }
86 }
87