| 20 |
|
* 5/31/90 Added conditional compile (REDEFW) for redefinition warning. |
| 21 |
|
* |
| 22 |
|
* 4/23/91 Added ':' assignment for constant expressions |
| 23 |
+ |
* |
| 24 |
+ |
* 8/7/91 Added optional context path to append to variable names |
| 25 |
|
*/ |
| 26 |
|
|
| 27 |
|
#include <stdio.h> |
| 36 |
|
|
| 37 |
|
#define newnode() (EPNODE *)ecalloc(1, sizeof(EPNODE)) |
| 38 |
|
|
| 39 |
< |
extern char *ecalloc(), *savestr(); |
| 39 |
> |
extern char *ecalloc(), *savestr(), *strcpy(); |
| 40 |
|
|
| 41 |
|
static double dvalue(); |
| 42 |
|
|
| 43 |
|
long eclock = -1; /* value storage timer */ |
| 44 |
|
|
| 45 |
+ |
static char context[MAXWORD]; /* current context path */ |
| 46 |
+ |
|
| 47 |
|
static VARDEF *hashtbl[NHASH]; /* definition list */ |
| 48 |
|
static int htndx; /* index for */ |
| 49 |
|
static VARDEF *htpos; /* ...dfirst() and */ |
| 76 |
|
} |
| 77 |
|
initfile(fp, fname, 0); |
| 78 |
|
while (nextc != EOF) |
| 79 |
< |
loaddefn(); |
| 79 |
> |
getstatement(); |
| 80 |
|
if (fname != NULL) |
| 81 |
|
fclose(fp); |
| 82 |
|
} |
| 89 |
|
{ |
| 90 |
|
initstr(str, fn, ln); |
| 91 |
|
while (nextc != EOF) |
| 92 |
< |
loaddefn(); |
| 92 |
> |
getstatement(); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
|
| 116 |
|
int assign; |
| 117 |
|
double val; |
| 118 |
|
{ |
| 119 |
+ |
char *qname; |
| 120 |
|
register EPNODE *ep1, *ep2; |
| 121 |
+ |
/* get qualified name */ |
| 122 |
+ |
qname = qualname(vname, 0); |
| 123 |
|
/* check for quick set */ |
| 124 |
< |
if ((ep1 = dlookup(vname)) != NULL && ep1->v.kid->type == SYM) { |
| 124 |
> |
if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) { |
| 125 |
|
ep2 = ep1->v.kid->sibling; |
| 126 |
|
if (ep2->type == NUM) { |
| 127 |
|
ep2->v.num = val; |
| 140 |
|
ep2->type = NUM; |
| 141 |
|
ep2->v.num = val; |
| 142 |
|
addekid(ep1, ep2); |
| 143 |
< |
dremove(vname); |
| 144 |
< |
dpush(ep1); |
| 143 |
> |
dremove(qname); |
| 144 |
> |
dpush(qname, ep1); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 152 |
|
|
| 153 |
|
while ((ep = dpop(name)) != NULL) { |
| 154 |
|
if (ep->type == ':') { |
| 155 |
< |
dpush(ep); /* don't clear constants */ |
| 155 |
> |
dpush(name, ep); /* don't clear constants */ |
| 156 |
|
return; |
| 157 |
|
} |
| 158 |
|
epfree(ep); |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
+ |
char * |
| 183 |
+ |
setcontext(ctx) /* set a new context path */ |
| 184 |
+ |
register char *ctx; |
| 185 |
+ |
{ |
| 186 |
+ |
register char *cpp; |
| 187 |
+ |
|
| 188 |
+ |
if (ctx == NULL) |
| 189 |
+ |
return(context); /* just asking */ |
| 190 |
+ |
if (!*ctx) { |
| 191 |
+ |
context[0] = '\0'; /* clear context */ |
| 192 |
+ |
return(context); |
| 193 |
+ |
} |
| 194 |
+ |
cpp = context; /* else copy it (carefully!) */ |
| 195 |
+ |
if (*ctx != CNTXMARK) |
| 196 |
+ |
*cpp++ = CNTXMARK; /* make sure there's a mark */ |
| 197 |
+ |
do { |
| 198 |
+ |
if (cpp >= context+MAXWORD-1) { |
| 199 |
+ |
*cpp = '\0'; |
| 200 |
+ |
wputs(context); |
| 201 |
+ |
wputs(": context path too long\n"); |
| 202 |
+ |
return(NULL); |
| 203 |
+ |
} |
| 204 |
+ |
if (isid(*ctx)) |
| 205 |
+ |
*cpp++ = *ctx++; |
| 206 |
+ |
else { |
| 207 |
+ |
*cpp++ = '_'; ctx++; |
| 208 |
+ |
} |
| 209 |
+ |
} while (*ctx); |
| 210 |
+ |
*cpp = '\0'; |
| 211 |
+ |
return(context); |
| 212 |
+ |
} |
| 213 |
+ |
|
| 214 |
+ |
|
| 215 |
+ |
char * |
| 216 |
+ |
qualname(nam, lvl) /* get qualified name */ |
| 217 |
+ |
register char *nam; |
| 218 |
+ |
int lvl; |
| 219 |
+ |
{ |
| 220 |
+ |
static char nambuf[MAXWORD]; |
| 221 |
+ |
register char *cp = nambuf, *cpp = context; |
| 222 |
+ |
/* check for repeat call */ |
| 223 |
+ |
if (nam == nambuf) |
| 224 |
+ |
return(lvl > 0 ? NULL : nambuf); |
| 225 |
+ |
/* copy name to static buffer */ |
| 226 |
+ |
while (*nam) { |
| 227 |
+ |
if (cp >= nambuf+MAXWORD-1) |
| 228 |
+ |
goto toolong; |
| 229 |
+ |
if ((*cp++ = *nam++) == CNTXMARK) |
| 230 |
+ |
cpp = NULL; /* flag a qualified name */ |
| 231 |
+ |
} |
| 232 |
+ |
if (cpp == NULL) { |
| 233 |
+ |
if (lvl > 0) |
| 234 |
+ |
return(NULL); /* no higher level */ |
| 235 |
+ |
if (cp[-1] == CNTXMARK) { |
| 236 |
+ |
cp--; cpp = context; /* current context explicitly */ |
| 237 |
+ |
} else |
| 238 |
+ |
cpp = ""; /* else fully qualified */ |
| 239 |
+ |
} else /* else skip the requested levels */ |
| 240 |
+ |
while (lvl-- > 0) { |
| 241 |
+ |
if (!*cpp) |
| 242 |
+ |
return(NULL); /* return NULL if past global level */ |
| 243 |
+ |
while (*++cpp && *cpp != CNTXMARK) |
| 244 |
+ |
; |
| 245 |
+ |
} |
| 246 |
+ |
while (*cpp) { /* copy context to static buffer */ |
| 247 |
+ |
if (cp >= nambuf+MAXWORD-1) |
| 248 |
+ |
goto toolong; |
| 249 |
+ |
*cp++ = *cpp++; |
| 250 |
+ |
} |
| 251 |
+ |
*cp = '\0'; |
| 252 |
+ |
return(nambuf); /* return qualified name */ |
| 253 |
+ |
toolong: |
| 254 |
+ |
*cp = '\0'; |
| 255 |
+ |
wputs(nambuf); |
| 256 |
+ |
wputs(": name too long\n"); |
| 257 |
+ |
return(NULL); |
| 258 |
+ |
} |
| 259 |
+ |
|
| 260 |
+ |
|
| 261 |
|
#ifdef OUTCHAN |
| 262 |
|
chanout(cs) /* set output channels */ |
| 263 |
|
int (*cs)(); |
| 310 |
|
varlookup(name) /* look up a variable */ |
| 311 |
|
char *name; |
| 312 |
|
{ |
| 313 |
+ |
int lvl = 0; |
| 314 |
+ |
register char *qname; |
| 315 |
|
register VARDEF *vp; |
| 316 |
< |
|
| 317 |
< |
for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next) |
| 318 |
< |
if (!strcmp(vp->name, name)) |
| 319 |
< |
return(vp); |
| 316 |
> |
/* find most qualified match */ |
| 317 |
> |
while ((qname = qualname(name, lvl++)) != NULL) |
| 318 |
> |
for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next) |
| 319 |
> |
if (!strcmp(vp->name, qname)) |
| 320 |
> |
return(vp); |
| 321 |
|
return(NULL); |
| 322 |
|
} |
| 323 |
|
|
| 329 |
|
register VARDEF *vp; |
| 330 |
|
int hv; |
| 331 |
|
|
| 332 |
+ |
if ((vp = varlookup(name)) != NULL) { |
| 333 |
+ |
vp->nlinks++; |
| 334 |
+ |
return(vp); |
| 335 |
+ |
} |
| 336 |
+ |
name = qualname(name, 0); /* use fully qualified name */ |
| 337 |
|
hv = hash(name); |
| 244 |
– |
for (vp = hashtbl[hv]; vp != NULL; vp = vp->next) |
| 245 |
– |
if (!strcmp(vp->name, name)) { |
| 246 |
– |
vp->nlinks++; |
| 247 |
– |
return(vp); |
| 248 |
– |
} |
| 338 |
|
vp = (VARDEF *)emalloc(sizeof(VARDEF)); |
| 339 |
|
vp->name = savestr(name); |
| 340 |
|
vp->nlinks = 1; |
| 422 |
|
} |
| 423 |
|
|
| 424 |
|
|
| 425 |
< |
dpush(ep) /* push on a definition */ |
| 425 |
> |
dpush(nm, ep) /* push on a definition */ |
| 426 |
> |
char *nm; |
| 427 |
|
register EPNODE *ep; |
| 428 |
|
{ |
| 429 |
|
register VARDEF *vp; |
| 430 |
|
|
| 431 |
< |
vp = varinsert(dname(ep)); |
| 431 |
> |
vp = varinsert(nm); |
| 432 |
|
ep->sibling = vp->def; |
| 433 |
|
vp->def = ep; |
| 434 |
|
} |
| 465 |
|
#endif |
| 466 |
|
|
| 467 |
|
|
| 468 |
< |
loaddefn() /* load next definition */ |
| 468 |
> |
getstatement() /* get next statement */ |
| 469 |
|
{ |
| 470 |
|
register EPNODE *ep; |
| 471 |
+ |
char *qname; |
| 472 |
|
EPNODE *lastdef; |
| 473 |
|
|
| 474 |
|
if (nextc == ';') { /* empty statement */ |
| 483 |
|
#endif |
| 484 |
|
{ /* ordinary definition */ |
| 485 |
|
ep = getdefn(); |
| 486 |
+ |
qname = qualname(dname(ep), 0); |
| 487 |
|
#ifdef REDEFW |
| 488 |
< |
if ((lastdef = dlookup(dname(ep))) != NULL) { |
| 489 |
< |
wputs(dname(ep)); |
| 488 |
> |
if ((lastdef = dlookup(qname)) != NULL) { |
| 489 |
> |
wputs(qname); |
| 490 |
|
if (lastdef->type == ':') |
| 491 |
|
wputs(": redefined constant expression\n"); |
| 492 |
|
else |
| 493 |
|
wputs(": redefined\n"); |
| 494 |
|
} |
| 495 |
|
#ifdef FUNCTION |
| 496 |
< |
else if (ep->v.kid->type == FUNC && |
| 497 |
< |
liblookup(ep->v.kid->v.kid->v.name) != NULL) { |
| 406 |
< |
wputs(ep->v.kid->v.kid->v.name); |
| 496 |
> |
else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) { |
| 497 |
> |
wputs(qname); |
| 498 |
|
wputs(": definition hides library function\n"); |
| 499 |
|
} |
| 500 |
|
#endif |
| 501 |
|
#endif |
| 502 |
|
if (ep->type == ':') |
| 503 |
< |
dremove(dname(ep)); |
| 503 |
> |
dremove(qname); |
| 504 |
|
else |
| 505 |
< |
dclear(dname(ep)); |
| 506 |
< |
dpush(ep); |
| 505 |
> |
dclear(qname); |
| 506 |
> |
dpush(qname, ep); |
| 507 |
|
} |
| 508 |
|
if (nextc != EOF) { |
| 509 |
|
if (nextc != ';') |