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