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.7 by greg, Fri Jul 20 08:30:23 1990 UTC vs.
Revision 1.13 by greg, Thu Aug 8 11:22:06 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 +            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 129 | Line 140 | double  val;
140      ep2->type = NUM;
141      ep2->v.num = val;
142      addekid(ep1, ep2);
143 <    dclear(vname);
144 <    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 153 | 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 +    wputs(nambuf);
256 +    wputs(": name too long\n");
257 +    return(NULL);
258 + }
259 +
260 +
261   #ifdef  OUTCHAN
262   chanout(cs)                     /* set output channels */
263   int  (*cs)();
# Line 166 | Line 271 | int  (*cs)();
271   #endif
272  
273  
274 < dclearall()                     /* clear all definitions */
274 > dcleanup(lvl)           /* clear definitions (0->vars,1->output,2->consts) */
275 > int  lvl;
276   {
277      register int  i;
278      register VARDEF  *vp;
# Line 174 | Line 280 | dclearall()                    /* clear all definitions */
280  
281      for (i = 0; i < NHASH; i++)
282          for (vp = hashtbl[i]; vp != NULL; vp = vp->next)
283 <            dclear(vp->name);
283 >            if (lvl >= 2)
284 >                dremove(vp->name);
285 >            else
286 >                dclear(vp->name);
287   #ifdef  OUTCHAN
288 <    for (ep = outchan; ep != NULL; ep = ep->sibling)
289 <        epfree(ep);
290 <    outchan = NULL;
288 >    if (lvl >= 1) {
289 >        for (ep = outchan; ep != NULL; ep = ep->sibling)
290 >            epfree(ep);
291 >        outchan = NULL;
292 >    }
293   #endif
294   }
295  
# Line 199 | Line 310 | VARDEF *
310   varlookup(name)                 /* look up a variable */
311   char  *name;
312   {
313 +    int  lvl = 0;
314 +    register char  *qname;
315      register VARDEF  *vp;
316 <    
317 <    for (vp = hashtbl[hash(name)]; vp != NULL; vp = vp->next)
318 <        if (!strcmp(vp->name, name))
319 <            return(vp);
316 >                                /* find most qualified match */
317 >    while ((qname = qualname(name, lvl++)) != NULL)
318 >        for (vp = hashtbl[hash(qname)]; vp != NULL; vp = vp->next)
319 >            if (!strcmp(vp->name, qname))
320 >                return(vp);
321      return(NULL);
322   }
323  
# Line 215 | Line 329 | char  *name;
329      register VARDEF  *vp;
330      int  hv;
331      
332 +    if ((vp = varlookup(name)) != NULL) {
333 +        vp->nlinks++;
334 +        return(vp);
335 +    }
336 +    name = qualname(name, 0);           /* use fully qualified name */
337      hv = hash(name);
219    for (vp = hashtbl[hv]; vp != NULL; vp = vp->next)
220        if (!strcmp(vp->name, name)) {
221            vp->nlinks++;
222            return(vp);
223        }
338      vp = (VARDEF *)emalloc(sizeof(VARDEF));
339      vp->name = savestr(name);
340      vp->nlinks = 1;
# Line 308 | Line 422 | char  *name;
422   }
423  
424  
425 < dpush(ep)                       /* push on a definition */
425 > dpush(nm, ep)                   /* push on a definition */
426 > char  *nm;
427   register EPNODE  *ep;
428   {
429      register VARDEF  *vp;
430  
431 <    vp = varinsert(dname(ep));
431 >    vp = varinsert(nm);
432      ep->sibling = vp->def;
433      vp->def = ep;
434   }
# Line 350 | Line 465 | EPNODE  *sp;
465   #endif
466  
467  
468 < loaddefn()                      /* load next definition */
468 > getstatement()                  /* get next statement */
469   {
470      register EPNODE  *ep;
471 +    char  *qname;
472 +    EPNODE  *lastdef;
473  
474      if (nextc == ';') {         /* empty statement */
475          scan();
# Line 366 | Line 483 | loaddefn()                     /* load next definition */
483   #endif
484      {                           /* ordinary definition */
485          ep = getdefn();
486 +        qname = qualname(dname(ep), 0);
487   #ifdef  REDEFW
488 <        if (dlookup(dname(ep)) != NULL) {
489 <                dclear(dname(ep));
490 <                wputs(dname(ep));
488 >        if ((lastdef = dlookup(qname)) != NULL) {
489 >            wputs(qname);
490 >            if (lastdef->type == ':')
491 >                wputs(": redefined constant expression\n");
492 >            else
493                  wputs(": redefined\n");
494          }
495 < #else
496 <        dclear(dname(ep));
495 > #ifdef  FUNCTION
496 >        else if (ep->v.kid->type == FUNC && liblookup(qname) != NULL) {
497 >            wputs(qname);
498 >            wputs(": definition hides library function\n");
499 >        }
500   #endif
501 <        dpush(ep);
501 > #endif
502 >        if (ep->type == ':')
503 >            dremove(qname);
504 >        else
505 >            dclear(qname);
506 >        dpush(qname, ep);
507      }
508      if (nextc != EOF) {
509          if (nextc != ';')
# Line 387 | Line 515 | loaddefn()                     /* load next definition */
515  
516   EPNODE *
517   getdefn()                       /* A -> SYM = E1 */
518 +                                /*      SYM : E1 */
519                                  /*      FUNC(SYM,..) = E1 */
520 +                                /*      FUNC(SYM,..) : E1 */
521   {
522      register EPNODE  *ep1, *ep2;
523  
# Line 421 | Line 551 | getdefn()                      /* A -> SYM = E1 */
551          curfunc = NULL;
552   #endif
553  
554 <    if (nextc != '=')
555 <        syntax("'=' expected");
426 <    scan();
554 >    if (nextc != '=' && nextc != ':')
555 >        syntax("'=' or ':' expected");
556  
557      ep2 = newnode();
558 <    ep2->type = '=';
558 >    ep2->type = nextc;
559 >    scan();
560      addekid(ep2, ep1);
561      addekid(ep2, getE1());
562  
# Line 499 | Line 629 | EPNODE  *d;
629          return(ep1->v.num);                     /* return if number */
630      ep2 = ep1->sibling;                         /* check time */
631      if (ep2->v.tick < 0 || ep2->v.tick < eclock) {
632 <        ep2->v.tick = eclock;
632 >        ep2->v.tick = d->type == ':' ? 1L<<30 : eclock;
633          ep2 = ep2->sibling;
634          ep2->v.num = evalue(ep1);               /* needs new value */
635      } else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines