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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 12/02/19 */
/* */
/* CONSTRAINT CONSTRUCTS-TO-C MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the constructs-to-c feature for */
/* constraint records. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.24: Added allowed-classes slot facet. */
/* */
/* Added environment parameter to GenClose. */
/* */
/* 6.30: Added support for path name argument to */
/* constructs-to-c. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/*************************************************************/
#include "setup.h"
#if CONSTRUCT_COMPILER && (! RUN_TIME)
#include "constant.h"
#include "conscomp.h"
#include "envrnmnt.h"
#include "memalloc.h"
#include "prntutil.h"
#include "router.h"
#include "sysdep.h"
#include "cstrncmp.h"
/***********************************************/
/* ConstraintsToCode: Produces the constraint */
/* record code for a run-time module created */
/* using the constructs-to-c function. */
/***********************************************/
void ConstraintsToCode(
Environment *theEnv,
const char *fileName,
const char *pathName,
char *fileNameBuffer,
unsigned fileID,
FILE *headerFP,
unsigned imageID,
unsigned maxIndices)
{
unsigned int i, j;
unsigned long count;
bool newHeader = true;
FILE *fp;
unsigned int version = 1;
int arrayVersion = 1;
unsigned long numberOfConstraints = 0;
CONSTRAINT_RECORD *tmpPtr;
/*===============================================*/
/* Count the total number of constraint records. */
/*===============================================*/
for (i = 0 ; i < SIZE_CONSTRAINT_HASH; i++)
{
for (tmpPtr = ConstraintData(theEnv)->ConstraintHashtable[i];
tmpPtr != NULL;
tmpPtr = tmpPtr->next)
{ tmpPtr->bsaveID = numberOfConstraints++; }
}
/*=====================================================*/
/* If dynamic constraint checking is disabled, then */
/* contraints won't be saved. If there are constraints */
/* which could be saved, then issue a warning message. */
/*=====================================================*/
if ((! GetDynamicConstraintChecking(theEnv)) && (numberOfConstraints != 0))
{
numberOfConstraints = 0;
PrintWarningID(theEnv,"CSTRNCMP",1,false);
WriteString(theEnv,STDWRN,"Constraints are not saved with a constructs-to-c image\n");
WriteString(theEnv,STDWRN," when dynamic constraint checking is disabled.\n");
}
if (numberOfConstraints == 0)
{ return; }
/*=================================================*/
/* Print the extern definition in the header file. */
/*=================================================*/
for (i = 1; i <= (numberOfConstraints / maxIndices) + 1 ; i++)
{ fprintf(headerFP,"extern CONSTRAINT_RECORD C%d_%d[];\n",imageID,i); }
/*==================*/
/* Create the file. */
/*==================*/
if ((fp = NewCFile(theEnv,fileName,pathName,fileNameBuffer,fileID,version,false)) == NULL) return;
/*===================*/
/* List the entries. */
/*===================*/
j = 0;
count = 0;
for (i = 0; i < SIZE_CONSTRAINT_HASH; i++)
{
for (tmpPtr = ConstraintData(theEnv)->ConstraintHashtable[i];
tmpPtr != NULL;
tmpPtr = tmpPtr->next)
{
if (newHeader)
{
fprintf(fp,"CONSTRAINT_RECORD C%d_%d[] = {\n",imageID,arrayVersion);
newHeader = false;
}
fprintf(fp,"{%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
tmpPtr->anyAllowed,
tmpPtr->symbolsAllowed,
tmpPtr->stringsAllowed,
tmpPtr->floatsAllowed,
tmpPtr->integersAllowed,
tmpPtr->instanceNamesAllowed,
tmpPtr->instanceAddressesAllowed,
tmpPtr->externalAddressesAllowed,
tmpPtr->factAddressesAllowed,
0, /* void allowed */
tmpPtr->anyRestriction,
tmpPtr->symbolRestriction,
tmpPtr->stringRestriction,
tmpPtr->floatRestriction,
tmpPtr->integerRestriction,
tmpPtr->classRestriction,
tmpPtr->instanceNameRestriction,
tmpPtr->multifieldsAllowed,
tmpPtr->singlefieldsAllowed,
tmpPtr->installed);
fprintf(fp,",0,"); /* bsaveIndex */
PrintHashedExpressionReference(theEnv,fp,tmpPtr->classList,imageID,maxIndices);
fprintf(fp,",");
PrintHashedExpressionReference(theEnv,fp,tmpPtr->restrictionList,imageID,maxIndices);
fprintf(fp,",");
PrintHashedExpressionReference(theEnv,fp,tmpPtr->minValue,imageID,maxIndices);
fprintf(fp,",");
PrintHashedExpressionReference(theEnv,fp,tmpPtr->maxValue,imageID,maxIndices);
fprintf(fp,",");
PrintHashedExpressionReference(theEnv,fp,tmpPtr->minFields,imageID,maxIndices);
fprintf(fp,",");
PrintHashedExpressionReference(theEnv,fp,tmpPtr->maxFields,imageID,maxIndices);
/* multifield slot */
fprintf(fp,",NULL");
/* next slot */
if (tmpPtr->next == NULL)
{ fprintf(fp,",NULL,"); }
else
{
if ((j + 1) >= maxIndices)
{ fprintf(fp,",&C%d_%d[%d],",imageID,arrayVersion + 1,0); }
else
{ fprintf(fp,",&C%d_%d[%d],",imageID,arrayVersion,j + 1); }
}
fprintf(fp,"%u,%u",tmpPtr->bucket,tmpPtr->count + 1);
count++;
j++;
if ((count == numberOfConstraints) || (j >= maxIndices))
{
fprintf(fp,"}};\n");
GenClose(theEnv,fp);
j = 0;
version++;
arrayVersion++;
if (count < numberOfConstraints)
{
if ((fp = NewCFile(theEnv,fileName,pathName,fileNameBuffer,1,version,false)) == NULL)
{ return; }
newHeader = true;
}
}
else
{ fprintf(fp,"},\n"); }
}
}
}
/**********************************************************/
/* PrintConstraintReference: Prints C code representation */
/* of a constraint record data structure reference. */
/**********************************************************/
void PrintConstraintReference(
Environment *theEnv,
FILE *fp,
CONSTRAINT_RECORD *cPtr,
unsigned int imageID,
unsigned int maxIndices)
{
if ((cPtr == NULL) || (! GetDynamicConstraintChecking(theEnv)))
{ fprintf(fp,"NULL"); }
else fprintf(fp,"&C%u_%ld[%ld]",imageID,
(cPtr->bsaveID / maxIndices) + 1,
cPtr->bsaveID % maxIndices);
}
#endif /* CONSTRUCT_COMPILER && (! RUN_TIME) */