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.8 by greg, Fri Feb 17 18:34:42 1995 UTC vs.
Revision 2.26 by greg, Thu Jul 24 15:41:13 2014 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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  "rtio.h"
36 + #include  "rtmisc.h"
37   #include  "calcomp.h"
38  
39   #ifndef  NHASH
# Line 38 | Line 44 | static char SCCSid[] = "$SunId$ LBL";
44  
45   #define  newnode()      (EPNODE *)ecalloc(1, sizeof(EPNODE))
46  
47 < extern char  *ecalloc(), *emalloc(), *savestr(), *strcpy();
47 > static double  dvalue(char  *name, EPNODE *d);
48  
49 < static double  dvalue();
49 > #define  MAXCLOCK       (1L<<31)        /* clock wrap value */
50  
51   unsigned long  eclock = 0;              /* value storage timer */
52  
53 < static char  context[MAXWORD+1];        /* current context path */
53 > #define  MAXCNTX        1023            /* maximum context length */
54  
55 + static char  context[MAXCNTX+1];        /* current context path */
56 +
57   static VARDEF  *hashtbl[NHASH];         /* definition list */
58   static int  htndx;                      /* index for */        
59   static VARDEF  *htpos;                  /* ...dfirst() and */
52 #ifdef  OUTCHAN
60   static EPNODE  *ochpos;                 /* ...dnext */
61   static EPNODE  *outchan;
55 #endif
62  
63 < #ifdef  FUNCTION
58 < EPNODE  *curfunc;
63 > EPNODE  *curfunc = NULL;
64   #define  dname(ep)      ((ep)->v.kid->type == SYM ? \
65                          (ep)->v.kid->v.name : \
66                          (ep)->v.kid->v.kid->v.name)
62 #else
63 #define  dname(ep)      ((ep)->v.kid->v.name)
64 #endif
67  
68  
69 < fcompile(fname)                 /* get definitions from a file */
70 < char  *fname;
69 > void
70 > fcompile(                       /* get definitions from a file */
71 >        char  *fname
72 > )
73   {
74      FILE  *fp;
75  
# Line 84 | Line 88 | char  *fname;
88   }
89  
90  
91 < scompile(str, fn, ln)           /* get definitions from a string */
92 < char  *str;
93 < char  *fn;
94 < int  ln;
91 > void
92 > scompile(               /* get definitions from a string */
93 >        char  *str,
94 >        char  *fn,
95 >        int  ln
96 > )
97   {
98      initstr(str, fn, ln);
99      while (nextc != EOF)
# Line 96 | Line 102 | int  ln;
102  
103  
104   double
105 < varvalue(vname)                 /* return a variable's value */
106 < char  *vname;
105 > varvalue(                       /* return a variable's value */
106 >        char  *vname
107 > )
108   {
109      return(dvalue(vname, dlookup(vname)));
110   }
111  
112  
113   double
114 < evariable(ep)                   /* evaluate a variable */
115 < EPNODE  *ep;
114 > evariable(                      /* evaluate a variable */
115 >        EPNODE  *ep
116 > )
117   {
118 <    register VARDEF  *dp = ep->v.ln;
118 >    VARDEF  *dp = ep->v.ln;
119  
120      return(dvalue(dp->name, dp->def));
121   }
122  
123  
124 < varset(vname, assign, val)      /* set a variable's value */
125 < char  *vname;
126 < int  assign;
127 < double  val;
124 > void
125 > varset(         /* set a variable's value */
126 >        char  *vname,
127 >        int  assign,
128 >        double  val
129 > )
130   {
131      char  *qname;
132 <    register EPNODE  *ep1, *ep2;
132 >    EPNODE  *ep1, *ep2;
133                                          /* get qualified name */
134      qname = qualname(vname, 0);
135                                          /* check for quick set */
136 <    if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM) {
136 >    if ((ep1 = dlookup(qname)) != NULL && ep1->v.kid->type == SYM &&
137 >                (ep1->type == ':') <= (assign == ':')) {
138          ep2 = ep1->v.kid->sibling;
139          if (ep2->type == NUM) {
140              ep2->v.num = val;
# Line 131 | Line 142 | double val;
142              return;
143          }
144      }
145 +    if (ep1 != NULL && esupport&E_REDEFW) {
146 +        wputs(qname);
147 +        if (ep1->type == ':')
148 +            wputs(": reset constant expression\n");
149 +        else
150 +            wputs(": reset expression\n");
151 +    }
152                                          /* hand build definition */
153      ep1 = newnode();
154      ep1->type = assign;
# Line 142 | Line 160 | double val;
160      ep2->type = NUM;
161      ep2->v.num = val;
162      addekid(ep1, ep2);
163 <    dremove(qname);
163 >    if (assign == ':')
164 >        dremove(qname);
165 >    else
166 >        dclear(qname);
167      dpush(qname, ep1);
168   }
169  
170  
171 < dclear(name)                    /* delete variable definitions of name */
172 < char  *name;
171 > void
172 > dclear(                 /* delete variable definitions of name */
173 >        char  *name
174 > )
175   {
176 <    register EPNODE  *ep;
176 >    EPNODE  *ep;
177  
178      while ((ep = dpop(name)) != NULL) {
179          if (ep->type == ':') {
# Line 162 | Line 185 | char  *name;
185   }
186  
187  
188 < dremove(name)                   /* delete all definitions of name */
189 < char  *name;
188 > void
189 > dremove(                        /* delete all definitions of name */
190 >        char  *name
191 > )
192   {
193 <    register EPNODE  *ep;
193 >    EPNODE  *ep;
194  
195      while ((ep = dpop(name)) != NULL)
196          epfree(ep);
197   }
198  
199  
200 < vardefined(name)        /* return non-zero if variable defined */
201 < char  *name;
200 > int
201 > vardefined(     /* return non-zero if variable defined */
202 >        char  *name
203 > )
204   {
205 <    register EPNODE  *dp;
205 >    EPNODE  *dp;
206  
207      return((dp = dlookup(name)) != NULL && dp->v.kid->type == SYM);
208   }
209  
210  
211   char *
212 < setcontext(ctx)                 /* set a new context path */
213 < register char  *ctx;
212 > setcontext(                     /* set a new context path */
213 >        char  *ctx
214 > )
215   {
216 <    register char  *cpp;
216 >    char  *cpp;
217  
218      if (ctx == NULL)
219          return(context);                /* just asking */
# Line 198 | Line 226 | register char  *ctx;
226      cpp = context;                      /* start context with mark */
227      *cpp++ = CNTXMARK;
228      do {                                /* carefully copy new context */
229 <        if (cpp >= context+MAXWORD)
229 >        if (cpp >= context+MAXCNTX)
230              break;                      /* just copy what we can */
231          if (isid(*ctx))
232              *cpp++ = *ctx++;
# Line 214 | Line 242 | register char  *ctx;
242  
243  
244   char *
245 < pushcontext(ctx)                /* push on another context */
246 < char  *ctx;
245 > pushcontext(            /* push on another context */
246 >        char  *ctx
247 > )
248   {
249 <    extern char  *strncpy(), *strcpy();
250 <    char  oldcontext[MAXWORD+1];
222 <    register int  n;
249 >    char  oldcontext[MAXCNTX+1];
250 >    int  n;
251  
252      strcpy(oldcontext, context);        /* save old context */
253      setcontext(ctx);                    /* set new context */
254      n = strlen(context);                /* tack on old */
255 <    if (n+strlen(oldcontext) > MAXWORD) {
256 <        strncpy(context+n, oldcontext, MAXWORD-n);
257 <        context[MAXWORD] = '\0';
255 >    if (n+strlen(oldcontext) > MAXCNTX) {
256 >        strncpy(context+n, oldcontext, MAXCNTX-n);
257 >        context[MAXCNTX] = '\0';
258      } else
259          strcpy(context+n, oldcontext);
260      return(context);
# Line 234 | Line 262 | char  *ctx;
262  
263  
264   char *
265 < popcontext()                    /* pop off top context */
265 > popcontext(void)                        /* pop off top context */
266   {
267 <    register char  *cp1, *cp2;
267 >    char  *cp1, *cp2;
268  
269      if (!context[0])                    /* nothing left to pop */
270          return(context);
# Line 244 | Line 272 | popcontext()                   /* pop off top context */
272      while (*++cp2 && *cp2 != CNTXMARK)
273          ;
274      cp1 = context;                      /* copy tail to front */
275 <    while (*cp1++ = *cp2++)
275 >    while ( (*cp1++ = *cp2++) )
276          ;
277      return(context);
278   }
279  
280  
281   char *
282 < qualname(nam, lvl)              /* get qualified name */
283 < register char  *nam;
284 < int  lvl;
282 > qualname(               /* get qualified name */
283 >        char  *nam,
284 >        int  lvl
285 > )
286   {
287 <    static char  nambuf[MAXWORD+1];
288 <    register char  *cp = nambuf, *cpp;
287 >    static char  nambuf[RMAXWORD+1];
288 >    char  *cp = nambuf, *cpp;
289                                  /* check for explicit local */
290      if (*nam == CNTXMARK)
291          if (lvl > 0)            /* only action is to refuse search */
# Line 267 | Line 296 | int  lvl;
296          return(lvl > 0 ? NULL : nam);
297                                  /* copy name to static buffer */
298      while (*nam) {
299 <        if (cp >= nambuf+MAXWORD)
299 >        if (cp >= nambuf+RMAXWORD)
300                  goto toolong;
301          *cp++ = *nam++;
302      }
# Line 286 | Line 315 | int  lvl;
315              ;
316      }
317      while (*cpp) {              /* copy context to static buffer */
318 <        if (cp >= nambuf+MAXWORD)
318 >        if (cp >= nambuf+RMAXWORD)
319              goto toolong;
320          *cp++ = *cpp++;
321      }
# Line 296 | Line 325 | toolong:
325   }
326  
327  
328 < incontext(qn)                   /* is qualified name in current context? */
329 < register char  *qn;
328 > int
329 > incontext(                      /* is qualified name in current context? */
330 >        char  *qn
331 > )
332   {
333 +    if (!context[0])                    /* global context accepts all */
334 +        return(1);
335      while (*qn && *qn != CNTXMARK)      /* find context mark */
336          qn++;
337      return(!strcmp(qn, context));
338   }
339  
340  
341 < #ifdef  OUTCHAN
342 < chanout(cs)                     /* set output channels */
343 < int  (*cs)();
341 > void
342 > chanout(                        /* set output channels */
343 >        void  (*cs)(int n, double v)
344 > )
345   {
346 <    register EPNODE  *ep;
346 >    EPNODE  *ep;
347  
348      for (ep = outchan; ep != NULL; ep = ep->sibling)
349          (*cs)(ep->v.kid->v.chan, evalue(ep->v.kid->sibling));
350  
351   }
318 #endif
352  
353  
354 < dcleanup(lvl)           /* clear definitions (0->vars,1->output,2->consts) */
355 < int  lvl;
354 > void
355 > dcleanup(               /* clear definitions (0->vars,1->output,2->consts) */
356 >        int  lvl
357 > )
358   {
359 <    register int  i;
360 <    register VARDEF  *vp;
361 <    register EPNODE  *ep;
359 >    int  i;
360 >    VARDEF  *vp;
361 >    EPNODE  *ep;
362                                  /* if context is global, clear all */
363      for (i = 0; i < NHASH; i++)
364          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
365 <            if (!context[0] || incontext(vp->name))
365 >            if (incontext(vp->name)) {
366                  if (lvl >= 2)
367                      dremove(vp->name);
368                  else
369                      dclear(vp->name);
370 < #ifdef  OUTCHAN
370 >            }
371      if (lvl >= 1) {
372          for (ep = outchan; ep != NULL; ep = ep->sibling)
373              epfree(ep);
374          outchan = NULL;
375      }
341 #endif
376   }
377  
378  
379   EPNODE *
380 < dlookup(name)                   /* look up a definition */
381 < char  *name;
380 > dlookup(                        /* look up a definition */
381 >        char  *name
382 > )
383   {
384 <    register VARDEF  *vp;
384 >    VARDEF  *vp;
385      
386      if ((vp = varlookup(name)) == NULL)
387          return(NULL);
# Line 355 | Line 390 | char  *name;
390  
391  
392   VARDEF *
393 < varlookup(name)                 /* look up a variable */
394 < char  *name;
393 > varlookup(                      /* look up a variable */
394 >        char  *name
395 > )
396   {
397      int  lvl = 0;
398 <    register char  *qname;
399 <    register VARDEF  *vp;
398 >    char  *qname;
399 >    VARDEF  *vp;
400                                  /* find most qualified match */
401      while ((qname = qualname(name, lvl++)) != NULL)
402          for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
# Line 371 | Line 407 | char  *name;
407  
408  
409   VARDEF *
410 < varinsert(name)                 /* get a link to a variable */
411 < char  *name;
410 > varinsert(                      /* get a link to a variable */
411 >        char  *name
412 > )
413   {
414 <    register VARDEF  *vp;
414 >    VARDEF  *vp;
415      int  hv;
416      
417      if ((vp = varlookup(name)) != NULL) {
# Line 382 | Line 419 | char  *name;
419          return(vp);
420      }
421      vp = (VARDEF *)emalloc(sizeof(VARDEF));
385 #ifdef  FUNCTION
422      vp->lib = liblookup(name);
387 #else
388    vp->lib = NULL;
389 #endif
423      if (vp->lib == NULL)                /* if name not in library */
424          name = qualname(name, 0);       /* use fully qualified version */
425      hv = hash(name);
# Line 399 | Line 432 | char  *name;
432   }
433  
434  
435 < #ifdef  FUNCTION
436 < libupdate(fn)                   /* update library links */
437 < char  *fn;
435 > void
436 > libupdate(                      /* update library links */
437 >        char  *fn
438 > )
439   {
440 <    register int  i;
441 <    register VARDEF  *vp;
440 >    int  i;
441 >    VARDEF  *vp;
442                                          /* if fn is NULL then relink all */
443      for (i = 0; i < NHASH; i++)
444          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
445              if (vp->lib != NULL || fn == NULL || !strcmp(fn, vp->name))
446                  vp->lib = liblookup(vp->name);
447   }
414 #endif
448  
449  
450 < varfree(ln)                             /* release link to variable */
451 < register VARDEF  *ln;
450 > void
451 > varfree(                                /* release link to variable */
452 >        VARDEF   *ln
453 > )
454   {
455 <    register VARDEF  *vp;
455 >    VARDEF  *vp;
456      int  hv;
457  
458      if (--ln->nlinks > 0)
# Line 438 | Line 473 | register VARDEF         *ln;
473  
474  
475   EPNODE *
476 < dfirst()                        /* return pointer to first definition */
476 > dfirst(void)                    /* return pointer to first definition */
477   {
478      htndx = 0;
479      htpos = NULL;
445 #ifdef  OUTCHAN
480      ochpos = outchan;
447 #endif
481      return(dnext());
482   }
483  
484  
485   EPNODE *
486 < dnext()                         /* return pointer to next definition */
486 > dnext(void)                             /* return pointer to next definition */
487   {
488 <    register EPNODE  *ep;
489 <    register char  *nm;
488 >    EPNODE  *ep;
489 >    char  *nm;
490  
491      while (htndx < NHASH) {
492          if (htpos == NULL)
# Line 466 | Line 499 | dnext()                                /* return pointer to next definition */
499                  return(ep);
500          }
501      }
469 #ifdef  OUTCHAN
502      if ((ep = ochpos) != NULL)
503          ochpos = ep->sibling;
504      return(ep);
473 #else
474    return(NULL);
475 #endif
505   }
506  
507  
508   EPNODE *
509 < dpop(name)                      /* pop a definition */
510 < char  *name;
509 > dpop(                   /* pop a definition */
510 >        char  *name
511 > )
512   {
513 <    register VARDEF  *vp;
514 <    register EPNODE  *dp;
513 >    VARDEF  *vp;
514 >    EPNODE  *dp;
515      
516      if ((vp = varlookup(name)) == NULL || vp->def == NULL)
517          return(NULL);
# Line 492 | Line 522 | char  *name;
522   }
523  
524  
525 < dpush(nm, ep)                   /* push on a definition */
526 < char  *nm;
527 < register EPNODE  *ep;
525 > void
526 > dpush(                  /* push on a definition */
527 >        char  *nm,
528 >        EPNODE   *ep
529 > )
530   {
531 <    register VARDEF  *vp;
531 >    VARDEF  *vp;
532  
533      vp = varinsert(nm);
534      ep->sibling = vp->def;
# Line 504 | Line 536 | register EPNODE         *ep;
536   }
537  
538  
539 < #ifdef  OUTCHAN
540 < addchan(sp)                     /* add an output channel assignment */
541 < EPNODE  *sp;
539 > void
540 > addchan(                        /* add an output channel assignment */
541 >        EPNODE  *sp
542 > )
543   {
544      int  ch = sp->v.kid->v.chan;
545 <    register EPNODE  *ep, *epl;
545 >    EPNODE  *ep, *epl;
546  
547      for (epl = NULL, ep = outchan; ep != NULL; epl = ep, ep = ep->sibling)
548          if (ep->v.kid->v.chan >= ch) {
# Line 532 | Line 565 | EPNODE *sp;
565      sp->sibling = NULL;
566  
567   }
535 #endif
568  
569  
570 < getstatement()                  /* get next statement */
570 > void
571 > getstatement(void)                      /* get next statement */
572   {
573 <    register EPNODE  *ep;
573 >    EPNODE  *ep;
574      char  *qname;
575 <    register VARDEF  *vdef;
575 >    VARDEF  *vdef;
576  
577      if (nextc == ';') {         /* empty statement */
578          scan();
579          return;
580      }
581 < #ifdef  OUTCHAN
582 <    if (nextc == '$') {         /* channel assignment */
581 >    if (esupport&E_OUTCHAN &&
582 >                nextc == '$') {         /* channel assignment */
583          ep = getchan();
584          addchan(ep);
585 <    } else
553 < #endif
554 <    {                           /* ordinary definition */
585 >    } else {                            /* ordinary definition */
586          ep = getdefn();
587          qname = qualname(dname(ep), 0);
588 < #ifdef  REDEFW
558 <        if ((vdef = varlookup(qname)) != NULL)
588 >        if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) {
589              if (vdef->def != NULL && epcmp(ep, vdef->def)) {
590                  wputs(qname);
591                  if (vdef->def->type == ':')
592                      wputs(": redefined constant expression\n");
593                  else
594                      wputs(": redefined\n");
595 <            }
566 < #ifdef  FUNCTION
567 <            else if (ep->v.kid->type == FUNC && vdef->lib != NULL) {
595 >            } else if (ep->v.kid->type == FUNC && vdef->lib != NULL) {
596                  wputs(qname);
597                  wputs(": definition hides library function\n");
598              }
599 < #endif
572 < #endif
599 >        }
600          if (ep->type == ':')
601              dremove(qname);
602          else
# Line 585 | Line 612 | getstatement()                 /* get next statement */
612  
613  
614   EPNODE *
615 < getdefn()                       /* A -> SYM = E1 */
616 <                                /*      SYM : E1 */
617 <                                /*      FUNC(SYM,..) = E1 */
618 <                                /*      FUNC(SYM,..) : E1 */
615 > getdefn(void)
616 >        /* A -> SYM = E1 */
617 >        /*      SYM : E1 */
618 >        /*      FUNC(SYM,..) = E1 */
619 >        /*      FUNC(SYM,..) : E1 */
620   {
621 <    register EPNODE  *ep1, *ep2;
621 >    EPNODE  *ep1, *ep2;
622  
623      if (!isalpha(nextc) && nextc != CNTXMARK)
624          syntax("illegal variable name");
# Line 599 | Line 627 | getdefn()                      /* A -> SYM = E1 */
627      ep1->type = SYM;
628      ep1->v.name = savestr(getname());
629  
630 < #ifdef  FUNCTION
603 <    if (nextc == '(') {
630 >    if (esupport&E_FUNCTION && nextc == '(') {
631          ep2 = newnode();
632          ep2->type = FUNC;
633          addekid(ep2, ep1);
# Line 608 | Line 635 | getdefn()                      /* A -> SYM = E1 */
635          do {
636              scan();
637              if (!isalpha(nextc))
638 <                syntax("illegal variable name");
638 >                syntax("illegal parameter name");
639              ep2 = newnode();
640              ep2->type = SYM;
641              ep2->v.name = savestr(getname());
# Line 618 | Line 645 | getdefn()                      /* A -> SYM = E1 */
645              syntax("')' expected");
646          scan();
647          curfunc = ep1;
648 <    } else
622 <        curfunc = NULL;
623 < #endif
648 >    }
649  
650      if (nextc != '=' && nextc != ':')
651          syntax("'=' or ':' expected");
# Line 631 | Line 656 | getdefn()                      /* A -> SYM = E1 */
656      addekid(ep2, ep1);
657      addekid(ep2, getE1());
658  
659 <    if (
635 < #ifdef  FUNCTION
636 <            ep1->type == SYM &&
637 < #endif
638 <            ep1->sibling->type != NUM) {
659 >    if (ep1->type == SYM && ep1->sibling->type != NUM) {
660          ep1 = newnode();
661 <        ep1->type = TICK;
661 >        ep1->type = CLKT;
662          ep1->v.tick = 0;
663          addekid(ep2, ep1);
664          ep1 = newnode();
665          ep1->type = NUM;
666          addekid(ep2, ep1);
667      }
668 +    curfunc = NULL;
669  
670      return(ep2);
671   }
672  
673  
652 #ifdef  OUTCHAN
674   EPNODE *
675 < getchan()                       /* A -> $N = E1 */
675 > getchan(void)                   /* A -> $N = E1 */
676   {
677 <    register EPNODE  *ep1, *ep2;
677 >    EPNODE  *ep1, *ep2;
678  
679      if (nextc != '$')
680          syntax("missing '$'");
# Line 674 | Line 695 | getchan()                      /* A -> $N = E1 */
695  
696      return(ep2);
697   }
677 #endif
698  
699  
700  
# Line 683 | Line 703 | getchan()                      /* A -> $N = E1 */
703   */
704  
705  
706 < static double
707 < dvalue(name, d)                 /* evaluate a variable */
688 < char  *name;
689 < EPNODE  *d;
706 > static double                   /* evaluate a variable */
707 > dvalue(char  *name, EPNODE      *d)
708   {
709 <    register EPNODE  *ep1, *ep2;
709 >    EPNODE  *ep1, *ep2;
710      
711      if (d == NULL || d->v.kid->type != SYM) {
712          eputs(name);
# Line 699 | Line 717 | EPNODE *d;
717      if (ep1->type == NUM)
718          return(ep1->v.num);                     /* return if number */
719      ep2 = ep1->sibling;                         /* check time */
720 <    if (ep2->v.tick == 0 || ep2->v.tick < eclock) {
721 <        ep2->v.tick = d->type == ':' ? ~0L : eclock;
720 >    if (eclock >= MAXCLOCK)
721 >        eclock = 1;                             /* wrap clock counter */
722 >    if (ep2->v.tick < MAXCLOCK &&
723 >                (ep2->v.tick == 0) | (ep2->v.tick != eclock)) {
724 >        ep2->v.tick = d->type == ':' ? MAXCLOCK : eclock;
725          ep2 = ep2->sibling;
726          ep2->v.num = evalue(ep1);               /* needs new value */
727      } else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines