| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /* xsswi.c  */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * Project: jpl for Yap Prolog | 
					
						
							|  |  |  | * Author: Steve Moyle and Vitor Santos Costa | 
					
						
							|  |  |  | * Email:  steve.moyle@comlab.ox.ac.uk | 
					
						
							|  |  |  | * Date:   21 January 2002 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * Copyright (c) 2002-2014 Vitor Santos Costa from an original version by Steve Moyle.  All rights reserved. | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | *   @file swi.c | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | *   @addtogroup swi-c-interface | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * @{ | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-27 11:49:42 +01:00
										 |  |  | #define PL_KERNEL 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | //=== includes ===============================================================
 | 
					
						
							|  |  |  | #include	<stdlib.h>
 | 
					
						
							|  |  |  | #include	<string.h>
 | 
					
						
							|  |  |  | #include	<stdio.h>
 | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | #include	<wchar.h>
 | 
					
						
							|  |  |  | #include	<assert.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | #include	<Yap.h>
 | 
					
						
							|  |  |  | #include	<Yatom.h>
 | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  | #include	<eval.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | #include	<YapHeap.h>
 | 
					
						
							|  |  |  | #include	<yapio.h>
 | 
					
						
							|  |  |  | #include	<YapText.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "swi.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | #if HAVE_MATH_H
 | 
					
						
							|  |  |  | #include	<math.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-30 12:52:23 -05:00
										 |  |  | #if HAVE_ERRNO_H
 | 
					
						
							|  |  |  | #include	<errno.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-11-04 21:41:49 +00:00
										 |  |  | #if HAVE_SIGNAL_H
 | 
					
						
							|  |  |  | #include	<signal.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | #if !HAVE_SNPRINTF
 | 
					
						
							|  |  |  | #define snprintf(X,Y,Z,A) sprintf(X,Z,A)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-21 08:29:28 -05:00
										 |  |  | #define PL_KERNEL 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-29 23:04:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 21:50:58 +00:00
										 |  |  | #ifdef USE_GMP
 | 
					
						
							|  |  |  | #include <gmp.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-22 01:59:48 +00:00
										 |  |  | #ifdef __WIN32__
 | 
					
						
							|  |  |  | /* Windows */ | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | //#include "pl-error.h"
 | 
					
						
							| 
									
										
										
										
											2013-11-21 00:22:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-11 06:20:16 -08:00
										 |  |  | static atom_t ATOM_nil; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 00:22:03 +00:00
										 |  |  | extern int		PL_unify_termv(term_t l, va_list args); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  | extern X_API Atom YAP_AtomFromSWIAtom(atom_t at); | 
					
						
							| 
									
										
										
										
											2011-03-02 23:19:39 +00:00
										 |  |  | extern X_API atom_t YAP_SWIAtomFromAtom(Atom at); | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  | do_gc(UInt sz) | 
					
						
							| 
									
										
										
										
											2012-04-18 00:57:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   /* always called from user_call_cpred */ | 
					
						
							| 
									
										
										
										
											2012-04-23 18:57:10 +01:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2012-04-18 00:57:43 +02:00
										 |  |  |   UInt arity; | 
					
						
							|  |  |  |   yamop *nextpc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (P && PREVOP(P,Osbpp)->opc == Yap_opcode(_call_usercpred)) { | 
					
						
							| 
									
										
										
										
											2014-05-30 01:06:09 +01:00
										 |  |  |     arity = PREVOP(P,Osbpp)->y_u.Osbpp.p->ArityOfPE; | 
					
						
							| 
									
										
										
										
											2012-04-18 00:57:43 +02:00
										 |  |  |     nextpc = P; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     arity = 0; | 
					
						
							|  |  |  |     nextpc = CP; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |   return Yap_gcl(sz, arity, ENV, nextpc); | 
					
						
							| 
									
										
										
										
											2012-04-18 00:57:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  | X_API extern Atom | 
					
						
							|  |  |  | YAP_AtomFromSWIAtom(atom_t at) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return SWIAtomToAtom(at); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-02 23:19:39 +00:00
										 |  |  | X_API extern atom_t | 
					
						
							|  |  |  | YAP_SWIAtomFromAtom(Atom at) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return AtomToSWIAtom(at); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-28 17:13:10 +01:00
										 |  |  | extern X_API Int YAP_PLArityOfSWIFunctor(functor_t at); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-02 19:41:48 +00:00
										 |  |  | /* This is silly, but let's keep it like that for now */ | 
					
						
							| 
									
										
										
										
											2010-07-28 17:13:10 +01:00
										 |  |  | X_API Int | 
					
						
							| 
									
										
										
										
											2010-12-02 12:10:03 +00:00
										 |  |  | YAP_PLArityOfSWIFunctor(functor_t f) { | 
					
						
							|  |  |  |   if (IsAtomTerm(f)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2010-12-02 12:10:03 +00:00
										 |  |  |   return ArityOfFunctor((Functor)f); | 
					
						
							| 
									
										
										
										
											2010-07-28 17:13:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2015-01-18 01:32:13 +00:00
										 |  |  | UserCPredicate(char *a, CPredicate def, unsigned long int arity, Term mod, int flags) | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-11-16 15:20:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   Term cm = CurrentModule; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   /* fprintf(stderr,"doing %s:%s/%d\n", RepAtom(AtomOfTerm(mod))->StrOfAE, a,arity); */ | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   CurrentModule = mod; | 
					
						
							| 
									
										
										
										
											2011-03-26 15:17:17 +00:00
										 |  |  |   Yap_InitCPred(a, arity, def, (UserCPredFlag|CArgsPredFlag|flags)); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   CurrentModule = cm; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | //!  @{
 | 
					
						
							| 
									
										
										
										
											2015-01-04 23:58:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @defgroup swi-ATOMS Atom Construction
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | *  @ingroup swi-c-interface | 
					
						
							|  |  |  | *  */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | static UInt | 
					
						
							|  |  |  | cvtFlags( unsigned flags ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   UInt inptype = 0; | 
					
						
							|  |  |  |   if (flags & CVT_ATOM) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_ATOM; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_STRING) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_STRING; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_LIST) { | 
					
						
							|  |  |  |     inptype |= (YAP_STRING_CODES|YAP_STRING_ATOMS); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_INTEGER) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_INT|YAP_STRING_BIG; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_FLOAT) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_FLOAT; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_VARIABLE) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_TERM; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_WRITE) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_TERM; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_WRITEQ) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_TERM|YAP_STRING_WQ; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & CVT_WRITE_CANONICAL) { | 
					
						
							|  |  |  |     inptype |= YAP_STRING_TERM|YAP_STRING_WC; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return inptype; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  void PL_agc_hook(void) */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief Atom garbage collection hook
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API PL_agc_hook_t | 
					
						
							|  |  |  | PL_agc_hook(PL_agc_hook_t entry) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  |   return (PL_agc_hook_t)YAP_AGCRegisterHook((YAP_agc_hook)entry); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  void PL_get_nchars(term_t ref, size_t *length, char **output, unsigned flags) */ | 
					
						
							|  |  |  | /** @brief extract a text representing the term _ref_. A pointer to a string with the text will
 | 
					
						
							|  |  |  | * be output to *_s_, and the size of the string will be written to *_length_, | 
					
						
							|  |  |  | * if _length_ is not null. | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * The following flags are recognised (as in the SWI manual ) | 
					
						
							|  |  |  | *   *CVT_ATOM* Convert if term is an atom. | 
					
						
							|  |  |  | *   *CVT_STRING* Convert if term is a string. | 
					
						
							|  |  |  | *   *CVT_LIST* Convert if term is a list of of character codes. | 
					
						
							|  |  |  | *   *CVT_INTEGER* Convert if term is an integer. | 
					
						
							|  |  |  | *   *CVT_FLOAT* Convert if term is a float. The characters returned are the same as write/1 would write for the floating point number. | 
					
						
							|  |  |  | *   *CVT_NUMBER* Convert if term is an integer or float. | 
					
						
							|  |  |  | *   *CVT_ATOMIC* Convert if term is atomic. | 
					
						
							|  |  |  | *   *CVT_VARIABLE* Convert variable to print-name | 
					
						
							|  |  |  | *   *CVT_WRITE* Convert any term that is not converted by any of the other flags using write/1. * If no BUF_* is provided, BUF_RING is implied. | 
					
						
							|  |  |  | *   *CVT_WRITE_CANONICAL* As CVT_WRITE, but using write_canonical/2. | 
					
						
							|  |  |  | *   *CVT_WRITEQ* As CVT_WRITE, but using writeq/2. | 
					
						
							|  |  |  | *   *CVT_ALL* Convert if term is any of the above, except for CVT_VARIABLE and CVT_WRITE*. | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | *   *CVT_EXCEPTION* If conversion fails due to a type error, raise a Prolog type error exception in addition to failure | 
					
						
							|  |  |  | *   *BUF_DISCARDABLE* Data must copied immediately | 
					
						
							|  |  |  | *   *BUF_RING* Data is stored in a ring of buffers, currenty implemented as BUF_DISCARDABLE | 
					
						
							|  |  |  | *   *BUF_MALLOC* Data is copied to a new buffer returned by PL_malloc(3). When no longer needed the user must call PL_free() on the data. | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | *   *REP_ISO_LATIN_1 | 
					
						
							|  |  |  | Text is in ISO Latin-1 encoding and the call fails if text cannot be represented. This flag has the value 0 and is thus the default. | 
					
						
							|  |  |  | *   *REP_UTF8* Convert the text to a UTF-8 string. This works for all text. | 
					
						
							|  |  |  | *   *REP_MB* Convert the text using the current locale | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_get_nchars(term_t l, size_t *lengthp, char **s, unsigned flags) | 
					
						
							|  |  |  | { CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-02-11 06:20:16 -08:00
										 |  |  |   seq_tv_t inp, out; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   inp.val.t = Yap_GetFromSlot( l ); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   inp.type = cvtFlags( flags ); | 
					
						
							|  |  |  |   out.type = YAP_STRING_CHARS; | 
					
						
							|  |  |  |   if (flags & (REP_UTF8|REP_MB)) { | 
					
						
							|  |  |  |     out.enc = ENC_ISO_UTF8; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     out.enc = ENC_ISO_LATIN1; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   if (flags & BUF_MALLOC) | 
					
						
							|  |  |  |     out.type |= YAP_STRING_MALLOC; | 
					
						
							|  |  |  |   if (!Yap_CVT_Text(&inp, &out PASS_REGS)) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   *s = out.val.c; | 
					
						
							|  |  |  |   if (lengthp) | 
					
						
							|  |  |  |     *lengthp = out.sz; | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PL_get_chars(term_t t, char **s, unsigned flags) | 
					
						
							|  |  |  | { return PL_get_nchars(t, NULL, s, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int PL_get_wchars(term_t l, size_t *lengthp, wchar_t **s, unsigned flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-02-11 06:20:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     seq_tv_t inp, out; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   inp.val.t = Yap_GetFromSlot( l ); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   inp.type = cvtFlags( flags ); | 
					
						
							|  |  |  |   out.type = YAP_STRING_WCHARS; | 
					
						
							|  |  |  |    if (flags & BUF_MALLOC) | 
					
						
							|  |  |  |     out.type |= YAP_STRING_MALLOC; | 
					
						
							|  |  |  |   if (!Yap_CVT_Text(&inp, &out PASS_REGS)) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   *s = out.val.w; | 
					
						
							|  |  |  |   if (lengthp) | 
					
						
							|  |  |  |     *lengthp = out.sz; | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_unify_chars(term_t l, int flags, size_t length, const char *s) | 
					
						
							|  |  |  | { CACHE_REGS | 
					
						
							|  |  |  |   seq_tv_t inp, out; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (flags & REP_UTF8) { | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     inp.val.c0 = s; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     if (length != (size_t)-1) { | 
					
						
							|  |  |  |       inp.sz = length; | 
					
						
							|  |  |  |       inp.type = YAP_STRING_CHARS|YAP_STRING_NCHARS; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       inp.enc = ENC_ISO_UTF8; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       inp.type = YAP_STRING_CHARS; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       inp.enc = ENC_ISO_LATIN1; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & PL_ATOM) { | 
					
						
							|  |  |  |     out.type = YAP_STRING_ATOM; | 
					
						
							|  |  |  |   } else if (flags & PL_STRING) { | 
					
						
							|  |  |  |     out.type  = YAP_STRING_STRING; | 
					
						
							|  |  |  |   } else if (flags & PL_CODE_LIST) { | 
					
						
							|  |  |  |     out.type  = YAP_STRING_CODES; | 
					
						
							|  |  |  |   } else if (flags & PL_CHAR_LIST) { | 
					
						
							|  |  |  |     out.type  = YAP_STRING_ATOMS; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (length != (size_t)-1) { | 
					
						
							|  |  |  |     out.max = length; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!Yap_CVT_Text(&inp, &out PASS_REGS)) | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     return 0L; | 
					
						
							|  |  |  |   return Yap_unify( Yap_GetFromSlot(l), out.val.t ); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief extract the text representation from atom
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API char* PL_atom_chars(atom_t a)	 /* SAM check type */ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-28 18:21:11 +00:00
										 |  |  |   Atom at = SWIAtomToAtom(a); | 
					
						
							|  |  |  |   if (IsWideAtom(at)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2012-10-28 18:21:11 +00:00
										 |  |  |   return RepAtom(at)->StrOfAE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief extract the text representation from atom, including its length
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2010-06-18 09:14:45 +01:00
										 |  |  | X_API char* PL_atom_nchars(atom_t a, size_t *len)	 /* SAM check type */ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *s = RepAtom(SWIAtomToAtom(a))->StrOfAE; | 
					
						
							|  |  |  |   *len = strlen(s); | 
					
						
							|  |  |  |   return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 23:58:23 +00:00
										 |  |  | //!  @}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @{
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | * @defgroup swi-term_references Term References | 
					
						
							|  |  |  | *  @ingroup swi-c-interface | 
					
						
							|  |  |  | *  */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /** @brief create a clean term reference
 | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | X_API term_t PL_new_term_ref(void) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   term_t to = Yap_NewSlots(1); | 
					
						
							|  |  |  |   return to; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /** @brief duplicate a term reference
 | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | X_API term_t PL_copy_term_ref(term_t from) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return Yap_InitSlot(Yap_GetFromSlot(from)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief create several new term references
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | * @par n is the number of references | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API term_t PL_new_term_refs(int n) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   term_t to = Yap_NewSlots(n); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return to; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief dispose of all term references created since after
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API void PL_reset_term_refs(term_t after) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   term_t new = Yap_NewSlots(1); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_RecoverSlots(after-new, new); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @}
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2015-01-04 23:58:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | //!  @{
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**  @defgroup swi-term_manipulation Term Manipulation
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | *  @ingroup swi-c-interface | 
					
						
							|  |  |  | *  */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | *  @defgroup swi-get-operations Reading Terms | 
					
						
							|  |  |  | *  @ingroup swi-term_manipulation | 
					
						
							|  |  |  | *  */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @brief *name is assigned the name and *arity the arity if term ts, or the operaton fails.
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_name_arity(term_t ts, atom_t *name, int *arity) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if (IsAtomTerm(t)) { | 
					
						
							|  |  |  |     *name = AtomToSWIAtom(AtomOfTerm(t)); | 
					
						
							|  |  |  |     *arity = 0; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (YAP_IsApplTerm(t)) { | 
					
						
							|  |  |  |     Functor f = FunctorOfTerm(t); | 
					
						
							|  |  |  |     if (IsExtensionFunctor(f)) { | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *name = AtomToSWIAtom(NameOfFunctor(f)); | 
					
						
							|  |  |  |     *arity = ArityOfFunctor(f); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (YAP_IsPairTerm(t)) { | 
					
						
							|  |  |  |     *name = AtomToSWIAtom(AtomDot); | 
					
						
							|  |  |  |     *arity = 2; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief a is assigned the argument index from term  ts
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_get_arg(int index, term_t ts, term_t a) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  |   if (IsVarTerm( t )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  |   if ( !IsApplTerm(t) ) { | 
					
						
							|  |  |  |     if (IsPairTerm(t)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |       if (index == 1){ | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |         Yap_PutInSlot(a,HeadOfTerm(t)); | 
					
						
							| 
									
										
										
										
											2014-09-02 14:19:23 -05:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |       } else if (index == 2) { | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |         Yap_PutInSlot(a,TailOfTerm(t)); | 
					
						
							| 
									
										
										
										
											2014-09-02 14:19:23 -05:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2014-09-02 14:19:23 -05:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     Functor f = FunctorOfTerm(t); | 
					
						
							|  |  |  |     if (IsExtensionFunctor(f)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2014-09-02 14:19:23 -05:00
										 |  |  |     if (index < 1 || index > ArityOfFunctor(f)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(a,ArgOfTerm(index, t)); | 
					
						
							| 
									
										
										
										
											2014-09-02 14:19:23 -05:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-16 00:27:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *ap is assigned the name and *ip the arity from term  ts
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  | X_API int PL_get_compound_name_arity(term_t ts, atom_t *ap, int *ip) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |   if ( !YAP_IsApplTerm(t) ) { | 
					
						
							|  |  |  |     if (YAP_IsPairTerm(t)) { | 
					
						
							|  |  |  |       if (ip) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       *ip = 2; | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |       if (ap) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       *ap = ATOM_nil; | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |       return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     Functor f = FunctorOfTerm( t ); | 
					
						
							|  |  |  |     if (IsExtensionFunctor(f)) return FALSE; | 
					
						
							|  |  |  |     if (ip) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *ip = ArityOfFunctor( f ); | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |     if (ap) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *ap = AtomToSWIAtom( NameOfFunctor( f )); | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *a is assigned the atom in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_atom(term_t ts, atom_t *a) | 
					
						
							| 
									
										
										
										
											2013-11-16 00:27:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if ( !IsAtomTerm(t)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   *a = AtomToSWIAtom(AtomOfTerm(t)); | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @brief *i is assigned the int in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | /*  int PL_get_integer(term_t t, int *i)
 | 
					
						
							|  |  |  | YAP: long int  YAP_IntOfTerm(Term) */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_integer(term_t ts, int *i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if (IsVarTerm(t) || !IsIntegerTerm(t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   *i = (int)IntegerOfTerm(t); | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @brief *i is assigned the boolean atom `true` or `false` in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_long(term_t ts, long *i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if (!YAP_IsIntTerm(t) ) { | 
					
						
							|  |  |  |     if (YAP_IsFloatTerm(t)) { | 
					
						
							|  |  |  |       double dbl = YAP_FloatOfTerm(t); | 
					
						
							|  |  |  |       if (dbl - (long)dbl == 0.0) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         *i = (long)dbl; | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2013-11-16 00:27:02 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   *i = YAP_IntOfTerm(t); | 
					
						
							| 
									
										
										
										
											2013-11-16 00:27:02 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_get_bool(term_t t, int *i)
 | 
					
						
							|  |  |  | YAP: long int  YAP_AtomOfTerm(Term) */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_bool(term_t ts, int *i) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   Atom at; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!IsAtomTerm(t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   at = AtomOfTerm(t); | 
					
						
							|  |  |  |   if (at == AtomTrue) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     *i = true; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (at == AtomFalse) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     *i = false; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @brief *a is assigned the int64 in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_int64(term_t ts, int64_t *i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if (!YAP_IsIntTerm(t) ) { | 
					
						
							|  |  |  |     if (YAP_IsFloatTerm(t)) { | 
					
						
							|  |  |  |       double dbl = YAP_FloatOfTerm(t); | 
					
						
							|  |  |  |       if (dbl - (int64_t)dbl == 0.0) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         *i = (int64_t)dbl; | 
					
						
							|  |  |  |         return	 1; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       #if SIZEOF_INT_P==4 && !USE_GMP
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         union { | 
					
						
							|  |  |  |           double d; | 
					
						
							|  |  |  |           int64_t i; | 
					
						
							|  |  |  |         } udbi_; | 
					
						
							|  |  |  |         udbi_.d = YAP_FloatOfTerm(t); | 
					
						
							|  |  |  |         *i = udbi_.i; | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #if USE_GMP
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |     else if (YAP_IsBigNumTerm(t)) { | 
					
						
							|  |  |  |       MP_INT g; | 
					
						
							|  |  |  |       char s[64]; | 
					
						
							|  |  |  |       YAP_BigNumOfTerm(t, (void *)&g); | 
					
						
							|  |  |  |       if (mpz_sizeinbase(&g,2) > 64) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |       mpz_get_str (s, 10, &g); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       sscanf(s, "%I64d", (long long int *)i); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       #else
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       sscanf(s, "%lld", (long long int *)i); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   *i = YAP_IntOfTerm(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | X_API int PL_get_int64_ex(term_t ts, int64_t *i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return PL_get_int64( ts,i); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *a is assigned the intptr_t in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2010-01-15 16:21:43 +00:00
										 |  |  | X_API int PL_get_intptr(term_t ts, intptr_t *a) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2010-01-15 16:21:43 +00:00
										 |  |  |   if ( !IsIntegerTerm(t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2010-01-15 16:21:43 +00:00
										 |  |  |   *a = (intptr_t)(IntegerOfTerm(t)); | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *a is assigned the uintptr_t in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  | X_API int PL_get_uintptr(term_t ts, uintptr_t *a) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   if ( !IsIntegerTerm(t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   *a = (uintptr_t)(IntegerOfTerm(t)); | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | #ifdef do_not_ld
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief a is assigned the argument index from term  ts
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int _PL_get_arg(int index, term_t ts, term_t a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if ( !YAP_IsApplTerm(t) ) { | 
					
						
							|  |  |  |     if (YAP_IsPairTerm(t)) { | 
					
						
							|  |  |  |       if (index == 1){ | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         Yap_PutInSlot(a,HeadOfTerm(t) PASS_REGS); | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       } else if (index == 2) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         Yap_PutInSlot(a,TailOfTerm(t) PASS_REGS); | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   Yap_PutInSlot(a,ArgOfTerm(index, t) PASS_REGS); | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @brief *a is assigned the string representation of the atom in term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_get_atom_chars(term_t ts, char **a)  /* SAM check type */ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2012-10-28 18:21:11 +00:00
										 |  |  |   if (!IsAtomTerm(t) || IsWideAtom(AtomOfTerm(t))) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2008-12-19 11:41:56 +00:00
										 |  |  |   *a = RepAtom(AtomOfTerm(t))->StrOfAE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *a is assigned the string representation of the atom in term  ts, and *len its size, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2010-07-05 16:00:12 +01:00
										 |  |  | X_API int PL_get_atom_nchars(term_t ts, size_t *len, char **s)  /* SAM check type */ | 
					
						
							| 
									
										
										
										
											2010-06-19 14:40:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2010-06-19 14:40:47 +01:00
										 |  |  |   if (!IsAtomTerm(t)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2010-06-19 14:40:47 +01:00
										 |  |  |   *s = RepAtom(AtomOfTerm(t))->StrOfAE; | 
					
						
							|  |  |  |   *len = strlen(*s); | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** PL_get_chars converts a term t to a string.
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | * From the SWI manual: | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * int PL_get_chars(term_t +t, char **s, unsigned flags) Convert the | 
					
						
							|  |  |  | * argument term t to a 0-terminated C-string. flags is a bitwise | 
					
						
							|  |  |  | * disjunction from two groups of constants. The first specifies which | 
					
						
							|  |  |  | * term-types should converted and the second how the argument is | 
					
						
							|  |  |  | * stored. Below is a specification of these constants. BUF_RING | 
					
						
							|  |  |  | * implies, if the data is not static (as from an atom), the data is | 
					
						
							|  |  |  | * copied to the next buffer from a ring of sixteen (16) buffers. This is a | 
					
						
							|  |  |  | * convenient way of converting multiple arguments passed to a foreign | 
					
						
							|  |  |  | * predicate to C-strings. If BUF_MALLOC is used, the data must be | 
					
						
							|  |  |  | * freed using free() when not needed any longer. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - CVT_ATOM Convert if term is an atom | 
					
						
							|  |  |  | - CVT_STRING Convert if term is a string | 
					
						
							|  |  |  | - CVT_LIST Convert if term is a list of integers between 1 and 255 | 
					
						
							|  |  |  | - CVT_INTEGER Convert if term is an integer (using %d) | 
					
						
							|  |  |  | - CVT_FLOAT Convert if term is a float (using %f) | 
					
						
							|  |  |  | - CVT_NUMBER Convert if term is a integer or float | 
					
						
							|  |  |  | - CVT_ATOMIC Convert if term is atomic | 
					
						
							|  |  |  | - CVT_VARIABLE Convert variable to print-name | 
					
						
							|  |  |  | - CVT_ALL Convert if term is any of the above, except for variables | 
					
						
							|  |  |  | - BUF_DISCARDABLE Data must copied immediately | 
					
						
							|  |  |  | - BUF_RING Data is stored in a ring of buffers | 
					
						
							|  |  |  | - BUF_MALLOC Data is copied to a new buffer returned by malloc(3) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *f is assigned the functor of term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_get_functor(term_t ts, functor_t *f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2008-12-19 11:41:56 +00:00
										 |  |  |   if ( IsAtomTerm(t)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     *f = t; | 
					
						
							| 
									
										
										
										
											2015-08-18 15:08:52 -05:00
										 |  |  |   } else if ( IsPairTerm(t)) { | 
					
						
							|  |  |  |     *f = FunctorToSWIFunctor(FunctorDot); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     *f = FunctorToSWIFunctor(FunctorOfTerm(t)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *f is assigned the floating  point number of term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_get_float(term_t ts, double *f) /* SAM type check*/ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |   if ( IsFloatTerm(t)) { | 
					
						
							|  |  |  |     *f = FloatOfTerm(t); | 
					
						
							|  |  |  |   } else if ( IsIntegerTerm(t)) { | 
					
						
							|  |  |  |     *f = IntegerOfTerm(t); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #if USE_GMP
 | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |   } else if (IsBigIntTerm(t)) { | 
					
						
							|  |  |  |     *f = Yap_gmp_to_float( t ); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #endif
 | 
					
						
							| 
									
										
										
										
											2010-08-04 02:46:26 +01:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2010-08-04 02:46:26 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | /** @brief *s is assigned the string representation of the term  ts, and *len its size, or the operation fails
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | X_API int PL_get_string_chars(term_t t, char **s, size_t *len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return PL_get_string(t, s, len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief *s is assigned the string representation of the string in term  ts, and *len its size, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | X_API int PL_get_string(term_t t, char **s, size_t *len) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term tt = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   if (!IsStringTerm(tt)) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     const unsigned char  *s0; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     s0 = UStringOfTerm(tt) ; | 
					
						
							|  |  |  |     if(s) | 
					
						
							|  |  |  |       *s = (char *)s0; | 
					
						
							|  |  |  |     if (len) { | 
					
						
							|  |  |  |       *len = strlen_utf8(s0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief h is assigned the head of the pair term  ts, and tl its tail, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_list(term_t ts, term_t h, term_t tl) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if (IsVarTerm(t) || !IsPairTerm(t) ) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(h,HeadOfTerm(t)); | 
					
						
							|  |  |  |   Yap_PutInSlot(tl,TailOfTerm(t)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-30 17:54:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief h is assigned the head of the pair term  ts, or the operation fails
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | X_API int PL_get_head(term_t ts, term_t h) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   if (!YAP_IsPairTerm(t) ) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(h,YAP_HeadOfTerm(t)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | *  @} | 
					
						
							|  |  |  | *  */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 23:58:23 +00:00
										 |  |  | //!  @{
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | *  @defgroup swi-unify-operations Unifying Terms | 
					
						
							|  |  |  | *  @ingroup swi-term_manipulation | 
					
						
							| 
									
										
										
										
											2015-01-04 23:58:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | *  */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  | /** @brief t unifies with the true/false value in a.
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  | X_API int PL_unify_bool(term_t t, int a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Term iterm = (a ? MkAtomTerm(AtomTrue) : MkAtomTerm(AtomFalse) ); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t ),iterm); | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | #if USE_GMP
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*******************************
 | 
					
						
							|  |  |  | *	       GMP		* | 
					
						
							|  |  |  | *******************************/ | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_get_mpz(term_t t, mpz_t mpz) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t0 = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return Yap_term_to_existing_big(t0, mpz); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_unify_mpz(term_t t, mpz_t mpz) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  |   Term iterm = Yap_MkBigIntTerm(mpz); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_get_mpq(term_t t, mpq_t mpz) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t0 = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return Yap_term_to_existing_rat(t0, mpz); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_unify_mpq(term_t t, mpq_t mpq) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  |   Term iterm = Yap_MkBigRatTerm(mpq); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_get_module(term_t t, module_t *m) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_get_module(term_t ts, module_t *m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   if (!IsAtomTerm(t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2013-11-13 10:38:20 +00:00
										 |  |  |   *m = Yap_GetModuleEntry(t); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_new_module(term_t t, module_t *m) */ | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | X_API module_t PL_new_module(atom_t swiat) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   Atom at = SWIAtomToAtom(swiat); | 
					
						
							|  |  |  |   Term t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   WRITE_LOCK(RepAtom(at)->ARWLock); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   t = Yap_Module(MkAtomTerm(at)); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   WRITE_UNLOCK(RepAtom(at)->ARWLock); | 
					
						
							| 
									
										
										
										
											2013-11-13 10:38:20 +00:00
										 |  |  |   return Yap_GetModuleEntry(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_get_atom(term_t t, YAP_Atom *a)
 | 
					
						
							|  |  |  | YAP: YAP_Atom YAP_AtomOfTerm(Term) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_get_nil(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   return ( t == TermNil ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_get_pointer(term_t t, int *i)
 | 
					
						
							|  |  |  | YAP: NO EQUIVALENT */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /* SAM TO DO */ | 
					
						
							|  |  |  | X_API int PL_get_pointer(term_t ts, void **i) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2012-02-12 12:28:37 +00:00
										 |  |  |   if (IsVarTerm(t) || !IsIntegerTerm(t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2012-02-12 12:28:37 +00:00
										 |  |  |   *i = (void *)IntegerOfTerm(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_get_tail(term_t ts, term_t tl) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   if (!YAP_IsPairTerm(t) ) { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(tl,YAP_TailOfTerm(t) ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* end PL_get_* functions  =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* begin PL_new_* functions =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  atom_t PL_new_atom(const char *)
 | 
					
						
							|  |  |  | YAP: YAP_Atom LookupAtom(char *) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /*  SAM should the following be used instead?
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | YAP_Atom  FullLookupAtom(char *) | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API atom_t PL_new_atom(const char *c) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   atom_t sat; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((at = Yap_CharsToAtom(c, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_new_atom" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-12-21 10:12:47 -02:00
										 |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   sat = AtomToSWIAtom(at); | 
					
						
							|  |  |  |   return sat; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API atom_t PL_new_atom_nchars(size_t len, const char *c) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   atom_t sat; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((at = Yap_NCharsToAtom(c, len, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_new_atom_nchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   sat = AtomToSWIAtom(at); | 
					
						
							|  |  |  |   return sat; | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API atom_t PL_new_atom_wchars(size_t len, const wchar_t *c) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Atom at; | 
					
						
							|  |  |  |   atom_t sat; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   while((at = Yap_NWCharsToAtom(c, len PASS_REGS)) == 0L) { | 
					
						
							|  |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_new_atom_wchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2007-09-27 15:25:34 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							|  |  |  |   sat = AtomToSWIAtom(at); | 
					
						
							|  |  |  |   return sat; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API wchar_t *PL_atom_wchars(atom_t name, size_t *sp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   Atom at = SWIAtomToAtom(name); | 
					
						
							|  |  |  |   if (!IsWideAtom(at)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   *sp = wcslen(RepAtom(at)->WStrOfAE); | 
					
						
							|  |  |  |   return RepAtom(at)->WStrOfAE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API functor_t PL_new_functor(atom_t name, int arity) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   functor_t f; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   Atom at = SWIAtomToAtom(name); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   if (arity == 0) { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     f = FunctorToSWIFunctor((Functor)MkAtomTerm(at)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     f = FunctorToSWIFunctor(Yap_MkFunctor(at,arity)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API atom_t PL_functor_name(functor_t f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   if (IsAtomTerm(f)) { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     return AtomToSWIAtom(AtomOfTerm((Term)SWIFunctorToFunctor(f))); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     return AtomToSWIAtom(NameOfFunctor(SWIFunctorToFunctor(f))); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_functor_arity(functor_t f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-19 11:41:56 +00:00
										 |  |  |   if (IsAtomTerm(f)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     return ArityOfFunctor(SWIFunctorToFunctor(f)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* end PL_new_* functions =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* begin PL_put_* functions =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_cons_functor(term_t d, functor_t f,...) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   va_list ap; | 
					
						
							|  |  |  |   int arity, i; | 
					
						
							| 
									
										
										
										
											2010-11-28 11:50:41 +00:00
										 |  |  |   Term *tmp, t; | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   Functor ff = SWIFunctorToFunctor(f); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   if (IsAtomTerm((Term)ff)) { | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(d, (YAP_Term)f ); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |     return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   arity = ArityOfFunctor(ff); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (Unsigned(HR)+arity > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |     if (!do_gc(arity*sizeof(CELL))) { | 
					
						
							| 
									
										
										
										
											2010-11-28 11:50:41 +00:00
										 |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (arity == 2 && ff == FunctorDot) { | 
					
						
							|  |  |  |     t = Yap_MkNewPairTerm(); | 
					
						
							|  |  |  |     tmp = RepPair(t); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     t = Yap_MkNewApplTerm(ff, arity); | 
					
						
							|  |  |  |     tmp = RepAppl(t)+1; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   va_start (ap, f); | 
					
						
							|  |  |  |   for (i = 0; i < arity; i++) { | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     Yap_unify(tmp[i],Yap_GetFromSlot(va_arg(ap, term_t))); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   va_end (ap); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(d,t ); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-28 11:50:41 +00:00
										 |  |  | X_API int PL_cons_functor_v(term_t d, functor_t f, term_t a0) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-11-28 11:50:41 +00:00
										 |  |  |   int arity, i; | 
					
						
							|  |  |  |   Term *tmp, t; | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   Functor ff = SWIFunctorToFunctor(f); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   if (IsAtomTerm((Term)ff)) { | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(d, (YAP_Term)f ); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |     return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   arity = ArityOfFunctor(ff); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (Unsigned(HR) > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |     if (!do_gc(0)) { | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-11-28 11:50:41 +00:00
										 |  |  |   if (arity == 2 && ff == FunctorDot) { | 
					
						
							|  |  |  |     t = Yap_MkNewPairTerm(); | 
					
						
							|  |  |  |     tmp = RepPair(t); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     t = Yap_MkNewApplTerm(ff, arity); | 
					
						
							|  |  |  |     tmp = RepAppl(t)+1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (i = 0; i < arity; i++) { | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     Yap_unify(tmp[i] , Yap_GetFromSlot(a0 ) ); | 
					
						
							| 
									
										
										
										
											2010-11-28 11:50:41 +00:00
										 |  |  |     a0++; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(d,t ); | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_cons_list(term_t d, term_t h, term_t t) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (Unsigned(HR) > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-10-09 16:30:18 +01:00
										 |  |  |     if (!do_gc(0)) { | 
					
						
							|  |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(d,MkPairTerm(Yap_GetFromSlot(h),Yap_GetFromSlot(t))); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_atom(term_t t, atom_t a) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,MkAtomTerm(SWIAtomToAtom(a))); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_atom_chars(term_t t, const char *s) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((at = Yap_CharsToAtom(s, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_atom_nchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-15 11:21:38 -06:00
										 |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,MkAtomTerm(at)); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  | X_API int PL_put_atom_nchars(term_t t, size_t len, const char *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((at = Yap_NCharsToAtom(s, len, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_atom_nchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-15 11:21:38 -06:00
										 |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,MkAtomTerm(at)); | 
					
						
							| 
									
										
										
										
											2010-06-18 23:24:36 +01:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_float(term_t t, double fl) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,YAP_MkFloatTerm(fl) ); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_functor(term_t t, functor_t f) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   long int  arity; | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   Functor ff = SWIFunctorToFunctor(f); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   if (IsAtomTerm((Term)ff)) { | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(t,(Term)ff); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     arity = ArityOfFunctor(ff); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     if (Unsigned(HR)+arity > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |       if (!do_gc(arity*sizeof(CELL))) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return FALSE; | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     if (arity == 2 && ff == FunctorDot){ | 
					
						
							|  |  |  |     } else | 
					
						
							|  |  |  |     Yap_PutInSlot(t,YAP_MkNewApplTerm((YAP_Functor)ff,arity) ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_integer(term_t t, long n) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(t,YAP_MkIntTerm(n)); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_put_boolean(term_t t, uintptr_t n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,(n==0?TermFalse:TermTrue)); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_int64(term_t t, int64_t n) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if SIZEOF_INT_P==8
 | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,MkIntegerTerm(n) ); | 
					
						
							| 
									
										
										
										
											2011-11-03 07:45:51 +09:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #elif USE_GMP
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   char s[64]; | 
					
						
							|  |  |  |   MP_INT rop; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2010-02-10 03:03:03 -06:00
										 |  |  |   snprintf(s, 64, "%I64d", (long long int)n); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #elif HAVE_SNPRINTF
 | 
					
						
							| 
									
										
										
										
											2010-02-10 03:03:03 -06:00
										 |  |  |   snprintf(s, 64, "%lld", (long long int)n); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   sprintf(s, "%lld", (long long int)n); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   mpz_init_set_str (&rop, s, 10); | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   Yap_PutInSlot(t,YAP_MkBigNumTerm((void *)&rop) PASS_REGS); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							|  |  |  |   // use a double, but will mess up writing.
 | 
					
						
							|  |  |  |   Int x = n; | 
					
						
							|  |  |  |   if (x == n) | 
					
						
							|  |  |  |   return PL_put_integer(t, x); | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     union { | 
					
						
							|  |  |  |       int64_t i; | 
					
						
							|  |  |  |       double d; | 
					
						
							|  |  |  |     } udi_; | 
					
						
							|  |  |  |     udi_.i = n; | 
					
						
							|  |  |  |     return PL_put_float(t, udi_.d); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | X_API int PL_put_intptr(term_t t, intptr_t n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(t,YAP_MkIntTerm(n)); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_put_uintptr(term_t t, uintptr_t n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(t,YAP_MkIntTerm(n) ); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_list(term_t t) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,YAP_MkNewPairTerm() ); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (Unsigned(HR) > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |     if (!do_gc(0)) { | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_list_chars(term_t t, const char *s) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Term nt; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((nt = Yap_CharsToListOfAtoms(s, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_put_string_nchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t, nt); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void PL_put_nil(term_t t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,TermNil); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  void PL_put_pointer(term_t -t, void *ptr)
 | 
					
						
							|  |  |  | YAP: NO EQUIVALENT */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /* SAM TO DO */ | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_pointer(term_t t, void *ptr) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-05-06 16:41:40 +01:00
										 |  |  |   YAP_Term tptr = YAP_MkIntTerm((YAP_Int)ptr); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,tptr ); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | X_API int PL_put_string_chars(term_t t, const char *chars) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Term nt; | 
					
						
							|  |  |  |   while((nt = Yap_CharsToString(chars, ENC_ISO_LATIN1 PASS_REGS)) | 
					
						
							|  |  |  | 	== 0L) { | 
					
						
							|  |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_putPL_put_string_chars" )) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t, nt); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-19 00:38:49 +01:00
										 |  |  | X_API int PL_put_string_nchars(term_t t, size_t len, const char *chars) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Term nt; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((nt = Yap_NCharsToString(chars, len, ENC_ISO_LATIN1 PASS_REGS)) | 
					
						
							|  |  |  | 	== 0L) { | 
					
						
							|  |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_putPL_put_string_chars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t, nt); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_term(term_t d, term_t s) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(d,Yap_GetFromSlot(s ) ); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  | X_API int PL_put_variable(term_t t) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t,MkVarTerm()); | 
					
						
							| 
									
										
										
										
											2010-01-15 00:49:05 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* end PL_put_* functions =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_raise_exception(term_t exception)
 | 
					
						
							|  |  |  | YAP: NO EQUIVALENT */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /* SAM TO DO */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_raise_exception(term_t exception) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-10-13 02:21:10 +01:00
										 |  |  |     LOCAL_Error_TYPE=YAP_NO_ERROR; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   EX = Yap_StoreTermInDB(Yap_GetFromSlot(exception),0); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API int PL_throw(term_t exception) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Throw(Yap_GetFromSlot(exception)); | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |   if (LOCAL_execution) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   longjmp(LOCAL_execution->q_env, 0); | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void  PL_fatal_error(const char *msg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   fprintf(stderr,"[ FATAL ERROR: %s ]\n",msg); | 
					
						
							|  |  |  |   Yap_exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 21:09:22 +00:00
										 |  |  | X_API int PL_warning(const char *msg, ...) { | 
					
						
							|  |  |  |   va_list args; | 
					
						
							|  |  |  |   va_start(args, msg); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   // just print the warning message and return?
 | 
					
						
							| 
									
										
										
										
											2009-11-19 21:09:22 +00:00
										 |  |  |   fprintf(stderr,"[Warning:"); | 
					
						
							|  |  |  |   fprintf(stderr,msg,args); | 
					
						
							|  |  |  |   fprintf(stderr,"]\n"); | 
					
						
							|  |  |  |   va_end(args); | 
					
						
							|  |  |  |   PL_fail; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /* begin PL_unify_* functions =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_unify(term_t t1, term_t t2) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t1 ),Yap_GetFromSlot(t2 )); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_atom(term_t ?t, atom  *at)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_atom(term_t t, atom_t at) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   YAP_Term cterm = MkAtomTerm(SWIAtomToAtom(at)); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return YAP_Unify(Yap_GetFromSlot(t),cterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_atom_chars(term_t ?t, const char *chars)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_atom_chars(term_t t, const char *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((at = Yap_CharsToAtom(s, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_atom_nchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), MkAtomTerm(at)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_atom_chars(term_t ?t, const char *chars)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | X_API int PL_unify_atom_nchars(term_t t, size_t len, const char *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((at = Yap_NCharsToAtom(s, len, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_atom_nchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Yap_AtomIncreaseHold(at); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), MkAtomTerm(at)); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_float(term_t ?t, double f)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_float(term_t t, double f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2014-03-11 15:36:02 +00:00
										 |  |  |   Term fterm = MkFloatTerm(f); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),fterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_integer(term_t ?t, long n)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_integer(term_t t, long n) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-01-26 22:26:08 +00:00
										 |  |  |   Term iterm = MkIntegerTerm(n); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 11:18:03 +00:00
										 |  |  | X_API int PL_unify_intptr(term_t t, intptr_t n) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-09 11:18:03 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Term iterm = MkIntegerTerm(n); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2014-11-09 11:18:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_unify_uintptr(term_t t, uintptr_t n) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-09 11:18:03 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Term iterm = MkIntegerTerm(n); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2014-11-09 11:18:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | X_API int PL_unify_boolean(term_t t, int n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Term iterm = (n==0?TermFalse:TermTrue); | 
					
						
							|  |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_integer(term_t ?t, long n)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | X_API int PL_unify_functor(term_t t, functor_t f) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term tt = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   Functor ff = SWIFunctorToFunctor(f); | 
					
						
							| 
									
										
										
										
											2011-02-14 11:28:07 -08:00
										 |  |  |   if (IsVarTerm(tt)) { | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     if (Unsigned(HR)+ArityOfFunctor(ff) > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |       if (!do_gc(0)) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return FALSE; | 
					
						
							| 
									
										
										
										
											2011-02-14 11:28:07 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-14 11:28:07 -08:00
										 |  |  |     return Yap_unify(tt, Yap_MkNewApplTerm(ff,ArityOfFunctor(ff))); | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-02-14 11:28:07 -08:00
										 |  |  |   if (IsPairTerm(tt)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return ff == FunctorDot; | 
					
						
							| 
									
										
										
										
											2011-02-14 11:28:07 -08:00
										 |  |  |   if (!IsApplTerm(tt)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   return ff == FunctorOfTerm(tt); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_integer(term_t ?t, long n)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_int64(term_t t, int64_t n) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if SIZEOF_INT_P==8
 | 
					
						
							| 
									
										
										
										
											2010-07-19 14:54:21 +01:00
										 |  |  |   Term iterm = MkIntegerTerm(n); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #elif USE_GMP
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   YAP_Term iterm; | 
					
						
							|  |  |  |   char s[64]; | 
					
						
							|  |  |  |   MP_INT rop; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2010-02-10 03:03:03 -06:00
										 |  |  |   snprintf(s, 64, "%I64d", (long long int)n); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #elif HAVE_SNPRINTF
 | 
					
						
							| 
									
										
										
										
											2010-02-10 03:03:03 -06:00
										 |  |  |   snprintf(s, 64, "%lld", (long long int)n); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   sprintf(s, "%lld", (long long int)n); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   mpz_init_set_str (&rop, s, 10); | 
					
						
							|  |  |  |   iterm = YAP_MkBigNumTerm((void *)&rop); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return YAP_Unify(Yap_GetFromSlot(t ),iterm); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2011-11-03 07:45:51 +09:00
										 |  |  |   if ((long)n == n) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return PL_unify_integer(t, n); | 
					
						
							|  |  |  |   // use a double, but will mess up writing.
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   else { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     union { | 
					
						
							|  |  |  |       int64_t i; | 
					
						
							|  |  |  |       double d; | 
					
						
							|  |  |  |     } udi_; | 
					
						
							|  |  |  |     udi_.i = n; | 
					
						
							|  |  |  |     return PL_unify_float(t, udi_.d); | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2010-07-19 14:54:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_list(term_t ?t, term_t +h, term_t -t)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  | X_API int PL_unify_list(term_t tt, term_t h, term_t tail) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   Term t; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (Unsigned(HR) > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |     if (!do_gc(0)) { | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   t = Deref(Yap_GetFromSlot(tt)); | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |   if (IsVarTerm(t)) { | 
					
						
							|  |  |  |     Term pairterm = Yap_MkNewPairTerm(); | 
					
						
							|  |  |  |     Yap_unify(t, pairterm); | 
					
						
							|  |  |  |     /* avoid calling deref */ | 
					
						
							|  |  |  |     t = pairterm; | 
					
						
							|  |  |  |   } else if (!IsPairTerm(t)) { | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(h,HeadOfTerm(t)); | 
					
						
							|  |  |  |   Yap_PutInSlot(tail,TailOfTerm(t) ); | 
					
						
							| 
									
										
										
										
											2010-05-03 17:02:49 +01:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_list(term_t ?t, term_t +h, term_t -t)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  | X_API int PL_unify_arg(int index, term_t tt, term_t arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Deref(Yap_GetFromSlot(tt )), to; | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |   if (index < 0) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |   if (IsVarTerm(t) || IsAtomOrIntTerm(t)) { | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } else if (IsPairTerm(t)) { | 
					
						
							|  |  |  |     if (index == 1) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     to = HeadOfTerm(t); | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |     else if (index == 2) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     to = TailOfTerm(t); | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     Functor f = FunctorOfTerm(t); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     if (IsExtensionFunctor(f)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |     if (index > ArityOfFunctor(f)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  |     to = ArgOfTerm(index, t); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(arg),to); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_list(term_t ?t, term_t +h, term_t -t)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_list_chars(term_t t, const char *chars) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Term chterm; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((chterm = Yap_CharsToListOfAtoms(chars, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_list_chars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_list(term_t ?t, term_t +h, term_t -t)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API int PL_unify_list_ncodes(term_t t, size_t len, const char *chars) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   Term chterm; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((chterm = Yap_NCharsToListOfCodes(chars, len, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_list_ncodes" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-18 00:29:07 +01:00
										 |  |  | X_API int | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  | PL_unify_list_codes(term_t t, const char *chars) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Term chterm; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((chterm = Yap_CharsToListOfCodes(chars, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_list_codes" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2010-06-18 00:29:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_nil(term_t ?l)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  | X_API int PL_unify_nil(term_t t) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), TermNil); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_pointer(term_t ?t, void *ptr)
 | 
					
						
							|  |  |  | YAP: NO EQUIVALENT */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /* SAM TO DO */ | 
					
						
							|  |  |  | X_API int PL_unify_pointer(term_t t, void *ptr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-05-06 16:41:40 +01:00
										 |  |  |   YAP_Term ptrterm = YAP_MkIntTerm((YAP_Int)ptr); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return YAP_Unify(Yap_GetFromSlot(t ), ptrterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_list(term_t ?t, term_t +h, term_t -t)
 | 
					
						
							|  |  |  | YAP long int  unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_unify_string_chars(term_t t, const char *chars) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Term chterm; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((chterm = Yap_CharsToString(chars, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_list_ncodes" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API int PL_unify_string_nchars(term_t t, size_t len, const char *chars) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Term chterm; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   while((chterm = Yap_NCharsToString(chars, len, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_list_ncodes" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_wchars(term_t ?t, int type, size_t len,, const pl_wchar_t *s)
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2008-07-24 16:02:04 +00:00
										 |  |  | X_API int PL_unify_wchars(term_t t, int type, size_t len, const pl_wchar_t *chars) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2008-07-24 16:02:04 +00:00
										 |  |  |   YAP_Term chterm; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   while (TRUE) { | 
					
						
							|  |  |  |     switch (type) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       case PL_ATOM: | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         Atom at; | 
					
						
							|  |  |  |         at = Yap_NWCharsToAtom(chars, len PASS_REGS); | 
					
						
							|  |  |  |         if (at) { | 
					
						
							|  |  |  |           Yap_AtomIncreaseHold(at); | 
					
						
							|  |  |  |           chterm = MkAtomTerm(at); | 
					
						
							|  |  |  |           return Yap_unify(Yap_GetFromSlot(t), chterm); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       break; | 
					
						
							|  |  |  |       case PL_UTF8_STRING: | 
					
						
							|  |  |  |       case PL_STRING: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       if ((chterm = Yap_NWCharsToString(chars, len PASS_REGS)) != 0) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return YAP_Unify(Yap_GetFromSlot(t ), chterm); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       case PL_CODE_LIST: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       if ((chterm = Yap_NWCharsToListOfCodes(chars, len PASS_REGS)) != 0) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return YAP_Unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       case PL_CHAR_LIST: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       if ((chterm = Yap_NWCharsToListOfAtoms(chars, len PASS_REGS)) != 0) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return YAP_Unify(Yap_GetFromSlot(t), chterm); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |       /* should give error?? */ | 
					
						
							|  |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_wchars" )) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2008-07-24 16:02:04 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |   int type; | 
					
						
							|  |  |  |   union { | 
					
						
							|  |  |  |     functor_t f; | 
					
						
							|  |  |  |     term_t t; | 
					
						
							|  |  |  |     atom_t a; | 
					
						
							|  |  |  |     long l; | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     double dbl; | 
					
						
							|  |  |  |     char *s; | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  |     struct { | 
					
						
							|  |  |  |       size_t n; | 
					
						
							|  |  |  |       char *s; | 
					
						
							|  |  |  |     } ns; | 
					
						
							|  |  |  |     struct { | 
					
						
							|  |  |  |       size_t n; | 
					
						
							|  |  |  |       wchar_t *w; | 
					
						
							|  |  |  |     } nw; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     void *p; | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  |     wchar_t *w; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } arg; | 
					
						
							|  |  |  | } arg_types; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static YAP_Term | 
					
						
							|  |  |  | MkBoolTerm(int b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (b) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return MkAtomTerm(AtomTrue); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return MkAtomTerm(AtomFalse); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  | #define MAX_DEPTH 64
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |   int nels; | 
					
						
							|  |  |  |   CELL *ptr; | 
					
						
							|  |  |  | } stack_el; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  int PL_unify_term(term_t ?t1, term_t ?t2)
 | 
					
						
							|  |  |  | YAP long int  YAP_Unify(YAP_Term* a, Term* b) */ | 
					
						
							| 
									
										
										
										
											2013-11-21 00:22:03 +00:00
										 |  |  | int PL_unify_termv(term_t l, va_list ap) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-01-20 12:04:50 -06:00
										 |  |  |   int type, res; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   int nels = 1; | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |   int depth = 1; | 
					
						
							|  |  |  |   Term a[1], *pt; | 
					
						
							|  |  |  |   stack_el stack[MAX_DEPTH]; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-20 12:04:50 -06:00
										 |  |  |   BACKUP_MACHINE_REGS(); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (Unsigned(HR) > Unsigned(ASP)-CreepFlag) { | 
					
						
							| 
									
										
										
										
											2012-04-20 23:45:16 +02:00
										 |  |  |     if (!do_gc(0)) { | 
					
						
							| 
									
										
										
										
											2011-01-20 12:04:50 -06:00
										 |  |  |       RECOVER_MACHINE_REGS(); | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |   pt = a; | 
					
						
							|  |  |  |   while (depth > 0) { | 
					
						
							|  |  |  |     while (nels > 0) { | 
					
						
							|  |  |  |       type = va_arg(ap, int); | 
					
						
							|  |  |  |       nels--; | 
					
						
							|  |  |  |       switch(type) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         case PL_VARIABLE: | 
					
						
							|  |  |  |         *pt++ = MkVarTerm(); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_BOOL: | 
					
						
							|  |  |  |         *pt++ = MkBoolTerm(va_arg(ap, int)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_ATOM: | 
					
						
							|  |  |  |         *pt++ = MkAtomTerm(SWIAtomToAtom(va_arg(ap, atom_t))); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_INTEGER: | 
					
						
							|  |  |  |         *pt++ = MkIntegerTerm(va_arg(ap, long)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_SHORT: | 
					
						
							|  |  |  |         *pt++ = MkIntegerTerm(va_arg(ap, int)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_LONG: | 
					
						
							|  |  |  |         *pt++ = MkIntegerTerm(va_arg(ap, long)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_INT: | 
					
						
							|  |  |  |         *pt++ = MkIntegerTerm(va_arg(ap, int)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_FLOAT: | 
					
						
							|  |  |  |         *pt++ = MkFloatTerm(va_arg(ap, double)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_STRING: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           Term chterm; | 
					
						
							|  |  |  |           const char *chars = va_arg(ap, char *); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	   | 
					
						
							|  |  |  |           while((chterm = Yap_CharsToString(chars, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |             if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_term" )) | 
					
						
							|  |  |  |             return FALSE; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           *pt++ = chterm; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_CHARS: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           Atom at; | 
					
						
							|  |  |  |           const char *chars = va_arg(ap, char *); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |           while((at = Yap_CharsToAtom(chars, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |             if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_term" )) | 
					
						
							|  |  |  |             return FALSE; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           *pt++ = MkAtomTerm(at); | 
					
						
							|  |  |  |           Yap_AtomIncreaseHold(at); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_NCHARS: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           Atom at; | 
					
						
							|  |  |  |           size_t sz = va_arg(ap, size_t); | 
					
						
							|  |  |  |           const char *chars = va_arg(ap, char *); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |           while((at = Yap_NCharsToAtom(chars, sz, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |             if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_term" )) | 
					
						
							|  |  |  |             return FALSE; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           *pt++ = MkAtomTerm(at); | 
					
						
							|  |  |  |           Yap_AtomIncreaseHold(at); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_NWCHARS: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           Atom at; | 
					
						
							|  |  |  |           size_t sz = va_arg(ap, size_t); | 
					
						
							|  |  |  |           const wchar_t *chars = va_arg(ap, wchar_t *); | 
					
						
							|  |  |  |           while((at = Yap_NWCharsToAtom(chars, sz PASS_REGS)) == 0L) { | 
					
						
							|  |  |  |             if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_term" )) | 
					
						
							|  |  |  |             return FALSE; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           *pt++ = MkAtomTerm(at); | 
					
						
							|  |  |  |           Yap_AtomIncreaseHold(at); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_TERM: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           Term t = Yap_GetFromSlot(va_arg(ap, size_t)); | 
					
						
							|  |  |  |           if (IsVarTerm(t) && VarOfTerm(t) >= ASP && VarOfTerm(t) < LCL0) { | 
					
						
							|  |  |  |             Yap_unify(*pt++, t); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           else { | 
					
						
							|  |  |  |             *pt++ = t; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_POINTER: | 
					
						
							|  |  |  |         *pt++ = MkIntegerTerm((Int)va_arg(ap, void *)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_INTPTR: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |           *pt++ = MkIntegerTerm((UInt)va_arg(ap, intptr_t)); | 
					
						
							|  |  |  |           break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         case PL_INT64: | 
					
						
							|  |  |  |         #if SIZEOF_INT_P==8
 | 
					
						
							|  |  |  |         *pt++ = MkIntegerTerm((Int)va_arg(ap, long int)); | 
					
						
							|  |  |  |         #elif USE_GMP
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           char s[64]; | 
					
						
							|  |  |  |           MP_INT rop; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           #ifdef _WIN32
 | 
					
						
							|  |  |  |           snprintf(s, 64, "%I64d", va_arg(ap, long long int)); | 
					
						
							|  |  |  |           #elif HAVE_SNPRINTF
 | 
					
						
							|  |  |  |           snprintf(s, 64, "%lld", va_arg(ap, long long int)); | 
					
						
							|  |  |  |           #else
 | 
					
						
							|  |  |  |           sprintf(s, "%lld", va_arg(ap, long long int)); | 
					
						
							|  |  |  |           #endif
 | 
					
						
							|  |  |  |           mpz_init_set_str (&rop, s, 10); | 
					
						
							|  |  |  |           *pt++ = YAP_MkBigNumTerm((void *)&rop); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         #else
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           int64_t i = (Int)va_arg(ap, int64_t); | 
					
						
							|  |  |  |           intptr_t x = i; | 
					
						
							|  |  |  |           if (x == i) | 
					
						
							|  |  |  |           *pt++ = MkIntegerTerm( x ); | 
					
						
							|  |  |  |           else { | 
					
						
							|  |  |  |             // use a double, but will mess up writing.
 | 
					
						
							|  |  |  |             union { | 
					
						
							|  |  |  |               int64_t i; | 
					
						
							|  |  |  |               double d; | 
					
						
							|  |  |  |             } udi_; | 
					
						
							|  |  |  |             udi_.i = i; | 
					
						
							|  |  |  |             *pt++ = MkFloatTerm(udi_.d); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         #endif
 | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_FUNCTOR: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           functor_t f = va_arg(ap, functor_t); | 
					
						
							|  |  |  |           Functor ff = SWIFunctorToFunctor(f); | 
					
						
							|  |  |  |           UInt arity = ArityOfFunctor(ff); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (!arity) { | 
					
						
							|  |  |  |             *pt++ = MkAtomTerm((Atom)f); | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             Term t = Yap_MkNewApplTerm(ff, arity); | 
					
						
							|  |  |  |             if (nels) { | 
					
						
							|  |  |  |               if (depth == MAX_DEPTH) { | 
					
						
							|  |  |  |                 fprintf(stderr,"ERROR: very deep term in PL_unify_term, change MAX_DEPTH from %d\n", MAX_DEPTH); | 
					
						
							|  |  |  |                 return FALSE; | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               stack[depth-1].nels = nels; | 
					
						
							|  |  |  |               stack[depth-1].ptr = pt+1; | 
					
						
							|  |  |  |               depth++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             *pt = t; | 
					
						
							|  |  |  |             if (ff == FunctorDot) | 
					
						
							|  |  |  |             pt = RepPair(t); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             pt = RepAppl(t)+1; | 
					
						
							|  |  |  |             nels = arity; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_FUNCTOR_CHARS: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           char *fname = va_arg(ap, char *); | 
					
						
							|  |  |  |           size_t arity = va_arg(ap, size_t); | 
					
						
							|  |  |  |           Atom at; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |           while((at = Yap_CharsToAtom(fname, ENC_ISO_LATIN1 PASS_REGS)) == 0L) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |             if (LOCAL_Error_TYPE && !Yap_SWIHandleError( "PL_unify_term" )) | 
					
						
							|  |  |  |             return FALSE; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           Yap_AtomIncreaseHold(at); | 
					
						
							|  |  |  |           if (!arity) { | 
					
						
							|  |  |  |             *pt++ = MkAtomTerm(at); | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             Functor ff; | 
					
						
							|  |  |  |             Term t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ff = Yap_MkFunctor(at,arity); | 
					
						
							|  |  |  |             t = Yap_MkNewApplTerm(ff, arity); | 
					
						
							|  |  |  |             if (nels) { | 
					
						
							|  |  |  |               if (depth == MAX_DEPTH) { | 
					
						
							|  |  |  |                 fprintf(stderr,"very deep term in PL_unify_term\n"); | 
					
						
							|  |  |  |                 return FALSE; | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |               stack[depth-1].nels = nels; | 
					
						
							|  |  |  |               stack[depth-1].ptr = pt+1; | 
					
						
							|  |  |  |               depth++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             *pt = t; | 
					
						
							|  |  |  |             if (ff == FunctorDot) | 
					
						
							|  |  |  |             pt = RepPair(t); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             pt = RepAppl(t)+1; | 
					
						
							|  |  |  |             nels = arity; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         case PL_LIST: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           Term t = Yap_MkNewPairTerm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (nels) { | 
					
						
							|  |  |  |             if (depth == MAX_DEPTH) { | 
					
						
							|  |  |  |               fprintf(stderr,"very deep term in PL_unify_term\n"); | 
					
						
							|  |  |  |               return FALSE; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             stack[depth-1].nels = nels; | 
					
						
							|  |  |  |             stack[depth].ptr = pt+1; | 
					
						
							|  |  |  |             depth++; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           *pt = t; | 
					
						
							|  |  |  |           pt = RepPair(t); | 
					
						
							|  |  |  |           nels = 2; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |         fprintf(stderr, "PL_unify_term: %d not supported\n", type); | 
					
						
							|  |  |  |         exit(1); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |     depth--; | 
					
						
							|  |  |  |     if (depth) { | 
					
						
							|  |  |  |       pt = stack[depth-1].ptr; | 
					
						
							|  |  |  |       nels = stack[depth-1].nels; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   res = Yap_unify(Yap_GetFromSlot(l),a[0]); | 
					
						
							| 
									
										
										
										
											2011-01-20 12:04:50 -06:00
										 |  |  |   RECOVER_MACHINE_REGS(); | 
					
						
							|  |  |  |   return res; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 00:22:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PL_unify_term(term_t t, ...) | 
					
						
							|  |  |  | { va_list args; | 
					
						
							|  |  |  |   int rval; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   va_start(args, t); | 
					
						
							|  |  |  |   rval = PL_unify_termv(t, args); | 
					
						
							|  |  |  |   va_end(args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return rval; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | /* end PL_unify_* functions =============================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  void PL_register_atom(atom_t atom) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API void PL_register_atom(atom_t atom) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-21 10:12:47 -02:00
										 |  |  |   Yap_AtomIncreaseHold(SWIAtomToAtom(atom)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | /*  void PL_unregister_atom(atom_t atom) */ | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API void PL_unregister_atom(atom_t atom) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-12-21 10:12:47 -02:00
										 |  |  |   Yap_AtomDecreaseHold(SWIAtomToAtom(atom)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_term_type(term_t t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   /* YAP_ does not support strings as different objects */ | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term v = Yap_GetFromSlot(t ); | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  |   if (IsVarTerm(v)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return PL_VARIABLE; | 
					
						
							| 
									
										
										
										
											2008-12-19 11:41:56 +00:00
										 |  |  |   } else if (IsAtomTerm(v)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return PL_ATOM; | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  |   } else if (IsIntegerTerm(v)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return PL_INTEGER; | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  |   } else if (IsStringTerm(v)) { | 
					
						
							|  |  |  |     return PL_STRING; | 
					
						
							|  |  |  |   } else if (IsFloatTerm(v)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return PL_FLOAT; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return PL_TERM; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_atom(term_t t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return IsAtomTerm(Yap_GetFromSlot(t)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  | X_API int PL_is_ground(term_t t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_IsGroundTerm(Yap_GetFromSlot(t)); | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-17 00:17:02 +00:00
										 |  |  | X_API int PL_is_acyclic(term_t t) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_IsAcyclicTerm(Yap_GetFromSlot(t)); | 
					
						
							| 
									
										
										
										
											2013-01-17 00:17:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-23 11:44:21 +01:00
										 |  |  | X_API int PL_is_callable(term_t t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t1 = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2010-06-23 11:44:21 +01:00
										 |  |  |   if (IsVarTerm(t1)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   if (IsAtomTerm(t1) || IsPairTerm(t1)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2010-06-23 11:44:21 +01:00
										 |  |  |   if (IsApplTerm(t1) && !IsExtensionFunctor(FunctorOfTerm(t1))) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2010-06-23 11:44:21 +01:00
										 |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_is_atomic(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-01-28 13:01:27 +00:00
										 |  |  |   return !IsVarTerm(t) || !IsApplTerm(t) || !IsPairTerm(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_compound(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-01-28 13:01:27 +00:00
										 |  |  |   return (IsApplTerm(t) || IsPairTerm(t)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_functor(term_t ts, functor_t f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   Functor ff = SWIFunctorToFunctor(f); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   if (YAP_IsApplTerm(t)) { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |     return FunctorOfTerm(t) == (Functor)ff; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else if (YAP_IsPairTerm(t)) { | 
					
						
							| 
									
										
										
										
											2011-02-15 05:54:19 -08:00
										 |  |  |     return ff == FunctorDot; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_float(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-01-28 13:01:27 +00:00
										 |  |  |   return !IsVarTerm(t) && IsFloatTerm(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_integer(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2011-02-27 03:40:27 -08:00
										 |  |  |   if (IsVarTerm(t)) return FALSE; | 
					
						
							|  |  |  |   if (IsIntTerm(t)) return TRUE; | 
					
						
							|  |  |  |   if (IsApplTerm(t)) { | 
					
						
							|  |  |  |     Functor f = FunctorOfTerm(t); | 
					
						
							|  |  |  |     if (f == FunctorLongInt) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return TRUE; | 
					
						
							| 
									
										
										
										
											2011-02-27 03:40:27 -08:00
										 |  |  |     if (f == FunctorBigInt) { | 
					
						
							|  |  |  |       CELL mask = RepAppl(t)[1]; | 
					
						
							|  |  |  |       return ( mask == BIG_INT ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_list(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts ); | 
					
						
							| 
									
										
										
										
											2012-10-08 18:26:40 +01:00
										 |  |  |   return !IsVarTerm(t) && (t == TermNil || IsPairTerm(t)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  | X_API int PL_is_pair(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-02-02 21:46:33 +00:00
										 |  |  |   return !IsVarTerm(t) && IsPairTerm(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  | X_API int | 
					
						
							|  |  |  | PL_skip_list(term_t list, term_t tail, size_t *len) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-02-03 02:40:41 +00:00
										 |  |  |   Term *l = Yap_AddressFromSlot(list); | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  |   Term *t; | 
					
						
							|  |  |  |   intptr_t length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   length = Yap_SkipList(l, &t); | 
					
						
							|  |  |  |   if ( len ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   *len = length; | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  |   if ( tail ) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   { Term t2 = Yap_GetFromSlot(tail); | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Yap_unify(t2, *t); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ( *t == TermNil ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return PL_LIST; | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  |   else if ( IsVarTerm(*t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return PL_PARTIAL_LIST; | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  |   else if ( IsPairTerm(*t) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return PL_CYCLIC_TERM; | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return PL_NOT_A_LIST; | 
					
						
							| 
									
										
										
										
											2012-01-09 23:29:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_is_number(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2014-01-28 13:01:27 +00:00
										 |  |  |   return IsIntegerTerm(t) || IsBigIntTerm(t) || IsFloatTerm(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_string(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   return IsStringTerm(t); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_is_variable(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return YAP_IsVarTerm(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_compare(term_t ts1, term_t ts2) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   YAP_Term t1 = Yap_GetFromSlot(ts1); | 
					
						
							|  |  |  |   YAP_Term t2 = Yap_GetFromSlot(ts2); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return YAP_CompareTerms(t1, t2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  | X_API char * | 
					
						
							|  |  |  | PL_record_external | 
					
						
							|  |  |  | (term_t ts, size_t *sz) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |   size_t len = 512, nsz; | 
					
						
							|  |  |  |   char *s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while(TRUE) { | 
					
						
							|  |  |  |     if (!(s = Yap_AllocCodeSpace(len))) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2012-02-02 23:25:09 +00:00
										 |  |  |     if ((nsz = Yap_ExportTerm(t, s, len, 0))) { | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |       *sz = nsz; | 
					
						
							| 
									
										
										
										
											2014-03-06 02:09:48 +00:00
										 |  |  |       return (char *)s; | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |       if (len < 16*1024) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       len = len *2; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       len += 16*1024; | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | partial implementation of recorded_external, does not guarantee endianness nor portability, and does not | 
					
						
							|  |  |  | support constraints. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_recorded_external | 
					
						
							| 
									
										
										
										
											2014-03-06 02:09:48 +00:00
										 |  |  | (const char *tp, term_t ts) | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   Term t = Yap_ImportTerm((void *)tp); | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |   if (t == 0) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(ts, t ); | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_erase_external | 
					
						
							|  |  |  | (char *tp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Yap_FreeCodeSpace(tp); | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | X_API record_t | 
					
						
							|  |  |  | PL_record(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2011-01-02 21:16:29 -06:00
										 |  |  |   return (record_t)YAP_Record(t); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 09:35:21 +00:00
										 |  |  | X_API int | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | PL_recorded(record_t db, term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-01-02 21:16:29 -06:00
										 |  |  |   Term t = YAP_Recorded((void *)db); | 
					
						
							|  |  |  |   if (t == ((CELL)0)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(ts,t ); | 
					
						
							| 
									
										
										
										
											2010-02-22 09:35:21 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-26 15:17:17 +00:00
										 |  |  | X_API record_t | 
					
						
							|  |  |  | PL_duplicate_record(record_t db) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Term t = YAP_Recorded((void *)db); | 
					
						
							|  |  |  |   if (t == ((CELL)0)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2011-03-26 15:17:17 +00:00
										 |  |  |   return (record_t)YAP_Record(t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | X_API void | 
					
						
							|  |  |  | PL_erase(record_t db) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-01-02 21:16:29 -06:00
										 |  |  |   YAP_Erase((void *)db); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API void PL_halt(int e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   YAP_Halt(e); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_action(int action,...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   va_list ap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   va_start (ap, action); | 
					
						
							|  |  |  |   switch (action) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_TRACE: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_TRACE not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_DEBUG: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_DEBUG not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_BACKTRACE: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_BACKTRACE not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_HALT: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       int halt_arg = va_arg(ap, int); | 
					
						
							|  |  |  |       YAP_Halt(halt_arg); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_ABORT: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |       YAP_Throw(MkAtomTerm(Yap_LookupAtom("abort"))); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_BREAK: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_BREAK not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_GUIAPP: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_GUIAPP not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_WRITE: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_WRITE not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_FLUSH: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_WRITE not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_ACTION_ATTACH_CONSOLE: | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     fprintf(stderr, "PL_ACTION_WRITE not supported\n"); | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   va_end (ap); | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API term_t | 
					
						
							|  |  |  | PL_exception(qid_t q) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   YAP_Term t; | 
					
						
							|  |  |  |   if (YAP_GoalHasException(&t)) { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |     CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     term_t to = Yap_NewSlots(1); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |     Yap_PutInSlot(to,t ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return to; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return 0L; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 09:35:21 +00:00
										 |  |  | X_API void | 
					
						
							|  |  |  | PL_clear_exception(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-07-21 12:49:06 +01:00
										 |  |  |   EX = NULL; | 
					
						
							| 
									
										
										
										
											2010-02-22 09:35:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int | 
					
						
							|  |  |  | PL_initialise(int myargc, char **myargv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   YAP_init_args init_args; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   memset((void *)&init_args,0,sizeof(init_args)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   init_args.Argv = myargv; | 
					
						
							|  |  |  |   init_args.Argc = myargc; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if BOOT_FROM_SAVED_STATE
 | 
					
						
							| 
									
										
										
										
											2009-05-02 14:11:54 -05:00
										 |  |  |   init_args.SavedState = "startup.yss"; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2010-06-06 23:05:54 +01:00
										 |  |  |   init_args.SavedState = NULL; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   init_args.YapLibDir = NULL; | 
					
						
							|  |  |  |   init_args.YapPrologBootFile = NULL; | 
					
						
							|  |  |  |   init_args.HaltAfterConsult = FALSE; | 
					
						
							|  |  |  |   init_args.FastBoot = FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-06 23:05:54 +01:00
										 |  |  |   init_args.MaxTableSpaceSize = 0; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   init_args.NumberWorkers = 1; | 
					
						
							|  |  |  |   init_args.SchedulerLoop = 10; | 
					
						
							|  |  |  |   init_args.DelayedReleaseLoad = 3; | 
					
						
							| 
									
										
										
										
											2010-04-13 00:31:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-22 03:56:01 -05:00
										 |  |  |   YAP_parse_yap_arguments(myargc,myargv,&init_args); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-10 10:06:51 +01:00
										 |  |  |   GLOBAL_PL_Argc = myargc; | 
					
						
							|  |  |  |   GLOBAL_PL_Argv = myargv; | 
					
						
							|  |  |  |   GLOBAL_InitialisedFromPL = TRUE; | 
					
						
							| 
									
										
										
										
											2014-03-18 17:27:12 +00:00
										 |  |  |   int rc = YAP_Init(&init_args) != YAP_BOOT_ERROR; | 
					
						
							| 
									
										
										
										
											2016-02-11 06:20:16 -08:00
										 |  |  |   ATOM_nil = YAP_SWIAtomFromAtom( AtomNil ); | 
					
						
							| 
									
										
										
										
											2014-03-18 17:27:12 +00:00
										 |  |  |   return rc; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							| 
									
										
										
										
											2010-04-13 00:31:48 +01:00
										 |  |  | PL_is_initialised(int *argcp, char ***argvp) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-10 10:06:51 +01:00
										 |  |  |   if (GLOBAL_InitialisedFromPL) { | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     if (argcp) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *argcp = GLOBAL_PL_Argc; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     if (argvp) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *argvp = GLOBAL_PL_Argv; | 
					
						
							| 
									
										
										
										
											2010-04-13 00:31:48 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-05-10 10:06:51 +01:00
										 |  |  |   return GLOBAL_InitialisedFromPL; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | X_API module_t | 
					
						
							|  |  |  | PL_context(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-13 10:38:20 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   return Yap_GetModuleEntry(LOCAL_SourceModule); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_strip_module(term_t raw, module_t *m, term_t plain) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-11-25 12:15:03 +01:00
										 |  |  |   Term m0, t; | 
					
						
							|  |  |  |   if (m) { | 
					
						
							|  |  |  |     if (*m) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     m0 = MkAtomTerm((*m)->AtomOfME); | 
					
						
							| 
									
										
										
										
											2013-11-25 12:15:03 +01:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     m0 = MkAtomTerm(AtomProlog); | 
					
						
							| 
									
										
										
										
											2013-11-25 12:15:03 +01:00
										 |  |  |   } else | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   m0 = USER_MODULE; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   t =  Yap_StripModule(Yap_GetFromSlot(raw), &m0); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  |   if (!t) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2013-11-13 10:38:20 +00:00
										 |  |  |   *m = Yap_GetModuleEntry(m0); | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(plain, t ); | 
					
						
							| 
									
										
										
										
											2008-08-01 21:44:25 +00:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API atom_t PL_module_name(module_t m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-13 10:38:20 +00:00
										 |  |  |   Atom at = m->AtomOfME; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   return AtomToSWIAtom(at); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API predicate_t PL_pred(functor_t f, module_t m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   Functor ff = SWIFunctorToFunctor(f); | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   Term mod = SWIModuleToModule(m); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-22 12:03:14 +00:00
										 |  |  |   if (IsAtomTerm((Term)f)) { | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |     return YAP_Predicate(YAP_AtomOfTerm((Term)f),0,mod); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |     return YAP_Predicate((YAP_Atom)NameOfFunctor(ff),ArityOfFunctor(ff),mod); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API predicate_t PL_predicate(const char *name, int arity, const char *m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   Term mod; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   Atom at; | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   if (m == NULL) { | 
					
						
							|  |  |  |     mod = CurrentModule; | 
					
						
							|  |  |  |     if (!mod) mod = USER_MODULE; | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |     Atom at; | 
					
						
							|  |  |  |     while (!(at = Yap_LookupAtom((char *)m))) { | 
					
						
							|  |  |  |       if (!Yap_growheap(FALSE, 0L, NULL)) { | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |         Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     mod = MkAtomTerm(at); | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |   while (!(at = Yap_LookupAtom((char *)name))) { | 
					
						
							|  |  |  |     if (!Yap_growheap(FALSE, 0L, NULL)) { | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage); | 
					
						
							| 
									
										
										
										
											2010-05-04 15:03:12 +01:00
										 |  |  |       return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return YAP_Predicate((YAP_Atom)at, arity, mod); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  | X_API int PL_unify_predicate(term_t head, predicate_t pred, int how) | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  |   PredEntry *pe = (PredEntry *)pred; | 
					
						
							|  |  |  |   Term ts[2], nt; | 
					
						
							|  |  |  |   if (!pe->ModuleOfPred) { | 
					
						
							|  |  |  |     ts[0] = pe->ModuleOfPred; | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  |     ts[0] = TermProlog; | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  |   if (how == GP_NAMEARITY) { | 
					
						
							|  |  |  |     Term nts[2]; | 
					
						
							|  |  |  |     nts[1] = MkIntegerTerm(pe->ArityOfPE); | 
					
						
							|  |  |  |     if (pe->ArityOfPE) { | 
					
						
							|  |  |  |       nts[0] = MkAtomTerm(NameOfFunctor(pe->FunctorOfPred)); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       nts[0] = MkAtomTerm((Atom)pe->FunctorOfPred); | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-10 21:14:38 +00:00
										 |  |  |     ts[1] = Yap_MkApplTerm(FunctorSlash, 2, nts); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     if (pe->ArityOfPE) { | 
					
						
							|  |  |  |       ts[1] = Yap_MkNewApplTerm(pe->FunctorOfPred, pe->ArityOfPE); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       ts[1] = MkAtomTerm((Atom)pe->FunctorOfPred); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   nt = Yap_MkApplTerm(FunctorModule, 2, ts); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(head),nt); | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API void PL_predicate_info(predicate_t p,atom_t *name, int *arity, module_t *m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   PredEntry *pd = (PredEntry *)p; | 
					
						
							|  |  |  |   Atom aname; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (pd->ArityOfPE) { | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |     if (arity) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *arity = pd->ArityOfPE; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |     aname = NameOfFunctor(pd->FunctorOfPred); | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |     if (arity) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *arity = 0; | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |     aname = (Atom)(pd->FunctorOfPred); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   if (pd->ModuleOfPred && m) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   *m = Yap_GetModuleEntry(pd->ModuleOfPred); | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   else if (m) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   *m = Yap_GetModuleEntry(TermProlog); | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   if (name) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   *name = AtomToSWIAtom(aname); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-14 06:58:03 -08:00
										 |  |  | #undef S_YREG
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-01 00:40:58 +01:00
										 |  |  | X_API fid_t | 
					
						
							|  |  |  | PL_open_foreign_frame(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   /* initialise a new marker choicepoint */ | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   choiceptr cp_b = ((choiceptr)(ASP-1))-1; | 
					
						
							|  |  |  |   cp_b->cp_tr = TR; | 
					
						
							|  |  |  |   cp_b->cp_h = HR; | 
					
						
							|  |  |  |   cp_b->cp_b = B; | 
					
						
							|  |  |  |   cp_b->cp_cp = CP; | 
					
						
							|  |  |  |   cp_b->cp_env = ENV; | 
					
						
							|  |  |  |   cp_b->cp_ap = NOCODE; | 
					
						
							|  |  |  |   #ifdef DEPTH_LIMIT
 | 
					
						
							|  |  |  |   cp_b->cp_depth = DEPTH; | 
					
						
							|  |  |  |   #endif /* DEPTH_LIMIT */
 | 
					
						
							|  |  |  |   cp_b->cp_a1 = MkIntTerm(Yap_StartSlots()); | 
					
						
							|  |  |  |   HB = HR; | 
					
						
							|  |  |  |   B = cp_b; | 
					
						
							|  |  |  |   ASP = (CELL *)B; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (fid_t)(LCL0-(CELL*)cp_b); | 
					
						
							| 
									
										
										
										
											2010-06-01 00:40:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void | 
					
						
							|  |  |  | PL_close_foreign_frame(fid_t f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   choiceptr cp_b = (choiceptr)(LCL0-(UInt)f); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Yap_CloseSlots( IntOfTerm(cp_b->cp_a1) ); | 
					
						
							| 
									
										
										
										
											2013-12-11 09:04:59 +00:00
										 |  |  |   B = cp_b; | 
					
						
							|  |  |  |   HB = B->cp_h; | 
					
						
							|  |  |  |   Yap_TrimTrail(); | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   B = cp_b->cp_b; | 
					
						
							|  |  |  |   CP = cp_b->cp_cp; | 
					
						
							|  |  |  |   ENV = cp_b->cp_env; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #ifdef DEPTH_LIMIT
 | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   DEPTH = cp_b->cp_depth; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif /* DEPTH_LIMIT */
 | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   HB = B->cp_h; | 
					
						
							| 
									
										
										
										
											2015-01-30 07:25:34 +00:00
										 |  |  |   ASP = ((CELL *)(cp_b+1))+1; | 
					
						
							| 
									
										
										
										
											2011-02-14 06:58:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | backtrack(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-02-14 06:58:03 -08:00
										 |  |  |   P = FAILCODE; | 
					
						
							|  |  |  |   Yap_absmi(0); | 
					
						
							|  |  |  |   TR = B->cp_tr; | 
					
						
							| 
									
										
										
										
											2010-06-01 00:40:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void | 
					
						
							|  |  |  | PL_rewind_foreign_frame(fid_t f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   choiceptr cp_b = (choiceptr)(LCL0-(UInt)f); | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   if (B != cp_b) { | 
					
						
							|  |  |  |     while (B->cp_b != cp_b) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     B = B->cp_b; | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-02-14 06:58:03 -08:00
										 |  |  |   backtrack(); | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   // restore to original location
 | 
					
						
							| 
									
										
										
										
											2011-03-14 20:54:19 +00:00
										 |  |  |   ASP = (CELL *)B; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Yap_CloseSlots( IntOfTerm(cp_b->cp_a1) ); | 
					
						
							| 
									
										
										
										
											2010-06-01 00:40:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void | 
					
						
							|  |  |  | PL_discard_foreign_frame(fid_t f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   choiceptr cp_b = (choiceptr)(LCL0-(UInt)f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (B != cp_b) { | 
					
						
							|  |  |  |     while (B->cp_b != cp_b) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     B = B->cp_b; | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |     backtrack(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Yap_CloseSlots( IntOfTerm(cp_b->cp_a1) ); | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   B = cp_b->cp_b; | 
					
						
							|  |  |  |   CP = cp_b->cp_cp; | 
					
						
							|  |  |  |   ENV = cp_b->cp_env; | 
					
						
							|  |  |  |   HB = B->cp_h; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #ifdef DEPTH_LIMIT
 | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   DEPTH = cp_b->cp_depth; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif /* DEPTH_LIMIT */
 | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   /* we can assume there was a slot before */ | 
					
						
							| 
									
										
										
										
											2015-01-30 07:25:34 +00:00
										 |  |  |   ASP = ((CELL *)(cp_b+1))+1; | 
					
						
							| 
									
										
										
										
											2010-06-01 00:40:58 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | X_API qid_t PL_open_query(module_t ctx, int flags, predicate_t p, term_t t0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* ignore flags  and module for now */ | 
					
						
							| 
									
										
										
										
											2014-03-13 16:57:21 +00:00
										 |  |  |   qid_t new = (qid_t)Yap_AllocCodeSpace(sizeof(struct open_query_struct)); | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |   new->oq = LOCAL_execution; | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   LOCAL_execution = new; | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   new->q_open=1; | 
					
						
							|  |  |  |   new->q_state=0; | 
					
						
							|  |  |  |   new->q_flags = flags; | 
					
						
							|  |  |  |   new->q_pe = (PredEntry *)p; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   new->q_g = t0; | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   return new; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_next_solution(qid_t qi) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   int result; | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   if (qi->q_open != 1) return 0; | 
					
						
							|  |  |  |   if (setjmp(LOCAL_execution->q_env)) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  |   // don't forget, on success these guys must create slots
 | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   if (qi->q_state == 0) { | 
					
						
							|  |  |  |     result = YAP_EnterGoal((YAP_PredEntryPtr)qi->q_pe, qi->q_g, &qi->q_h); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |     LOCAL_AllowRestart = qi->q_open; | 
					
						
							|  |  |  |     result = YAP_RetryGoal(&qi->q_h); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   qi->q_state = 1; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   if (result == 0) { | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |     YAP_LeaveGoal(FALSE, &qi->q_h); | 
					
						
							|  |  |  |     qi->q_open = 0; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void PL_cut_query(qid_t qi) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   if (qi->q_open != 1 || qi->q_state == 0) return; | 
					
						
							|  |  |  |   YAP_LeaveGoal(FALSE, &qi->q_h); | 
					
						
							|  |  |  |   qi->q_open = 0; | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |   LOCAL_execution = qi->oq; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Yap_FreeCodeSpace( (char *)qi ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API void PL_close_query(qid_t qi) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-18 14:26:24 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-11-20 22:29:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   if (EX && !(qi->q_flags & (PL_Q_CATCH_EXCEPTION))) { | 
					
						
							| 
									
										
										
										
											2013-01-18 14:26:24 +00:00
										 |  |  |     EX = NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   /* need to implement backtracking here */ | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   if (qi->q_open != 1 || qi->q_state == 0) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return; | 
					
						
							| 
									
										
										
										
											2013-01-18 14:26:24 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   YAP_LeaveGoal(FALSE, &qi->q_h); | 
					
						
							|  |  |  |   qi->q_open = 0; | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |   LOCAL_execution = qi->oq; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:49:41 +00:00
										 |  |  |   Yap_FreeCodeSpace( (char *)qi ); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_call_predicate(module_t ctx, int flags, predicate_t p, term_t t0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-02-06 00:17:09 +00:00
										 |  |  |   fid_t f = PL_open_foreign_frame(); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   qid_t qi = PL_open_query(ctx, flags, p, t0); | 
					
						
							|  |  |  |   int ret = PL_next_solution(qi); | 
					
						
							| 
									
										
										
										
											2010-04-23 16:43:29 +01:00
										 |  |  |   PL_cut_query(qi); | 
					
						
							| 
									
										
										
										
											2013-02-06 00:17:09 +00:00
										 |  |  |   PL_close_foreign_frame(f); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 16:50:43 +01:00
										 |  |  | X_API int PL_toplevel(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-22 03:56:01 -05:00
										 |  |  |   while (TRUE) { | 
					
						
							|  |  |  |     if (YAP_RunGoal(MkAtomTerm(Yap_FullLookupAtom("$live")))) { | 
					
						
							|  |  |  |       return TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-03-13 12:54:06 +00:00
										 |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2009-10-23 16:50:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_call(term_t tp, module_t m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   int out; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BACKUP_B(); | 
					
						
							|  |  |  |   BACKUP_H(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Term t[2], g; | 
					
						
							|  |  |  |   t[0] = SWIModuleToModule(m); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   t[1] = Yap_GetFromSlot(tp ); | 
					
						
							| 
									
										
										
										
											2010-01-26 12:21:06 +00:00
										 |  |  |   g = Yap_MkApplTerm(FunctorModule,2,t); | 
					
						
							|  |  |  |   out =  YAP_RunGoal(g); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   RECOVER_H(); | 
					
						
							|  |  |  |   RECOVER_B(); | 
					
						
							|  |  |  |   return out; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 12:19:51 +01:00
										 |  |  | X_API void PL_register_foreign_in_module(const char *module, const char *name, int arity, pl_function_t function, int flags) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |   Term tmod; | 
					
						
							| 
									
										
										
										
											2015-01-18 01:32:13 +00:00
										 |  |  |   Int nflags = 0; | 
					
						
							| 
									
										
										
										
											2009-04-14 01:25:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #ifdef DEBUG
 | 
					
						
							| 
									
										
										
										
											2013-12-14 12:35:18 +00:00
										 |  |  |   if (flags & (PL_FA_CREF)) { | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |     fprintf(stderr,"PL_register_foreign_in_module called with non-implemented flag %x when creating predicate %s:%s/%d\n", flags, module, name, arity); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2009-03-13 19:40:27 +00:00
										 |  |  |   if (module == NULL) { | 
					
						
							|  |  |  |     tmod = CurrentModule; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     tmod = MkAtomTerm(Yap_LookupAtom((char *)module)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   if (flags & PL_FA_VARARGS) { | 
					
						
							| 
									
										
										
										
											2009-06-01 21:49:24 -05:00
										 |  |  |     nflags = SWIEnvPredFlag; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & PL_FA_TRANSPARENT) { | 
					
						
							|  |  |  |     nflags |= ModuleTransparentPredFlag; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     nflags |= CArgsPredFlag; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (flags & PL_FA_NONDETERMINISTIC) { | 
					
						
							| 
									
										
										
										
											2010-01-15 16:21:43 +00:00
										 |  |  |     Yap_InitCPredBackCut((char *)name, arity, sizeof(struct foreign_context)/sizeof(CELL), (CPredicate)function, (CPredicate)function, (CPredicate)function, UserCPredFlag|nflags); | 
					
						
							| 
									
										
										
										
											2009-06-01 21:49:24 -05:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     UserCPredicate((char *)name,(CPredicate)function,arity,tmod,nflags); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-01-18 01:32:13 +00:00
										 |  |  |   if (flags & PL_FA_NOTRACE) { | 
					
						
							|  |  |  |     Yap_SetNoTrace((char *)name, arity, tmod); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 16:50:43 +01:00
										 |  |  | X_API void PL_register_extensions(const PL_extension *ptr) | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  |   // implemented as register foreign
 | 
					
						
							|  |  |  |   // may cause problems during initialization?
 | 
					
						
							| 
									
										
										
										
											2009-06-01 21:49:24 -05:00
										 |  |  |   PL_load_extensions(ptr); | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  | X_API void | 
					
						
							|  |  |  | PL_register_extensions_in_module(const char *module, const PL_extension *e) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  |   // implemented as register foreign
 | 
					
						
							|  |  |  |   /* ignore flags for now */ | 
					
						
							|  |  |  |   while(e->predicate_name != NULL) { | 
					
						
							|  |  |  |     PL_register_foreign_in_module(module, e->predicate_name, e->arity, e->function, e->flags); | 
					
						
							|  |  |  |     e++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 12:19:51 +01:00
										 |  |  | X_API void PL_register_foreign(const char *name, int arity, pl_function_t function, int flags) | 
					
						
							| 
									
										
										
										
											2010-01-25 12:29:51 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   PL_register_foreign_in_module(NULL, name, arity, function, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 16:50:43 +01:00
										 |  |  | X_API void PL_load_extensions(const PL_extension *ptr) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   /* ignore flags for now */ | 
					
						
							|  |  |  |   while(ptr->predicate_name != NULL) { | 
					
						
							| 
									
										
										
										
											2009-06-01 21:49:24 -05:00
										 |  |  |     PL_register_foreign_in_module(NULL, ptr->predicate_name, ptr->arity, ptr->function, ptr->flags); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     ptr++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | X_API  int PL_is_inf(term_t st) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Deref(Yap_GetFromSlot(st)); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   if (IsVarTerm(t)) return FALSE; | 
					
						
							|  |  |  |   if (!IsFloatTerm(t)) return FALSE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if HAVE_ISINF
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   Float fl; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   fl = FloatOfTerm(t); | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  |   return isinf(fl); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #elif HAVE_FPCLASS
 | 
					
						
							| 
									
										
										
										
											2014-05-04 22:26:53 +01:00
										 |  |  |   Float fl; | 
					
						
							|  |  |  |   fl = FloatOfTerm(t); | 
					
						
							| 
									
										
										
										
											2009-06-15 14:58:57 -05:00
										 |  |  |   return (fpclass(fl) == FP_NINF || fpclass(fl) == FP_PINF); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2009-06-15 14:58:57 -05:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_thread_self(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if THREADS
 | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   if (pthread_getspecific(Yap_yaamregs_key) == NULL) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return -1; | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  |   return (worker_id+1)<<3; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   return -2; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  | /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
					
						
							|  |  |  | PL_thread_raise() is used  for  re-routing   interrupts  in  the Windows | 
					
						
							|  |  |  | version, where the signal handler is running  from a different thread as | 
					
						
							|  |  |  | Prolog. | 
					
						
							|  |  |  | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  | int | 
					
						
							|  |  |  | PL_thread_raise(int tid, int sig) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  |   PL_LOCK(L_THREAD); | 
					
						
							|  |  |  |   if ( tid < 1  ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   { error: | 
					
						
							|  |  |  |     PL_UNLOCK(L_THREAD); | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  |   if ( !REMOTE_ThreadHandle(tid).in_use ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   goto error; | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-11 00:28:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  |   PL_UNLOCK(L_THREAD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-11 00:28:38 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PL_thread_raise(int tid, int sig) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if 0
 | 
					
						
							| 
									
										
										
										
											2013-11-11 00:28:38 +00:00
										 |  |  |   if ( !raiseSignal(NULL, sig) ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2013-11-11 00:28:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 21:41:49 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-11-04 15:31:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  | X_API int PL_unify_thread_id(term_t t, int i) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   Term iterm = MkIntegerTerm(i); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t),iterm); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | pl_thread_self(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if THREADS
 | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  |   if (pthread_getspecific(Yap_yaamregs_key) == NULL) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return -1; | 
					
						
							| 
									
										
										
										
											2013-03-10 16:44:46 -05:00
										 |  |  |   return worker_id; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  |   return -2; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | X_API int PL_thread_attach_engine(const PL_thread_attr_t *attr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  |   int wid = pl_thread_self(); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   if (wid < 0) { | 
					
						
							|  |  |  |     /* we do not have an engine */ | 
					
						
							|  |  |  |     if (attr) { | 
					
						
							|  |  |  |       YAP_thread_attr yapt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       yapt.ssize = attr->local_size; | 
					
						
							|  |  |  |       yapt.tsize = attr->global_size; | 
					
						
							| 
									
										
										
										
											2014-05-25 20:50:07 +01:00
										 |  |  |       yapt.alias = MkAtomTerm(Yap_LookupAtom(attr->alias)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |       yapt.cancel =  attr->cancel; | 
					
						
							|  |  |  |       wid = YAP_ThreadCreateEngine(&yapt); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       wid = YAP_ThreadCreateEngine(NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (wid < 0) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     if (YAP_ThreadAttachEngine(wid)) { | 
					
						
							|  |  |  |       return wid; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     /* attach myself again */ | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |     return YAP_ThreadAttachEngine(wid); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int PL_thread_destroy_engine(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  |   int wid = pl_thread_self(); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (wid < 0) { | 
					
						
							|  |  |  |     /* we do not have an engine */ | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   YAP_ThreadDetachEngine(wid); | 
					
						
							|  |  |  |   return YAP_ThreadDestroyEngine(wid); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_thread_at_exit(void (*function)(void *), void *closure, int global) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* don't do nothing for now */ | 
					
						
							|  |  |  |   fprintf(stderr,"%% YAP ERROR: PL_thread_at_exit not implemented yet\n"); | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API PL_engine_t | 
					
						
							|  |  |  | PL_create_engine(const PL_thread_attr_t *attr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if THREADS
 | 
					
						
							| 
									
										
										
										
											2012-12-11 22:07:10 +00:00
										 |  |  |   int eng; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   if (attr) { | 
					
						
							|  |  |  |     YAP_thread_attr yapt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     yapt.ssize = attr->local_size; | 
					
						
							|  |  |  |     yapt.tsize = attr->global_size; | 
					
						
							| 
									
										
										
										
											2014-05-25 20:50:07 +01:00
										 |  |  |     yapt.alias = MkAtomTerm(Yap_LookupAtom(attr->alias)); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     yapt.cancel =  attr->cancel; | 
					
						
							| 
									
										
										
										
											2012-12-11 22:07:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     eng = YAP_ThreadCreateEngine(&yapt); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2012-12-11 22:07:10 +00:00
										 |  |  |     eng = YAP_ThreadCreateEngine(NULL); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-12-11 22:07:10 +00:00
										 |  |  |   if (eng >= 0) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return  Yap_local[eng]; | 
					
						
							|  |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2012-12-11 22:07:10 +00:00
										 |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_destroy_engine(PL_engine_t e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if THREADS
 | 
					
						
							| 
									
										
										
										
											2011-05-11 18:22:58 +01:00
										 |  |  |   return YAP_ThreadDestroyEngine(((struct worker_local *)e)->ThreadHandle_.current_yaam_regs->worker_id_); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | X_API int | 
					
						
							|  |  |  | PL_set_engine(PL_engine_t engine, PL_engine_t *old) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if THREADS
 | 
					
						
							| 
									
										
										
										
											2013-03-10 16:39:54 +00:00
										 |  |  |   int cwid = pl_thread_self(), nwid; | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   if (cwid >= 0) { | 
					
						
							| 
									
										
										
										
											2011-05-09 19:36:51 +01:00
										 |  |  |     if (old) *old = (PL_engine_t)(Yap_local[cwid]); | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |   if (!engine) { | 
					
						
							|  |  |  |     if (cwid < 0) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     return PL_ENGINE_INVAL; | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |     if (!YAP_ThreadDetachEngine(worker_id)) { | 
					
						
							|  |  |  |       return PL_ENGINE_INVAL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return PL_ENGINE_SET; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   if (engine == PL_ENGINE_MAIN) { | 
					
						
							|  |  |  |     nwid = 0; | 
					
						
							|  |  |  |   } else if (engine == PL_ENGINE_CURRENT) { | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |     if (cwid < 0) { | 
					
						
							|  |  |  |       if (old) *old = NULL; | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |       return PL_ENGINE_INVAL; | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |     return PL_ENGINE_SET; | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2012-12-11 22:07:10 +00:00
										 |  |  |     nwid = ((struct worker_local *)engine)->ThreadHandle_.id; | 
					
						
							| 
									
										
										
										
											2010-07-23 12:07:33 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |   MUTEX_LOCK(&(REMOTE_ThreadHandle(nwid).tlock)); | 
					
						
							| 
									
										
										
										
											2012-06-29 15:38:49 -05:00
										 |  |  |   if (REMOTE_ThreadHandle(nwid).ref_count) { | 
					
						
							| 
									
										
										
										
											2014-03-20 13:33:17 +00:00
										 |  |  |     MUTEX_UNLOCK(&(REMOTE_ThreadHandle(nwid).tlock)); | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |     if (cwid != nwid) { | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |       return PL_ENGINE_INUSE; | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return PL_ENGINE_SET; | 
					
						
							| 
									
										
										
										
											2010-07-23 15:54:13 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (!YAP_ThreadAttachEngine(nwid)) { | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  |     return PL_ENGINE_INVAL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return PL_ENGINE_SET; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #else
 | 
					
						
							| 
									
										
										
										
											2011-05-09 19:36:51 +01:00
										 |  |  |   if (old) *old = (PL_engine_t)&Yap_local; | 
					
						
							| 
									
										
										
										
											2010-07-25 11:19:07 +01:00
										 |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | X_API void * | 
					
						
							| 
									
										
										
										
											2010-11-24 08:44:03 +00:00
										 |  |  | PL_malloc(size_t sz) | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-24 08:44:03 +00:00
										 |  |  |   if ( sz == 0 ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2010-11-24 08:44:03 +00:00
										 |  |  |   return (void *)malloc((long unsigned int)sz); | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 21:09:22 +00:00
										 |  |  | X_API void * | 
					
						
							| 
									
										
										
										
											2010-11-24 08:44:03 +00:00
										 |  |  | PL_realloc(void *ptr, size_t sz) | 
					
						
							| 
									
										
										
										
											2009-11-19 21:09:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-24 08:44:03 +00:00
										 |  |  |   if (ptr) { | 
					
						
							|  |  |  |     if (sz) { | 
					
						
							|  |  |  |       return realloc((char *)ptr,(long unsigned int)sz); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       free(ptr); | 
					
						
							|  |  |  |       return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return PL_malloc(sz); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-11-19 21:09:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | X_API void | 
					
						
							|  |  |  | PL_free(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-24 08:44:03 +00:00
										 |  |  |   if (obj) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   free(obj); | 
					
						
							| 
									
										
										
										
											2008-02-12 17:03:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  | X_API int | 
					
						
							| 
									
										
										
										
											2009-07-18 13:38:38 -07:00
										 |  |  | PL_eval_expression_to_int64_ex(term_t t, int64_t *val) | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term res = Yap_Eval(Yap_GetFromSlot(t)); | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |   if (!res) { | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (IsIntegerTerm(res)) { | 
					
						
							|  |  |  |     *val = IntegerOfTerm(res); | 
					
						
							|  |  |  |     return TRUE; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #if  SIZEOF_INT_P==4 && USE_GMP
 | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |   } else if (YAP_IsBigNumTerm(res)) { | 
					
						
							|  |  |  |     MP_INT g; | 
					
						
							|  |  |  |     char s[64]; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |     YAP_BigNumOfTerm(t, (void *)&g); | 
					
						
							|  |  |  |     if (mpz_sizeinbase(&g,2) > 64) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |       Yap_Error(EVALUATION_ERROR_INT_OVERFLOW, Yap_GetFromSlot(t), "integer_overflow"); | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     mpz_get_str (s, 10, &g); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2010-02-10 03:03:03 -06:00
										 |  |  |     sscanf(s, "%I64d", (long long int *)val); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #else
 | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |     sscanf(s, "%lld", (long long int *)val); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #endif
 | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     #endif
 | 
					
						
							| 
									
										
										
										
											2009-06-04 10:21:24 -05:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   Yap_Error(TYPE_ERROR_ATOM, Yap_GetFromSlot(t), "integer_expression"); | 
					
						
							| 
									
										
										
										
											2009-06-01 18:01:30 -05:00
										 |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | foreign_t | 
					
						
							| 
									
										
										
										
											2011-03-15 23:49:28 +00:00
										 |  |  | _PL_retry(intptr_t v) | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-15 23:49:28 +00:00
										 |  |  |   return (((uintptr_t)(v)<<FRG_REDO_BITS)|REDO_INT); | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | foreign_t | 
					
						
							|  |  |  | _PL_retry_address(void *addr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-15 23:49:28 +00:00
										 |  |  |   return (((uintptr_t)(addr))|REDO_PTR); | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API int | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | PL_foreign_control(control_t ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch (ctx->control) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case FRG_REDO: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return PL_REDO; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case FRG_FIRST_CALL: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return PL_FIRST_CALL; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return PL_CUTTED; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API intptr_t | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | PL_foreign_context(control_t ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch (ctx->control) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case FRG_FIRST_CALL: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return 0L; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return (intptr_t)(ctx->context); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API void * | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  | PL_foreign_context_address(control_t ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch (ctx->control) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case FRG_FIRST_CALL: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2010-01-06 11:25:15 -02:00
										 |  |  |     return (void *)(ctx->context); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API int | 
					
						
							|  |  |  | PL_get_signum_ex(term_t sig, int *n) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   char *s; | 
					
						
							|  |  |  |   int i = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ( PL_get_integer(sig, &i) ) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   } else if ( IsAtomTerm(Yap_GetFromSlot(sig) ) ) | 
					
						
							|  |  |  |   { s = RepAtom(AtomOfTerm(Yap_GetFromSlot(sig)))->StrOfAE; | 
					
						
							|  |  |  |     i = Yap_signal_index(s); | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   else | 
					
						
							|  |  |  |   { Yap_Error(TYPE_ERROR_ATOM, Yap_GetFromSlot(sig), "signal handling"); | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | if ( i > 0 && i < 32 )		/* where to get these? */ | 
					
						
							|  |  |  | { *n = i; | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | Yap_Error(DOMAIN_ERROR_OUT_OF_RANGE, Yap_GetFromSlot(sig) , "signal handling"); | 
					
						
							|  |  |  | return FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-07-24 16:02:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | typedef struct blob { | 
					
						
							|  |  |  |   Functor f; | 
					
						
							|  |  |  |   CELL type; | 
					
						
							|  |  |  |   MP_INT blinfo;  /* total size should go here */ | 
					
						
							|  |  |  |   PL_blob_t *blb; | 
					
						
							|  |  |  |   size_t size; | 
					
						
							|  |  |  |   CELL  blob_data[1]; | 
					
						
							|  |  |  | } blob_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  | X_API intptr_t | 
					
						
							|  |  |  | PL_query(int query) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch(query) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_QUERY_ARGC: | 
					
						
							| 
									
										
										
										
											2011-05-25 16:40:36 +01:00
										 |  |  |     return (intptr_t)GLOBAL_argc; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_QUERY_ARGV: | 
					
						
							| 
									
										
										
										
											2011-05-25 16:40:36 +01:00
										 |  |  |     return (intptr_t)GLOBAL_argv; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_QUERY_USER_CPU: | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |     return (intptr_t)Yap_cputime(); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     case PL_QUERY_VERSION: | 
					
						
							| 
									
										
										
										
											2011-07-26 23:32:38 +01:00
										 |  |  |     return (intptr_t)60300; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  |     fprintf(stderr,"Unimplemented PL_query %d\n",query); | 
					
						
							|  |  |  |     return (intptr_t)0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-15 16:51:55 +00:00
										 |  |  | X_API void | 
					
						
							|  |  |  | PL_cleanup_fork(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-01-15 16:51:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 23:35:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | X_API void (*PL_signal(int sig, void (*func)(int)))(int) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   //  return Yap_signal2(sig,func);
 | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-06 02:09:48 +00:00
										 |  |  | X_API void PL_on_halt(int (*f)(int, void *), void *closure) | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-24 14:00:53 +01:00
										 |  |  |   Yap_HaltRegisterHook((HaltHookFunc)f,closure); | 
					
						
							| 
									
										
										
										
											2010-06-17 00:34:29 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-17 14:47:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  | #define is_signalled() unlikely(LD && LD->signal.pending != 0)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef O_PLMT
 | 
					
						
							|  |  |  | #include <pthread.h>
 | 
					
						
							|  |  |  | static pthread_key_t atomgen_key; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct scan_atoms { | 
					
						
							|  |  |  |   Int pos; | 
					
						
							|  |  |  |   Atom atom; | 
					
						
							|  |  |  | } scan_atoms_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | str_prefix(const char *p0, char *s) | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  |   char *p = (char *)p0; | 
					
						
							|  |  |  |   while (*p && *p == *s) { p++; s++; } | 
					
						
							|  |  |  |   return p[0] == '\0'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | atom_generator(const char *prefix, char **hit, int state) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     CACHE_REGS | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  |   struct scan_atoms *index; | 
					
						
							|  |  |  |   Atom            catom; | 
					
						
							|  |  |  |   Int            i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ( !state ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   { index = (struct scan_atoms *)malloc(sizeof(struct scan_atoms)); | 
					
						
							|  |  |  |     i = 0; | 
					
						
							|  |  |  |     catom = NIL; | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  |   } else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     index = LOCAL_search_atoms; | 
					
						
							|  |  |  |     catom = index->atom; | 
					
						
							|  |  |  |     i = index->pos; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (catom != NIL || i < AtomHashTableSize) { | 
					
						
							|  |  |  |     //    if ( is_signalled() )		/* Notably allow windows version */
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     //      PL_handle_signals();		/* to break out on ^C */
 | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  |     AtomEntry *ap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (catom == NIL) { | 
					
						
							|  |  |  |       /* move away from current hash table line */ | 
					
						
							|  |  |  |       READ_LOCK(HashChain[i].AERWLock); | 
					
						
							|  |  |  |       catom = HashChain[i].Entry; | 
					
						
							|  |  |  |       READ_UNLOCK(HashChain[i].AERWLock); | 
					
						
							|  |  |  |       i++; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       ap = RepAtom(catom); | 
					
						
							|  |  |  |       READ_LOCK(ap->ARWLock); | 
					
						
							|  |  |  |       if ( str_prefix(prefix, ap->StrOfAE) ) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |           CACHE_REGS | 
					
						
							|  |  |  |         index->pos = i; | 
					
						
							|  |  |  |         index->atom = ap->NextOfAE; | 
					
						
							|  |  |  |         LOCAL_search_atoms = index; | 
					
						
							|  |  |  |         *hit = ap->StrOfAE; | 
					
						
							|  |  |  |         READ_UNLOCK(ap->ARWLock); | 
					
						
							|  |  |  |         return TRUE; | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       catom = ap->NextOfAE; | 
					
						
							|  |  |  |       READ_UNLOCK(ap->ARWLock); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   LOCAL_search_atoms = NULL; | 
					
						
							|  |  |  |   free(index); | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | PL_atom_generator(const char *prefix, int state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char * hit = NULL; | 
					
						
							|  |  |  |   if (atom_generator(prefix, &hit, state)) { | 
					
						
							|  |  |  |     return hit; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-17 12:43:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  | X_API pl_wchar_t *PL_atom_generator_w(const pl_wchar_t *pref, pl_wchar_t *buffer, size_t buflen, int state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 19:01:18 +00:00
										 |  |  | const char *Yap_GetCurrentPredName(void); | 
					
						
							|  |  |  | Int Yap_GetCurrentPredArity(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char * | 
					
						
							|  |  |  | Yap_GetCurrentPredName(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-02-11 19:01:18 +00:00
										 |  |  |   if (!PP) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return NULL; | 
					
						
							| 
									
										
										
										
											2011-02-11 19:01:18 +00:00
										 |  |  |   if (PP->ArityOfPE) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return NameOfFunctor(PP->FunctorOfPred)->StrOfAE; | 
					
						
							| 
									
										
										
										
											2011-02-11 19:01:18 +00:00
										 |  |  |   return   RepAtom((Atom)(PP->FunctorOfPred))->StrOfAE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Int | 
					
						
							|  |  |  | Yap_GetCurrentPredArity(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-02-11 19:01:18 +00:00
										 |  |  |   if (!PP) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return (Int)0; | 
					
						
							| 
									
										
										
										
											2011-02-11 19:01:18 +00:00
										 |  |  |   return PP->ArityOfPE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-02-03 11:23:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2007-10-18 08:24:16 +00:00
										 |  |  | Yap_swi_install(void) | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-29 23:17:06 +00:00
										 |  |  |   Yap_install_blobs(); | 
					
						
							| 
									
										
										
										
											2007-10-10 09:44:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-12 18:42:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-23 22:41:12 +01:00
										 |  |  | X_API int | 
					
						
							|  |  |  | PL_raise(int sig) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-09-02 14:19:23 -05:00
										 |  |  |   Yap_signal(YAP_INT_SIGNAL); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   return 1; | 
					
						
							| 
									
										
										
										
											2014-04-23 22:41:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | raiseSignal(void *ld, int sig); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | raiseSignal(void *ld, int sig) | 
					
						
							| 
									
										
										
										
											2014-04-23 22:41:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #if THREADSX
 | 
					
						
							| 
									
										
										
										
											2014-04-23 22:41:12 +01:00
										 |  |  |   if (sig == SIG_THREAD_SIGNAL) { | 
					
						
							|  |  |  |     Yap_signal(YAP_ITI_SIGNAL); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     return TRUE; | 
					
						
							| 
									
										
										
										
											2014-04-23 22:41:12 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2014-04-23 22:41:12 +01:00
										 |  |  |   fprintf(stderr, "Unsupported signal %d\n", sig); | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 15:06:10 -08:00
										 |  |  | #if THREADS
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | void Yap_LockStream(void *s) | 
					
						
							| 
									
										
										
										
											2011-02-27 15:06:10 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   //  if ( s->mutex ) recursiveMutexLock(s->mutex);
 | 
					
						
							| 
									
										
										
										
											2011-02-27 15:06:10 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | void Yap_UnLockStream(void *s) | 
					
						
							| 
									
										
										
										
											2011-02-27 15:06:10 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   //  if ( s->mutex ) recursiveMutexUnlock(s->mutex);
 | 
					
						
							| 
									
										
										
										
											2011-02-27 15:06:10 -08:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 03:40:27 -08:00
										 |  |  | extern term_t Yap_CvtTerm(term_t ts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | term_t Yap_CvtTerm(term_t ts) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term t = Yap_GetFromSlot(ts); | 
					
						
							| 
									
										
										
										
											2011-02-27 03:40:27 -08:00
										 |  |  |   if (IsVarTerm(t)) return ts; | 
					
						
							|  |  |  |   if (IsPairTerm(t)) return ts; | 
					
						
							|  |  |  |   if (IsAtomTerm(t)) return ts; | 
					
						
							|  |  |  |   if (IsIntTerm(t)) return ts; | 
					
						
							|  |  |  |   if (IsApplTerm(t)) { | 
					
						
							|  |  |  |     Functor f = FunctorOfTerm(t); | 
					
						
							|  |  |  |     if (IsExtensionFunctor(f)) { | 
					
						
							|  |  |  |       if (f == FunctorBigInt) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         big_blob_type flag = RepAppl(t)[1]; | 
					
						
							|  |  |  |         switch (flag) { | 
					
						
							|  |  |  |           case BIG_INT: | 
					
						
							|  |  |  |           return ts; | 
					
						
							|  |  |  |           case BIG_RATIONAL: | 
					
						
							|  |  |  |           #if USE_GMP
 | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             MP_RAT *b = Yap_BigRatOfTerm(t); | 
					
						
							|  |  |  |             Term ta[2]; | 
					
						
							|  |  |  |             ta[0] = Yap_MkBigIntTerm(mpq_numref(b)); | 
					
						
							|  |  |  |             if (ta[0] == TermNil) | 
					
						
							|  |  |  |             return ts; | 
					
						
							|  |  |  |             ta[1] = Yap_MkBigIntTerm(mpq_denref(b)); | 
					
						
							|  |  |  |             if (ta[1] == TermNil) | 
					
						
							|  |  |  |             return ts; | 
					
						
							|  |  |  |             return Yap_InitSlot(Yap_MkApplTerm(FunctorRDiv, 2, ta)); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           #endif
 | 
					
						
							|  |  |  |           case EMPTY_ARENA: | 
					
						
							|  |  |  |           case ARRAY_INT: | 
					
						
							|  |  |  |           case ARRAY_FLOAT: | 
					
						
							|  |  |  |           case CLAUSE_LIST: | 
					
						
							|  |  |  |           case EXTERNAL_BLOB: | 
					
						
							|  |  |  |           return Yap_InitSlot(MkIntTerm(0)); | 
					
						
							|  |  |  |           default: | 
					
						
							|  |  |  |           return ts; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-03-11 19:49:32 +00:00
										 |  |  |       } else if (f == FunctorDBRef) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |         Term ta[0]; | 
					
						
							|  |  |  |         ta[0] = MkIntegerTerm((Int)DBRefOfTerm(t)); | 
					
						
							|  |  |  |         return Yap_InitSlot(Yap_MkApplTerm(FunctorDBREF, 1, ta)); | 
					
						
							| 
									
										
										
										
											2011-02-27 03:40:27 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return ts; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | char * PL_cwd(char *cwd, size_t cwdlen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return (char *)Yap_getcwd( (const char *)cwd, cwdlen ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-12 17:49:11 +01:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | * @} | 
					
						
							|  |  |  | * @} | 
					
						
							|  |  |  | */ |