ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/caldefn.c
(Generate patch)

Comparing ray/src/common/caldefn.c (file contents):
Revision 1.12 by greg, Wed Jul 17 11:12:02 1991 UTC vs.
Revision 1.16 by greg, Thu Aug 8 14:38:00 1991 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
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>
# Line 34 | Line 36 | static char SCCSid[] = "$SunId$ LBL";
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 */
# Line 72 | Line 76 | char  *fname;
76      }
77      initfile(fp, fname, 0);
78      while (nextc != EOF)
79 <        loaddefn();
79 >        getstatement();
80      if (fname != NULL)
81          fclose(fp);
82   }
# Line 85 | Line 89 | int  ln;
89   {
90      initstr(str, fn, ln);
91      while (nextc != EOF)
92 <        loaddefn();
92 >        getstatement();
93   }
94  
95  
# Line 112 | Line 116 | char  *vname;
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;
# Line 133 | Line 140 | double  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  
# Line 145 | Line 152 | char  *name;
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);
# Line 172 | Line 179 | char  *name;
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 explicit global */
223 +    if (*nam == CNTXMARK)
224 +        return(lvl > 0 ? NULL : nam+1);
225 +                                /* check for repeat call */
226 +    if (nam == nambuf)
227 +        return(lvl > 0 ? NULL : nambuf);
228 +                                /* copy name to static buffer */
229 +    while (*nam) {
230 +        if (cp >= nambuf+MAXWORD-1)
231 +                goto toolong;
232 +        if ((*cp++ = *nam++) == CNTXMARK)
233 +            cpp = NULL;         /* flag a qualified name */
234 +    }
235 +    if (cpp == NULL) {
236 +        if (lvl > 0)
237 +            return(NULL);               /* no higher level */
238 +        if (cp[-1] == CNTXMARK) {
239 +            cp--; cpp = context;        /* current context explicitly */
240 +        } else
241 +            cpp = "";                   /* else fully qualified */
242 +    } else                      /* else skip the requested levels */
243 +        while (lvl-- > 0) {
244 +            if (!*cpp)
245 +                return(NULL);   /* return NULL if past global level */
246 +            while (*++cpp && *cpp != CNTXMARK)
247 +                ;
248 +        }
249 +    while (*cpp) {              /* copy context to static buffer */
250 +        if (cp >= nambuf+MAXWORD-1)
251 +            goto toolong;
252 +        *cp++ = *cpp++;
253 +    }
254 +    *cp = '\0';
255 +    return(nambuf);             /* return qualified name */
256 + toolong:
257 +    *cp = '\0';
258 +    eputs(nambuf);
259 +    eputs(": name too long\n");
260 +    quit(1);
261 + }
262 +
263 +
264 + incontext(qn)                   /* is qualified name in current context? */
265 + register char  *qn;
266 + {
267 +    while (*qn && *qn != CNTXMARK)      /* find context mark */
268 +        ;
269 +    return(!strcmp(qn, context));
270 + }
271 +
272 +
273   #ifdef  OUTCHAN
274   chanout(cs)                     /* set output channels */
275   int  (*cs)();
# Line 191 | Line 289 | int  lvl;
289      register int  i;
290      register VARDEF  *vp;
291      register EPNODE  *ep;
292 <
292 >                                /* if context is global, clear all */
293      for (i = 0; i < NHASH; i++)
294          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
295 <            if (lvl >= 2)
296 <                dremove(vp->name);
297 <            else
298 <                dclear(vp->name);
295 >            if (!context[0] || incontext(vp->name))
296 >                if (lvl >= 2)
297 >                    dremove(vp->name);
298 >                else
299 >                    dclear(vp->name);
300   #ifdef  OUTCHAN
301      if (lvl >= 1) {
302          for (ep = outchan; ep != NULL; ep = ep->sibling)
# Line 224 | Line 323 | VARDEF *
323   varlookup(name)                 /* look up a variable */
324   char  *name;
325   {
326 +    int  lvl = 0;
327 +    register char  *qname;
328      register VARDEF  *vp;
329 <    
330 <    for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next)
331 <        if (!strcmp(vp->name, name))
332 <            return(vp);
329 >                                /* find most qualified match */
330 >    while ((qname = qualname(name, lvl++)) != NULL)
331 >        for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
332 >            if (!strcmp(vp->name, qname))
333 >                return(vp);
334      return(NULL);
335   }
336  
# Line 238 | Line 340 | varinsert(name)                        /* get a link to a variable */
340   char  *name;
341   {
342      register VARDEF  *vp;
343 +    LIBR  *libp;
344      int  hv;
345      
346 +    if ((vp = varlookup(name)) != NULL) {
347 +        vp->nlinks++;
348 +        return(vp);
349 +    }
350 + #ifdef  FUNCTION
351 +    libp = liblookup(name);
352 + #else
353 +    libp = NULL;
354 + #endif
355 +    if (libp == NULL)                   /* if name not in library */
356 +        name = qualname(name, 0);       /* use fully qualified version */
357      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        }
358      vp = (VARDEF *)emalloc(sizeof(VARDEF));
359      vp->name = savestr(name);
360      vp->nlinks = 1;
361      vp->def = NULL;
362 <    vp->lib = NULL;
362 >    vp->lib = libp;
363      vp->next = hashtbl[hv];
364      hashtbl[hv] = vp;
365      return(vp);
# Line 333 | Line 442 | char  *name;
442   }
443  
444  
445 < dpush(ep)                       /* push on a definition */
445 > dpush(nm, ep)                   /* push on a definition */
446 > char  *nm;
447   register EPNODE  *ep;
448   {
449      register VARDEF  *vp;
450  
451 <    vp = varinsert(dname(ep));
451 >    vp = varinsert(nm);
452      ep->sibling = vp->def;
453      vp->def = ep;
454   }
# Line 375 | Line 485 | EPNODE  *sp;
485   #endif
486  
487  
488 < loaddefn()                      /* load next definition */
488 > getstatement()                  /* get next statement */
489   {
490      register EPNODE  *ep;
491 <    EPNODE  *lastdef;
491 >    char  *qname;
492 >    register VARDEF  *vdef;
493  
494      if (nextc == ';') {         /* empty statement */
495          scan();
# Line 392 | Line 503 | loaddefn()                     /* load next definition */
503   #endif
504      {                           /* ordinary definition */
505          ep = getdefn();
506 +        qname = qualname(dname(ep), 0);
507   #ifdef  REDEFW
508 <        if ((lastdef = dlookup(dname(ep))) != NULL) {
509 <            wputs(dname(ep));
510 <            if (lastdef->type == ':')
511 <                wputs(": redefined constant expression\n");
512 <            else
513 <                wputs(": redefined\n");
514 <        }
508 >        if ((vdef = varlookup(qname)) != NULL)
509 >            if (vdef->def != NULL) {
510 >                wputs(qname);
511 >                if (vdef->def->type == ':')
512 >                    wputs(": redefined constant expression\n");
513 >                else
514 >                    wputs(": redefined\n");
515 >            }
516   #ifdef  FUNCTION
517 <        else if (ep->v.kid->type == FUNC &&
518 <                        liblookup(ep->v.kid->v.kid->v.name) != NULL) {
519 <            wputs(ep->v.kid->v.kid->v.name);
520 <            wputs(": definition hides library function\n");
408 <        }
517 >            else if (ep->v.kid->type == FUNC && vdef->lib != NULL) {
518 >                wputs(qname);
519 >                wputs(": definition hides library function\n");
520 >            }
521   #endif
522   #endif
523          if (ep->type == ':')
524 <            dremove(dname(ep));
524 >            dremove(qname);
525          else
526 <            dclear(dname(ep));
527 <        dpush(ep);
526 >            dclear(qname);
527 >        dpush(qname, ep);
528      }
529      if (nextc != EOF) {
530          if (nextc != ';')

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines