1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Store variable definitions. |
6 |
|
* |
15 |
|
* 11/16/88 Added VARDEF structure for hard linking. |
16 |
|
* |
17 |
|
* 5/31/90 Added conditional compile (REDEFW) for redefinition warning. |
18 |
+ |
* |
19 |
+ |
* 4/23/91 Added ':' assignment for constant expressions |
20 |
+ |
* |
21 |
+ |
* 8/7/91 Added optional context path to append to variable names |
22 |
+ |
* |
23 |
+ |
* 5/17/2001 Fixed clock counter wrapping behavior |
24 |
+ |
* |
25 |
+ |
* 2/19/03 Eliminated conditional compiles in favor of esupport extern. |
26 |
|
*/ |
27 |
|
|
28 |
< |
#include <stdio.h> |
28 |
> |
#include "copyright.h" |
29 |
|
|
30 |
|
#include <ctype.h> |
31 |
|
|
32 |
+ |
#include "rterror.h" |
33 |
+ |
#include "rtio.h" |
34 |
+ |
#include "rtmisc.h" |
35 |
|
#include "calcomp.h" |
36 |
|
|
37 |
< |
#ifndef NHASH |
38 |
< |
#define NHASH 521 /* hash size (a prime!) */ |
37 |
> |
#ifndef NHASH |
38 |
> |
#define NHASH 521 /* hash size (a prime!) */ |
39 |
|
#endif |
40 |
|
|
41 |
< |
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
41 |
> |
#define hash(s) (shash(s)%NHASH) |
42 |
|
|
43 |
< |
extern char *ecalloc(), *savestr(); |
43 |
> |
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
44 |
|
|
45 |
< |
static double dvalue(); |
45 |
> |
static double dvalue(char *name, EPNODE *d); |
46 |
|
|
47 |
< |
long eclock = -1; /* value storage timer */ |
47 |
> |
#define MAXCLOCK (1L<<31) /* clock wrap value */ |
48 |
|
|
49 |
+ |
unsigned long eclock = 0; /* value storage timer */ |
50 |
+ |
|
51 |
+ |
#define MAXCNTX 1023 /* maximum context length */ |
52 |
+ |
|
53 |
+ |
static char context[MAXCNTX+1]; /* current context path */ |
54 |
+ |
|
55 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
56 |
|
static int htndx; /* index for */ |
57 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
44 |
– |
#ifdef OUTCHAN |
58 |
|
static EPNODE *ochpos; /* ...dnext */ |
59 |
|
static EPNODE *outchan; |
47 |
– |
#endif |
60 |
|
|
61 |
< |
#ifdef FUNCTION |
62 |
< |
EPNODE *curfunc; |
51 |
< |
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
61 |
> |
EPNODE *curfunc = NULL; |
62 |
> |
#define dname(ep) ((ep)->v.kid->type == SYM ? \ |
63 |
|
(ep)->v.kid->v.name : \ |
64 |
|
(ep)->v.kid->v.kid->v.name) |
54 |
– |
#else |
55 |
– |
#define dname(ep) ((ep)->v.kid->v.name) |
56 |
– |
#endif |
65 |
|
|
66 |
|
|
67 |
< |
fcompile(fname) /* get definitions from a file */ |
68 |
< |
char *fname; |
67 |
> |
void |
68 |
> |
fcompile( /* get definitions from a file */ |
69 |
> |
char *fname |
70 |
> |
) |
71 |
|
{ |
72 |
|
FILE *fp; |
73 |
|
|
80 |
|
} |
81 |
|
initfile(fp, fname, 0); |
82 |
|
while (nextc != EOF) |
83 |
< |
loaddefn(); |
83 |
> |
getstatement(); |
84 |
|
if (fname != NULL) |
85 |
|
fclose(fp); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
< |
scompile(str, fn, ln) /* get definitions from a string */ |
90 |
< |
char *str; |
91 |
< |
char *fn; |
92 |
< |
int ln; |
89 |
> |
void |
90 |
> |
scompile( /* get definitions from a string */ |
91 |
> |
char *str, |
92 |
> |
char *fn, |
93 |
> |
int ln |
94 |
> |
) |
95 |
|
{ |
96 |
|
initstr(str, fn, ln); |
97 |
|
while (nextc != EOF) |
98 |
< |
loaddefn(); |
98 |
> |
getstatement(); |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
double |
103 |
< |
varvalue(vname) /* return a variable's value */ |
104 |
< |
char *vname; |
103 |
> |
varvalue( /* return a variable's value */ |
104 |
> |
char *vname |
105 |
> |
) |
106 |
|
{ |
107 |
|
return(dvalue(vname, dlookup(vname))); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
double |
112 |
< |
evariable(ep) /* evaluate a variable */ |
113 |
< |
EPNODE *ep; |
112 |
> |
evariable( /* evaluate a variable */ |
113 |
> |
EPNODE *ep |
114 |
> |
) |
115 |
|
{ |
116 |
< |
register VARDEF *dp = ep->v.ln; |
116 |
> |
VARDEF *dp = ep->v.ln; |
117 |
|
|
118 |
|
return(dvalue(dp->name, dp->def)); |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
< |
varset(vname, val) /* set a variable's value */ |
123 |
< |
char *vname; |
124 |
< |
double val; |
122 |
> |
void |
123 |
> |
varset( /* set a variable's value */ |
124 |
> |
char *vname, |
125 |
> |
int assign, |
126 |
> |
double val |
127 |
> |
) |
128 |
|
{ |
129 |
< |
register EPNODE *ep1, *ep2; |
129 |
> |
char *qname; |
130 |
> |
EPNODE *ep1, *ep2; |
131 |
> |
/* get qualified name */ |
132 |
> |
qname = qualname(vname, 0); |
133 |
|
/* check for quick set */ |
134 |
< |
if ((ep1 = dlookup(vname)) != NULL && ep1->v.kid->type == SYM) { |
134 |
> |
if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM && |
135 |
> |
(ep1->type == ':') <= (assign == ':')) { |
136 |
|
ep2 = ep1->v.kid->sibling; |
137 |
|
if (ep2->type == NUM) { |
138 |
|
ep2->v.num = val; |
139 |
< |
ep2->sibling->v.tick = -1; |
139 |
> |
ep1->type = assign; |
140 |
|
return; |
141 |
|
} |
142 |
|
} |
143 |
+ |
if (ep1 != NULL && esupport&E_REDEFW) { |
144 |
+ |
wputs(qname); |
145 |
+ |
if (ep1->type == ':') |
146 |
+ |
wputs(": reset constant expression\n"); |
147 |
+ |
else |
148 |
+ |
wputs(": reset expression\n"); |
149 |
+ |
} |
150 |
|
/* hand build definition */ |
151 |
|
ep1 = newnode(); |
152 |
< |
ep1->type = '='; |
152 |
> |
ep1->type = assign; |
153 |
|
ep2 = newnode(); |
154 |
|
ep2->type = SYM; |
155 |
|
ep2->v.name = savestr(vname); |
158 |
|
ep2->type = NUM; |
159 |
|
ep2->v.num = val; |
160 |
|
addekid(ep1, ep2); |
161 |
< |
ep2 = newnode(); |
162 |
< |
ep2->type = TICK; |
163 |
< |
ep2->v.tick = -1; |
164 |
< |
addekid(ep1, ep2); |
165 |
< |
ep2 = newnode(); |
138 |
< |
ep2->type = NUM; |
139 |
< |
addekid(ep1, ep2); |
140 |
< |
dclear(vname); |
141 |
< |
dpush(ep1); |
161 |
> |
if (assign == ':') |
162 |
> |
dremove(qname); |
163 |
> |
else |
164 |
> |
dclear(qname); |
165 |
> |
dpush(qname, ep1); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
< |
dclear(name) /* delete all definitions of name */ |
170 |
< |
char *name; |
169 |
> |
void |
170 |
> |
dclear( /* delete variable definitions of name */ |
171 |
> |
char *name |
172 |
> |
) |
173 |
|
{ |
174 |
< |
register EPNODE *ep; |
174 |
> |
EPNODE *ep; |
175 |
|
|
176 |
+ |
while ((ep = dpop(name)) != NULL) { |
177 |
+ |
if (ep->type == ':') { |
178 |
+ |
dpush(name, ep); /* don't clear constants */ |
179 |
+ |
return; |
180 |
+ |
} |
181 |
+ |
epfree(ep); |
182 |
+ |
} |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
|
186 |
+ |
void |
187 |
+ |
dremove( /* delete all definitions of name */ |
188 |
+ |
char *name |
189 |
+ |
) |
190 |
+ |
{ |
191 |
+ |
EPNODE *ep; |
192 |
+ |
|
193 |
|
while ((ep = dpop(name)) != NULL) |
194 |
|
epfree(ep); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
< |
vardefined(name) /* return non-zero if variable defined */ |
199 |
< |
char *name; |
198 |
> |
int |
199 |
> |
vardefined( /* return '=' or ':' if variable/constant defined */ |
200 |
> |
char *name |
201 |
> |
) |
202 |
|
{ |
203 |
< |
register EPNODE *dp; |
203 |
> |
EPNODE *dp; |
204 |
|
|
205 |
< |
return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM); |
205 |
> |
if ((dp = dlookup(name)) == NULL || dp->v.kid->type != SYM) |
206 |
> |
return(0); |
207 |
> |
return(dp->type); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
< |
#ifdef OUTCHAN |
212 |
< |
chanout() /* set output channels */ |
211 |
> |
char * |
212 |
> |
setcontext( /* set a new context path */ |
213 |
> |
char *ctx |
214 |
> |
) |
215 |
|
{ |
216 |
< |
register EPNODE *ep; |
216 |
> |
char *cpp; |
217 |
|
|
218 |
< |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
219 |
< |
chanset(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
218 |
> |
if (ctx == NULL) |
219 |
> |
return(context); /* just asking */ |
220 |
> |
while (*ctx == CNTXMARK) |
221 |
> |
ctx++; /* skip past marks */ |
222 |
> |
if (!*ctx) { |
223 |
> |
context[0] = '\0'; /* empty means clear context */ |
224 |
> |
return(context); |
225 |
> |
} |
226 |
> |
cpp = context; /* start context with mark */ |
227 |
> |
*cpp++ = CNTXMARK; |
228 |
> |
do { /* carefully copy new context */ |
229 |
> |
if (cpp >= context+MAXCNTX) |
230 |
> |
break; /* just copy what we can */ |
231 |
> |
if (isid(*ctx)) |
232 |
> |
*cpp++ = *ctx++; |
233 |
> |
else { |
234 |
> |
*cpp++ = '_'; ctx++; |
235 |
> |
} |
236 |
> |
} while (*ctx); |
237 |
> |
while (cpp[-1] == CNTXMARK) /* cannot end in context mark */ |
238 |
> |
cpp--; |
239 |
> |
*cpp = '\0'; |
240 |
> |
return(context); |
241 |
> |
} |
242 |
|
|
243 |
+ |
|
244 |
+ |
char * |
245 |
+ |
pushcontext( /* push on another context */ |
246 |
+ |
char *ctx |
247 |
+ |
) |
248 |
+ |
{ |
249 |
+ |
char oldcontext[MAXCNTX+1]; |
250 |
+ |
int n; |
251 |
+ |
|
252 |
+ |
strcpy(oldcontext, context); /* save old context */ |
253 |
+ |
setcontext(ctx); /* set new context */ |
254 |
+ |
n = strlen(context); /* tack on old */ |
255 |
+ |
if (n+strlen(oldcontext) > MAXCNTX) { |
256 |
+ |
strncpy(context+n, oldcontext, MAXCNTX-n); |
257 |
+ |
context[MAXCNTX] = '\0'; |
258 |
+ |
} else |
259 |
+ |
strcpy(context+n, oldcontext); |
260 |
+ |
return(context); |
261 |
|
} |
173 |
– |
#endif |
262 |
|
|
263 |
|
|
264 |
< |
dclearall() /* clear all definitions */ |
264 |
> |
char * |
265 |
> |
popcontext(void) /* pop off top context */ |
266 |
|
{ |
267 |
< |
register int i; |
179 |
< |
register VARDEF *vp; |
180 |
< |
register EPNODE *ep; |
267 |
> |
char *cp1, *cp2; |
268 |
|
|
269 |
+ |
if (!context[0]) /* nothing left to pop */ |
270 |
+ |
return(context); |
271 |
+ |
cp2 = context; /* find mark */ |
272 |
+ |
while (*++cp2 && *cp2 != CNTXMARK) |
273 |
+ |
; |
274 |
+ |
cp1 = context; /* copy tail to front */ |
275 |
+ |
while ( (*cp1++ = *cp2++) ) |
276 |
+ |
; |
277 |
+ |
return(context); |
278 |
+ |
} |
279 |
+ |
|
280 |
+ |
|
281 |
+ |
char * |
282 |
+ |
qualname( /* get qualified name */ |
283 |
+ |
char *nam, |
284 |
+ |
int lvl |
285 |
+ |
) |
286 |
+ |
{ |
287 |
+ |
static char nambuf[RMAXWORD+1]; |
288 |
+ |
char *cp = nambuf, *cpp; |
289 |
+ |
/* check for explicit local */ |
290 |
+ |
if (*nam == CNTXMARK) |
291 |
+ |
if (lvl > 0) /* only action is to refuse search */ |
292 |
+ |
return(NULL); |
293 |
+ |
else |
294 |
+ |
nam++; |
295 |
+ |
else if (nam == nambuf) /* check for repeat call */ |
296 |
+ |
return(lvl > 0 ? NULL : nam); |
297 |
+ |
/* copy name to static buffer */ |
298 |
+ |
while (*nam) { |
299 |
+ |
if (cp >= nambuf+RMAXWORD) |
300 |
+ |
goto toolong; |
301 |
+ |
*cp++ = *nam++; |
302 |
+ |
} |
303 |
+ |
/* check for explicit global */ |
304 |
+ |
if (cp > nambuf && cp[-1] == CNTXMARK) { |
305 |
+ |
if (lvl > 0) |
306 |
+ |
return(NULL); |
307 |
+ |
*--cp = '\0'; |
308 |
+ |
return(nambuf); /* already qualified */ |
309 |
+ |
} |
310 |
+ |
cpp = context; /* else skip the requested levels */ |
311 |
+ |
while (lvl-- > 0) { |
312 |
+ |
if (!*cpp) |
313 |
+ |
return(NULL); /* return NULL if past global level */ |
314 |
+ |
while (*++cpp && *cpp != CNTXMARK) |
315 |
+ |
; |
316 |
+ |
} |
317 |
+ |
while (*cpp) { /* copy context to static buffer */ |
318 |
+ |
if (cp >= nambuf+RMAXWORD) |
319 |
+ |
goto toolong; |
320 |
+ |
*cp++ = *cpp++; |
321 |
+ |
} |
322 |
+ |
toolong: |
323 |
+ |
*cp = '\0'; |
324 |
+ |
return(nambuf); /* return qualified name */ |
325 |
+ |
} |
326 |
+ |
|
327 |
+ |
|
328 |
+ |
int |
329 |
+ |
incontext( /* is qualified name in current context? */ |
330 |
+ |
char *qn |
331 |
+ |
) |
332 |
+ |
{ |
333 |
+ |
if (!context[0]) /* global context accepts all */ |
334 |
+ |
return(1); |
335 |
+ |
while (*qn && *qn != CNTXMARK) /* find context mark */ |
336 |
+ |
qn++; |
337 |
+ |
return(!strcmp(qn, context)); |
338 |
+ |
} |
339 |
+ |
|
340 |
+ |
|
341 |
+ |
void |
342 |
+ |
chanout( /* set output channels */ |
343 |
+ |
void (*cs)(int n, double v) |
344 |
+ |
) |
345 |
+ |
{ |
346 |
+ |
EPNODE *ep; |
347 |
+ |
|
348 |
+ |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
349 |
+ |
(*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling)); |
350 |
+ |
|
351 |
+ |
} |
352 |
+ |
|
353 |
+ |
|
354 |
+ |
void |
355 |
+ |
dcleanup( /* clear definitions (0->vars,1->output,2->consts) */ |
356 |
+ |
int lvl |
357 |
+ |
) |
358 |
+ |
{ |
359 |
+ |
int i; |
360 |
+ |
VARDEF *vp; |
361 |
+ |
EPNODE *ep; |
362 |
+ |
/* if context is global, clear all */ |
363 |
|
for (i = 0; i < NHASH; i++) |
364 |
|
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
365 |
< |
dclear(vp->name); |
366 |
< |
#ifdef OUTCHAN |
367 |
< |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
368 |
< |
epfree(ep); |
369 |
< |
outchan = NULL; |
370 |
< |
#endif |
365 |
> |
if (incontext(vp->name)) { |
366 |
> |
if (lvl >= 2) |
367 |
> |
dremove(vp->name); |
368 |
> |
else |
369 |
> |
dclear(vp->name); |
370 |
> |
} |
371 |
> |
if (lvl >= 1) { |
372 |
> |
for (ep = outchan; ep != NULL; ep = ep->sibling) |
373 |
> |
epfree(ep); |
374 |
> |
outchan = NULL; |
375 |
> |
} |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
EPNODE * |
380 |
< |
dlookup(name) /* look up a definition */ |
381 |
< |
char *name; |
380 |
> |
dlookup( /* look up a definition */ |
381 |
> |
char *name |
382 |
> |
) |
383 |
|
{ |
384 |
< |
register VARDEF *vp; |
384 |
> |
VARDEF *vp; |
385 |
|
|
386 |
|
if ((vp = varlookup(name)) == NULL) |
387 |
< |
return(NULL); |
387 |
> |
return(NULL); |
388 |
|
return(vp->def); |
389 |
|
} |
390 |
|
|
391 |
|
|
392 |
|
VARDEF * |
393 |
< |
varlookup(name) /* look up a variable */ |
394 |
< |
char *name; |
393 |
> |
varlookup( /* look up a variable */ |
394 |
> |
char *name |
395 |
> |
) |
396 |
|
{ |
397 |
< |
register VARDEF *vp; |
398 |
< |
|
399 |
< |
for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next) |
400 |
< |
if (!strcmp(vp->name, name)) |
401 |
< |
return(vp); |
397 |
> |
int lvl = 0; |
398 |
> |
char *qname; |
399 |
> |
VARDEF *vp; |
400 |
> |
/* find most qualified match */ |
401 |
> |
while ((qname = qualname(name, lvl++)) != NULL) |
402 |
> |
for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) |
403 |
> |
if (!strcmp(vp->name, qname)) |
404 |
> |
return(vp); |
405 |
|
return(NULL); |
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
|
VARDEF * |
410 |
< |
varinsert(name) /* get a link to a variable */ |
411 |
< |
char *name; |
410 |
> |
varinsert( /* get a link to a variable */ |
411 |
> |
char *name |
412 |
> |
) |
413 |
|
{ |
414 |
< |
register VARDEF *vp; |
415 |
< |
int hv; |
414 |
> |
VARDEF *vp; |
415 |
> |
int hv; |
416 |
|
|
417 |
< |
hv = hash(name); |
418 |
< |
for (vp = hashtbl[hv]; vp != NULL; vp = vp->next) |
419 |
< |
if (!strcmp(vp->name, name)) { |
420 |
< |
vp->nlinks++; |
229 |
< |
return(vp); |
230 |
< |
} |
417 |
> |
if ((vp = varlookup(name)) != NULL) { |
418 |
> |
vp->nlinks++; |
419 |
> |
return(vp); |
420 |
> |
} |
421 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
422 |
+ |
vp->lib = liblookup(name); |
423 |
+ |
if (vp->lib == NULL) /* if name not in library */ |
424 |
+ |
name = qualname(name, 0); /* use fully qualified version */ |
425 |
+ |
hv = hash(name); |
426 |
|
vp->name = savestr(name); |
427 |
|
vp->nlinks = 1; |
428 |
|
vp->def = NULL; |
235 |
– |
vp->lib = NULL; |
429 |
|
vp->next = hashtbl[hv]; |
430 |
|
hashtbl[hv] = vp; |
431 |
|
return(vp); |
432 |
|
} |
433 |
|
|
434 |
|
|
435 |
< |
varfree(ln) /* release link to variable */ |
436 |
< |
register VARDEF *ln; |
435 |
> |
void |
436 |
> |
libupdate( /* update library links */ |
437 |
> |
char *fn |
438 |
> |
) |
439 |
|
{ |
440 |
< |
register VARDEF *vp; |
441 |
< |
int hv; |
440 |
> |
int i; |
441 |
> |
VARDEF *vp; |
442 |
> |
/* if fn is NULL then relink all */ |
443 |
> |
for (i = 0; i < NHASH; i++) |
444 |
> |
for (vp = hashtbl[i]; vp != NULL; vp = vp->next) |
445 |
> |
if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name)) |
446 |
> |
vp->lib = liblookup(vp->name); |
447 |
> |
} |
448 |
|
|
449 |
+ |
|
450 |
+ |
void |
451 |
+ |
varfree( /* release link to variable */ |
452 |
+ |
VARDEF *ln |
453 |
+ |
) |
454 |
+ |
{ |
455 |
+ |
VARDEF *vp; |
456 |
+ |
int hv; |
457 |
+ |
|
458 |
|
if (--ln->nlinks > 0) |
459 |
< |
return; /* still active */ |
459 |
> |
return; /* still active */ |
460 |
|
|
461 |
|
hv = hash(ln->name); |
462 |
|
vp = hashtbl[hv]; |
463 |
|
if (vp == ln) |
464 |
< |
hashtbl[hv] = vp->next; |
464 |
> |
hashtbl[hv] = vp->next; |
465 |
|
else { |
466 |
< |
while (vp->next != ln) /* must be in list */ |
467 |
< |
vp = vp->next; |
468 |
< |
vp->next = ln->next; |
466 |
> |
while (vp->next != ln) /* must be in list */ |
467 |
> |
vp = vp->next; |
468 |
> |
vp->next = ln->next; |
469 |
|
} |
470 |
|
freestr(ln->name); |
471 |
|
efree((char *)ln); |
473 |
|
|
474 |
|
|
475 |
|
EPNODE * |
476 |
< |
dfirst() /* return pointer to first definition */ |
476 |
> |
dfirst(void) /* return pointer to first definition */ |
477 |
|
{ |
478 |
|
htndx = 0; |
479 |
|
htpos = NULL; |
270 |
– |
#ifdef OUTCHAN |
480 |
|
ochpos = outchan; |
272 |
– |
#endif |
481 |
|
return(dnext()); |
482 |
|
} |
483 |
|
|
484 |
|
|
485 |
|
EPNODE * |
486 |
< |
dnext() /* return pointer to next definition */ |
486 |
> |
dnext(void) /* return pointer to next definition */ |
487 |
|
{ |
488 |
< |
register EPNODE *ep; |
488 |
> |
EPNODE *ep; |
489 |
> |
char *nm; |
490 |
|
|
491 |
|
while (htndx < NHASH) { |
492 |
< |
if (htpos == NULL) |
493 |
< |
htpos = hashtbl[htndx++]; |
494 |
< |
while (htpos != NULL) { |
495 |
< |
ep = htpos->def; |
496 |
< |
htpos = htpos->next; |
497 |
< |
if (ep != NULL) |
498 |
< |
return(ep); |
499 |
< |
} |
492 |
> |
if (htpos == NULL) |
493 |
> |
htpos = hashtbl[htndx++]; |
494 |
> |
while (htpos != NULL) { |
495 |
> |
ep = htpos->def; |
496 |
> |
nm = htpos->name; |
497 |
> |
htpos = htpos->next; |
498 |
> |
if (ep != NULL && incontext(nm)) |
499 |
> |
return(ep); |
500 |
> |
} |
501 |
|
} |
292 |
– |
#ifdef OUTCHAN |
502 |
|
if ((ep = ochpos) != NULL) |
503 |
< |
ochpos = ep->sibling; |
503 |
> |
ochpos = ep->sibling; |
504 |
|
return(ep); |
296 |
– |
#else |
297 |
– |
return(NULL); |
298 |
– |
#endif |
505 |
|
} |
506 |
|
|
507 |
|
|
508 |
|
EPNODE * |
509 |
< |
dpop(name) /* pop a definition */ |
510 |
< |
char *name; |
509 |
> |
dpop( /* pop a definition */ |
510 |
> |
char *name |
511 |
> |
) |
512 |
|
{ |
513 |
< |
register VARDEF *vp; |
514 |
< |
register EPNODE *dp; |
513 |
> |
VARDEF *vp; |
514 |
> |
EPNODE *dp; |
515 |
|
|
516 |
|
if ((vp = varlookup(name)) == NULL || vp->def == NULL) |
517 |
< |
return(NULL); |
517 |
> |
return(NULL); |
518 |
|
dp = vp->def; |
519 |
|
vp->def = dp->sibling; |
520 |
|
varfree(vp); |
522 |
|
} |
523 |
|
|
524 |
|
|
525 |
< |
dpush(ep) /* push on a definition */ |
526 |
< |
register EPNODE *ep; |
525 |
> |
void |
526 |
> |
dpush( /* push on a definition */ |
527 |
> |
char *nm, |
528 |
> |
EPNODE *ep |
529 |
> |
) |
530 |
|
{ |
531 |
< |
register VARDEF *vp; |
531 |
> |
VARDEF *vp; |
532 |
|
|
533 |
< |
vp = varinsert(dname(ep)); |
533 |
> |
vp = varinsert(nm); |
534 |
|
ep->sibling = vp->def; |
535 |
|
vp->def = ep; |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
< |
#ifdef OUTCHAN |
540 |
< |
addchan(sp) /* add an output channel assignment */ |
541 |
< |
EPNODE *sp; |
539 |
> |
void |
540 |
> |
addchan( /* add an output channel assignment */ |
541 |
> |
EPNODE *sp |
542 |
> |
) |
543 |
|
{ |
544 |
< |
int ch = sp->v.kid->v.chan; |
545 |
< |
register EPNODE *ep, *epl; |
544 |
> |
int ch = sp->v.kid->v.chan; |
545 |
> |
EPNODE *ep, *epl; |
546 |
|
|
547 |
|
for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling) |
548 |
|
if (ep->v.kid->v.chan >= ch) { |
565 |
|
sp->sibling = NULL; |
566 |
|
|
567 |
|
} |
357 |
– |
#endif |
568 |
|
|
569 |
|
|
570 |
< |
loaddefn() /* load next definition */ |
570 |
> |
void |
571 |
> |
getstatement(void) /* get next statement */ |
572 |
|
{ |
573 |
< |
register EPNODE *ep; |
573 |
> |
EPNODE *ep; |
574 |
> |
char *qname; |
575 |
> |
VARDEF *vdef; |
576 |
|
|
577 |
|
if (nextc == ';') { /* empty statement */ |
578 |
|
scan(); |
579 |
|
return; |
580 |
|
} |
581 |
< |
#ifdef OUTCHAN |
582 |
< |
if (nextc == '$') { /* channel assignment */ |
581 |
> |
if (esupport&E_OUTCHAN && |
582 |
> |
nextc == '$') { /* channel assignment */ |
583 |
|
ep = getchan(); |
584 |
|
addchan(ep); |
585 |
< |
} else |
373 |
< |
#endif |
374 |
< |
{ /* ordinary definition */ |
585 |
> |
} else { /* ordinary definition */ |
586 |
|
ep = getdefn(); |
587 |
< |
#ifdef REDEFW |
588 |
< |
if (dlookup(dname(ep)) != NULL) { |
589 |
< |
dclear(dname(ep)); |
590 |
< |
wputs(dname(ep)); |
591 |
< |
wputs(": redefined\n"); |
587 |
> |
qname = qualname(dname(ep), 0); |
588 |
> |
if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) { |
589 |
> |
if (vdef->def != NULL && epcmp(ep, vdef->def)) { |
590 |
> |
wputs(qname); |
591 |
> |
if (vdef->def->type == ':') |
592 |
> |
wputs(": redefined constant expression\n"); |
593 |
> |
else |
594 |
> |
wputs(": redefined\n"); |
595 |
> |
} else if (ep->v.kid->type == FUNC && vdef->lib != NULL) { |
596 |
> |
wputs(qname); |
597 |
> |
wputs(": definition hides library function\n"); |
598 |
> |
} |
599 |
|
} |
600 |
< |
#else |
601 |
< |
dclear(dname(ep)); |
602 |
< |
#endif |
603 |
< |
dpush(ep); |
600 |
> |
if (ep->type == ':') |
601 |
> |
dremove(qname); |
602 |
> |
else |
603 |
> |
dclear(qname); |
604 |
> |
dpush(qname, ep); |
605 |
|
} |
606 |
|
if (nextc != EOF) { |
607 |
|
if (nextc != ';') |
612 |
|
|
613 |
|
|
614 |
|
EPNODE * |
615 |
< |
getdefn() /* A -> SYM = E1 */ |
616 |
< |
/* FUNC(SYM,..) = E1 */ |
615 |
> |
getdefn(void) |
616 |
> |
/* A -> SYM = E1 */ |
617 |
> |
/* SYM : E1 */ |
618 |
> |
/* FUNC(SYM,..) = E1 */ |
619 |
> |
/* FUNC(SYM,..) : E1 */ |
620 |
|
{ |
621 |
< |
register EPNODE *ep1, *ep2; |
621 |
> |
EPNODE *ep1, *ep2; |
622 |
|
|
623 |
< |
if (!isalpha(nextc)) |
623 |
> |
if (!isalpha(nextc) && nextc != CNTXMARK) |
624 |
|
syntax("illegal variable name"); |
625 |
|
|
626 |
|
ep1 = newnode(); |
627 |
|
ep1->type = SYM; |
628 |
|
ep1->v.name = savestr(getname()); |
629 |
|
|
630 |
< |
#ifdef FUNCTION |
409 |
< |
if (nextc == '(') { |
630 |
> |
if (esupport&E_FUNCTION && nextc == '(') { |
631 |
|
ep2 = newnode(); |
632 |
|
ep2->type = FUNC; |
633 |
|
addekid(ep2, ep1); |
635 |
|
do { |
636 |
|
scan(); |
637 |
|
if (!isalpha(nextc)) |
638 |
< |
syntax("illegal variable name"); |
638 |
> |
syntax("illegal parameter name"); |
639 |
|
ep2 = newnode(); |
640 |
|
ep2->type = SYM; |
641 |
|
ep2->v.name = savestr(getname()); |
642 |
+ |
if (strchr(ep2->v.name, CNTXMARK) != NULL) |
643 |
+ |
syntax("illegal parameter name"); |
644 |
|
addekid(ep1, ep2); |
645 |
|
} while (nextc == ','); |
646 |
|
if (nextc != ')') |
647 |
|
syntax("')' expected"); |
648 |
|
scan(); |
649 |
|
curfunc = ep1; |
650 |
< |
} else |
428 |
< |
curfunc = NULL; |
429 |
< |
#endif |
650 |
> |
} |
651 |
|
|
652 |
< |
if (nextc != '=') |
653 |
< |
syntax("'=' expected"); |
433 |
< |
scan(); |
652 |
> |
if (nextc != '=' && nextc != ':') |
653 |
> |
syntax("'=' or ':' expected"); |
654 |
|
|
655 |
|
ep2 = newnode(); |
656 |
< |
ep2->type = '='; |
656 |
> |
ep2->type = nextc; |
657 |
> |
scan(); |
658 |
|
addekid(ep2, ep1); |
659 |
|
addekid(ep2, getE1()); |
660 |
|
|
661 |
< |
#ifdef FUNCTION |
441 |
< |
if (ep1->type == SYM) |
442 |
< |
#endif |
443 |
< |
{ |
661 |
> |
if (ep1->type == SYM && ep1->sibling->type != NUM) { |
662 |
|
ep1 = newnode(); |
663 |
< |
ep1->type = TICK; |
664 |
< |
ep1->v.tick = -1; |
663 |
> |
ep1->type = CLKT; |
664 |
> |
ep1->v.tick = 0; |
665 |
|
addekid(ep2, ep1); |
666 |
|
ep1 = newnode(); |
667 |
|
ep1->type = NUM; |
668 |
|
addekid(ep2, ep1); |
669 |
|
} |
670 |
+ |
curfunc = NULL; |
671 |
|
|
672 |
|
return(ep2); |
673 |
|
} |
674 |
|
|
675 |
|
|
457 |
– |
#ifdef OUTCHAN |
676 |
|
EPNODE * |
677 |
< |
getchan() /* A -> $N = E1 */ |
677 |
> |
getchan(void) /* A -> $N = E1 */ |
678 |
|
{ |
679 |
< |
register EPNODE *ep1, *ep2; |
679 |
> |
EPNODE *ep1, *ep2; |
680 |
|
|
681 |
|
if (nextc != '$') |
682 |
|
syntax("missing '$'"); |
697 |
|
|
698 |
|
return(ep2); |
699 |
|
} |
482 |
– |
#endif |
700 |
|
|
701 |
|
|
702 |
|
|
705 |
|
*/ |
706 |
|
|
707 |
|
|
708 |
< |
static double |
709 |
< |
dvalue(name, d) /* evaluate a variable */ |
493 |
< |
char *name; |
494 |
< |
EPNODE *d; |
708 |
> |
static double /* evaluate a variable */ |
709 |
> |
dvalue(char *name, EPNODE *d) |
710 |
|
{ |
711 |
< |
register EPNODE *ep1, *ep2; |
711 |
> |
EPNODE *ep1, *ep2; |
712 |
|
|
713 |
|
if (d == NULL || d->v.kid->type != SYM) { |
714 |
|
eputs(name); |
716 |
|
quit(1); |
717 |
|
} |
718 |
|
ep1 = d->v.kid->sibling; /* get expression */ |
719 |
+ |
if (ep1->type == NUM) |
720 |
+ |
return(ep1->v.num); /* return if number */ |
721 |
+ |
if (esupport&E_RCONST && d->type == ':') { |
722 |
+ |
wputs(name); |
723 |
+ |
wputs(": assigned non-constant value\n"); |
724 |
+ |
} |
725 |
|
ep2 = ep1->sibling; /* check time */ |
726 |
< |
if (ep2->v.tick < 0 || ep2->v.tick < eclock) { |
727 |
< |
ep2->v.tick = eclock; |
726 |
> |
if (eclock >= MAXCLOCK) |
727 |
> |
eclock = 1; /* wrap clock counter */ |
728 |
> |
if (ep2->v.tick < MAXCLOCK && |
729 |
> |
(ep2->v.tick == 0) | (ep2->v.tick != eclock)) { |
730 |
> |
ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock; |
731 |
|
ep2 = ep2->sibling; |
732 |
< |
ep2->v.num = evalue(ep1); /* compute new value */ |
732 |
> |
ep2->v.num = evalue(ep1); /* needs new value */ |
733 |
|
} else |
734 |
< |
ep2 = ep2->sibling; /* reuse old value */ |
734 |
> |
ep2 = ep2->sibling; /* else reuse old value */ |
735 |
|
|
736 |
|
return(ep2->v.num); |
513 |
– |
} |
514 |
– |
|
515 |
– |
|
516 |
– |
static int |
517 |
– |
hash(s) /* hash a string */ |
518 |
– |
register char *s; |
519 |
– |
{ |
520 |
– |
register int rval = 0; |
521 |
– |
|
522 |
– |
while (*s) |
523 |
– |
rval += *s++; |
524 |
– |
|
525 |
– |
return(rval % NHASH); |
737 |
|
} |