| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Yap.h"
 | 
					
						
							|  |  |  | #include "Yatom.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** 
 | 
					
						
							|  |  |  |  * Scan a list of arguments and output results to a pre-processed vector.  | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * @param listl: input list | 
					
						
							|  |  |  |  * @param def parameter definition | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * @return all arguments, some of them set, some of them not. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static xarg * | 
					
						
							|  |  |  | matchKey(Atom key, xarg *e0, int n, const param_t *def) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int i; | 
					
						
							|  |  |  |   for (i=0; i< n; i++) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     if (!strcmp((char *)def->name, (char *)RepAtom(key)->StrOfAE)) { | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       return e0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     def++; | 
					
						
							|  |  |  |     e0++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Returns the index of an argument key, or -1 if not found. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | Yap_ArgKey(Atom key, const param_t *def, int n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int i; | 
					
						
							|  |  |  |   for (i=0; i< n; i++) { | 
					
						
							|  |  |  |     if (!strcmp((char *)def->name, (char *)RepAtom(key)->StrOfAE)) { | 
					
						
							|  |  |  |       return i; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     def++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static xarg * | 
					
						
							|  |  |  | failed( yap_error_number e, Term t, xarg *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   free( a );       | 
					
						
							|  |  |  |   LOCAL_Error_TYPE = e; | 
					
						
							|  |  |  |   LOCAL_Error_Term = t; | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  | xarg * | 
					
						
							|  |  |  | Yap_ArgListToVector (Term listl, const param_t *def, int n) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-02-20 01:30:52 +00:00
										 |  |  |   listl = Deref(listl); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     xarg *a = calloc(  n , sizeof(xarg) ); | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |   LOCAL_Error_TYPE = YAP_NO_ERROR; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   if (IsApplTerm(listl) && FunctorOfTerm(listl) == FunctorModule) | 
					
						
							|  |  |  |     listl = ArgOfTerm(2,listl); | 
					
						
							|  |  |  |   if (!IsPairTerm(listl) && listl != TermNil) { | 
					
						
							|  |  |  |     if (IsVarTerm(listl) ) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( INSTANTIATION_ERROR, listl, a);       | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     if (IsAtomTerm(listl) ) { | 
					
						
							|  |  |  |       xarg *na = matchKey( AtomOfTerm(listl), a, n, def); | 
					
						
							|  |  |  |       if (!na) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } else if (IsApplTerm(listl)) { | 
					
						
							|  |  |  |       Functor f = FunctorOfTerm( listl ); | 
					
						
							|  |  |  |       if (IsExtensionFunctor(f)) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       arity_t arity = ArityOfFunctor( f ); | 
					
						
							|  |  |  |       if (arity != 1) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       xarg *na = matchKey( NameOfFunctor( f ), a, n, def); | 
					
						
							|  |  |  |       if (!na) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-02-20 01:30:52 +00:00
										 |  |  |       na->used = true; | 
					
						
							|  |  |  |       na->tvalue = ArgOfTerm(1,listl); | 
					
						
							|  |  |  |       return a; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     listl = MkPairTerm( listl, TermNil ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   while (IsPairTerm(listl)) { | 
					
						
							|  |  |  |     Term hd = HeadOfTerm( listl ); | 
					
						
							|  |  |  |     listl = TailOfTerm( listl ); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     if (IsVarTerm(hd) || IsVarTerm(listl))  { | 
					
						
							|  |  |  |       if (IsVarTerm(hd)) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( INSTANTIATION_ERROR, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( INSTANTIATION_ERROR, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (IsAtomTerm(hd)) { | 
					
						
							|  |  |  |       xarg *na = matchKey( AtomOfTerm( hd ), a, n, def); | 
					
						
							|  |  |  |       if (!na) | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |         return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a); | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       na->used = true; | 
					
						
							|  |  |  |       na->tvalue = TermNil; | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } else if (IsApplTerm( hd )) { | 
					
						
							|  |  |  |       Functor f = FunctorOfTerm( hd ); | 
					
						
							|  |  |  |       if (IsExtensionFunctor(f)) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_PARAMETER, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       arity_t arity = ArityOfFunctor( f ); | 
					
						
							|  |  |  |       if (arity != 1) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( DOMAIN_ERROR_OUT_OF_RANGE, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       xarg *na = matchKey( NameOfFunctor( f ), a, n, def); | 
					
						
							| 
									
										
										
										
											2015-11-05 16:39:34 +00:00
										 |  |  |       if (!na) { | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |           return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a); | 
					
						
							| 
									
										
										
										
											2015-11-05 19:22:40 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-02-20 01:30:52 +00:00
										 |  |  |       na->used = true; | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       na->tvalue = ArgOfTerm(1, hd);       | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( TYPE_ERROR_PARAMETER, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   if (IsVarTerm(listl)) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |     return failed( INSTANTIATION_ERROR, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   } else if (listl != TermNil) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |     return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |   return a; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | }               | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static xarg * | 
					
						
							|  |  |  | matchKey2(Atom key, xarg *e0, int n, const param2_t *def) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int i; | 
					
						
							|  |  |  |   for (i=0; i< n; i++) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     if (!strcmp((char*)def->name, (char*)RepAtom(key)->StrOfAE)) { | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       return e0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     def++; | 
					
						
							|  |  |  |     e0++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | /// Yap_ArgList2ToVector is much the same as before,
 | 
					
						
							|  |  |  | /// but assumes parameters also have something called a
 | 
					
						
							|  |  |  | /// scope
 | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  | xarg * | 
					
						
							|  |  |  | Yap_ArgList2ToVector (Term listl, const param2_t *def, int n) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |     xarg *a = calloc(  n , sizeof(xarg) ); | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |   if (!IsPairTerm(listl) && listl != TermNil) { | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     if (IsVarTerm(listl) ) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( INSTANTIATION_ERROR, listl, a);       | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     if (IsAtomTerm(listl) ) { | 
					
						
							|  |  |  |       xarg *na = matchKey2( AtomOfTerm(listl), a, n, def); | 
					
						
							|  |  |  |       if (!na) { | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |         return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, listl, a); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (IsApplTerm(listl)) { | 
					
						
							|  |  |  |       Functor f = FunctorOfTerm( listl ); | 
					
						
							|  |  |  |       if (IsExtensionFunctor(f)) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_PARAMETER, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       arity_t arity = ArityOfFunctor( f ); | 
					
						
							|  |  |  |       if (arity != 1) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       xarg *na = matchKey2( NameOfFunctor( f ), a, n, def); | 
					
						
							|  |  |  |       if (!na) { | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |         return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, listl, a); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     listl = MkPairTerm( listl, TermNil ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   while (IsPairTerm(listl)) { | 
					
						
							|  |  |  |     Term hd = HeadOfTerm( listl ); | 
					
						
							|  |  |  |     if (IsVarTerm(hd))  { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( INSTANTIATION_ERROR, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (IsAtomTerm(hd)) { | 
					
						
							|  |  |  |       xarg *na = matchKey2( AtomOfTerm( hd ), a, n, def); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       if (!na) { | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |         return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       na->used = true; | 
					
						
							|  |  |  |       na->tvalue = TermNil; | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } else if (IsApplTerm( hd )) { | 
					
						
							|  |  |  |       Functor f = FunctorOfTerm( hd ); | 
					
						
							|  |  |  |       if (IsExtensionFunctor(f)) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |         return failed( TYPE_ERROR_PARAMETER, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       arity_t arity = ArityOfFunctor( f ); | 
					
						
							|  |  |  |       if (arity != 1) { | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |         return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a); | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       xarg *na = matchKey2( NameOfFunctor( f ), a, n, def); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       if (na) { | 
					
						
							|  |  |  | 	na->used = 1; | 
					
						
							|  |  |  | 	na->tvalue = ArgOfTerm(1, hd); | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2016-02-13 02:53:18 +00:00
										 |  |  |         return failed( DOMAIN_ERROR_GENERIC_ARGUMENT, hd, a); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |       return failed( INSTANTIATION_ERROR, hd, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     listl = TailOfTerm(listl); | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (IsVarTerm(listl))  { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |     return failed( INSTANTIATION_ERROR, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-10-08 02:11:10 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   if (TermNil != listl) { | 
					
						
							| 
									
										
										
										
											2016-02-11 05:57:03 -08:00
										 |  |  |     return failed( TYPE_ERROR_LIST, listl, a);       | 
					
						
							| 
									
										
										
										
											2015-06-18 00:16:07 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   return a; | 
					
						
							|  |  |  | }		 |