1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 11/17/17 */
/* */
/* EVALUATION HEADER FILE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides routines for evaluating expressions. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.23: Correction for FalseSymbol/TrueSymbol. DR0859 */
/* */
/* 6.24: Renamed BOOLEAN macro type to intBool. */
/* */
/* Added EvaluateAndStoreInDataObject function. */
/* */
/* 6.30: Added support for passing context information */
/* to user defined functions. */
/* */
/* Added support for external address hash table */
/* and subtyping. */
/* */
/* Changed integer type/precision. */
/* */
/* Support for long long integers. */
/* */
/* Changed garbage collection algorithm. */
/* */
/* Support for DATA_OBJECT_ARRAY primitive. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* Converted API macros to function calls. */
/* */
/* 6.40: Added Env prefix to GetEvaluationError and */
/* SetEvaluationError functions. */
/* */
/* Added Env prefix to GetHaltExecution and */
/* SetHaltExecution functions. */
/* */
/* Removed LOCALE definition. */
/* */
/* Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/* */
/* UDF redesign. */
/* */
/* Removed DATA_OBJECT_ARRAY primitive type. */
/* */
/*************************************************************/
#ifndef _H_evaluatn
#pragma once
#define _H_evaluatn
#include "constant.h"
#include "entities.h"
typedef struct functionCallBuilder FunctionCallBuilder;
struct functionCallBuilder
{
Environment *fcbEnv;
CLIPSValue *contents;
size_t bufferReset;
size_t length;
size_t bufferMaximum;
};
typedef enum
{
FCBE_NO_ERROR = 0,
FCBE_NULL_POINTER_ERROR,
FCBE_FUNCTION_NOT_FOUND_ERROR,
FCBE_INVALID_FUNCTION_ERROR,
FCBE_ARGUMENT_COUNT_ERROR,
FCBE_ARGUMENT_TYPE_ERROR,
FCBE_PROCESSING_ERROR
} FunctionCallBuilderError;
#define PARAMETERS_UNBOUNDED USHRT_MAX
#define C_POINTER_EXTERNAL_ADDRESS 0
struct externalAddressType
{
const char *name;
void (*shortPrintFunction)(Environment *,const char *,void *);
void (*longPrintFunction)(Environment *,const char *,void *);
bool (*discardFunction)(Environment *,void *);
void (*newFunction)(UDFContext *,UDFValue *);
bool (*callFunction)(UDFContext *,UDFValue *,UDFValue *);
};
#define CoerceToLongInteger(t,v) ((t == INTEGER_TYPE) ? ValueToLong(v) : (long) ValueToDouble(v))
#define CoerceToInteger(t,v) ((t == INTEGER_TYPE) ? (int) ValueToLong(v) : (int) ValueToDouble(v))
#define CoerceToDouble(t,v) ((t == INTEGER_TYPE) ? (double) ValueToLong(v) : ValueToDouble(v))
#define GetFirstArgument() (EvaluationData(theEnv)->CurrentExpression->argList)
#define GetNextArgument(ep) (ep->nextArg)
#define MAXIMUM_PRIMITIVES 150
#define MAXIMUM_EXTERNAL_ADDRESS_TYPES 10
#define BITS_PER_BYTE 8
#define BitwiseTest(n,b) (((n) & (char) (1 << (b))) ? true : false)
#define BitwiseSet(n,b) (n |= (char) (1 << (b)))
#define BitwiseClear(n,b) (n &= (char) ~(1 << (b)))
#define CountToBitMapSize(c) (((c) + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE)
#define TestBitMap(map,id) BitwiseTest(map[(id) / BITS_PER_BYTE],(id) % BITS_PER_BYTE)
#define SetBitMap(map,id) BitwiseSet(map[(id) / BITS_PER_BYTE],(id) % BITS_PER_BYTE)
#define ClearBitMap(map,id) BitwiseClear(map[(id) / BITS_PER_BYTE],(id) % BITS_PER_BYTE)
#define EVALUATION_DATA 44
struct evaluationData
{
struct expr *CurrentExpression;
bool EvaluationError;
bool HaltExecution;
int CurrentEvaluationDepth;
int numberOfAddressTypes;
struct entityRecord *PrimitivesArray[MAXIMUM_PRIMITIVES];
struct externalAddressType *ExternalAddressTypes[MAXIMUM_EXTERNAL_ADDRESS_TYPES];
};
#define EvaluationData(theEnv) ((struct evaluationData *) GetEnvironmentData(theEnv,EVALUATION_DATA))
void InitializeEvaluationData(Environment *);
bool EvaluateExpression(Environment *,struct expr *,UDFValue *);
void SetEvaluationError(Environment *,bool);
bool GetEvaluationError(Environment *);
void SetHaltExecution(Environment *,bool);
bool GetHaltExecution(Environment *);
void ReturnValues(Environment *,UDFValue *,bool);
void WriteUDFValue(Environment *,const char *,UDFValue *);
void WriteCLIPSValue(Environment *,const char *,CLIPSValue *);
void SetMultifieldErrorValue(Environment *,UDFValue *);
void CopyDataObject(Environment *,UDFValue *,UDFValue *,int);
void AtomInstall(Environment *,unsigned short,void *);
void AtomDeinstall(Environment *,unsigned short,void *);
void Retain(Environment *,TypeHeader *);
void Release(Environment *,TypeHeader *);
void RetainCV(Environment *,CLIPSValue *);
void ReleaseCV(Environment *,CLIPSValue *);
void RetainUDFV(Environment *,UDFValue *);
void ReleaseUDFV(Environment *,UDFValue *);
struct expr *ConvertValueToExpression(Environment *,UDFValue *);
unsigned long GetAtomicHashValue(unsigned short,void *,unsigned short);
void InstallPrimitive(Environment *,struct entityRecord *,int);
int InstallExternalAddressType(Environment *,struct externalAddressType *);
void TransferDataObjectValues(UDFValue *,UDFValue *);
struct expr *FunctionReferenceExpression(Environment *,const char *);
bool GetFunctionReference(Environment *,const char *,Expression *);
bool DOsEqual(UDFValue *,UDFValue *);
bool EvaluateAndStoreInDataObject(Environment *,bool,Expression *,UDFValue *,bool);
void ResetErrorFlags(Environment *);
FunctionCallBuilder *CreateFunctionCallBuilder(Environment *,size_t);
void FCBAppendUDFValue(FunctionCallBuilder *,UDFValue *);
void FCBAppend(FunctionCallBuilder *,CLIPSValue *);
void FCBAppendCLIPSInteger(FunctionCallBuilder *,CLIPSInteger *);
void FCBAppendInteger(FunctionCallBuilder *,long long);
void FCBAppendCLIPSFloat(FunctionCallBuilder *,CLIPSFloat *);
void FCBAppendFloat(FunctionCallBuilder *,double);
void FCBAppendCLIPSLexeme(FunctionCallBuilder *,CLIPSLexeme *);
void FCBAppendSymbol(FunctionCallBuilder *,const char *);
void FCBAppendString(FunctionCallBuilder *,const char *);
void FCBAppendInstanceName(FunctionCallBuilder *,const char *);
void FCBAppendCLIPSExternalAddress(FunctionCallBuilder *,CLIPSExternalAddress *);
void FCBAppendFact(FunctionCallBuilder *,Fact *);
void FCBAppendInstance(FunctionCallBuilder *,Instance *);
void FCBAppendMultifield(FunctionCallBuilder *,Multifield *);
void FCBDispose(FunctionCallBuilder *);
void FCBReset(FunctionCallBuilder *);
FunctionCallBuilderError FCBCall(FunctionCallBuilder *,const char *,CLIPSValue *);
#define CVIsType(cv,cvType) (((1 << (((TypeHeader *) (cv)->value)->type)) & (cvType)) ? true : false)
#define ValueIsType(value,vType) ((1 << (((TypeHeader *) value)->type)) & (vType))
#define CVCoerceToFloat(cv) (((cv)->header->type == FLOAT_TYPE) ? \
((cv)->floatValue->contents) : \
((double) (cv)->integerValue->contents))
#define CVCoerceToInteger(cv) (((cv)->header->type == INTEGER_TYPE) ? \
((cv)->integerValue->contents) : \
((long long) (cv)->floatValue->contents))
#endif /* _H_evaluatn */