home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / plbin.zip / pl / src / pl-funct.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  5KB  |  211 lines

  1. /*  pl-funct.c,v 1.1.1.1 1992/05/26 11:52:19 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     See ../LICENCE to find out about your rights.
  5.     jan@swi.psy.uva.nl
  6.  
  7.     Purpose: Functor (re) allocation
  8. */
  9.  
  10. #include "pl-incl.h"
  11.  
  12. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. Functor (name/arity) handling.  A functor is a unique object (like atoms).
  14. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  15.  
  16. static FunctorDef functorDefTable[FUNCTORHASHSIZE];
  17.  
  18. FunctorDef
  19. lookupFunctorDef(atom, arity)
  20. register Atom atom;
  21. register int arity;
  22. { int v = pointerHashValue(atom, FUNCTORHASHSIZE);
  23.   register FunctorDef f;
  24.  
  25.   DEBUG(9, printf("Lookup functor %s/%d = ", stringAtom(atom), arity));
  26.   for(f = functorDefTable[v]; f && !isRef((word)f); f = f->next)
  27.   { if (atom == f->name && f->arity == arity)
  28.     { DEBUG(9, printf("%D (old)\n", f));
  29.       return f;
  30.     }
  31.   }
  32.   f = (FunctorDef) allocHeap(sizeof(struct functorDef));
  33.   f->next = functorDefTable[v];
  34.   f->type = FUNCTOR_TYPE;
  35.   f->name = atom;
  36.   f->arity = arity;
  37.   functorDefTable[v] = f;
  38.   statistics.functors++;
  39.  
  40.   DEBUG(9, printf("%D (new)\n", f));
  41.  
  42.   return f;
  43. }
  44.  
  45.  
  46. int
  47. atomIsFunctor(atom)
  48. Atom atom;
  49. { int v = pointerHashValue(atom, FUNCTORHASHSIZE);
  50.   FunctorDef f;
  51.  
  52.   for(f = functorDefTable[v]; f && !isRef((word)f); f = f->next)
  53.   { if ( atom == f->name )
  54.       return f->arity;
  55.   }
  56.  
  57.   return -1;
  58. }
  59.  
  60.  
  61. FunctorDef
  62. isCurrentFunctor(atom, arity)
  63. Atom atom;
  64. int arity;
  65. { int v = pointerHashValue(atom, FUNCTORHASHSIZE);
  66.   FunctorDef f;
  67.  
  68.   for(f = functorDefTable[v]; f && !isRef((word)f); f = f->next)
  69.   { if (atom == f->name && f->arity == arity)
  70.       return f;
  71.   }
  72.  
  73.   return (FunctorDef) NULL;
  74. }
  75.  
  76.  
  77. bool
  78. atomIsProcedureModule(atom, m)
  79. Atom atom;
  80. Module m;
  81. { int v = pointerHashValue(atom, FUNCTORHASHSIZE);
  82.   FunctorDef f;
  83.   Procedure proc;
  84.  
  85.   for(f = functorDefTable[v]; f && !isRef((word)f); f = f->next)
  86.   { if ( atom == f->name &&
  87.      (proc = isCurrentProcedure(f, m)) != (Procedure)NULL &&
  88.      isDefinedProcedure(proc) )
  89.       succeed;
  90.   }
  91.  
  92.   fail;
  93. }
  94.  
  95.  
  96. bool
  97. atomIsProcedure(atom)
  98. Atom atom;
  99. { Symbol s;
  100.  
  101.   for_table(s, moduleTable)
  102.     if ( atomIsProcedureModule(atom, (Module)s->value) )         
  103.       succeed;
  104.  
  105.   fail;
  106. }
  107.  
  108.  
  109. struct functorDef functors[] = {
  110. #include "pl-funct.ic"
  111. { (FunctorDef)NULL,    FUNCTOR_TYPE,    (Atom) NULL, 0 }
  112. };
  113.  
  114. void
  115. initFunctors()
  116. { register int n;
  117.  
  118.   { register FunctorDef *f;
  119.     for(n=0, f=functorDefTable; n < (FUNCTORHASHSIZE-1); n++, f++)
  120.       *f = (FunctorDef)makeRef(f+1);
  121.     *f = (FunctorDef) NULL;
  122.   }
  123.  
  124.   { register FunctorDef f;
  125.     register int v;
  126.  
  127.     for( f = &functors[0]; f->name; f++ )
  128.     { v = pointerHashValue(f->name, FUNCTORHASHSIZE);
  129.       f->next = functorDefTable[v];
  130.       functorDefTable[v] = f;
  131.       statistics.functors++;
  132.     }
  133.   }
  134. }
  135.  
  136. #if TEST
  137. checkFunctors()
  138. { register FunctorDef f;
  139.   int n;
  140.  
  141.   for( n=0; n < FUNCTORHASHSIZE; n++ )
  142.   { f = functorDefTable[n];
  143.     for( ;f && !isRef((word)f); f = f->next )
  144.     { if ( f->type != FUNCTOR_TYPE )
  145.         printf("[ERROR: Functor %D has bad type: %D]\n", f, f->type);
  146.       if ( f->arity < 0 || f->arity > 10 )    /* debugging only ! */
  147.         printf("[ERROR: Functor %D has dubious arity: %d]\n", f, f->arity);
  148.       if ( !inCore(f->name) || f->name->type != ATOM_TYPE )
  149.         printf("[ERROR: Functor %D has illegal name: %D]\n", f, f->name);
  150.       if ( !( f->next == (FunctorDef) NULL ||
  151.           isRef((word)f->next) ||
  152.           inCore(f->next)) )
  153.     printf("[ERROR: Functor %D has illegal next: %D]\n", f, f->next);
  154.     }
  155.     if ( (isRef((word)f) &&
  156.      ((FunctorDef *) unRef((word)f) != &functorDefTable[n+1])) )
  157.       printf("[ERROR: Bad continuation pointer (fDef, n=%d)]\n", n);
  158.     if ( f == (FunctorDef) NULL && n != (FUNCTORHASHSIZE-1) )
  159.       printf("[ERROR: illegal end pointer (fDef, n=%d)]\n", n);
  160.   }
  161. }
  162. #endif
  163.  
  164. word
  165. pl_current_functor(name, arity, h)
  166. Word name, arity;
  167. word h;
  168. { FunctorDef fdef;
  169.  
  170.   switch( ForeignControl(h) )
  171.   { case FRG_FIRST_CALL:
  172.       if ( (!isAtom(*name) && !isVar(*name))
  173.     || (!isInteger(*arity) && !isVar(*arity)))
  174.     return warning("current_functor/2: instantiation fault");
  175.  
  176.       if (isInteger(*arity) && isAtom(*name))
  177.     if (isCurrentFunctor((Atom)*name, (int)valNum(*arity)) != (FunctorDef) NULL)
  178.       succeed;
  179.     else
  180.       fail;
  181.  
  182.       fdef = functorDefTable[0];
  183.       break;
  184.     case FRG_REDO:
  185.       fdef = (FunctorDef) ForeignContextAddress(h);
  186.       break;
  187.     case FRG_CUTTED:
  188.     default:
  189.       succeed;
  190.   }
  191.  
  192.   DEBUG(9, printf("current_functor(): fdef = %D\n", fdef));
  193.   for(; fdef; fdef = fdef->next)
  194.   { while( isRef((word)fdef) )
  195.     { fdef = *((FunctorDef *)unRef(fdef));
  196.       if (fdef == (FunctorDef) NULL)
  197.     fail;
  198.     }
  199.     if (arity == 0)
  200.       continue;
  201.     if ( unifyAtomic(name, fdef->name) == FALSE ||
  202.      unifyAtomic(arity, consNum(fdef->arity)) == FALSE)
  203.       continue;
  204.     DEBUG(9, printf("Returning backtrack point %D\n", fdef->next));
  205.  
  206.     return_next_table(FunctorDef, fdef);
  207.   }
  208.  
  209.   fail;
  210. }
  211.