📄 expressn.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 06/05/06 */
/* */
/* EXPRESSION MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Contains routines for creating, deleting, */
/* compacting, installing, and hashing expressions. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Donnell */
/* */
/* Revision History: */
/* */
/* 6.23: Changed name of variable exp to theExp */
/* because of Unix compiler warnings of shadowed */
/* definitions. */
/* */
/* 6.24: Corrected link errors with non-default */
/* setup.h configuration settings. */
/* */
/*************************************************************/
#define _EXPRESSN_SOURCE_
#include "setup.h"
#include <stdio.h>
#define _STDIO_INCLUDED_
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "bload.h"
#include "memalloc.h"
#include "envrnmnt.h"
#include "router.h"
#include "extnfunc.h"
#include "exprnops.h"
#include "prntutil.h"
#include "evaluatn.h"
#include "expressn.h"
#define PRIME_ONE 257
#define PRIME_TWO 263
#define PRIME_THREE 269
/****************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/****************************************/
#if (! RUN_TIME)
static long ListToPacked(struct expr *,
struct expr *,long);
static EXPRESSION_HN *FindHashedExpression(void *,EXPRESSION *,unsigned *,EXPRESSION_HN **);
static unsigned HashExpression(EXPRESSION *);
#endif
static void DeallocateExpressionData(void *);
/**************************************************/
/* InitExpressionData: Initializes the function */
/* pointers used in generating some expressions */
/* and the expression hash table. */
/**************************************************/
globle void InitExpressionData(
void *theEnv)
{
#if ! RUN_TIME
register unsigned i;
#endif
AllocateEnvironmentData(theEnv,EXPRESSION_DATA,sizeof(struct expressionData),DeallocateExpressionData);
#if ! RUN_TIME
InitExpressionPointers(theEnv);
ExpressionData(theEnv)->ExpressionHashTable = (EXPRESSION_HN **)
gm2(theEnv,(int) (sizeof(EXPRESSION_HN *) * EXPRESSION_HASH_SIZE));
for (i = 0 ; i < EXPRESSION_HASH_SIZE ; i++)
ExpressionData(theEnv)->ExpressionHashTable[i] = NULL;
#endif
}
/*****************************************/
/* DeallocateExpressionData: Deallocates */
/* environment data for expressions. */
/*****************************************/
static void DeallocateExpressionData(
void *theEnv)
{
#if ! RUN_TIME
int i;
EXPRESSION_HN *tmpPtr, *nextPtr;
#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
if (! Bloaded(theEnv))
#endif
{
for (i = 0; i < EXPRESSION_HASH_SIZE; i++)
{
tmpPtr = ExpressionData(theEnv)->ExpressionHashTable[i];
while (tmpPtr != NULL)
{
nextPtr = tmpPtr->next;
ReturnPackedExpression(theEnv,tmpPtr->exp);
rtn_struct(theEnv,exprHashNode,tmpPtr);
tmpPtr = nextPtr;
}
}
}
rm(theEnv,ExpressionData(theEnv)->ExpressionHashTable,
(int) (sizeof(EXPRESSION_HN *) * EXPRESSION_HASH_SIZE));
#else
#if MAC_MCW || IBM_MCW || MAC_XCD
#pragma unused(theEnv)
#endif
#endif
#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
if ((ExpressionData(theEnv)->NumberOfExpressions != 0) && Bloaded(theEnv))
{
genlongfree(theEnv,(void *) ExpressionData(theEnv)->ExpressionArray,
ExpressionData(theEnv)->NumberOfExpressions * sizeof(struct expr));
}
#endif
}
/****************************************************/
/* InitExpressionPointers: Initializes the function */
/* pointers used in generating some expressions. */
/****************************************************/
globle void InitExpressionPointers(
void *theEnv)
{
ExpressionData(theEnv)->PTR_AND = (void *) FindFunction(theEnv,"and");
ExpressionData(theEnv)->PTR_OR = (void *) FindFunction(theEnv,"or");
ExpressionData(theEnv)->PTR_EQ = (void *) FindFunction(theEnv,"eq");
ExpressionData(theEnv)->PTR_NEQ = (void *) FindFunction(theEnv,"neq");
ExpressionData(theEnv)->PTR_NOT = (void *) FindFunction(theEnv,"not");
if ((ExpressionData(theEnv)->PTR_AND == NULL) || (ExpressionData(theEnv)->PTR_OR == NULL) ||
(ExpressionData(theEnv)->PTR_EQ == NULL) || (ExpressionData(theEnv)->PTR_NEQ == NULL) || (ExpressionData(theEnv)->PTR_NOT == NULL))
{
SystemError(theEnv,"EXPRESSN",1);
EnvExitRouter(theEnv,EXIT_FAILURE);
}
}
/***************************************************/
/* ExpressionInstall: Increments the busy count of */
/* atomic data values found in an expression. */
/***************************************************/
globle void ExpressionInstall(
void *theEnv,
struct expr *expression)
{
if (expression == NULL) return;
while (expression != NULL)
{
AtomInstall(theEnv,expression->type,expression->value);
ExpressionInstall(theEnv,expression->argList);
expression = expression->nextArg;
}
}
/*****************************************************/
/* ExpressionDeinstall: Decrements the busy count of */
/* atomic data values found in an expression. */
/*****************************************************/
globle void ExpressionDeinstall(
void *theEnv,
struct expr *expression)
{
if (expression == NULL) return;
while (expression != NULL)
{
AtomDeinstall(theEnv,expression->type,expression->value);
ExpressionDeinstall(theEnv,expression->argList);
expression = expression->nextArg;
}
}
#if (! RUN_TIME)
/***********************************************************************/
/* PackExpression: Copies an expression (created using multiple memory */
/* requests) into an array (created using a single memory request) */
/* while maintaining all appropriate links in the expression. A */
/* packed expression requires less total memory because it reduces */
/* the overhead required for multiple memory allocations. */
/***********************************************************************/
globle struct expr *PackExpression(
void *theEnv,
struct expr *original)
{
struct expr *packPtr;
if (original == NULL) return (NULL);
packPtr = (struct expr *)
gm3(theEnv,(long) sizeof (struct expr) *
(long) ExpressionSize(original));
ListToPacked(original,packPtr,0L);
return(packPtr);
}
/***********************************************************/
/* ListToPacked: Copies a list of expressions to an array. */
/***********************************************************/
static long ListToPacked(
struct expr *original,
struct expr *destination,
long count)
{
long i;
if (original == NULL) { return(count); }
while (original != NULL)
{
i = count;
count++;
destination[i].type = original->type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -