#include "setup.h"
#if DEFINSTANCES_CONSTRUCT
#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
#include "bload.h"
#include "dfinsbin.h"
#endif
#if CONSTRUCT_COMPILER && (! RUN_TIME)
#include "dfinscmp.h"
#endif
#include "argacces.h"
#include "classcom.h"
#include "classfun.h"
#include "cstrccom.h"
#include "cstrcpsr.h"
#include "constant.h"
#include "constrct.h"
#include "envrnmnt.h"
#include "evaluatn.h"
#include "extnfunc.h"
#include "insfun.h"
#include "inspsr.h"
#include "memalloc.h"
#include "modulpsr.h"
#include "modulutl.h"
#include "pprint.h"
#include "prntutil.h"
#include "router.h"
#include "scanner.h"
#include "symbol.h"
#include "utility.h"
#include "defins.h"
#define ACTIVE_RLN "active"
#if (! BLOAD_ONLY) && (! RUN_TIME)
static bool ParseDefinstances(Environment *,const char *);
static CLIPSLexeme *ParseDefinstancesName(Environment *,const char *,bool *);
static void RemoveDefinstances(Environment *,Definstances *);
static void SaveDefinstances(Environment *,Defmodule *,const char *,void *);
#endif
#if ! RUN_TIME
static void *AllocateModule(Environment *);
static void ReturnModule(Environment *,void *);
static bool ClearDefinstancesReady(Environment *,void *);
static void CheckDefinstancesBusy(Environment *,ConstructHeader *,void *);
static void DestroyDefinstancesAction(Environment *,ConstructHeader *,void *);
#else
static void RuntimeDefinstancesAction(Environment *,ConstructHeader *,void *);
#endif
static void ResetDefinstances(Environment *,void *);
static void ResetDefinstancesAction(Environment *,ConstructHeader *,void *);
static void DeallocateDefinstancesData(Environment *);
void SetupDefinstances(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,DEFINSTANCES_DATA,sizeof(struct definstancesData),DeallocateDefinstancesData);
DefinstancesData(theEnv)->DefinstancesModuleIndex =
RegisterModuleItem(theEnv,"definstances",
#if (! RUN_TIME)
AllocateModule,
ReturnModule,
#else
NULL,NULL,
#endif
#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
BloadDefinstancesModuleRef,
#else
NULL,
#endif
#if CONSTRUCT_COMPILER && (! RUN_TIME)
DefinstancesCModuleReference,
#else
NULL,
#endif
(FindConstructFunction *) FindDefinstancesInModule);
DefinstancesData(theEnv)->DefinstancesConstruct =
AddConstruct(theEnv,"definstances","definstances",
#if (! BLOAD_ONLY) && (! RUN_TIME)
ParseDefinstances,
#else
NULL,
#endif
(FindConstructFunction *) FindDefinstances,
GetConstructNamePointer,GetConstructPPForm,
GetConstructModuleItem,
(GetNextConstructFunction *) GetNextDefinstances,
SetNextConstruct,
(IsConstructDeletableFunction *) DefinstancesIsDeletable,
(DeleteConstructFunction *) Undefinstances,
#if (! BLOAD_ONLY) && (! RUN_TIME)
(FreeConstructFunction *) RemoveDefinstances
#else
NULL
#endif
);
#if ! RUN_TIME
AddClearReadyFunction(theEnv,"definstances",ClearDefinstancesReady,0,NULL);
#if ! BLOAD_ONLY
AddUDF(theEnv,"undefinstances","v",1,1,"y",UndefinstancesCommand,"UndefinstancesCommand",NULL);
AddSaveFunction(theEnv,"definstances",SaveDefinstances,0,NULL);
#endif
#if DEBUGGING_FUNCTIONS
AddUDF(theEnv,"ppdefinstances","vs",1,2,";y;ldsyn",PPDefinstancesCommand,"PPDefinstancesCommand",NULL);
AddUDF(theEnv,"list-definstances","v",0,1,"y",ListDefinstancesCommand,"ListDefinstancesCommand",NULL);
#endif
AddUDF(theEnv,"get-definstances-list","m",0,1,"y",GetDefinstancesListFunction,"GetDefinstancesListFunction",NULL);
AddUDF(theEnv,"definstances-module","y",1,1,"y",GetDefinstancesModuleCommand,"GetDefinstancesModuleCommand",NULL);
#endif
AddResetFunction(theEnv,"definstances",ResetDefinstances,0,NULL);
#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
SetupDefinstancesBload(theEnv);
#endif
#if CONSTRUCT_COMPILER && (! RUN_TIME)
SetupDefinstancesCompiler(theEnv);
#endif
}
static void DeallocateDefinstancesData(
Environment *theEnv)
{
#if ! RUN_TIME
struct definstancesModule *theModuleItem;
Defmodule *theModule;
#if BLOAD || BLOAD_AND_BSAVE
if (Bloaded(theEnv)) return;
#endif
DoForAllConstructs(theEnv,DestroyDefinstancesAction,DefinstancesData(theEnv)->DefinstancesModuleIndex,false,NULL);
for (theModule = GetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = GetNextDefmodule(theEnv,theModule))
{
theModuleItem = (struct definstancesModule *)
GetModuleItem(theEnv,theModule,
DefinstancesData(theEnv)->DefinstancesModuleIndex);
rtn_struct(theEnv,definstancesModule,theModuleItem);
}
#else
#if MAC_XCD
#pragma unused(theEnv)
#endif
#endif
}
#if ! RUN_TIME
static void DestroyDefinstancesAction(
Environment *theEnv,
ConstructHeader *theConstruct,
void *buffer)
{
#if MAC_XCD
#pragma unused(buffer)
#endif
#if (! BLOAD_ONLY) && (! RUN_TIME)
struct definstances *theDefinstances = (struct definstances *) theConstruct;
if (theDefinstances == NULL) return;
ReturnPackedExpression(theEnv,theDefinstances->mkinstance);
DestroyConstructHeader(theEnv,&theDefinstances->header);
rtn_struct(theEnv,definstances,theDefinstances);
#else
#if MAC_XCD
#pragma unused(theConstruct,theEnv)
#endif
#endif
}
#endif
#if RUN_TIME
static void RuntimeDefinstancesAction(
Environment *theEnv,
ConstructHeader *theConstruct,
void *buffer)
{
#if MAC_XCD
#pragma unused(buffer)
#endif
Definstances *theDefinstances = (Definstances *) theConstruct;
theDefinstances->header.env = theEnv;
}
void DefinstancesRunTimeInitialize(
Environment *theEnv)
{
DoForAllConstructs(theEnv,RuntimeDefinstancesAction,DefinstancesData(theEnv)->DefinstancesModuleIndex,true,NULL);
}
#endif
Definstances *GetNextDefinstances(
Environment *theEnv,
Definstances *theDefinstances)
{
return (Definstances *) GetNextConstructItem(theEnv,&theDefinstances->header,
DefinstancesData(theEnv)->DefinstancesModuleIndex);
}
Definstances *FindDefinstances(
Environment *theEnv,
const char *name)
{
return (Definstances *) FindNamedConstructInModuleOrImports(theEnv,name,DefinstancesData(theEnv)->DefinstancesConstruct);
}
Definstances *FindDefinstancesInModule(
Environment *theEnv,
const char *name)
{
return (Definstances *) FindNamedConstructInModule(theEnv,name,DefinstancesData(theEnv)->DefinstancesConstruct);
}
bool DefinstancesIsDeletable(
Definstances *theDefinstances)
{
Environment *theEnv = theDefinstances->header.env;
if (! ConstructsDeletable(theEnv))
{ return false; }
return (theDefinstances->busy == 0) ? true : false;
}
void UndefinstancesCommand(
Environment *theEnv,
UDFContext *context,
UDFValue *returnValue)
{
UndefconstructCommand(context,"undefinstances",DefinstancesData(theEnv)->DefinstancesConstruct);
}
void GetDefinstancesModuleCommand(
Environment *theEnv,
UDFContext *context,
UDFValue *returnValue)
{
returnValue->value = GetConstructModuleCommand(context,"definstances-module",DefinstancesData(theEnv)->DefinstancesConstruct);
}
bool Undefinstances(
Definstances *theDefinstances,
Environment *allEnv)
{
Environment *theEnv;
if (theDefinstances == NULL)
{
theEnv = allEnv;
return Undefconstruct(theEnv,NULL,DefinstancesData(theEnv)->DefinstancesConstruct);
}
else
{
theEnv = theDefinstances->header.env;
return Undefconstruct(theEnv,&theDefinstances->header,DefinstancesData(theEnv)->DefinstancesConstruct);
}
}
#if DEBUGGING_FUNCTIONS
void PPDefinstancesCommand(
Environment *theEnv,
UDFContext *context,
UDFValue *returnValue)
{
PPConstructCommand(context,"ppdefinstances",DefinstancesData(theEnv)->DefinstancesConstruct,returnValue);
}
void ListDefinstancesCommand(
Environment *theEnv,
UDFContext *context,
UDFValue *returnValue)
{
ListConstructCommand(context,DefinstancesData(theEnv)->DefinstancesConstruct);
}
void ListDefinstances(
Environment *theEnv,
const char *logicalName,
Defmodule *theModule)
{
ListConstruct(theEnv,DefinstancesData(theEnv)->DefinstancesConstruct,logicalName,theModule);
}
#endif
void GetDefinstancesListFunction(
Environment *theEnv,
UDFContext *context,
UDFValue *returnValue)
{
GetConstructListFunction(context,returnValue,DefinstancesData(theEnv)->DefinstancesConstruct);
}
void GetDefinstancesList(
Environment *theEnv,
CLIPSValue *returnValue,
Defmodule *theModule)
{
UDFValue result;
GetConstructList(theEnv,&result,DefinstancesData(theEnv)->DefinstancesConstruct,theModule);
NormalizeMultifield(theEnv,&result);
returnValue->value = result.value;
}
#if (! BLOAD_ONLY) && (! RUN_TIME)
static bool ParseDefinstances(
Environment *theEnv,
const char *readSource)
{
CLIPSLexeme *dname;
struct functionDefinition *mkinsfcall;
Expression *mkinstance,*mkbot = NULL;
Definstances *dobj;
bool active;
SetPPBufferStatus(theEnv,true);
FlushPPBuffer(theEnv);
SetIndentDepth(theEnv,3);
SavePPBuffer(theEnv,"(definstances ");
#if BLOAD || BLOAD_AND_BSAVE
if ((Bloaded(theEnv)) && (! ConstructData(theEnv)->CheckSyntaxMode))
{
CannotLoadWithBloadMessage(theEnv,"definstances");
return true;
}
#endif
dname = ParseDefinstancesName(theEnv,readSource,&active);
if (dname == NULL)
return true;
dobj = get_struct(theEnv,definstances);
InitializeConstructHeader(theEnv,"definstances",DEFINSTANCES,&dobj->header,dname);
dobj->busy = 0;
dobj->mkinstance = NULL;
#if DEFRULE_CONSTRUCT
if (active)
mkinsfcall = FindFunction(theEnv,"active-make-instance");
else
mkinsfcall = FindFunction(theEnv,"make-instance");
#else
mkinsfcall = FindFunction(theEnv,"make-instance");
#endif
while (DefclassData(theEnv)->ObjectParseToken.tknType == LEFT_PARENTHESIS_TOKEN)
{
mkinstance = GenConstant(theEnv,UNKNOWN_VALUE,mkinsfcall);
mkinstance = ParseInitializeInstance(theEnv,mkinstance,readSource);
if (mkinstance == NULL)
{
ReturnExpression(theEnv,dobj->mkinstance);
rtn_struct(theEnv,definstances,dobj);
return true;
}
if (ExpressionContainsVariables(mkinstance,false) == true)
{
LocalVariableErrorMessage(theEnv,"definstances");
ReturnExpression(theEnv,mkinstance);
ReturnExpression(theEnv,dobj->mkinstance);
rtn_struct(theEnv,definstances,dobj);
return true;
}
if (mkbot == NULL)
dobj->mkinstance = mkinstance;
else
GetNextArgument(mkbot) = mkinstance;
mkbot = mkinstance;
GetToken(theEnv,readSource,&DefclassData(theEnv)->ObjectParseToken);
PPBackup(theEnv);
PPCRAndIndent(theEnv);
SavePPBuffer(theEnv,DefclassData(theEnv)->ObjectParseToken.printForm);
}
if (DefclassData(theEnv)->ObjectParseToken.tknType != RIGHT_PARENTHESIS_TOKEN)
{
ReturnExpression(theEnv,dobj->mkinstance);
rtn_struct(theEnv,definstances,dobj);
SyntaxErrorMessage(theEnv,"definstances");
return true;
}
else
{
if (ConstructData(theEnv)->CheckSyntaxMode)
{
ReturnExpression(theEnv,dobj->mkinstance);
rtn_struct(theEnv,definstances,dobj);
return false;
}
#if DEBUGGING_FUNCTIONS
if (GetConserveMemory(theEnv) == false)
{
if (dobj->mkinstance != NULL)
PPBackup(theEnv);
PPBackup(theEnv);
SavePPBuffer(theEnv,")\n");
SetDefinstancesPPForm(theEnv,dobj,CopyPPBuffer(theEnv));
}
#endif
mkinstance = dobj->mkinstance;
dobj->mkinstance = PackExpression(theEnv,mkinstance);
ReturnExpression(theEnv,mkinstance);
IncrementLexemeCount(GetDefinstancesNamePointer(theEnv,dobj));
ExpressionInstall(theEnv,dobj->mkinstance);
}
AddConstructToModule(&dobj->header);
return false;
}
static CLIPSLexeme *ParseDefinstancesName(
Environment *theEnv,
const char *readSource,
bool *active)
{
CLIPSLexeme *dname;
*active = false;
dname = GetConstructNameAndComment(theEnv,readSource,&DefclassData(theEnv)->ObjectParseToken,"definstances",
(FindConstructFunction *) FindDefinstancesInModule,
(DeleteConstructFunction *) Undefinstances,"@",
true,false,true,false);
if (dname == NULL)
return NULL;
#if DEFRULE_CONSTRUCT
if ((DefclassData(theEnv)->ObjectParseToken.tknType != SYMBOL_TOKEN) ? false :
(strcmp(DefclassData(theEnv)->ObjectParseToken.lexemeValue->contents,ACTIVE_RLN) == 0))
{
PPBackup(theEnv);
PPBackup(theEnv);
SavePPBuffer(theEnv," ");
SavePPBuffer(theEnv,DefclassData(theEnv)->ObjectParseToken.printForm);
PPCRAndIndent(theEnv);
GetToken(theEnv,readSource,&DefclassData(theEnv)->ObjectParseToken);
*active = true;
}
#endif
if (DefclassData(theEnv)->ObjectParseToken.tknType == STRING_TOKEN)
{
PPBackup(theEnv);
PPBackup(theEnv);
SavePPBuffer(theEnv," ");
SavePPBuffer(theEnv,DefclassData(theEnv)->ObjectParseToken.printForm);
PPCRAndIndent(theEnv);
GetToken(theEnv,readSource,&DefclassData(theEnv)->ObjectParseToken);
}
return(dname);
}
static void RemoveDefinstances(
Environment *theEnv,
Definstances *theDefinstances)
{
ReleaseLexeme(theEnv,theDefinstances->header.name);
ExpressionDeinstall(theEnv,theDefinstances->mkinstance);
ReturnPackedExpression(theEnv,theDefinstances->mkinstance);
SetDefinstancesPPForm(theEnv,theDefinstances,NULL);
ClearUserDataList(theEnv,theDefinstances->header.usrData);
rtn_struct(theEnv,definstances,theDefinstances);
}
static void SaveDefinstances(
Environment *theEnv,
Defmodule *theModule,
const char *logName,
void *context)
{
SaveConstruct(theEnv,theModule,logName,DefinstancesData(theEnv)->DefinstancesConstruct);
}
#endif
#if ! RUN_TIME
static void *AllocateModule(
Environment *theEnv)
{
return (void *) get_struct(theEnv,definstancesModule);
}
static void ReturnModule(
Environment *theEnv,
void *theItem)
{
#if (! BLOAD_ONLY)
FreeConstructHeaderModule(theEnv,(struct defmoduleItemHeader *) theItem,DefinstancesData(theEnv)->DefinstancesConstruct);
#endif
rtn_struct(theEnv,definstancesModule,theItem);
}
static bool ClearDefinstancesReady(
Environment *theEnv,
void *context)
{
bool flagBuffer = true;
DoForAllConstructs(theEnv,CheckDefinstancesBusy,DefinstancesData(theEnv)->DefinstancesModuleIndex,
false,&flagBuffer);
return(flagBuffer);
}
static void CheckDefinstancesBusy(
Environment *theEnv,
ConstructHeader *theDefinstances,
void *userBuffer)
{
#if MAC_XCD
#pragma unused(theEnv)
#endif
if (((Definstances *) theDefinstances)->busy > 0)
{ *((bool *) userBuffer) = false; }
}
#endif
static void ResetDefinstances(
Environment *theEnv,
void *context)
{
DoForAllConstructs(theEnv,ResetDefinstancesAction,DefinstancesData(theEnv)->DefinstancesModuleIndex,true,NULL);
}
static void ResetDefinstancesAction(
Environment *theEnv,
ConstructHeader *vDefinstances,
void *userBuffer)
{
#if MAC_XCD
#pragma unused(userBuffer)
#endif
Definstances *theDefinstances = (Definstances *) vDefinstances;
Expression *theExp;
UDFValue temp;
SaveCurrentModule(theEnv);
SetCurrentModule(theEnv,vDefinstances->whichModule->theModule);
theDefinstances->busy++;
for (theExp = theDefinstances->mkinstance ;
theExp != NULL ;
theExp = GetNextArgument(theExp))
{
EvaluateExpression(theEnv,theExp,&temp);
if (EvaluationData(theEnv)->HaltExecution ||
(temp.value == FalseSymbol(theEnv)))
{
RestoreCurrentModule(theEnv);
theDefinstances->busy--;
return;
}
}
theDefinstances->busy--;
RestoreCurrentModule(theEnv);
}
const char *DefinstancesName(
Definstances *theDefinstances)
{
return GetConstructNameString(&theDefinstances->header);
}
const char *DefinstancesPPForm(
Definstances *theDefinstances)
{
return GetConstructPPForm(&theDefinstances->header);
}
void SetDefinstancesPPForm(
Environment *theEnv,
Definstances *theDefinstances,
const char *thePPForm)
{
SetConstructPPForm(theEnv,&theDefinstances->header,thePPForm);
}
const char *DefinstancesModule(
Definstances *theDefinstances)
{
return GetConstructModuleName(&theDefinstances->header);
}
CLIPSLexeme *GetDefinstancesNamePointer(
Environment *theEnv,
Definstances *theDefinstances)
{
return GetConstructNamePointer(&theDefinstances->header);
}
const char *DefinstancesModuleName(
Environment *theEnv,
Definstances *theDefinstances)
{
return GetConstructModuleName(&theDefinstances->header);
}
#endif