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.4 by greg, Thu Jul 19 11:06:37 1990 UTC vs.
Revision 1.14 by greg, Thu Aug 8 12:12:16 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18   *  11/16/88  Added VARDEF structure for hard linking.
19   *
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 32 | 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 70 | 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 83 | Line 89 | int  ln;
89   {
90      initstr(str, fn, ln);
91      while (nextc != EOF)
92 <        loaddefn();
92 >        getstatement();
93   }
94  
95  
# Line 105 | Line 111 | EPNODE  *ep;
111   }
112  
113  
114 < varset(vname, val)              /* set a variable's value */
114 > varset(vname, assign, val)      /* set a variable's value */
115   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;
128 <            ep2->sibling->v.tick = -1;
128 >            ep1->type = assign;
129              return;
130          }
131      }
132                                          /* hand build definition */
133      ep1 = newnode();
134 <    ep1->type = '=';
134 >    ep1->type = assign;
135      ep2 = newnode();
136      ep2->type = SYM;
137      ep2->v.name = savestr(vname);
# Line 130 | Line 140 | double  val;
140      ep2->type = NUM;
141      ep2->v.num = val;
142      addekid(ep1, ep2);
143 <    ep2 = newnode();
144 <    ep2->type = TICK;
135 <    ep2->v.tick = -1;
136 <    addekid(ep1, ep2);
137 <    ep2 = newnode();
138 <    ep2->type = NUM;
139 <    addekid(ep1, ep2);
140 <    dclear(vname);
141 <    dpush(ep1);
143 >    dremove(qname);
144 >    dpush(qname, ep1);
145   }
146  
147  
148 < dclear(name)                    /* delete all definitions of name */
148 > dclear(name)                    /* delete variable definitions of name */
149   char  *name;
150   {
151      register EPNODE  *ep;
152  
153 +    while ((ep = dpop(name)) != NULL) {
154 +        if (ep->type == ':') {
155 +            dpush(name, ep);            /* don't clear constants */
156 +            return;
157 +        }
158 +        epfree(ep);
159 +    }
160 + }
161 +
162 +
163 + dremove(name)                   /* delete all definitions of name */
164 + char  *name;
165 + {
166 +    register EPNODE  *ep;
167 +
168      while ((ep = dpop(name)) != NULL)
169          epfree(ep);
170   }
# Line 161 | 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()                       /* set output channels */
271 > chanout(cs)                     /* set output channels */
272 > int  (*cs)();
273   {
274      register EPNODE  *ep;
275  
276      for (ep = outchan; ep != NULL; ep = ep->sibling)
277 <        chanset(ep->v.kid->v.chan, evalue(ep->v.kid->sibling));
277 >        (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling));
278  
279   }
280   #endif
281  
282  
283 < dclearall()                     /* clear all definitions */
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 <            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 <    for (ep = outchan; ep != NULL; ep = ep->sibling)
299 <        epfree(ep);
300 <    outchan = NULL;
298 >    if (lvl >= 1) {
299 >        for (ep = outchan; ep != NULL; ep = ep->sibling)
300 >            epfree(ep);
301 >        outchan = NULL;
302 >    }
303   #endif
304   }
305  
# Line 206 | 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 222 | 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);
226    for (vp = hashtbl[hv]; vp != NULL; vp = vp->next)
227        if (!strcmp(vp->name, name)) {
228            vp->nlinks++;
229            return(vp);
230        }
348      vp = (VARDEF *)emalloc(sizeof(VARDEF));
349      vp->name = savestr(name);
350      vp->nlinks = 1;
# Line 315 | 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 357 | 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 373 | 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));
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 < #else
506 <        dclear(dname(ep));
505 > #ifdef  FUNCTION
506 >        else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) {
507 >            wputs(qname);
508 >            wputs(": definition hides library function\n");
509 >        }
510   #endif
511 <        dpush(ep);
511 > #endif
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 394 | Line 525 | loaddefn()                     /* load next definition */
525  
526   EPNODE *
527   getdefn()                       /* A -> SYM = E1 */
528 +                                /*      SYM : E1 */
529                                  /*      FUNC(SYM,..) = E1 */
530 +                                /*      FUNC(SYM,..) : E1 */
531   {
532      register EPNODE  *ep1, *ep2;
533  
# Line 428 | Line 561 | getdefn()                      /* A -> SYM = E1 */
561          curfunc = NULL;
562   #endif
563  
564 <    if (nextc != '=')
565 <        syntax("'=' expected");
433 <    scan();
564 >    if (nextc != '=' && nextc != ':')
565 >        syntax("'=' or ':' expected");
566  
567      ep2 = newnode();
568 <    ep2->type = '=';
568 >    ep2->type = nextc;
569 >    scan();
570      addekid(ep2, ep1);
571      addekid(ep2, getE1());
572  
573 +    if (
574   #ifdef  FUNCTION
575 <    if (ep1->type == SYM)
575 >            ep1->type == SYM &&
576   #endif
577 <    {
577 >            ep1->sibling->type != NUM) {
578          ep1 = newnode();
579          ep1->type = TICK;
580          ep1->v.tick = -1;
# Line 501 | Line 635 | EPNODE  *d;
635          quit(1);
636      }
637      ep1 = d->v.kid->sibling;                    /* get expression */
638 +    if (ep1->type == NUM)
639 +        return(ep1->v.num);                     /* return if number */
640      ep2 = ep1->sibling;                         /* check time */
641      if (ep2->v.tick < 0 || ep2->v.tick < eclock) {
642 <        ep2->v.tick = eclock;
642 >        ep2->v.tick = d->type == ':' ? 1L<<30 : eclock;
643          ep2 = ep2->sibling;
644 <        ep2->v.num = evalue(ep1);               /* compute new value */
644 >        ep2->v.num = evalue(ep1);               /* needs new value */
645      } else
646 <        ep2 = ep2->sibling;                     /* reuse old value */
646 >        ep2 = ep2->sibling;                     /* else reuse old value */
647  
648      return(ep2->v.num);
649   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines