|
|
1.1 root 1: /* Copyright (c) 1988 AT&T */
2: /* All Rights Reserved */
3:
4: /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
5: /* The copyright notice above does not evidence any */
6: /* actual or intended publication of such source code. */
7:
8: /* @(#)picasso:symtab.c 1.0 */
9: #include <ctype.h>
10: #include "picasso.h"
11: #include "y.tab.h"
12:
13: YYSTYPE getvar(s) /* return value of variable s (usually pointer) */
14: char *s;
15: {
16: struct symtab *p;
17: static YYSTYPE bug;
18:
19: p = lookup(s);
20: if (p == NULL) {
21: if (islower(s[0]))
22: yyerror("no such variable as %s", s);
23: else
24: yyerror("no such place as %s", s);
25: return(bug);
26: }
27: if (s != p->s_name) free(s);
28: return(p->s_val);
29: }
30:
31: double getsub(s, n)
32: char *s;
33: int n;
34: {
35: struct symtab *p;
36:
37: p = lookup(s);
38: if (p == NULL)
39: fatal("array %s not declared", s);
40: else if (n > p->s_dim)
41: fatal("variable %s has dimension %d < %d", s, p->s_dim, n);
42: else if (p->s_dim == 0)
43: return p->s_val.f;
44: else
45: return p->s_val.a[n-1];
46: }
47:
48: double setvar(s, n, v)
49: char *s;
50: int n;
51: double v;
52: {
53: struct symtab *p;
54:
55: p = lookup(s);
56: if (p == NULL)
57: fatal("array %s not declared", s);
58: else if (n > p->s_dim)
59: fatal("variable %s has dimension %d < %d", s, p->s_dim, n);
60: else if (p->s_dim == 0)
61: p->s_val.f = v;
62: else
63: p->s_val.a[n-1] = v;
64: if (s != p->s_name) free(s);
65: return v;
66: }
67:
68: double setsize(s, v) /* kludge: textsize = v implies textspace = v * r */
69: char *s;
70: double v;
71: {
72: double r;
73: struct symtab *p, *q;
74:
75: p = lookup(s);
76: q = lookup("textspace");
77: r = q->s_val.f/p->s_val.f;
78: q->s_val.f = v * r;
79: p->s_val.f = v;
80: return v;
81: }
82:
83: struct symtab *newvar(s, t) /* allocate variable with name s, type t */
84: char *s; /* assumes s derives from a tostring */
85: int t;
86: {
87: struct symtab *p;
88:
89: p = (struct symtab *) malloc(sizeof(struct symtab));
90: if (p == NULL) {
91: yyerror("out of symtab space with %s", s);
92: exit(1);
93: }
94: p->s_name = s;
95: p->s_type = t;
96: p->s_dim = 0;
97: p->s_next = stack[nstack].p_symtab;
98: stack[nstack].p_symtab = p; /* stick it at front */
99: return(p);
100: }
101:
102: struct symtab *findvar(s, t) /* make variable named s in table */
103: char *s; /* will reuse local or global symbol */
104: {
105: struct symtab *p;
106:
107: if ((p = lookup(s)) == NULL)
108: p = newvar(s, t);
109: if (s != p->s_name) free(s);
110: return(p);
111: }
112:
113: double setarray(s)
114: char *s;
115: {
116: struct symtab *p;
117:
118: p = findvar (s, VARNAME);
119: if (p->s_dim < nexpr-1) { /* existing array is too short */
120: if (p->s_dim > 0)
121: free (p->s_val.a);
122: }
123: if ((p->s_dim = nexpr-1) > 0) {
124: p->s_val.a = (float *)malloc((unsigned)nexpr * sizeof(float));
125: if (p->s_val.a == NULL)
126: fatal("out of array space with %s", p->s_name);
127: }
128: if (p->s_dim == 0)
129: p->s_val.f = exprlist[0];
130: else
131: memcpy ((char *)p->s_val.a, (char *)exprlist,
132: nexpr * sizeof(float));
133: nexpr = 0;
134: return exprlist[0];
135: }
136:
137: double getfval(s) /* return float value of variable s */
138: char *s;
139: {
140: struct symtab *p;
141:
142: if ((p = lookup(s)) == NULL) {
143: yyerror("%s undefined", s);
144: return 0.;
145: }
146: else if (p->s_dim == 0)
147: return p->s_val.f;
148: else /* should probably place all */
149: return (double)*p->s_val.a; /* array values in exprlist. */
150: }
151:
152: setfval(s, f) /* set variable s to f */
153: char *s;
154: double f;
155: {
156: struct symtab *p;
157:
158: if ((p = lookup(s)) == NULL)
159: p = newvar(s, VARNAME);
160: if (p->s_dim == 0)
161: p->s_val.f = f;
162: else
163: *p->s_val.a = f;
164: }
165:
166: struct symtab *makevar(s, t, v) /* make variable named s in table */
167: char *s; /* assumes s is static or from tostring */
168: int t; /* if s is already defined as an array, */
169: valtype v; /* only its first element is set. */
170: {
171: struct symtab *p;
172: obj *o;
173:
174: for (p = stack[nstack].p_symtab; p != NULL; p = p->s_next)
175: if (strcmp(s, p->s_name) == 0)
176: break;
177: if (p == NULL)
178: p = newvar(s, t);
179: #if 1
180: /* seems like a PLACENAME should always point to an object */
181: if (t == PLACENAME) {
182: for (o = objhead; o->o_next != objtail; o = o->o_next)
183: if (o == v.o)
184: break;
185: v.o = o;
186: }
187: #endif
188: if (p->s_dim == 0)
189: p->s_val = v;
190: else
191: *p->s_val.a = v.f;
192: if (s != p->s_name) free(s);
193: return(p);
194: }
195:
196: struct symtab *lookup(s) /* find s in symtab */
197: char *s;
198: {
199: int i;
200: struct symtab *p;
201:
202: for (i = nstack; i >= 0; i--) /* look in each active symtab */
203: for (p = stack[i].p_symtab; p != NULL; p = p->s_next)
204: if (strcmp(s, p->s_name) == 0)
205: return(p);
206: return(NULL);
207: }
208:
209: freesymtab(p) /* free space used by symtab at p */
210: struct symtab *p;
211: {
212: struct symtab *q;
213:
214: for ( ; p != NULL; p = q) {
215: q = p->s_next;
216: free(p->s_name); /* assumes done with tostring */
217: if (p->s_dim)
218: free(p->s_val.a);
219: free(p);
220: }
221: }
222:
223: freedef(s) /* free definition for string s */
224: char *s;
225: {
226: struct symtab *p, *q, *op;
227:
228: for (p = op = q = stack[nstack].p_symtab; p != NULL; p = p->s_next) {
229: if (strcmp(s, p->s_name) == 0) { /* got it */
230: if (p->s_type != DEFNAME)
231: break;
232: if (p == op) /* 1st elem */
233: stack[nstack].p_symtab = p->s_next;
234: else
235: q->s_next = p->s_next;
236: free(p->s_name);
237: free(p->s_val.p);
238: free(p);
239: return;
240: }
241: q = p;
242: }
243: yyerror("%s is not defined at this point", s);
244: }
245:
246: static int i, M;
247:
248: first_xy (xsym, ysym, xp, yp)
249: struct symtab *xsym, *ysym;
250: double *xp, *yp;
251: {
252: if ((M = xsym->s_dim) == 0)
253: *xp = xsym->s_val.f;
254: else
255: *xp = xsym->s_val.a[0];
256: if (ysym->s_dim == 0)
257: *yp = ysym->s_val.f;
258: else
259: *yp = ysym->s_val.a[0];
260: if (ysym->s_dim > M)
261: M = ysym->s_dim;
262: i = 0;
263: }
264:
265: next_xy (xsym, ysym, xp, yp)
266: struct symtab *xsym, *ysym;
267: double *xp, *yp;
268: {
269: if (++i > M)
270: return 0;
271: if (xsym->s_dim == 0)
272: *xp = xsym->s_val.f;
273: else
274: *xp = xsym->s_val.a[i % (xsym->s_dim+1)];
275: if (ysym->s_dim == 0)
276: *yp = ysym->s_val.f;
277: else
278: *yp = ysym->s_val.a[i % (ysym->s_dim+1)];
279: return 1;
280: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.