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));
|
|
|
|
STD_PROTO(void InitModules, (void));
|
|
|
|
|
|
|
|
#define ByteAdr(X) ((char *) &(X))
|
|
|
|
Term
|
|
|
|
Module_Name(CODEADDR cap)
|
|
|
|
{
|
|
|
|
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-02-04 16:12:54 +00:00
|
|
|
SMALLUNSGN
|
2001-04-09 20:54:03 +01:00
|
|
|
LookupModule(Term a)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NoOfModules; ++i)
|
|
|
|
if (ModuleName[i] == a)
|
|
|
|
return (i);
|
|
|
|
ModuleName[i = NoOfModules++] = a;
|
2002-04-09 16:12:14 +01:00
|
|
|
if (NoOfModules == MaxModules) {
|
|
|
|
Error(SYSTEM_ERROR,a,"number of modules overflowed");
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_current_module(void)
|
|
|
|
{ /* $current_module(Old,New) */
|
|
|
|
Term t;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!unify_constant(ARG1, ModuleName[CurrentModule]))
|
|
|
|
return (0);
|
|
|
|
t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
|
|
|
return (0);
|
|
|
|
for (i = 0; i < NoOfModules; ++i)
|
|
|
|
if (ModuleName[i] == t) {
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = i;
|
2001-06-06 20:10:51 +01:00
|
|
|
return (TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = NoOfModules;
|
2001-06-06 20:10:51 +01:00
|
|
|
ModuleName[NoOfModules++] = t;
|
|
|
|
return (TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_current_module1(void)
|
|
|
|
{ /* $current_module(Old) */
|
|
|
|
if (!unify_constant(ARG1, ModuleName[CurrentModule]))
|
|
|
|
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)) {
|
|
|
|
return(unify(tname, ModuleName[IntOfTerm(Deref(ARG2))]));
|
|
|
|
}else {
|
|
|
|
t = MkIntTerm(LookupModule(Deref(ARG1)));
|
|
|
|
unify(t,ARG2);
|
|
|
|
ARG2 = t;
|
|
|
|
}
|
2001-11-15 00:01:43 +00:00
|
|
|
return(TRUE);
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
|
|
|
InitModules(void)
|
|
|
|
{
|
2001-06-06 20:10:51 +01:00
|
|
|
ModuleName[PrimitivesModule = 0] =
|
2001-04-09 20:54:03 +01:00
|
|
|
MkAtomTerm(LookupAtom("prolog"));
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
ModuleName[1] = MkAtomTerm(LookupAtom("user"));
|
|
|
|
InitCPred("$current_module", 2, p_current_module, SafePredFlag|SyncPredFlag);
|
|
|
|
InitCPred("$current_module", 1, p_current_module1, SafePredFlag|SyncPredFlag);
|
2001-06-06 20:10:51 +01:00
|
|
|
InitCPred("$change_module", 1, p_change_module, SafePredFlag|SyncPredFlag);
|
2001-11-15 00:01:43 +00:00
|
|
|
InitCPred("$module_number", 2, p_module_number, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|