home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / lang / xlisp.sha / xlsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-17  |  4.7 KB  |  216 lines

  1. /* xlsym - symbol handling routines */
  2.  
  3. #include "xlisp.h"
  4.  
  5. /* external variables */
  6. extern NODE *oblist,*keylist;
  7. extern NODE *s_unbound;
  8. extern NODE *xlstack;
  9.  
  10. /* forward declarations */
  11. FORWARD NODE *symenter();
  12. FORWARD NODE *xlmakesym();
  13. FORWARD NODE *findprop();
  14.  
  15. /* xlenter - enter a symbol into the oblist or keylist */
  16. NODE *xlenter(name,type)
  17.   char *name;
  18. {
  19.     return (symenter(name,type,(*name == ':' ? keylist : oblist)));
  20. }
  21.  
  22. /* symenter - enter a symbol into a package */
  23. LOCAL NODE *symenter(name,type,listsym)
  24.   char *name; int type; NODE *listsym;
  25. {
  26.     NODE *oldstk,*lsym,*nsym,newsym;
  27.     int cmp;
  28.  
  29.     /* check for nil */
  30.     if (strcmp(name,"nil") == 0)
  31.     return (NIL);
  32.  
  33.     /* check for symbol already in table */
  34.     lsym = NIL;
  35.     nsym = listsym->n_symvalue;
  36.     while (nsym) {
  37.     if ((cmp = strcmp(name,xlsymname(car(nsym)))) <= 0)
  38.         break;
  39.     lsym = nsym;
  40.     nsym = cdr(nsym);
  41.     }
  42.  
  43.     /* check to see if we found it */
  44.     if (nsym && cmp == 0)
  45.     return (car(nsym));
  46.  
  47.     /* make a new symbol node and link it into the list */
  48.     oldstk = xlsave(&newsym,NULL);
  49.     newsym.n_ptr = newnode(LIST);
  50.     rplaca(newsym.n_ptr,xlmakesym(name,type));
  51.     rplacd(newsym.n_ptr,nsym);
  52.     if (lsym)
  53.     rplacd(lsym,newsym.n_ptr);
  54.     else
  55.     listsym->n_symvalue = newsym.n_ptr;
  56.     xlstack = oldstk;
  57.  
  58.     /* return the new symbol */
  59.     return (car(newsym.n_ptr));
  60. }
  61.  
  62. /* xlsenter - enter a symbol with a static print name */
  63. NODE *xlsenter(name)
  64.   char *name;
  65. {
  66.     return (xlenter(name,STATIC));
  67. }
  68.  
  69. /* xlintern - intern a symbol onto the oblist */
  70. NODE *xlintern(sym)
  71.   NODE *sym;
  72. {
  73.     NODE *oldstk,*lsym,*nsym,newsym;
  74.     char *name;
  75.     int cmp;
  76.  
  77.     /* get the symbol's print name */
  78.     name = xlsymname(sym);
  79.  
  80.     /* check for nil */
  81.     if (strcmp(name,"nil") == 0)
  82.     return (NIL);
  83.  
  84.     /* check for symbol already in table */
  85.     lsym = NIL;
  86.     nsym = oblist->n_symvalue;
  87.     while (nsym) {
  88.     if ((cmp = strcmp(name,xlsymname(car(nsym)))) <= 0)
  89.         break;
  90.     lsym = nsym;
  91.     nsym = cdr(nsym);
  92.     }
  93.  
  94.     /* check to see if we found it */
  95.     if (nsym && cmp == 0)
  96.     return (car(nsym));
  97.  
  98.     /* link the symbol into the oblist */
  99.     oldstk = xlsave(&newsym,NULL);
  100.     newsym.n_ptr = newnode(LIST);
  101.     rplaca(newsym.n_ptr,sym);
  102.     rplacd(newsym.n_ptr,nsym);
  103.     if (lsym)
  104.     rplacd(lsym,newsym.n_ptr);
  105.     else
  106.     oblist->n_symvalue = newsym.n_ptr;
  107.     xlstack = oldstk;
  108.  
  109.     /* return the symbol */
  110.     return (sym);
  111. }
  112.  
  113. /* xlmakesym - make a new symbol node */
  114. NODE *xlmakesym(name,type)
  115.   char *name;
  116. {
  117.     NODE *oldstk,sym,*str;
  118.  
  119.     /* create a new stack frame */
  120.     oldstk = xlsave(&sym,NULL);
  121.  
  122.     /* make a new symbol node */
  123.     sym.n_ptr = newnode(SYM);
  124.     sym.n_ptr->n_symvalue = (*name == ':' ? sym.n_ptr : s_unbound);
  125.     sym.n_ptr->n_symplist = newnode(LIST);
  126.     rplaca(sym.n_ptr->n_symplist,str = newnode(STR));
  127.     str->n_str = (type == DYNAMIC ? strsave(name) : name);
  128.     str->n_strtype = type;
  129.  
  130.     /* restore the previous stack frame */
  131.     xlstack = oldstk;
  132.  
  133.     /* return the new symbol node */
  134.     return (sym.n_ptr);
  135. }
  136.  
  137. /* xlsymname - return the print name of a symbol */
  138. char *xlsymname(sym)
  139.   NODE *sym;
  140. {
  141.     return (car(sym->n_symplist)->n_str);
  142. }
  143.  
  144. /* xlgetprop - get the value of a property */
  145. NODE *xlgetprop(sym,prp)
  146.   NODE *sym,*prp;
  147. {
  148.     NODE *p;
  149.  
  150.     return ((p = findprop(sym,prp)) ? car(p) : NIL);
  151. }
  152.  
  153. /* xlputprop - put a property value onto the property list */
  154. xlputprop(sym,val,prp)
  155.   NODE *sym,*val,*prp;
  156. {
  157.     NODE *oldstk,p,*pair;
  158.  
  159.     if ((pair = findprop(sym,prp)) == NIL) {
  160.     oldstk = xlsave(&p,NULL);
  161.     p.n_ptr = newnode(LIST);
  162.     rplaca(p.n_ptr,prp);
  163.     rplacd(p.n_ptr,pair = newnode(LIST));
  164.     rplaca(pair,val);
  165.     rplacd(pair,cdr(sym->n_symplist));
  166.     rplacd(sym->n_symplist,p.n_ptr);
  167.     xlstack = oldstk;
  168.     }
  169.     rplaca(pair,val);
  170. }
  171.  
  172. /* xlremprop - remove a property from a property list */
  173. xlremprop(sym,prp)
  174.   NODE *sym,*prp;
  175. {
  176.     NODE *last,*p;
  177.  
  178.     last = NIL;
  179.     for (p = cdr(sym->n_symplist); consp(p) && consp(cdr(p)); p = cdr(last)) {
  180.     if (car(p) == prp)
  181.         if (last)
  182.         rplacd(last,cdr(cdr(p)));
  183.         else
  184.         rplacd(sym->n_symplist,cdr(cdr(p)));
  185.     last = cdr(p);
  186.     }
  187. }
  188.  
  189. /* findprop - find a property pair */
  190. LOCAL NODE *findprop(sym,prp)
  191.   NODE *sym,*prp;
  192. {
  193.     NODE *p;
  194.  
  195.     for (p = cdr(sym->n_symplist); consp(p) && consp(cdr(p)); p = cdr(cdr(p)))
  196.     if (car(p) == prp)
  197.         return (cdr(p));
  198.     return (NIL);
  199. }
  200.  
  201. /* xlsinit - symbol initialization routine */
  202. xlsinit()
  203. {
  204.     /* initialize the oblist */
  205.     oblist = xlmakesym("*oblist*",STATIC);
  206.     oblist->n_symvalue = newnode(LIST);
  207.     rplaca(oblist->n_symvalue,oblist);
  208.  
  209.     /* initialize the keyword list */
  210.     keylist = xlsenter("*keylist*");
  211.  
  212.     /* enter the unbound symbol indicator */
  213.     s_unbound = xlsenter("*unbound*");
  214.     s_unbound->n_symvalue = s_unbound;
  215. }
  216.