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 2.2 by greg, Fri May 15 16:38:49 1992 UTC vs.
Revision 2.18 by schorsch, Thu Jul 17 09:21:29 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *
# Line 22 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
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  <stdio.h>
31 + #include  <string.h>
32   #include  <ctype.h>
33  
34 + #include  "rterror.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(), *strcpy();
43 > #define  newnode()      (EPNODE *)ecalloc(1, sizeof(EPNODE))
44  
45 < static int  hash();
45 > static double  dvalue(char  *name, EPNODE *d);
46  
47 < static double  dvalue();
47 > #define  MAXCLOCK       (1L<<31)        /* clock wrap value */
48  
49 < long  eclock = -1;                      /* value storage timer */
49 > unsigned long  eclock = 0;              /* value storage timer */
50  
51 < static char  context[MAXWORD+1];        /* current context path */
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 */
52 #ifdef  OUTCHAN
58   static EPNODE  *ochpos;                 /* ...dnext */
59   static EPNODE  *outchan;
55 #endif
60  
61 < #ifdef  FUNCTION
62 < EPNODE  *curfunc;
59 < #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)
62 #else
63 #define  dname(ep)      ((ep)->v.kid->v.name)
64 #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  
# Line 84 | Line 86 | char  *fname;
86   }
87  
88  
89 < scompile(str, fn, ln)           /* get definitions from a string */
90 < char  *str;
91 < char  *fn;
92 < int  ln;
89 > void
90 > scompile(               /* get definitions from a string */
91 >        char  *str,
92 >        char  *fn,
93 >        int  ln
94 > )
95   {
96      initstr(str, fn, ln);
97      while (nextc != EOF)
# Line 96 | Line 100 | int  ln;
100  
101  
102   double
103 < varvalue(vname)                 /* return a variable's value */
104 < char  *vname;
103 > varvalue(                       /* return a variable's value */
104 >        char  *vname
105 > )
106   {
107      return(dvalue(vname, dlookup(vname)));
108   }
109  
110  
111   double
112 < evariable(ep)                   /* evaluate a variable */
113 < EPNODE  *ep;
112 > evariable(                      /* evaluate a variable */
113 >        EPNODE  *ep
114 > )
115   {
116      register VARDEF  *dp = ep->v.ln;
117  
# Line 113 | Line 119 | EPNODE  *ep;
119   }
120  
121  
122 < varset(vname, assign, val)      /* set a variable's value */
123 < char  *vname;
124 < int  assign;
125 < double  val;
122 > void
123 > varset(         /* set a variable's value */
124 >        char  *vname,
125 >        int  assign,
126 >        double  val
127 > )
128   {
129      char  *qname;
130      register EPNODE  *ep1, *ep2;
# Line 147 | Line 155 | double  val;
155   }
156  
157  
158 < dclear(name)                    /* delete variable definitions of name */
159 < char  *name;
158 > void
159 > dclear(                 /* delete variable definitions of name */
160 >        char  *name
161 > )
162   {
163      register EPNODE  *ep;
164  
# Line 162 | Line 172 | char  *name;
172   }
173  
174  
175 < dremove(name)                   /* delete all definitions of name */
176 < char  *name;
175 > void
176 > dremove(                        /* delete all definitions of name */
177 >        char  *name
178 > )
179   {
180      register EPNODE  *ep;
181  
# Line 172 | Line 184 | char  *name;
184   }
185  
186  
187 < vardefined(name)        /* return non-zero if variable defined */
188 < char  *name;
187 > int
188 > vardefined(     /* return non-zero if variable defined */
189 >        char  *name
190 > )
191   {
192      register EPNODE  *dp;
193  
# Line 182 | Line 196 | char  *name;
196  
197  
198   char *
199 < setcontext(ctx)                 /* set a new context path */
200 < register char  *ctx;
199 > setcontext(                     /* set a new context path */
200 >        register char  *ctx
201 > )
202   {
203      register char  *cpp;
204  
205      if (ctx == NULL)
206          return(context);                /* just asking */
207 +    while (*ctx == CNTXMARK)
208 +        ctx++;                          /* skip past marks */
209      if (!*ctx) {
210 <        context[0] = '\0';              /* clear context */
210 >        context[0] = '\0';              /* empty means clear context */
211          return(context);
212      }
213 <    cpp = context;                      /* else copy it (carefully!) */
214 <    if (*ctx != CNTXMARK)
215 <        *cpp++ = CNTXMARK;              /* make sure there's a mark */
216 <    do {
200 <        if (cpp >= context+MAXWORD)
213 >    cpp = context;                      /* start context with mark */
214 >    *cpp++ = CNTXMARK;
215 >    do {                                /* carefully copy new context */
216 >        if (cpp >= context+MAXCNTX)
217              break;                      /* just copy what we can */
218          if (isid(*ctx))
219              *cpp++ = *ctx++;
# Line 205 | Line 221 | register char  *ctx;
221              *cpp++ = '_'; ctx++;
222          }
223      } while (*ctx);
224 +    while (cpp[-1] == CNTXMARK)         /* cannot end in context mark */
225 +        cpp--;
226      *cpp = '\0';
227      return(context);
228   }
229  
230  
231   char *
232 < qualname(nam, lvl)              /* get qualified name */
233 < register char  *nam;
234 < int  lvl;
232 > pushcontext(            /* push on another context */
233 >        char  *ctx
234 > )
235   {
236 <    static char  nambuf[MAXWORD+1];
236 >    char  oldcontext[MAXCNTX+1];
237 >    register int  n;
238 >
239 >    strcpy(oldcontext, context);        /* save old context */
240 >    setcontext(ctx);                    /* set new context */
241 >    n = strlen(context);                /* tack on old */
242 >    if (n+strlen(oldcontext) > MAXCNTX) {
243 >        strncpy(context+n, oldcontext, MAXCNTX-n);
244 >        context[MAXCNTX] = '\0';
245 >    } else
246 >        strcpy(context+n, oldcontext);
247 >    return(context);
248 > }
249 >
250 >
251 > char *
252 > popcontext(void)                        /* pop off top context */
253 > {
254 >    register char  *cp1, *cp2;
255 >
256 >    if (!context[0])                    /* nothing left to pop */
257 >        return(context);
258 >    cp2 = context;                      /* find mark */
259 >    while (*++cp2 && *cp2 != CNTXMARK)
260 >        ;
261 >    cp1 = context;                      /* copy tail to front */
262 >    while (*cp1++ = *cp2++)
263 >        ;
264 >    return(context);
265 > }
266 >
267 >
268 > char *
269 > qualname(               /* get qualified name */
270 >        register char  *nam,
271 >        int  lvl
272 > )
273 > {
274 >    static char  nambuf[RMAXWORD+1];
275      register char  *cp = nambuf, *cpp;
276                                  /* check for explicit local */
277      if (*nam == CNTXMARK)
# Line 227 | Line 283 | int  lvl;
283          return(lvl > 0 ? NULL : nam);
284                                  /* copy name to static buffer */
285      while (*nam) {
286 <        if (cp >= nambuf+MAXWORD)
286 >        if (cp >= nambuf+RMAXWORD)
287                  goto toolong;
288          *cp++ = *nam++;
289      }
# Line 246 | Line 302 | int  lvl;
302              ;
303      }
304      while (*cpp) {              /* copy context to static buffer */
305 <        if (cp >= nambuf+MAXWORD)
305 >        if (cp >= nambuf+RMAXWORD)
306              goto toolong;
307          *cp++ = *cpp++;
308      }
# Line 256 | Line 312 | toolong:
312   }
313  
314  
315 < incontext(qn)                   /* is qualified name in current context? */
316 < register char  *qn;
315 > int
316 > incontext(                      /* is qualified name in current context? */
317 >        register char  *qn
318 > )
319   {
320 +    if (!context[0])                    /* global context accepts all */
321 +        return(1);
322      while (*qn && *qn != CNTXMARK)      /* find context mark */
323          qn++;
324      return(!strcmp(qn, context));
325   }
326  
327  
328 < #ifdef  OUTCHAN
329 < chanout(cs)                     /* set output channels */
330 < int  (*cs)();
328 > void
329 > chanout(                        /* set output channels */
330 >        void  (*cs)(int n, double v)
331 > )
332   {
333      register EPNODE  *ep;
334  
# Line 275 | Line 336 | int  (*cs)();
336          (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling));
337  
338   }
278 #endif
339  
340  
341 < dcleanup(lvl)           /* clear definitions (0->vars,1->output,2->consts) */
342 < int  lvl;
341 > void
342 > dcleanup(               /* clear definitions (0->vars,1->output,2->consts) */
343 >        int  lvl
344 > )
345   {
346      register int  i;
347      register VARDEF  *vp;
# Line 287 | Line 349 | int  lvl;
349                                  /* if context is global, clear all */
350      for (i = 0; i < NHASH; i++)
351          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
352 <            if (!context[0] || incontext(vp->name))
352 >            if (incontext(vp->name))
353                  if (lvl >= 2)
354                      dremove(vp->name);
355                  else
356                      dclear(vp->name);
295 #ifdef  OUTCHAN
357      if (lvl >= 1) {
358          for (ep = outchan; ep != NULL; ep = ep->sibling)
359              epfree(ep);
360          outchan = NULL;
361      }
301 #endif
362   }
363  
364  
365   EPNODE *
366 < dlookup(name)                   /* look up a definition */
367 < char  *name;
366 > dlookup(                        /* look up a definition */
367 >        char  *name
368 > )
369   {
370      register VARDEF  *vp;
371      
372      if ((vp = varlookup(name)) == NULL)
373 <        return(NULL);
373 >        return(NULL);
374      return(vp->def);
375   }
376  
377  
378   VARDEF *
379 < varlookup(name)                 /* look up a variable */
380 < char  *name;
379 > varlookup(                      /* look up a variable */
380 >        char  *name
381 > )
382   {
383 <    int  lvl = 0;
383 >    int  lvl = 0;
384      register char  *qname;
385      register VARDEF  *vp;
386 <                                /* find most qualified match */
386 >                                /* find most qualified match */
387      while ((qname = qualname(name, lvl++)) != NULL)
388          for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
389              if (!strcmp(vp->name, qname))
# Line 331 | Line 393 | char  *name;
393  
394  
395   VARDEF *
396 < varinsert(name)                 /* get a link to a variable */
397 < char  *name;
396 > varinsert(                      /* get a link to a variable */
397 >        char  *name
398 > )
399   {
400      register VARDEF  *vp;
401 <    register LIBR  *libp;
339 <    int  hv;
401 >    int  hv;
402      
403      if ((vp = varlookup(name)) != NULL) {
404          vp->nlinks++;
405          return(vp);
406      }
407 < #ifdef  FUNCTION
408 <    libp = liblookup(name);
409 < #else
348 <    libp = NULL;
349 < #endif
350 <    if (libp == NULL)                   /* if name not in library */
407 >    vp = (VARDEF *)emalloc(sizeof(VARDEF));
408 >    vp->lib = liblookup(name);
409 >    if (vp->lib == NULL)                /* if name not in library */
410          name = qualname(name, 0);       /* use fully qualified version */
411      hv = hash(name);
353    vp = (VARDEF *)emalloc(sizeof(VARDEF));
412      vp->name = savestr(name);
413      vp->nlinks = 1;
414      vp->def = NULL;
357    vp->lib = libp;
415      vp->next = hashtbl[hv];
416      hashtbl[hv] = vp;
417      return(vp);
418   }
419  
420  
421 < #ifdef  FUNCTION
422 < libupdate(fn)                   /* update library links */
423 < char  *fn;
421 > void
422 > libupdate(                      /* update library links */
423 >        char  *fn
424 > )
425   {
426      register int  i;
427      register VARDEF  *vp;
# Line 373 | Line 431 | char  *fn;
431              if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name))
432                  vp->lib = liblookup(vp->name);
433   }
376 #endif
434  
435  
436 < varfree(ln)                             /* release link to variable */
437 < register VARDEF  *ln;
436 > void
437 > varfree(                                /* release link to variable */
438 >        register VARDEF  *ln
439 > )
440   {
441      register VARDEF  *vp;
442 <    int  hv;
442 >    int  hv;
443  
444      if (--ln->nlinks > 0)
445 <        return;                         /* still active */
445 >        return;                         /* still active */
446  
447      hv = hash(ln->name);
448      vp = hashtbl[hv];
449      if (vp == ln)
450 <        hashtbl[hv] = vp->next;
450 >        hashtbl[hv] = vp->next;
451      else {
452 <        while (vp->next != ln)          /* must be in list */
453 <                vp = vp->next;
454 <        vp->next = ln->next;
452 >        while (vp->next != ln)          /* must be in list */
453 >                vp = vp->next;
454 >        vp->next = ln->next;
455      }
456      freestr(ln->name);
457      efree((char *)ln);
# Line 400 | Line 459 | register VARDEF  *ln;
459  
460  
461   EPNODE *
462 < dfirst()                        /* return pointer to first definition */
462 > dfirst(void)                    /* return pointer to first definition */
463   {
464      htndx = 0;
465      htpos = NULL;
407 #ifdef  OUTCHAN
466      ochpos = outchan;
409 #endif
467      return(dnext());
468   }
469  
470  
471   EPNODE *
472 < dnext()                         /* return pointer to next definition */
472 > dnext(void)                             /* return pointer to next definition */
473   {
474      register EPNODE  *ep;
475      register char  *nm;
476  
477      while (htndx < NHASH) {
478 <        if (htpos == NULL)
479 <                htpos = hashtbl[htndx++];
480 <        while (htpos != NULL) {
481 <            ep = htpos->def;
478 >        if (htpos == NULL)
479 >                htpos = hashtbl[htndx++];
480 >        while (htpos != NULL) {
481 >            ep = htpos->def;
482              nm = htpos->name;
483 <            htpos = htpos->next;
484 <            if (ep != NULL && incontext(nm))
485 <                return(ep);
486 <        }
483 >            htpos = htpos->next;
484 >            if (ep != NULL && incontext(nm))
485 >                return(ep);
486 >        }
487      }
431 #ifdef  OUTCHAN
488      if ((ep = ochpos) != NULL)
489 <        ochpos = ep->sibling;
489 >        ochpos = ep->sibling;
490      return(ep);
435 #else
436    return(NULL);
437 #endif
491   }
492  
493  
494   EPNODE *
495 < dpop(name)                      /* pop a definition */
496 < char  *name;
495 > dpop(                   /* pop a definition */
496 >        char  *name
497 > )
498   {
499      register VARDEF  *vp;
500      register EPNODE  *dp;
501      
502      if ((vp = varlookup(name)) == NULL || vp->def == NULL)
503 <        return(NULL);
503 >        return(NULL);
504      dp = vp->def;
505      vp->def = dp->sibling;
506      varfree(vp);
# Line 454 | Line 508 | char  *name;
508   }
509  
510  
511 < dpush(nm, ep)                   /* push on a definition */
512 < char  *nm;
513 < register EPNODE  *ep;
511 > void
512 > dpush(                  /* push on a definition */
513 >        char  *nm,
514 >        register EPNODE  *ep
515 > )
516   {
517      register VARDEF  *vp;
518  
# Line 466 | Line 522 | register EPNODE  *ep;
522   }
523  
524  
525 < #ifdef  OUTCHAN
526 < addchan(sp)                     /* add an output channel assignment */
527 < EPNODE  *sp;
525 > void
526 > addchan(                        /* add an output channel assignment */
527 >        EPNODE  *sp
528 > )
529   {
530 <    int  ch = sp->v.kid->v.chan;
530 >    int  ch = sp->v.kid->v.chan;
531      register EPNODE  *ep, *epl;
532  
533      for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling)
# Line 494 | Line 551 | EPNODE  *sp;
551      sp->sibling = NULL;
552  
553   }
497 #endif
554  
555  
556 < getstatement()                  /* get next statement */
556 > void
557 > getstatement(void)                      /* get next statement */
558   {
559      register EPNODE  *ep;
560      char  *qname;
# Line 507 | Line 564 | getstatement()                 /* get next statement */
564          scan();
565          return;
566      }
567 < #ifdef  OUTCHAN
568 <    if (nextc == '$') {         /* channel assignment */
567 >    if (esupport&E_OUTCHAN &&
568 >                nextc == '$') {         /* channel assignment */
569          ep = getchan();
570          addchan(ep);
571 <    } else
515 < #endif
516 <    {                           /* ordinary definition */
571 >    } else {                            /* ordinary definition */
572          ep = getdefn();
573          qname = qualname(dname(ep), 0);
574 < #ifdef  REDEFW
575 <        if ((vdef = varlookup(qname)) != NULL)
521 <            if (vdef->def != NULL) {
574 >        if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL)
575 >            if (vdef->def != NULL && epcmp(ep, vdef->def)) {
576                  wputs(qname);
577                  if (vdef->def->type == ':')
578                      wputs(": redefined constant expression\n");
579                  else
580                      wputs(": redefined\n");
581 <            }
528 < #ifdef  FUNCTION
529 <            else if (ep->v.kid->type == FUNC && vdef->lib != NULL) {
581 >            } else if (ep->v.kid->type == FUNC && vdef->lib != NULL) {
582                  wputs(qname);
583                  wputs(": definition hides library function\n");
584              }
533 #endif
534 #endif
585          if (ep->type == ':')
586              dremove(qname);
587          else
# Line 547 | Line 597 | getstatement()                 /* get next statement */
597  
598  
599   EPNODE *
600 < getdefn()                       /* A -> SYM = E1 */
601 <                                /*      SYM : E1 */
602 <                                /*      FUNC(SYM,..) = E1 */
603 <                                /*      FUNC(SYM,..) : E1 */
600 > getdefn(void)
601 >        /* A -> SYM = E1 */
602 >        /*      SYM : E1 */
603 >        /*      FUNC(SYM,..) = E1 */
604 >        /*      FUNC(SYM,..) : E1 */
605   {
606      register EPNODE  *ep1, *ep2;
607  
# Line 561 | Line 612 | getdefn()                      /* A -> SYM = E1 */
612      ep1->type = SYM;
613      ep1->v.name = savestr(getname());
614  
615 < #ifdef  FUNCTION
565 <    if (nextc == '(') {
615 >    if (esupport&E_FUNCTION && nextc == '(') {
616          ep2 = newnode();
617          ep2->type = FUNC;
618          addekid(ep2, ep1);
# Line 580 | Line 630 | getdefn()                      /* A -> SYM = E1 */
630              syntax("')' expected");
631          scan();
632          curfunc = ep1;
633 <    } else
584 <        curfunc = NULL;
585 < #endif
633 >    }
634  
635      if (nextc != '=' && nextc != ':')
636          syntax("'=' or ':' expected");
# Line 593 | Line 641 | getdefn()                      /* A -> SYM = E1 */
641      addekid(ep2, ep1);
642      addekid(ep2, getE1());
643  
644 <    if (
597 < #ifdef  FUNCTION
598 <            ep1->type == SYM &&
599 < #endif
600 <            ep1->sibling->type != NUM) {
644 >    if (ep1->type == SYM && ep1->sibling->type != NUM) {
645          ep1 = newnode();
646          ep1->type = TICK;
647 <        ep1->v.tick = -1;
647 >        ep1->v.tick = 0;
648          addekid(ep2, ep1);
649          ep1 = newnode();
650          ep1->type = NUM;
651          addekid(ep2, ep1);
652      }
653 +    curfunc = NULL;
654  
655      return(ep2);
656   }
657  
658  
614 #ifdef  OUTCHAN
659   EPNODE *
660 < getchan()                       /* A -> $N = E1 */
660 > getchan(void)                   /* A -> $N = E1 */
661   {
662      register EPNODE  *ep1, *ep2;
663  
# Line 636 | Line 680 | getchan()                      /* A -> $N = E1 */
680  
681      return(ep2);
682   }
639 #endif
683  
684  
685  
# Line 645 | Line 688 | getchan()                      /* A -> $N = E1 */
688   */
689  
690  
691 < static double
692 < dvalue(name, d)                 /* evaluate a variable */
650 < char  *name;
651 < EPNODE  *d;
691 > static double                   /* evaluate a variable */
692 > dvalue(char  *name, EPNODE      *d)
693   {
694      register EPNODE  *ep1, *ep2;
695      
# Line 661 | Line 702 | EPNODE  *d;
702      if (ep1->type == NUM)
703          return(ep1->v.num);                     /* return if number */
704      ep2 = ep1->sibling;                         /* check time */
705 <    if (ep2->v.tick < 0 || ep2->v.tick < eclock) {
706 <        ep2->v.tick = d->type == ':' ? 1L<<30 : eclock;
705 >    if (eclock >= MAXCLOCK)
706 >        eclock = 1;                             /* wrap clock counter */
707 >    if (ep2->v.tick < MAXCLOCK &&
708 >                ep2->v.tick == 0 | ep2->v.tick != eclock) {
709 >        ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock;
710          ep2 = ep2->sibling;
711          ep2->v.num = evalue(ep1);               /* needs new value */
712      } else
713          ep2 = ep2->sibling;                     /* else reuse old value */
714  
715      return(ep2->v.num);
672 }
673
674
675 static int
676 hash(s)                         /* hash a string */
677 register char  *s;
678 {
679    register int  rval = 0;
680
681    while (*s)
682        rval += *s++;
683    
684    return(rval % NHASH);
716   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines