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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 05/29/19 */
/* */
/* EXPRESSION OPERATIONS MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides utility routines for manipulating and */
/* examining expressions. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* Brian L. Dantes */
/* */
/* Revision History: */
/* */
/* 6.24: Renamed BOOLEAN macro type to intBool. */
/* */
/* 6.30: Add NegateExpression function. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* 6.31: Removed FACT_ADDRESS type from */
/* ExpressionContainsVariables function. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* UDF redesign. */
/* */
/* Eval support for run time and bload only. */
/* */
/*************************************************************/
#include "setup.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cstrnchk.h"
#include "cstrnops.h"
#include "cstrnutl.h"
#include "envrnmnt.h"
#include "extnfunc.h"
#include "memalloc.h"
#include "prntutil.h"
#include "router.h"
#include "exprnops.h"
/************************************/
/* CheckArgumentAgainstRestriction: */
/************************************/
bool CheckArgumentAgainstRestriction(
Environment *theEnv,
struct expr *theExpression,
unsigned theRestriction)
{
CONSTRAINT_RECORD *cr1, *cr2, *cr3;
/*=============================================*/
/* Generate a constraint record for the actual */
/* argument passed to the function. */
/*=============================================*/
cr1 = ExpressionToConstraintRecord(theEnv,theExpression);
/*================================================*/
/* Generate a constraint record based on the type */
/* of argument expected by the function. */
/*================================================*/
cr2 = ArgumentTypeToConstraintRecord(theEnv,theRestriction);
/*===============================================*/
/* Intersect the two constraint records and then */
/* discard them. */
/*===============================================*/
cr3 = IntersectConstraints(theEnv,cr1,cr2);
RemoveConstraint(theEnv,cr1);
RemoveConstraint(theEnv,cr2);
/*====================================================*/
/* If the intersection of the two constraint records */
/* is empty, then the argument passed to the function */
/* doesn't satisfy the restrictions for the argument. */
/*====================================================*/
if (UnmatchableConstraint(cr3))
{
RemoveConstraint(theEnv,cr3);
return true;
}
/*===================================================*/
/* The argument satisfies the function restrictions. */
/*===================================================*/
RemoveConstraint(theEnv,cr3);
return false;
}
/******************************************************/
/* ConstantExpression: Returns true if the expression */
/* is a constant, otherwise false. */
/******************************************************/
bool ConstantExpression(
struct expr *testPtr)
{
while (testPtr != NULL)
{
if ((testPtr->type != SYMBOL_TYPE) && (testPtr->type != STRING_TYPE) &&
#if OBJECT_SYSTEM
(testPtr->type != INSTANCE_NAME_TYPE) && (testPtr->type != INSTANCE_ADDRESS_TYPE) &&
#endif
(testPtr->type != INTEGER_TYPE) && (testPtr->type != FLOAT_TYPE))
{ return false; }
testPtr = testPtr->nextArg;
}
return true;
}
/******************************************/
/* ConstantType: Returns true if the type */
/* is a constant, otherwise false. */
/******************************************/
bool ConstantType(
int theType)
{
switch (theType)
{
case SYMBOL_TYPE:
case STRING_TYPE:
case INTEGER_TYPE:
case FLOAT_TYPE:
#if OBJECT_SYSTEM
case INSTANCE_NAME_TYPE:
case INSTANCE_ADDRESS_TYPE:
#endif
return true;
}
return false;
}
/*****************************************************************************/
/* IdenticalExpression: Determines if two expressions are identical. Returns */
/* true if the expressions are identical, otherwise false is returned. */
/*****************************************************************************/
bool IdenticalExpression(
struct expr *firstList,
struct expr *secondList)
{
/*==============================================*/
/* Compare each argument in both expressions by */
/* following the nextArg list. */
/*==============================================*/
for (;
(firstList != NULL) && (secondList != NULL);
firstList = firstList->nextArg, secondList = secondList->nextArg)
{
/*=========================*/
/* Compare type and value. */
/*=========================*/
if (firstList->type != secondList->type)
{ return false; }
if (firstList->value != secondList->value)
{ return false; }
/*==============================*/
/* Compare the arguments lists. */
/*==============================*/
if (IdenticalExpression(firstList->argList,secondList->argList) == false)
{ return false; }
}
/*=====================================================*/
/* If firstList and secondList aren't both NULL, then */
/* one of the lists contains more expressions than the */
/* other. */
/*=====================================================*/
if (firstList != secondList) return false;
/*============================*/
/* Expressions are identical. */
/*============================*/
return true;
}
/****************************************************/
/* CountArguments: Returns the number of structures */
/* stored in an expression as traversed through */
/* the nextArg pointer but not the argList */
/* pointer. */
/****************************************************/
unsigned short CountArguments(
struct expr *testPtr)
{
unsigned short size = 0;
while (testPtr != NULL)
{
size++;
testPtr = testPtr->nextArg;
}
return size;
}
/******************************************/
/* CopyExpresssion: Copies an expression. */
/******************************************/
struct expr *CopyExpression(
Environment *theEnv,
struct expr *original)
{
struct expr *topLevel, *next, *last;
if (original == NULL) return NULL;
topLevel = GenConstant(theEnv,original->type,original->value);
topLevel->argList = CopyExpression(theEnv,original->argList);
last = topLevel;
original = original->nextArg;
while (original != NULL)
{
next = GenConstant(theEnv,original->type,original->value);
next->argList = CopyExpression(theEnv,original->argList);
last->nextArg = next;
last = next;
original = original->nextArg;
}
return(topLevel);
}
/************************************************************/
/* ExpressionContainsVariables: Determines if an expression */
/* contains any variables. Returns true if the expression */
/* contains any variables, otherwise false is returned. */
/************************************************************/
bool ExpressionContainsVariables(
struct expr *theExpression,
bool globalsAreVariables)
{
while (theExpression != NULL)
{
if (theExpression->argList != NULL)
{
if (ExpressionContainsVariables(theExpression->argList,globalsAreVariables))
{ return true; }
}
if ((theExpression->type == MF_VARIABLE) ||
(theExpression->type == SF_VARIABLE) ||
(((theExpression->type == GBL_VARIABLE) ||
(theExpression->type == MF_GBL_VARIABLE)) &&
(globalsAreVariables == true)))
{ return true; }
theExpression = theExpression->nextArg;
}
return false;
}
/*****************************************/
/* ExpressionSize: Returns the number of */
/* structures stored in an expression. */
/*****************************************/
unsigned long ExpressionSize(
struct expr *testPtr)
{
unsigned long size = 0;
while (testPtr != NULL)
{
size++;
if (testPtr->argList != NULL)
{ size += ExpressionSize(testPtr->argList); }
testPtr = testPtr->nextArg;
}
return size;
}
/************************************************/
/* GenConstant: Generates a constant expression */
/* value of type string, symbol, or number. */
/************************************************/
struct expr *GenConstant(
Environment *theEnv,
unsigned short type,
void *value)
{
struct expr *top;
top = get_struct(theEnv,expr);
top->nextArg = NULL;
top->argList = NULL;
top->type = type;
top->value = value;
return top;
}
/*************************************************/
/* PrintExpression: Pretty prints an expression. */
/*************************************************/
void PrintExpression(
Environment *theEnv,
const char *fileid,
struct expr *theExpression)
{
struct expr *oldExpression;
if (theExpression == NULL)
{ return; }
while (theExpression != NULL)
{
switch (theExpression->type)
{
case SF_VARIABLE:
case GBL_VARIABLE:
WriteString(theEnv,fileid,"?");
WriteString(theEnv,fileid,theExpression->lexemeValue->contents);
break;
case MF_VARIABLE:
case MF_GBL_VARIABLE:
WriteString(theEnv,fileid,"$?");
WriteString(theEnv,fileid,theExpression->lexemeValue->contents);
break;
case FCALL:
WriteString(theEnv,fileid,"(");
WriteString(theEnv,fileid,ExpressionFunctionCallName(theExpression)->contents);
if (theExpression->argList != NULL) { WriteString(theEnv,fileid," "); }
PrintExpression(theEnv,fileid,theExpression->argList);
WriteString(theEnv,fileid,")");
break;
default:
oldExpression = EvaluationData(theEnv)->CurrentExpression;
EvaluationData(theEnv)->CurrentExpression = theExpression;
PrintAtom(theEnv,fileid,theExpression->type,theExpression->value);
EvaluationData(theEnv)->CurrentExpression = oldExpression;
break;
}
theExpression = theExpression->nextArg;
if (theExpression != NULL) WriteString(theEnv,fileid," ");
}
return;
}
/*************************************************************************/
/* CombineExpressions: Combines two expressions into a single equivalent */
/* expression. Mainly serves to merge expressions containing "and" */
/* and "or" expressions without unnecessary duplication of the "and" */
/* and "or" expressions (i.e., two "and" expressions can be merged by */
/* placing them as arguments within another "and" expression, but it */
/* is more efficient to add the arguments of one of the "and" */
/* expressions to the list of arguments for the other and expression). */
/*************************************************************************/
struct expr *CombineExpressions(
Environment *theEnv,
struct expr *expr1,
struct expr *expr2)
{
struct expr *tempPtr;
/*===========================================================*/
/* If the 1st expression is NULL, return the 2nd expression. */
/*===========================================================*/
if (expr1 == NULL) return(expr2);
/*===========================================================*/
/* If the 2nd expression is NULL, return the 1st expression. */
/*===========================================================*/
if (expr2 == NULL) return(expr1);
/*============================================================*/
/* If the 1st expression is an "and" expression, and the 2nd */
/* expression is not an "and" expression, then include the */
/* 2nd expression in the argument list of the 1st expression. */
/*============================================================*/
if ((expr1->value == ExpressionData(theEnv)->PTR_AND) &&
(expr2->value != ExpressionData(theEnv)->PTR_AND))
{
tempPtr = expr1->argList;
if (tempPtr == NULL)
{
rtn_struct(theEnv,expr,expr1);
return(expr2);
}
while (tempPtr->nextArg != NULL)
{ tempPtr = tempPtr->nextArg; }
tempPtr->nextArg = expr2;
return(expr1);
}
/*============================================================*/
/* If the 2nd expression is an "and" expression, and the 1st */
/* expression is not an "and" expression, then include the */
/* 1st expression in the argument list of the 2nd expression. */
/*============================================================*/
if ((expr1->value != ExpressionData(theEnv)->PTR_AND) &&
(expr2->value == ExpressionData(theEnv)->PTR_AND))
{
tempPtr = expr2->argList;
if (tempPtr == NULL)
{
rtn_struct(theEnv,expr,expr2);
return(expr1);
}
expr2->argList = expr1;
expr1->nextArg = tempPtr;
return(expr2);
}
/*===========================================================*/
/* If both expressions are "and" expressions, then add the */
/* 2nd expression to the argument list of the 1st expression */
/* and throw away the extraneous "and" expression. */
/*===========================================================*/
if ((expr1->value == ExpressionData(theEnv)->PTR_AND) &&
(expr2->value == ExpressionData(theEnv)->PTR_AND))
{
tempPtr = expr1->argList;
if (tempPtr == NULL)
{
rtn_struct(theEnv,expr,expr1);
return(expr2);
}
while (tempPtr->nextArg != NULL)
{ tempPtr = tempPtr->nextArg; }
tempPtr->nextArg = expr2->argList;
rtn_struct(theEnv,expr,expr2);
return(expr1);
}
/*=====================================================*/
/* If neither expression is an "and" expression, then */
/* create an "and" expression and add both expressions */
/* to the argument list of that "and" expression. */
/*=====================================================*/
tempPtr = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_AND);
tempPtr->argList = expr1;
expr1->nextArg = expr2;
return(tempPtr);
}
/*********************/
/* NegateExpression: */
/*********************/
struct expr *NegateExpression(
Environment *theEnv,
struct expr *theExpression)
{
struct expr *tempPtr;
/*=========================================*/
/* If the expression is NULL, return NULL. */
/*=========================================*/
if (theExpression == NULL) return NULL;
/*==================================================*/
/* The expression is already wrapped within a "not" */
/* function call, just remove the function call. */
/*==================================================*/
if (theExpression->value == ExpressionData(theEnv)->PTR_NOT)
{
tempPtr = theExpression->argList;
rtn_struct(theEnv,expr,theExpression);
return(tempPtr);
}
/*===================================================*/
/* Wrap the expression within a "not" function call. */
/*===================================================*/
tempPtr = GenConstant(theEnv,FCALL,ExpressionData(theEnv)->PTR_NOT);
tempPtr->argList = theExpression;
return(tempPtr);
}
/********************************************************/
/* AppendExpressions: Attaches an expression to the end */
/* of another expression's nextArg list. */
/********************************************************/
struct expr *AppendExpressions(
struct expr *expr1,
struct expr *expr2)
{
struct expr *tempPtr;
/*===========================================================*/
/* If the 1st expression is NULL, return the 2nd expression. */
/*===========================================================*/
if (expr1 == NULL) return(expr2);
/*===========================================================*/
/* If the 2nd expression is NULL, return the 1st expression. */
/*===========================================================*/
if (expr2 == NULL) return(expr1);
/*====================================*/
/* Find the end of the 1st expression */
/* and attach the 2nd expression. */
/*====================================*/
tempPtr = expr1;
while (tempPtr->nextArg != NULL) tempPtr = tempPtr->nextArg;
tempPtr->nextArg = expr2;
/*===============================*/
/* Return the merged expression. */
/*===============================*/
return(expr1);
}