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.9 by greg, Wed Apr 24 08:17:22 1991 UTC vs.
Revision 1.14 by greg, Thu Aug 8 12:12:16 1991 UTC

# Line 19 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19   *
20   *  5/31/90  Added conditional compile (REDEFW) for redefinition warning.
21   *
22 < *  4/23/91  Added ':' defines for constant expressions (RCONST)
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 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 +    eputs(nambuf);
256 +    eputs(": name too long\n");
257 +    quit(1);
258 + }
259 +
260 +
261 + incontext(qn)                   /* is qualified name in current context? */
262 + register char  *qn;
263 + {
264 +    while (*qn && *qn != CNTXMARK)      /* find context mark */
265 +        ;
266 +    return(!strcmp(qn, context));
267 + }
268 +
269 +
270   #ifdef  OUTCHAN
271   chanout(cs)                     /* set output channels */
272   int  (*cs)();
# Line 185 | Line 280 | int  (*cs)();
280   #endif
281  
282  
283 < dcleanup(cons, ochans)          /* clear definitions */
284 < int  cons, ochans;
283 > dcleanup(lvl)           /* clear definitions (0->vars,1->output,2->consts) */
284 > int  lvl;
285   {
286      register int  i;
287      register VARDEF  *vp;
288      register EPNODE  *ep;
289 <
289 >                                /* if context is global, clear all */
290      for (i = 0; i < NHASH; i++)
291          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
292 <            if (cons)
293 <                dremove(vp->name);
294 <            else
295 <                dclear(vp->name);
292 >            if (!context[0] || incontext(vp->name))
293 >                if (lvl >= 2)
294 >                    dremove(vp->name);
295 >                else
296 >                    dclear(vp->name);
297   #ifdef  OUTCHAN
298 <    if (ochans) {
298 >    if (lvl >= 1) {
299          for (ep = outchan; ep != NULL; ep = ep->sibling)
300              epfree(ep);
301          outchan = NULL;
# Line 224 | Line 320 | VARDEF *
320   varlookup(name)                 /* look up a variable */
321   char  *name;
322   {
323 +    int  lvl = 0;
324 +    register char  *qname;
325      register VARDEF  *vp;
326 <    
327 <    for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next)
328 <        if (!strcmp(vp->name, name))
329 <            return(vp);
326 >                                /* find most qualified match */
327 >    while ((qname = qualname(name, lvl++)) != NULL)
328 >        for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
329 >            if (!strcmp(vp->name, qname))
330 >                return(vp);
331      return(NULL);
332   }
333  
# Line 240 | Line 339 | char  *name;
339      register VARDEF  *vp;
340      int  hv;
341      
342 +    if ((vp = varlookup(name)) != NULL) {
343 +        vp->nlinks++;
344 +        return(vp);
345 +    }
346 +    name = qualname(name, 0);           /* use fully qualified name */
347      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        }
348      vp = (VARDEF *)emalloc(sizeof(VARDEF));
349      vp->name = savestr(name);
350      vp->nlinks = 1;
# Line 333 | Line 432 | char  *name;
432   }
433  
434  
435 < dpush(ep)                       /* push on a definition */
435 > dpush(nm, ep)                   /* push on a definition */
436 > char  *nm;
437   register EPNODE  *ep;
438   {
439      register VARDEF  *vp;
440  
441 <    vp = varinsert(dname(ep));
441 >    vp = varinsert(nm);
442      ep->sibling = vp->def;
443      vp->def = ep;
444   }
# Line 375 | Line 475 | EPNODE  *sp;
475   #endif
476  
477  
478 < loaddefn()                      /* load next definition */
478 > getstatement()                  /* get next statement */
479   {
480      register EPNODE  *ep;
481 +    char  *qname;
482 +    EPNODE  *lastdef;
483  
484      if (nextc == ';') {         /* empty statement */
485          scan();
# Line 391 | Line 493 | loaddefn()                     /* load next definition */
493   #endif
494      {                           /* ordinary definition */
495          ep = getdefn();
496 +        qname = qualname(dname(ep), 0);
497   #ifdef  REDEFW
498 <        if (dlookup(dname(ep)) != NULL) {
499 <            dclear(dname(ep));
500 <            wputs(dname(ep));
398 <            if (dlookup(dname(ep)) == NULL)
399 <                wputs(": redefined\n");
400 <            else
498 >        if ((lastdef = dlookup(qname)) != NULL) {
499 >            wputs(qname);
500 >            if (lastdef->type == ':')
501                  wputs(": redefined constant expression\n");
502 +            else
503 +                wputs(": redefined\n");
504          }
505   #ifdef  FUNCTION
506 <        else if (ep->v.kid->type == FUNC &&
507 <                        liblookup(ep->v.kid->v.kid->v.name) != NULL) {
508 <            wputs(ep->v.kid->v.kid->v.name);
407 <            wputs(": redefined library function\n");
506 >        else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) {
507 >            wputs(qname);
508 >            wputs(": definition hides library function\n");
509          }
510   #endif
410 #else
411        dclear(dname(ep));
511   #endif
512 <        dpush(ep);
512 >        if (ep->type == ':')
513 >            dremove(qname);
514 >        else
515 >            dclear(qname);
516 >        dpush(qname, ep);
517      }
518      if (nextc != EOF) {
519          if (nextc != ';')
# Line 465 | Line 568 | getdefn()                      /* A -> SYM = E1 */
568      ep2->type = nextc;
569      scan();
570      addekid(ep2, ep1);
468 #ifdef  RCONST
469    if (
470 #ifdef  FUNCTION
471            ep1->type == SYM &&
472 #endif
473            ep2->type == ':')
474        addekid(ep2, rconst(getE1()));
475    else
476 #endif
571      addekid(ep2, getE1());
572  
573      if (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines