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
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Module_Name(CODEADDR cap)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
PredEntry *ap = (PredEntry *)cap;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
return(ModuleName[CurrentModule]);
|
2001-10-30 16:42:05 +00:00
|
|
|
else {
|
2001-04-09 20:54:03 +01:00
|
|
|
return (ModuleName[ap->ModuleOfPred]);
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static SMALLUNSGN
|
2001-04-09 20:54:03 +01:00
|
|
|
LookupModule(Term a)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2002-11-12 00:33:48 +00:00
|
|
|
for (i = 0; i < NoOfModules; ++i) {
|
|
|
|
if (ModuleName[i] == a) {
|
2001-04-09 20:54:03 +01:00
|
|
|
return (i);
|
2002-11-12 00:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
ModuleName[i = NoOfModules++] = a;
|
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);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
SMALLUNSGN
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_LookupModule(Term a)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return(LookupModule(a));
|
|
|
|
}
|
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;
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_unify_constant(ARG1, ModuleName[CurrentModule]))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (0);
|
|
|
|
t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
|
|
|
return (0);
|
2002-11-12 00:33:48 +00:00
|
|
|
CurrentModule = LookupModule(t);
|
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) */
|
2002-11-18 18:18:05 +00:00
|
|
|
if (!Yap_unify_constant(ARG1, ModuleName[CurrentModule]))
|
2001-04-09 20:54:03 +01:00
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:10:51 +01:00
|
|
|
static Int
|
|
|
|
p_change_module(void)
|
|
|
|
{ /* $change_module(New) */
|
2001-11-15 00:01:43 +00:00
|
|
|
SMALLUNSGN mod = LookupModule(Deref(ARG1));
|
|
|
|
CurrentModule = mod;
|
2001-06-06 20:10:51 +01:00
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
2001-10-30 16:42:05 +00:00
|
|
|
static Int
|
|
|
|
p_module_number(void)
|
2002-01-18 15:55:33 +00:00
|
|
|
{ /* $module_number(Mod,Num) */
|
|
|
|
Term tname = Deref(ARG1);
|
|
|
|
Term t;
|
|
|
|
if (IsVarTerm(tname)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(tname, ModuleName[IntOfTerm(Deref(ARG2))]));
|
2002-01-18 15:55:33 +00:00
|
|
|
}else {
|
|
|
|
t = MkIntTerm(LookupModule(Deref(ARG1)));
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_unify(t,ARG2);
|
2002-01-18 15:55:33 +00:00
|
|
|
ARG2 = t;
|
|
|
|
}
|
2001-11-15 00:01:43 +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)
|
|
|
|
{
|
|
|
|
Int mod = IntOfTerm(EXTRA_CBACK_ARG(1,1));
|
|
|
|
Term t = ModuleName[mod];
|
|
|
|
|
|
|
|
if (mod == NoOfModules) {
|
|
|
|
cut_fail();
|
|
|
|
}
|
|
|
|
EXTRA_CBACK_ARG(1,1) = MkIntTerm(mod+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());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2001-06-06 20:10:51 +01:00
|
|
|
ModuleName[PrimitivesModule = 0] =
|
2002-11-18 18:18:05 +00:00
|
|
|
MkAtomTerm(Yap_LookupAtom("prolog"));
|
2002-05-20 06:24:13 +01:00
|
|
|
ModuleName[1] =
|
2002-11-18 18:18:05 +00:00
|
|
|
MkAtomTerm(Yap_LookupAtom("user"));
|
2002-05-20 06:24:13 +01:00
|
|
|
NoOfModules = 2;
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = 0;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("$current_module", 2, p_current_module, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred("$current_module", 1, p_current_module1, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred("$change_module", 1, p_change_module, SafePredFlag|SyncPredFlag);
|
|
|
|
Yap_InitCPred("$module_number", 2, p_module_number, SafePredFlag);
|
|
|
|
Yap_InitCPredBack("$all_current_modules", 1, 1, init_current_module, cont_current_module,
|
2002-09-17 01:14:23 +01:00
|
|
|
SafePredFlag|SyncPredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|