2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
File: modules.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: module support *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
|
|
|
|
STATIC_PROTO(Int p_current_module, (void));
|
|
|
|
STATIC_PROTO(Int p_current_module1, (void));
|
|
|
|
|
|
|
|
#define ByteAdr(X) ((char *) &(X))
|
|
|
|
Term
|
2004-02-12 12:37:12 +00:00
|
|
|
Yap_Module_Name(PredEntry *ap)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2004-02-12 12:37:12 +00:00
|
|
|
Term mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (!ap->ModuleOfPred)
|
|
|
|
/* If the system predicate is a metacall I should return the
|
|
|
|
module for the metacall, which I will suppose has to be
|
|
|
|
reachable from the current module anyway.
|
|
|
|
|
|
|
|
So I will return the current module in case the system
|
|
|
|
predicate is a meta-call. Otherwise it will still work.
|
|
|
|
*/
|
2004-02-12 12:37:12 +00:00
|
|
|
mod = CurrentModule;
|
2001-10-30 16:42:05 +00:00
|
|
|
else {
|
2004-02-12 12:37:12 +00:00
|
|
|
mod = ap->ModuleOfPred;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
2004-02-12 12:37:12 +00:00
|
|
|
if (mod) return mod;
|
|
|
|
return TermProlog;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2004-08-11 17:14:55 +01:00
|
|
|
static int
|
2001-04-09 20:54:03 +01:00
|
|
|
LookupModule(Term a)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2003-08-27 14:37:10 +01:00
|
|
|
|
2004-02-13 23:53:20 +00:00
|
|
|
/* prolog module */
|
|
|
|
if (a == 0)
|
|
|
|
return 0;
|
2006-04-28 16:48:33 +01:00
|
|
|
LOCK(ModulesLock);
|
2002-11-12 00:33:48 +00:00
|
|
|
for (i = 0; i < NoOfModules; ++i) {
|
|
|
|
if (ModuleName[i] == a) {
|
2006-04-28 16:48:33 +01:00
|
|
|
UNLOCK(ModulesLock);
|
2004-02-12 12:37:12 +00:00
|
|
|
return i;
|
2002-11-12 00:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
ModuleName[i = NoOfModules++] = a;
|
2006-04-28 16:48:33 +01:00
|
|
|
UNLOCK(ModulesLock);
|
2002-04-09 16:12:14 +01:00
|
|
|
if (NoOfModules == MaxModules) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(SYSTEM_ERROR,a,"number of modules overflowed");
|
2002-04-09 16:12:14 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2004-08-11 17:14:55 +01:00
|
|
|
Term
|
|
|
|
Yap_Module(Term tmod)
|
|
|
|
{
|
|
|
|
return ModuleName[LookupModule(tmod)];
|
|
|
|
}
|
|
|
|
|
2004-02-12 12:37:12 +00:00
|
|
|
struct pred_entry *
|
|
|
|
Yap_ModulePred(Term mod)
|
|
|
|
{
|
|
|
|
return ModulePred[LookupModule(mod)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Yap_NewModulePred(Term mod, struct pred_entry *ap)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2004-02-12 12:37:12 +00:00
|
|
|
Term imod = LookupModule(mod);
|
|
|
|
ap->NextPredOfModule = ModulePred[imod];
|
|
|
|
ModulePred[imod] = ap;
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
2002-11-12 00:33:48 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
p_current_module(void)
|
|
|
|
{ /* $current_module(Old,New) */
|
|
|
|
Term t;
|
|
|
|
|
2004-02-12 12:37:12 +00:00
|
|
|
if (CurrentModule) {
|
|
|
|
if(!Yap_unify_constant(ARG1, CurrentModule))
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
if (!Yap_unify_constant(ARG1, TermProlog))
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
2004-02-12 12:37:12 +00:00
|
|
|
return FALSE;
|
|
|
|
if (t == TermProlog) {
|
2004-05-13 21:54:58 +01:00
|
|
|
CurrentModule = PROLOG_MODULE;
|
2004-02-12 12:37:12 +00:00
|
|
|
} else {
|
|
|
|
CurrentModule = t;
|
|
|
|
LookupModule(CurrentModule);
|
|
|
|
}
|
2001-06-06 20:10:51 +01:00
|
|
|
return (TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_current_module1(void)
|
|
|
|
{ /* $current_module(Old) */
|
2004-02-12 12:37:12 +00:00
|
|
|
if (CurrentModule)
|
|
|
|
return Yap_unify_constant(ARG1, CurrentModule);
|
|
|
|
return Yap_unify_constant(ARG1, TermProlog);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2001-06-06 20:10:51 +01:00
|
|
|
static Int
|
|
|
|
p_change_module(void)
|
|
|
|
{ /* $change_module(New) */
|
2004-02-12 12:37:12 +00:00
|
|
|
Term mod = Deref(ARG1);
|
|
|
|
LookupModule(mod);
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = mod;
|
2004-02-12 12:37:12 +00:00
|
|
|
return TRUE;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
2002-09-17 01:14:23 +01:00
|
|
|
static Int
|
|
|
|
cont_current_module(void)
|
|
|
|
{
|
2004-02-13 23:53:20 +00:00
|
|
|
Int imod = IntOfTerm(EXTRA_CBACK_ARG(1,1));
|
|
|
|
Term t = ModuleName[imod];
|
2002-09-17 01:14:23 +01:00
|
|
|
|
2006-04-28 16:48:33 +01:00
|
|
|
LOCK(ModulesLock);
|
2004-02-13 23:53:20 +00:00
|
|
|
if (imod == NoOfModules) {
|
2006-04-28 16:48:33 +01:00
|
|
|
UNLOCK(ModulesLock);
|
2002-09-17 01:14:23 +01:00
|
|
|
cut_fail();
|
|
|
|
}
|
2006-04-28 16:48:33 +01:00
|
|
|
UNLOCK(ModulesLock);
|
2004-02-13 23:53:20 +00:00
|
|
|
EXTRA_CBACK_ARG(1,1) = MkIntTerm(imod+1);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG1,t));
|
2002-09-17 01:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
init_current_module(void)
|
|
|
|
{ /* current_module(?ModuleName) */
|
|
|
|
EXTRA_CBACK_ARG(1,1) = MkIntTerm(0);
|
|
|
|
return (cont_current_module());
|
|
|
|
}
|
|
|
|
|
2004-02-06 02:26:23 +00:00
|
|
|
void
|
|
|
|
Yap_InitModulesC(void)
|
|
|
|
{
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$current_module", 2, p_current_module, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$current_module", 1, p_current_module1, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$change_module", 1, p_change_module, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
2004-02-06 02:26:23 +00:00
|
|
|
Yap_InitCPredBack("$all_current_modules", 1, 1, init_current_module, cont_current_module,
|
2004-11-18 22:32:40 +00:00
|
|
|
SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
2004-02-06 02:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitModules(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2004-02-13 23:53:20 +00:00
|
|
|
ModuleName[PROLOG_MODULE] =
|
2004-02-12 12:37:12 +00:00
|
|
|
TermProlog;
|
|
|
|
ModuleName[1] =
|
|
|
|
USER_MODULE;
|
|
|
|
ModuleName[2] =
|
|
|
|
IDB_MODULE;
|
|
|
|
ModuleName[3] =
|
|
|
|
ATTRIBUTES_MODULE;
|
|
|
|
ModuleName[4] =
|
|
|
|
CHARSIO_MODULE;
|
|
|
|
ModuleName[5] =
|
|
|
|
TERMS_MODULE;
|
2006-05-17 19:38:11 +01:00
|
|
|
ModuleName[6] =
|
|
|
|
SYSTEM_MODULE;
|
2006-08-02 19:18:31 +01:00
|
|
|
ModuleName[7] =
|
|
|
|
READUTIL_MODULE;
|
2006-08-25 20:50:35 +01:00
|
|
|
ModuleName[8] =
|
2006-12-27 01:32:38 +00:00
|
|
|
HACKS_MODULE;
|
|
|
|
ModuleName[9] =
|
2006-08-25 20:50:35 +01:00
|
|
|
GLOBALS_MODULE;
|
2006-12-27 01:32:38 +00:00
|
|
|
NoOfModules = 10;
|
2004-02-12 12:37:12 +00:00
|
|
|
CurrentModule = PROLOG_MODULE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|