00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #define _FACTRHS_SOURCE_
00028
00029 #include <stdio.h>
00030 #define _STDIO_INCLUDED_
00031 #include <string.h>
00032
00033 #include "setup.h"
00034
00035 #if DEFTEMPLATE_CONSTRUCT
00036
00037 #include "constant.h"
00038 #include "envrnmnt.h"
00039 #include "extnfunc.h"
00040 #include "modulutl.h"
00041 #include "modulpsr.h"
00042 #include "pattern.h"
00043 #include "prntutil.h"
00044 #include "cstrcpsr.h"
00045
00046 #if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
00047 #include "bload.h"
00048 #endif
00049
00050 #include "tmpltpsr.h"
00051 #include "tmpltrhs.h"
00052 #include "tmpltutl.h"
00053 #include "exprnpsr.h"
00054 #include "strngrtr.h"
00055 #include "router.h"
00056
00057 #include "factrhs.h"
00058
00059
00060
00061
00062
00063 #if RUN_TIME || BLOAD_ONLY || BLOAD || BLOAD_AND_BSAVE
00064 static void NoSuchTemplateError(void *,char *);
00065 #endif
00066
00067 #if (! RUN_TIME)
00068
00069
00070
00071
00072
00073
00074
00075
00076 globle struct expr *BuildRHSAssert(
00077 void *theEnv,
00078 char *logicalName,
00079 struct token *theToken,
00080 int *error,
00081 int atLeastOne,
00082 int readFirstParen,
00083 char *whereParsed)
00084 {
00085 struct expr *lastOne, *nextOne, *assertList, *stub;
00086
00087 *error = FALSE;
00088
00089
00090
00091
00092
00093
00094
00095
00096 if (readFirstParen == FALSE)
00097 {
00098 if (theToken->type == RPAREN)
00099 {
00100 if (atLeastOne)
00101 {
00102 *error = TRUE;
00103 SyntaxErrorMessage(theEnv,whereParsed);
00104 }
00105 return(NULL);
00106 }
00107 }
00108
00109
00110
00111
00112
00113 lastOne = assertList = NULL;
00114 while ((nextOne = GetRHSPattern(theEnv,logicalName,theToken,
00115 error,FALSE,readFirstParen,
00116 TRUE,RPAREN)) != NULL)
00117 {
00118 PPCRAndIndent(theEnv);
00119
00120 stub = GenConstant(theEnv,FCALL,(void *) FindFunction(theEnv,"assert"));
00121 stub->argList = nextOne;
00122 nextOne = stub;
00123
00124 if (lastOne == NULL)
00125 { assertList = nextOne; }
00126 else
00127 { lastOne->nextArg = nextOne; }
00128 lastOne = nextOne;
00129
00130 readFirstParen = TRUE;
00131 }
00132
00133
00134
00135
00136
00137 if (*error)
00138 {
00139 ReturnExpression(theEnv,assertList);
00140 return(NULL);
00141 }
00142
00143
00144
00145
00146
00147 if (theToken->type == RPAREN)
00148 {
00149 PPBackup(theEnv);
00150 PPBackup(theEnv);
00151 SavePPBuffer(theEnv,")");
00152 }
00153
00154
00155
00156
00157
00158
00159 if (assertList == NULL)
00160 {
00161 if (atLeastOne)
00162 {
00163 *error = TRUE;
00164 SyntaxErrorMessage(theEnv,whereParsed);
00165 }
00166
00167 return(NULL);
00168 }
00169
00170
00171
00172
00173
00174
00175 if (assertList->nextArg != NULL)
00176 {
00177 stub = GenConstant(theEnv,FCALL,(void *) FindFunction(theEnv,"progn"));
00178 stub->argList = assertList;
00179 assertList = stub;
00180 }
00181
00182
00183
00184
00185
00186 return(assertList);
00187 }
00188
00189 #endif
00190
00191
00192
00193
00194
00195
00196
00197 globle struct expr *GetRHSPattern(
00198 void *theEnv,
00199 char *readSource,
00200 struct token *tempToken,
00201 int *error,
00202 int constantsOnly,
00203 int readFirstParen,
00204 int checkFirstParen,
00205 int endType)
00206 {
00207 struct expr *lastOne = NULL;
00208 struct expr *nextOne, *firstOne, *argHead = NULL;
00209 int printError, count;
00210 struct deftemplate *theDeftemplate;
00211 struct symbolHashNode *templateName;
00212 char *nullBitMap = "\0";
00213
00214
00215
00216
00217
00218 *error = FALSE;
00219
00220 if (readFirstParen) GetToken(theEnv,readSource,tempToken);
00221
00222 if (checkFirstParen)
00223 {
00224 if (tempToken->type == endType) return(NULL);
00225
00226 if (tempToken->type != LPAREN)
00227 {
00228 SyntaxErrorMessage(theEnv,"RHS patterns");
00229 *error = TRUE;
00230 return(NULL);
00231 }
00232 }
00233
00234
00235
00236
00237
00238
00239 GetToken(theEnv,readSource,tempToken);
00240 if (tempToken->type != SYMBOL)
00241 {
00242 SyntaxErrorMessage(theEnv,"first field of a RHS pattern");
00243 *error = TRUE;
00244 return(NULL);
00245 }
00246 else if ((strcmp(ValueToString(tempToken->value),"=") == 0) ||
00247 (strcmp(ValueToString(tempToken->value),":") == 0))
00248 {
00249 SyntaxErrorMessage(theEnv,"first field of a RHS pattern");
00250 *error = TRUE;
00251 return(NULL);
00252 }
00253
00254
00255
00256
00257
00258 templateName = (struct symbolHashNode *) tempToken->value;
00259
00260 if (ReservedPatternSymbol(theEnv,ValueToString(templateName),NULL))
00261 {
00262 ReservedPatternSymbolErrorMsg(theEnv,ValueToString(templateName),"a relation name");
00263 *error = TRUE;
00264 return(NULL);
00265 }
00266
00267
00268
00269
00270
00271 if (FindModuleSeparator(ValueToString(templateName)))
00272 {
00273 IllegalModuleSpecifierMessage(theEnv);
00274
00275 *error = TRUE;
00276 return(NULL);
00277 }
00278
00279
00280
00281
00282
00283
00284
00285 theDeftemplate = (struct deftemplate *)
00286 FindImportedConstruct(theEnv,"deftemplate",NULL,ValueToString(templateName),
00287 &count,TRUE,NULL);
00288
00289 if (count > 1)
00290 {
00291 AmbiguousReferenceErrorMessage(theEnv,"deftemplate",ValueToString(templateName));
00292 *error = TRUE;
00293 return(NULL);
00294 }
00295
00296
00297
00298
00299
00300
00301 if (theDeftemplate == NULL)
00302 #if (! BLOAD_ONLY) && (! RUN_TIME)
00303 {
00304 #if BLOAD || BLOAD_AND_BSAVE
00305 if ((Bloaded(theEnv)) && (! ConstructData(theEnv)->CheckSyntaxMode))
00306 {
00307 NoSuchTemplateError(theEnv,ValueToString(templateName));
00308 *error = TRUE;
00309 return(NULL);
00310 }
00311 #endif
00312 #if DEFMODULE_CONSTRUCT
00313 if (FindImportExportConflict(theEnv,"deftemplate",((struct defmodule *) EnvGetCurrentModule(theEnv)),ValueToString(templateName)))
00314 {
00315 ImportExportConflictMessage(theEnv,"implied deftemplate",ValueToString(templateName),NULL,NULL);
00316 *error = TRUE;
00317 return(NULL);
00318 }
00319 #endif
00320 if (! ConstructData(theEnv)->CheckSyntaxMode)
00321 { theDeftemplate = CreateImpliedDeftemplate(theEnv,(SYMBOL_HN *) templateName,TRUE); }
00322 }
00323 #else
00324 {
00325 NoSuchTemplateError(theEnv,ValueToString(templateName));
00326 *error = TRUE;
00327 return(NULL);
00328 }
00329 #endif
00330
00331
00332
00333
00334
00335
00336 if ((theDeftemplate != NULL) && (theDeftemplate->implied == FALSE))
00337 {
00338 firstOne = GenConstant(theEnv,DEFTEMPLATE_PTR,theDeftemplate);
00339 firstOne->nextArg = ParseAssertTemplate(theEnv,readSource,tempToken,
00340 error,endType,
00341 constantsOnly,theDeftemplate);
00342 if (*error)
00343 {
00344 ReturnExpression(theEnv,firstOne);
00345 firstOne = NULL;
00346 }
00347
00348 return(firstOne);
00349 }
00350
00351
00352
00353
00354
00355 firstOne = GenConstant(theEnv,DEFTEMPLATE_PTR,theDeftemplate);
00356
00357 #if (! RUN_TIME) && (! BLOAD_ONLY)
00358 SavePPBuffer(theEnv," ");
00359 #endif
00360
00361 while ((nextOne = GetAssertArgument(theEnv,readSource,tempToken,
00362 error,endType,constantsOnly,&printError)) != NULL)
00363 {
00364 if (argHead == NULL) argHead = nextOne;
00365 else lastOne->nextArg = nextOne;
00366 lastOne = nextOne;
00367 #if (! RUN_TIME) && (! BLOAD_ONLY)
00368 SavePPBuffer(theEnv," ");
00369 #endif
00370 }
00371
00372
00373
00374
00375
00376 if (*error)
00377 {
00378 if (printError) SyntaxErrorMessage(theEnv,"RHS patterns");
00379 ReturnExpression(theEnv,firstOne);
00380 ReturnExpression(theEnv,argHead);
00381 return(NULL);
00382 }
00383
00384
00385
00386
00387
00388
00389 #if (! RUN_TIME) && (! BLOAD_ONLY)
00390 PPBackup(theEnv);
00391 PPBackup(theEnv);
00392 SavePPBuffer(theEnv,tempToken->printForm);
00393 #endif
00394
00395
00396
00397
00398
00399
00400
00401 firstOne->nextArg = GenConstant(theEnv,FACT_STORE_MULTIFIELD,EnvAddBitMap(theEnv,(void *) nullBitMap,1));
00402 firstOne->nextArg->argList = argHead;
00403
00404
00405
00406
00407
00408 return(firstOne);
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419 globle struct expr *GetAssertArgument(
00420 void *theEnv,
00421 char *logicalName,
00422 struct token *theToken,
00423 int *error,
00424 int endType,
00425 int constantsOnly,
00426 int *printError)
00427 {
00428 #if ! RUN_TIME
00429 struct expr *nextField;
00430 #else
00431 struct expr *nextField = NULL;
00432 #endif
00433
00434
00435
00436
00437
00438
00439 *printError = TRUE;
00440 GetToken(theEnv,logicalName,theToken);
00441 if (theToken->type == endType) return(NULL);
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 if ((theToken->type == SYMBOL) ?
00455 (strcmp(ValueToString(theToken->value),"=") == 0) :
00456 (theToken->type == LPAREN))
00457 {
00458 if (constantsOnly)
00459 {
00460 *error = TRUE;
00461 return(NULL);
00462 }
00463
00464 #if ! RUN_TIME
00465 if (theToken->type == LPAREN) nextField = Function1Parse(theEnv,logicalName);
00466 else nextField = Function0Parse(theEnv,logicalName);
00467 if (nextField == NULL)
00468 #endif
00469 {
00470 *printError = FALSE;
00471 *error = TRUE;
00472 }
00473 #if ! RUN_TIME
00474 else
00475 {
00476 theToken->type= RPAREN;
00477 theToken->value = (void *) EnvAddSymbol(theEnv,")");
00478 theToken->printForm = ")";
00479 }
00480 #endif
00481
00482 return(nextField);
00483 }
00484
00485
00486
00487
00488
00489 if ((theToken->type == SYMBOL) || (theToken->type == STRING) ||
00490 #if OBJECT_SYSTEM
00491 (theToken->type == INSTANCE_NAME) ||
00492 #endif
00493 (theToken->type == FLOAT) || (theToken->type == INTEGER))
00494 { return(GenConstant(theEnv,theToken->type,theToken->value)); }
00495
00496
00497
00498
00499
00500
00501 if ((theToken->type == SF_VARIABLE) ||
00502 #if DEFGLOBAL_CONSTRUCT
00503 (theToken->type == GBL_VARIABLE) ||
00504 (theToken->type == MF_GBL_VARIABLE) ||
00505 #endif
00506 (theToken->type == MF_VARIABLE))
00507 {
00508 if (constantsOnly)
00509 {
00510 *error = TRUE;
00511 return(NULL);
00512 }
00513
00514 return(GenConstant(theEnv,theToken->type,theToken->value));
00515 }
00516
00517
00518
00519
00520
00521
00522 *error = TRUE;
00523 return(NULL);
00524 }
00525
00526
00527
00528
00529
00530 globle struct fact *StringToFact(
00531 void *theEnv,
00532 char *str)
00533 {
00534 struct token theToken;
00535 struct fact *factPtr;
00536 unsigned numberOfFields = 0, whichField;
00537 struct expr *assertArgs, *tempPtr;
00538 int error = FALSE;
00539 DATA_OBJECT theResult;
00540
00541
00542
00543
00544
00545
00546 SetEvaluationError(theEnv,FALSE);
00547
00548 OpenStringSource(theEnv,"assert_str",str,0);
00549
00550 assertArgs = GetRHSPattern(theEnv,"assert_str",&theToken,
00551 &error,FALSE,TRUE,
00552 TRUE,RPAREN);
00553
00554 CloseStringSource(theEnv,"assert_str");
00555
00556
00557
00558
00559
00560 if ((assertArgs == NULL) && (! error))
00561 {
00562 SyntaxErrorMessage(theEnv,"RHS patterns");
00563 ReturnExpression(theEnv,assertArgs);
00564 return(NULL);
00565 }
00566
00567 if (error)
00568 {
00569 ReturnExpression(theEnv,assertArgs);
00570 return(NULL);
00571 }
00572
00573 if (ExpressionContainsVariables(assertArgs,FALSE))
00574 {
00575 LocalVariableErrorMessage(theEnv,"the assert-string function");
00576 SetEvaluationError(theEnv,TRUE);
00577 ReturnExpression(theEnv,assertArgs);
00578 return(NULL);
00579 }
00580
00581
00582
00583
00584
00585
00586 for (tempPtr = assertArgs->nextArg; tempPtr != NULL; tempPtr = tempPtr->nextArg)
00587 { numberOfFields++; }
00588
00589 factPtr = (struct fact *) CreateFactBySize(theEnv,numberOfFields);
00590 factPtr->whichDeftemplate = (struct deftemplate *) assertArgs->value;
00591
00592
00593
00594
00595
00596 ExpressionInstall(theEnv,assertArgs);
00597 whichField = 0;
00598 for (tempPtr = assertArgs->nextArg; tempPtr != NULL; tempPtr = tempPtr->nextArg)
00599 {
00600 EvaluateExpression(theEnv,tempPtr,&theResult);
00601 factPtr->theProposition.theFields[whichField].type = theResult.type;
00602 factPtr->theProposition.theFields[whichField].value = theResult.value;
00603 whichField++;
00604 }
00605 ExpressionDeinstall(theEnv,assertArgs);
00606 ReturnExpression(theEnv,assertArgs);
00607
00608
00609
00610
00611
00612 return(factPtr);
00613 }
00614
00615 #if RUN_TIME || BLOAD_ONLY || BLOAD || BLOAD_AND_BSAVE
00616
00617
00618
00619
00620
00621
00622
00623 static void NoSuchTemplateError(
00624 void *theEnv,
00625 char *templateName)
00626 {
00627 PrintErrorID(theEnv,"FACTRHS",1,FALSE);
00628 EnvPrintRouter(theEnv,WERROR,"Template ");
00629 EnvPrintRouter(theEnv,WERROR,templateName);
00630 EnvPrintRouter(theEnv,WERROR," does not exist for assert.\n");
00631 }
00632
00633 #endif
00634
00635 #endif
00636
00637