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 _DEFAULT_SOURCE_
00028
00029 #include "setup.h"
00030
00031 #include <stdio.h>
00032 #define _STDIO_INCLUDED_
00033 #include <stdlib.h>
00034 #include <string.h>
00035
00036 #include "constant.h"
00037 #include "constrnt.h"
00038 #include "cstrnchk.h"
00039 #include "multifld.h"
00040 #include "inscom.h"
00041 #include "exprnpsr.h"
00042 #include "scanner.h"
00043 #include "router.h"
00044 #include "factmngr.h"
00045 #include "cstrnutl.h"
00046 #include "envrnmnt.h"
00047
00048 #include "default.h"
00049
00050
00051
00052
00053
00054 static void *FindDefaultValue(void *,int,CONSTRAINT_RECORD *,void *);
00055
00056
00057
00058
00059
00060 globle void DeriveDefaultFromConstraints(
00061 void *theEnv,
00062 CONSTRAINT_RECORD *constraints,
00063 DATA_OBJECT *theDefault,
00064 int multifield,
00065 int garbageMultifield)
00066 {
00067 unsigned short theType;
00068 unsigned long minFields;
00069 void *theValue;
00070
00071
00072
00073
00074
00075
00076
00077 if (constraints == NULL)
00078 {
00079 if (multifield)
00080 {
00081 SetpType(theDefault,MULTIFIELD);
00082 SetpDOBegin(theDefault,1);
00083 SetpDOEnd(theDefault,0);
00084 if (garbageMultifield) SetpValue(theDefault,(void *) EnvCreateMultifield(theEnv,0L));
00085 else SetpValue(theDefault,(void *) CreateMultifield2(theEnv,0L));
00086 }
00087 else
00088 {
00089 theDefault->type = SYMBOL;
00090 theDefault->value = EnvAddSymbol(theEnv,"nil");
00091 }
00092
00093 return;
00094 }
00095
00096
00097
00098
00099
00100 if (constraints->anyAllowed || constraints->symbolsAllowed)
00101 {
00102 theType = SYMBOL;
00103 theValue = FindDefaultValue(theEnv,SYMBOL,constraints,EnvAddSymbol(theEnv,"nil"));
00104 }
00105
00106 else if (constraints->stringsAllowed)
00107 {
00108 theType = STRING;
00109 theValue = FindDefaultValue(theEnv,STRING,constraints,EnvAddSymbol(theEnv,""));
00110 }
00111
00112 else if (constraints->integersAllowed)
00113 {
00114 theType = INTEGER;
00115 theValue = FindDefaultValue(theEnv,INTEGER,constraints,EnvAddLong(theEnv,0LL));
00116 }
00117
00118 else if (constraints->floatsAllowed)
00119 {
00120 theType = FLOAT;
00121 theValue = FindDefaultValue(theEnv,FLOAT,constraints,EnvAddDouble(theEnv,0.0));
00122 }
00123 #if OBJECT_SYSTEM
00124 else if (constraints->instanceNamesAllowed)
00125 {
00126 theType = INSTANCE_NAME;
00127 theValue = FindDefaultValue(theEnv,INSTANCE_NAME,constraints,EnvAddSymbol(theEnv,"nil"));
00128 }
00129
00130 else if (constraints->instanceAddressesAllowed)
00131 {
00132 theType = INSTANCE_ADDRESS;
00133 theValue = (void *) &InstanceData(theEnv)->DummyInstance;
00134 }
00135 #endif
00136 #if DEFTEMPLATE_CONSTRUCT
00137 else if (constraints->factAddressesAllowed)
00138 {
00139 theType = FACT_ADDRESS;
00140 theValue = (void *) &FactData(theEnv)->DummyFact;
00141 }
00142 #endif
00143 else if (constraints->externalAddressesAllowed)
00144 {
00145 theType = EXTERNAL_ADDRESS;
00146 theValue = EnvAddExternalAddress(theEnv,NULL,0);
00147 }
00148
00149 else
00150 {
00151 theType = SYMBOL;
00152 theValue = EnvAddSymbol(theEnv,"nil");
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162 if (multifield)
00163 {
00164 if (constraints->minFields == NULL) minFields = 0;
00165 else if (constraints->minFields->value == SymbolData(theEnv)->NegativeInfinity) minFields = 0;
00166 else minFields = (unsigned long) ValueToLong(constraints->minFields->value);
00167
00168 SetpType(theDefault,MULTIFIELD);
00169 SetpDOBegin(theDefault,1);
00170 SetpDOEnd(theDefault,(long) minFields);
00171 if (garbageMultifield) SetpValue(theDefault,(void *) EnvCreateMultifield(theEnv,minFields));
00172 else SetpValue(theDefault,(void *) CreateMultifield2(theEnv,minFields));
00173
00174 for (; minFields > 0; minFields--)
00175 {
00176 SetMFType(GetpValue(theDefault),minFields,theType);
00177 SetMFValue(GetpValue(theDefault),minFields,theValue);
00178 }
00179 }
00180 else
00181 {
00182 theDefault->type = theType;
00183 theDefault->value = theValue;
00184 }
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 static void *FindDefaultValue(
00198 void *theEnv,
00199 int theType,
00200 CONSTRAINT_RECORD *theConstraints,
00201 void *standardDefault)
00202 {
00203 struct expr *theList;
00204
00205
00206
00207
00208
00209
00210
00211 theList = theConstraints->restrictionList;
00212 while (theList != NULL)
00213 {
00214 if (theList->type == theType) return(theList->value);
00215 theList = theList->nextArg;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 if (theType == INTEGER)
00225 {
00226 if (theConstraints->minValue->type == INTEGER)
00227 { return(theConstraints->minValue->value); }
00228 else if (theConstraints->minValue->type == FLOAT)
00229 { return(EnvAddLong(theEnv,(long long) ValueToDouble(theConstraints->minValue->value))); }
00230 else if (theConstraints->maxValue->type == INTEGER)
00231 { return(theConstraints->maxValue->value); }
00232 else if (theConstraints->maxValue->type == FLOAT)
00233 { return(EnvAddLong(theEnv,(long long) ValueToDouble(theConstraints->maxValue->value))); }
00234 }
00235 else if (theType == FLOAT)
00236 {
00237 if (theConstraints->minValue->type == FLOAT)
00238 { return(theConstraints->minValue->value); }
00239 else if (theConstraints->minValue->type == INTEGER)
00240 { return(EnvAddDouble(theEnv,(double) ValueToLong(theConstraints->minValue->value))); }
00241 else if (theConstraints->maxValue->type == FLOAT)
00242 { return(theConstraints->maxValue->value); }
00243 else if (theConstraints->maxValue->type == INTEGER)
00244 { return(EnvAddDouble(theEnv,(double) ValueToLong(theConstraints->maxValue->value))); }
00245 }
00246
00247
00248
00249
00250
00251
00252 return(standardDefault);
00253 }
00254
00255 #if (! RUN_TIME) && (! BLOAD_ONLY)
00256
00257
00258
00259
00260 globle struct expr *ParseDefault(
00261 void *theEnv,
00262 char *readSource,
00263 int multifield,
00264 int dynamic,
00265 int evalStatic,
00266 int *noneSpecified,
00267 int *deriveSpecified,
00268 int *error)
00269 {
00270 struct expr *defaultList = NULL, *lastDefault = NULL;
00271 struct expr *newItem, *tmpItem;
00272 struct token theToken;
00273 DATA_OBJECT theValue;
00274 CONSTRAINT_RECORD *rv;
00275 int specialVarCode;
00276
00277 *noneSpecified = FALSE;
00278 *deriveSpecified = FALSE;
00279
00280 SavePPBuffer(theEnv," ");
00281 GetToken(theEnv,readSource,&theToken);
00282
00283
00284
00285
00286
00287
00288 while (theToken.type != RPAREN)
00289 {
00290
00291
00292
00293
00294 newItem = ParseAtomOrExpression(theEnv,readSource,&theToken);
00295 if (newItem == NULL)
00296 {
00297 ReturnExpression(theEnv,defaultList);
00298 *error = TRUE;
00299 return(NULL);
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 if ((newItem->type == SF_VARIABLE) || (newItem->type == MF_VARIABLE))
00309 {
00310 if (strcmp(ValueToString(newItem->value),"NONE") == 0)
00311 { specialVarCode = 0; }
00312 else if (strcmp(ValueToString(newItem->value),"DERIVE") == 0)
00313 { specialVarCode = 1; }
00314 else
00315 { specialVarCode = -1; }
00316
00317 if ((dynamic) ||
00318 (newItem->type == MF_VARIABLE) ||
00319 (specialVarCode == -1) ||
00320 ((specialVarCode != -1) && (defaultList != NULL)))
00321 {
00322 if (dynamic) SyntaxErrorMessage(theEnv,"default-dynamic attribute");
00323 else SyntaxErrorMessage(theEnv,"default attribute");
00324 ReturnExpression(theEnv,newItem);
00325 ReturnExpression(theEnv,defaultList);
00326 *error = TRUE;
00327 return(NULL);
00328 }
00329
00330 ReturnExpression(theEnv,newItem);
00331
00332
00333
00334
00335
00336
00337 GetToken(theEnv,readSource,&theToken);
00338
00339 if (theToken.type != RPAREN)
00340 {
00341 if (dynamic) SyntaxErrorMessage(theEnv,"default-dynamic attribute");
00342 else SyntaxErrorMessage(theEnv,"default attribute");
00343 PPBackup(theEnv);
00344 SavePPBuffer(theEnv," ");
00345 SavePPBuffer(theEnv,theToken.printForm);
00346 *error = TRUE;
00347 }
00348
00349 if (specialVarCode == 0)
00350 *noneSpecified = TRUE;
00351 else
00352 *deriveSpecified = TRUE;
00353 return(NULL);
00354 }
00355
00356
00357
00358
00359
00360
00361 if (ExpressionContainsVariables(newItem,FALSE) == TRUE)
00362 {
00363 ReturnExpression(theEnv,defaultList);
00364 ReturnExpression(theEnv,newItem);
00365 *error = TRUE;
00366 if (dynamic) SyntaxErrorMessage(theEnv,"default-dynamic attribute");
00367 else SyntaxErrorMessage(theEnv,"default attribute");
00368 return(NULL);
00369 }
00370
00371
00372
00373
00374
00375 if (lastDefault == NULL)
00376 { defaultList = newItem; }
00377 else
00378 { lastDefault->nextArg = newItem; }
00379 lastDefault = newItem;
00380
00381
00382
00383
00384
00385 SavePPBuffer(theEnv," ");
00386 GetToken(theEnv,readSource,&theToken);
00387 }
00388
00389
00390
00391
00392
00393 PPBackup(theEnv);
00394 PPBackup(theEnv);
00395 SavePPBuffer(theEnv,")");
00396
00397
00398
00399
00400
00401
00402 if (multifield == FALSE)
00403 {
00404 if (defaultList == NULL)
00405 { *error = TRUE; }
00406 else if (defaultList->nextArg != NULL)
00407 { *error = TRUE; }
00408 else
00409 {
00410 rv = ExpressionToConstraintRecord(theEnv,defaultList);
00411 rv->multifieldsAllowed = FALSE;
00412 if (UnmatchableConstraint(rv)) *error = TRUE;
00413 RemoveConstraint(theEnv,rv);
00414 }
00415
00416 if (*error)
00417 {
00418 PrintErrorID(theEnv,"DEFAULT",1,TRUE);
00419 EnvPrintRouter(theEnv,WERROR,"The default value for a single field slot must be a single field value\n");
00420 ReturnExpression(theEnv,defaultList);
00421 return(NULL);
00422 }
00423 }
00424
00425
00426
00427
00428
00429
00430 if (dynamic || (! evalStatic) || (defaultList == NULL)) return(defaultList);
00431
00432 tmpItem = defaultList;
00433 newItem = defaultList;
00434
00435 defaultList = NULL;
00436
00437 while (newItem != NULL)
00438 {
00439 SetEvaluationError(theEnv,FALSE);
00440 if (EvaluateExpression(theEnv,newItem,&theValue)) *error = TRUE;
00441
00442 if ((theValue.type == MULTIFIELD) &&
00443 (multifield == FALSE) &&
00444 (*error == FALSE))
00445 {
00446 PrintErrorID(theEnv,"DEFAULT",1,TRUE);
00447 EnvPrintRouter(theEnv,WERROR,"The default value for a single field slot must be a single field value\n");
00448 *error = TRUE;
00449 }
00450
00451 if (*error)
00452 {
00453 ReturnExpression(theEnv,tmpItem);
00454 ReturnExpression(theEnv,defaultList);
00455 *error = TRUE;
00456 return(NULL);
00457 }
00458
00459 lastDefault = ConvertValueToExpression(theEnv,&theValue);
00460
00461 defaultList = AppendExpressions(defaultList,lastDefault);
00462
00463 newItem = newItem->nextArg;
00464 }
00465
00466 ReturnExpression(theEnv,tmpItem);
00467
00468
00469
00470
00471
00472 return(defaultList);
00473 }
00474
00475 #endif