📄 moduldef.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 06/05/06 */
/* */
/* DEFMODULE MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Defines basic defmodule primitive functions such */
/* as allocating and deallocating, traversing, and finding */
/* defmodule data structures. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Donnell */
/* */
/* Revision History: */
/* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
/* */
/* Corrected compilation errors for files */
/* generated by constructs-to-c. DR0861 */
/* */
/* 6.24: Renamed BOOLEAN macro type to intBool. */
/* */
/*************************************************************/
#define _MODULDEF_SOURCE_
#include "setup.h"
#include <stdio.h>
#include <string.h>
#define _STDIO_INCLUDED_
#include "memalloc.h"
#include "constant.h"
#include "router.h"
#include "extnfunc.h"
#include "argacces.h"
#include "constrct.h"
#include "modulpsr.h"
#include "modulcmp.h"
#include "modulbsc.h"
#include "utility.h"
#include "envrnmnt.h"
#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
#include "bload.h"
#include "modulbin.h"
#endif
#include "moduldef.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
#if (! RUN_TIME)
static void ReturnDefmodule(void *,struct defmodule *,intBool);
#endif
static void DeallocateDefmoduleData(void *);
/**************************************************************/
/* InitializeDefmodules: Initializes the defmodule construct. */
/**************************************************************/
globle void AllocateDefmoduleGlobals(
void *theEnv)
{
AllocateEnvironmentData(theEnv,DEFMODULE_DATA,sizeof(struct defmoduleData),NULL);
AddEnvironmentCleanupFunction(theEnv,"defmodules",DeallocateDefmoduleData,-1000);
DefmoduleData(theEnv)->CallModuleChangeFunctions = TRUE;
DefmoduleData(theEnv)->MainModuleRedefinable = TRUE;
}
/****************************************************/
/* DeallocateDefmoduleData: Deallocates environment */
/* data for the defmodule construct. */
/****************************************************/
static void DeallocateDefmoduleData(
void *theEnv)
{
struct moduleStackItem *tmpMSPtr, *nextMSPtr;
struct moduleItem *tmpMIPtr, *nextMIPtr;
#if (! RUN_TIME) && (! BLOAD_ONLY)
struct defmodule *tmpDMPtr, *nextDMPtr;
struct portConstructItem *tmpPCPtr, *nextPCPtr;
#endif
#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
int i;
unsigned long space;
#endif
#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
for (i = 0; i < DefmoduleData(theEnv)->BNumberOfDefmodules; i++)
{
if (DefmoduleData(theEnv)->DefmoduleArray[i].itemsArray != NULL)
{
rm(theEnv,DefmoduleData(theEnv)->DefmoduleArray[i].itemsArray,
sizeof(void *) * GetNumberOfModuleItems(theEnv));
}
}
space = DefmoduleData(theEnv)->BNumberOfDefmodules * sizeof(struct defmodule);
if (space != 0)
{
genlongfree(theEnv,(void *) DefmoduleData(theEnv)->DefmoduleArray,space);
DefmoduleData(theEnv)->ListOfDefmodules = NULL;
}
space = DefmoduleData(theEnv)->NumberOfPortItems * sizeof(struct portItem);
if (space != 0) genlongfree(theEnv,(void *) DefmoduleData(theEnv)->PortItemArray,space);
#endif
#if (! RUN_TIME) && (! BLOAD_ONLY)
tmpDMPtr = DefmoduleData(theEnv)->ListOfDefmodules;
while (tmpDMPtr != NULL)
{
nextDMPtr = tmpDMPtr->next;
ReturnDefmodule(theEnv,tmpDMPtr,TRUE);
tmpDMPtr = nextDMPtr;
}
tmpPCPtr = DefmoduleData(theEnv)->ListOfPortConstructItems;
while (tmpPCPtr != NULL)
{
nextPCPtr = tmpPCPtr->next;
rtn_struct(theEnv,portConstructItem,tmpPCPtr);
tmpPCPtr = nextPCPtr;
}
#endif
tmpMSPtr = DefmoduleData(theEnv)->ModuleStack;
while (tmpMSPtr != NULL)
{
nextMSPtr = tmpMSPtr->next;
rtn_struct(theEnv,moduleStackItem,tmpMSPtr);
tmpMSPtr = nextMSPtr;
}
tmpMIPtr = DefmoduleData(theEnv)->ListOfModuleItems;
while (tmpMIPtr != NULL)
{
nextMIPtr = tmpMIPtr->next;
rtn_struct(theEnv,moduleItem,tmpMIPtr);
tmpMIPtr = nextMIPtr;
}
#if (! RUN_TIME) && (! BLOAD_ONLY)
DeallocateCallList(theEnv,DefmoduleData(theEnv)->AfterModuleDefinedFunctions);
#endif
DeallocateCallList(theEnv,DefmoduleData(theEnv)->AfterModuleChangeFunctions);
}
/**************************************************************/
/* InitializeDefmodules: Initializes the defmodule construct. */
/**************************************************************/
globle void InitializeDefmodules(
void *theEnv)
{
DefmoduleBasicCommands(theEnv);
#if (! RUN_TIME)
CreateMainModule(theEnv);
#endif
#if DEFMODULE_CONSTRUCT && (! RUN_TIME) && (! BLOAD_ONLY)
AddConstruct(theEnv,"defmodule","defmodules",ParseDefmodule,NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL);
#endif
#if (! RUN_TIME) && DEFMODULE_CONSTRUCT
EnvDefineFunction2(theEnv,"get-current-module", 'w',
PTIEF GetCurrentModuleCommand,
"GetCurrentModuleCommand", "00");
EnvDefineFunction2(theEnv,"set-current-module", 'w',
PTIEF SetCurrentModuleCommand,
"SetCurrentModuleCommand", "11w");
#endif
}
/******************************************************/
/* RegisterModuleItem: Called to register a construct */
/* which can be placed within a module. */
/******************************************************/
globle int RegisterModuleItem(
void *theEnv,
char *theItem,
void *(*allocateFunction)(void *),
void (*freeFunction)(void *,void *),
void *(*bloadModuleReference)(void *,int),
void (*constructsToCModuleReference)(void *,FILE *,int,int,int),
void *(*findFunction)(void *,char *))
{
struct moduleItem *newModuleItem;
newModuleItem = get_struct(theEnv,moduleItem);
newModuleItem->name = theItem;
newModuleItem->allocateFunction = allocateFunction;
newModuleItem->freeFunction = freeFunction;
newModuleItem->bloadModuleReference = bloadModuleReference;
newModuleItem->constructsToCModuleReference = constructsToCModuleReference;
newModuleItem->findFunction = findFunction;
newModuleItem->moduleIndex = DefmoduleData(theEnv)->NumberOfModuleItems++;
newModuleItem->next = NULL;
if (DefmoduleData(theEnv)->LastModuleItem == NULL)
{
DefmoduleData(theEnv)->ListOfModuleItems = newModuleItem;
DefmoduleData(theEnv)->LastModuleItem = newModuleItem;
}
else
{
DefmoduleData(theEnv)->LastModuleItem->next = newModuleItem;
DefmoduleData(theEnv)->LastModuleItem = newModuleItem;
}
return(newModuleItem->moduleIndex);
}
/***********************************************************/
/* GetListOfModuleItems: Returns the list of module items. */
/***********************************************************/
globle struct moduleItem *GetListOfModuleItems(
void *theEnv)
{
return (DefmoduleData(theEnv)->ListOfModuleItems);
}
/***************************************************************/
/* GetNumberOfModuleItems: Returns the number of module items. */
/***************************************************************/
globle int GetNumberOfModuleItems(
void *theEnv)
{
return (DefmoduleData(theEnv)->NumberOfModuleItems);
}
/********************************************************/
/* FindModuleItem: Finds the module item data structure */
/* corresponding to the specified name. */
/********************************************************/
globle struct moduleItem *FindModuleItem(
void *theEnv,
char *theName)
{
struct moduleItem *theModuleItem;
for (theModuleItem = DefmoduleData(theEnv)->ListOfModuleItems;
theModuleItem != NULL;
theModuleItem = theModuleItem->next)
{ if (strcmp(theModuleItem->name,theName) == 0) return(theModuleItem); }
return(NULL);
}
/******************************************/
/* EnvGetCurrentModule: Returns a pointer */
/* to the current module. */
/******************************************/
globle void *EnvGetCurrentModule(
void *theEnv)
{
return ((void *) DefmoduleData(theEnv)->CurrentModule);
}
/**************************************************************/
/* EnvSetCurrentModule: Sets the value of the current module. */
/**************************************************************/
globle void *EnvSetCurrentModule(
void *theEnv,
void *xNewValue)
{
struct defmodule *newValue = (struct defmodule *) xNewValue;
struct callFunctionItem *changeFunctions;
void *rv;
/*=============================================*/
/* Change the current module to the specified */
/* module and save the previous current module */
/* for the return value. */
/*=============================================*/
rv = (void *) DefmoduleData(theEnv)->CurrentModule;
DefmoduleData(theEnv)->CurrentModule = newValue;
/*==========================================================*/
/* Call the list of registered functions that need to know */
/* when the module has changed. The module change functions */
/* should only be called if this is a "real" module change. */
/* Many routines temporarily change the module to look for */
/* constructs, etc. The SaveCurrentModule function will */
/* disable the change functions from being called. */
/*==========================================================*/
if (DefmoduleData(theEnv)->CallModuleChangeFunctions)
{
DefmoduleData(theEnv)->ModuleChangeIndex++;
changeFunctions = DefmoduleData(theEnv)->AfterModuleChangeFunctions;
while (changeFunctions != NULL)
{
(* (void (*)(void *)) changeFunctions->func)(theEnv);
changeFunctions = changeFunctions->next;
}
}
/*=====================================*/
/* Return the previous current module. */
/*=====================================*/
return(rv);
}
/********************************************************/
/* SaveCurrentModule: Saves current module on stack and */
/* prevents SetCurrentModule() from calling change */
/* functions */
/********************************************************/
globle void SaveCurrentModule(
void *theEnv)
{
MODULE_STACK_ITEM *tmp;
tmp = get_struct(theEnv,moduleStackItem);
tmp->changeFlag = DefmoduleData(theEnv)->CallModuleChangeFunctions;
DefmoduleData(theEnv)->CallModuleChangeFunctions = FALSE;
tmp->theModule = DefmoduleData(theEnv)->CurrentModule;
tmp->next = DefmoduleData(theEnv)->ModuleStack;
DefmoduleData(theEnv)->ModuleStack = tmp;
}
/**********************************************************/
/* RestoreCurrentModule: Restores saved module and resets */
/* ability of SetCurrentModule() to call changed */
/* functions to previous state */
/**********************************************************/
globle void RestoreCurrentModule(
void *theEnv)
{
MODULE_STACK_ITEM *tmp;
tmp = DefmoduleData(theEnv)->ModuleStack;
DefmoduleData(theEnv)->ModuleStack = tmp->next;
DefmoduleData(theEnv)->CallModuleChangeFunctions = tmp->changeFlag;
DefmoduleData(theEnv)->CurrentModule = tmp->theModule;
rtn_struct(theEnv,moduleStackItem,tmp);
}
/*************************************************************/
/* GetModuleItem: Returns the data pointer for the specified */
/* module item in the specified module. If no module is */
/* indicated, then the module item for the current module */
/* is returned. */
/*************************************************************/
globle void *GetModuleItem(
void *theEnv,
struct defmodule *theModule,
int moduleItemIndex)
{
if (theModule == NULL)
{
if (DefmoduleData(theEnv)->CurrentModule == NULL) return(NULL);
theModule = DefmoduleData(theEnv)->CurrentModule;
}
if (theModule->itemsArray == NULL) return (NULL);
return ((void *) theModule->itemsArray[moduleItemIndex]);
}
/************************************************************/
/* SetModuleItem: Sets the data pointer for the specified */
/* module item in the specified module. If no module is */
/* indicated, then the module item for the current module */
/* is returned. */
/************************************************************/
globle void SetModuleItem(
void *theEnv,
struct defmodule *theModule,
int moduleItemIndex,
void *newValue)
{
if (theModule == NULL)
{
if (DefmoduleData(theEnv)->CurrentModule == NULL) return;
theModule = DefmoduleData(theEnv)->CurrentModule;
}
if (theModule->itemsArray == NULL) return;
theModule->itemsArray[moduleItemIndex] = (struct defmoduleItemHeader *) newValue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -