home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / xlispplu / xlisp2tc / xlsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-25  |  5.3 KB  |  247 lines

  1. /* xlsym - symbol handling routines */
  2. /*    Copyright (c) 1985, by David Michael Betz
  3.     All Rights Reserved
  4.     Permission is granted for unrestricted non-commercial use    */
  5.  
  6. #include "xlisp.h"
  7.  
  8. /* external variables */
  9. extern LVAL obarray,s_unbound;
  10. extern LVAL xlenv,xlfenv,xldenv;
  11.  
  12. /* forward declarations */
  13. #ifdef PROTOTYPES
  14. LOCAL(LVAL) findprop(LVAL,LVAL) ;
  15. #else
  16. FORWARD LVAL findprop();
  17. #endif PROTOTYPES
  18.  
  19. /* xlenter - enter a symbol into the obarray */
  20. LVAL xlenter(name)
  21.   char *name;
  22. {
  23.     LVAL sym,array;
  24.     int i;
  25.  
  26.     /* check for nil */
  27.     if (strcmp(name,"NIL") == 0)
  28.     return (NIL);
  29.  
  30.     /* check for symbol already in table */
  31.     array = getvalue(obarray);
  32.     i = hash(name,HSIZE);
  33.     for (sym = getelement(array,i); sym; sym = cdr(sym))
  34.     if (strcmp(name,getstring(getpname(car(sym)))) == 0)
  35.         return (car(sym));
  36.  
  37.     /* make a new symbol node and link it into the list */
  38.     xlsave1(sym);
  39.     sym = consd(getelement(array,i));
  40.     rplaca(sym,xlmakesym(name));
  41.     setelement(array,i,sym);
  42.     xlpop();
  43.  
  44.     /* return the new symbol */
  45.     return (car(sym));
  46. }
  47.  
  48. /* xlmakesym - make a new symbol node */
  49. LVAL xlmakesym(name)
  50.   char *name;
  51. {
  52.     LVAL sym;
  53.     sym = cvsymbol(name);
  54.     if (*name == ':')
  55.     setvalue(sym,sym);
  56.     return (sym);
  57. }
  58.  
  59. /* xlgetvalue - get the value of a symbol (with check) */
  60. LVAL xlgetvalue(sym)
  61.   LVAL sym;
  62. {
  63.     LVAL val;
  64.  
  65.     /* look for the value of the symbol */
  66.     while ((val = xlxgetvalue(sym)) == s_unbound)
  67.     xlunbound(sym);
  68.  
  69.     /* return the value */
  70.     return (val);
  71. }
  72.  
  73. /* xlxgetvalue - get the value of a symbol */
  74. LVAL xlxgetvalue(sym)
  75.   LVAL sym;
  76. {
  77.     register LVAL fp,ep;
  78.     LVAL val;
  79.  
  80.     /* check the environment list */
  81.     for (fp = xlenv; fp; fp = cdr(fp))
  82.  
  83.     /* check for an instance variable */
  84.     if ((ep = car(fp)) && objectp(car(ep))) {
  85.         if (xlobgetvalue(ep,sym,&val))
  86.         return (val);
  87.     }
  88.  
  89.     /* check an environment stack frame */
  90.     else {
  91.         for (; ep; ep = cdr(ep))
  92.         if (sym == car(car(ep)))
  93.             return (cdr(car(ep)));
  94.     }
  95.  
  96.     /* return the global value */
  97.     return (getvalue(sym));
  98. }
  99.  
  100. /* xlsetvalue - set the value of a symbol */
  101. void xlsetvalue(sym,val)
  102.   LVAL sym,val;
  103. {
  104.     register LVAL fp,ep;
  105.  
  106.     /* look for the symbol in the environment list */
  107.     for (fp = xlenv; fp; fp = cdr(fp))
  108.  
  109.     /* check for an instance variable */
  110.     if ((ep = car(fp)) && objectp(car(ep))) {
  111.         if (xlobsetvalue(ep,sym,val))
  112.         return;
  113.     }
  114.  
  115.     /* check an environment stack frame */
  116.     else {
  117.         for (; ep; ep = cdr(ep))
  118.         if (sym == car(car(ep))) {
  119.             rplacd(car(ep),val);
  120.             return;
  121.         }
  122.     }
  123.  
  124.     /* store the global value */
  125.     setvalue(sym,val);
  126. }
  127.  
  128. /* xlgetfunction - get the functional value of a symbol (with check) */
  129. LVAL xlgetfunction(sym)
  130.   LVAL sym;
  131. {
  132.     LVAL val;
  133.  
  134.     /* look for the functional value of the symbol */
  135.     while ((val = xlxgetfunction(sym)) == s_unbound)
  136.     xlfunbound(sym);
  137.  
  138.     /* return the value */
  139.     return (val);
  140. }
  141.  
  142. /* xlxgetfunction - get the functional value of a symbol */
  143. LVAL xlxgetfunction(sym)
  144.   LVAL sym;
  145. {
  146.     register LVAL fp,ep;
  147.  
  148.     /* check the environment list */
  149.     for (fp = xlfenv; fp; fp = cdr(fp))
  150.     for (ep = car(fp); ep; ep = cdr(ep))
  151.         if (sym == car(car(ep)))
  152.         return (cdr(car(ep)));
  153.  
  154.     /* return the global value */
  155.     return (getfunction(sym));
  156. }
  157.  
  158. /* xlsetfunction - set the functional value of a symbol */
  159. void xlsetfunction(sym,val)
  160.   LVAL sym,val;
  161. {
  162.     register LVAL fp,ep;
  163.  
  164.     /* look for the symbol in the environment list */
  165.     for (fp = xlfenv; fp; fp = cdr(fp))
  166.     for (ep = car(fp); ep; ep = cdr(ep))
  167.         if (sym == car(car(ep))) {
  168.         rplacd(car(ep),val);
  169.         return;
  170.         }
  171.  
  172.     /* store the global value */
  173.     setfunction(sym,val);
  174. }
  175.  
  176. /* xlgetprop - get the value of a property */
  177. LVAL xlgetprop(sym,prp)
  178.   LVAL sym,prp;
  179. {
  180.     LVAL p;
  181.     return ((p = findprop(sym,prp)) ? car(p) : NIL);
  182. }
  183.  
  184. /* xlputprop - put a property value onto the property list */
  185. void xlputprop(sym,val,prp)
  186.   LVAL sym,val,prp;
  187. {
  188.     LVAL pair;
  189.     if (pair = findprop(sym,prp))
  190.     rplaca(pair,val);
  191.     else
  192.     setplist(sym,cons(prp,cons(val,getplist(sym))));
  193. }
  194.  
  195. /* xlremprop - remove a property from a property list */
  196. void xlremprop(sym,prp)
  197.   LVAL sym,prp;
  198. {
  199.     LVAL last,p;
  200.     last = NIL;
  201.     for (p = getplist(sym); consp(p) && consp(cdr(p)); p = cdr(last)) {
  202.     if (car(p) == prp)
  203.         if (last)
  204.         rplacd(last,cdr(cdr(p)));
  205.         else
  206.         setplist(sym,cdr(cdr(p)));
  207.     last = cdr(p);
  208.     }
  209. }
  210.  
  211. /* findprop - find a property pair */
  212. LOCAL(LVAL) findprop(sym,prp)
  213.   LVAL sym,prp;
  214. {
  215.     LVAL p;
  216.     for (p = getplist(sym); consp(p) && consp(cdr(p)); p = cdr(cdr(p)))
  217.     if (car(p) == prp)
  218.         return (cdr(p));
  219.     return (NIL);
  220. }
  221.  
  222. /* hash - hash a symbol name string */
  223. int hash(str,len)
  224.   char *str;
  225. {
  226.     int i;
  227.     for (i = 0; *str; )
  228.     i = (i << 2) ^ *str++;
  229.     i %= len;
  230.     return (i < 0 ? -i : i);
  231. }
  232.  
  233. /* xlsinit - symbol initialization routine */
  234. void xlsinit()
  235. {
  236.     LVAL array,p;
  237.  
  238.     /* initialize the obarray */
  239.     obarray = xlmakesym("*OBARRAY*");
  240.     array = newvector(HSIZE);
  241.     setvalue(obarray,array);
  242.  
  243.     /* add the symbol *OBARRAY* to the obarray */
  244.     p = consa(obarray);
  245.     setelement(array,hash("*OBARRAY*",HSIZE),p);
  246. }
  247.