📄 argacces.c
字号:
/*==============================================*/ /* Get the number of arguments for the function */ /* currently being evaluated. */ /*==============================================*/ numberOfArguments = RtnArgCount(); /*=========================================================*/ /* If the function satisfies expected number of arguments, */ /* constraint, then return the number of arguments found. */ /*=========================================================*/ if (countRelation == EXACTLY) { if (numberOfArguments == expectedNumber) return(numberOfArguments); } else if (countRelation == AT_LEAST) { if (numberOfArguments >= expectedNumber) return(numberOfArguments); } else if (countRelation == NO_MORE_THAN) { if (numberOfArguments <= expectedNumber) return(numberOfArguments); } /*================================================*/ /* The correct number of arguments was not found. */ /* Generate an error message and return -1. */ /*================================================*/ ExpectedCountError(functionName,countRelation,expectedNumber); SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); return(-1); }/****************************************************************//* ArgRangeCheck: Checks that the number of arguments passed to *//* a function falls within a specified minimum and maximum *//* range. The number of arguments passed to the function is *//* returned if no error occurs, otherwise -1 is returned. *//****************************************************************/globle int ArgRangeCheck(functionName,min,max) char *functionName; int min, max; { int numberOfArguments; numberOfArguments = RtnArgCount(); if ((numberOfArguments < min) || (numberOfArguments > max)) { PrintErrorID("ARGACCES",1,CLIPS_FALSE); PrintCLIPS(WERROR,"Function "); PrintCLIPS(WERROR,functionName); PrintCLIPS(WERROR," expected at least "); PrintLongInteger(WERROR,(long) min); PrintCLIPS(WERROR," and no more than "); PrintLongInteger(WERROR,(long) max); PrintCLIPS(WERROR," arguments.\n"); SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); return(-1); } return(numberOfArguments); }/*******************************************************************//* ArgTypeCheck: Retrieves the nth argument passed to the function *//* call currently being evaluated and determines if it matches a *//* specified type. Returns TRUE if the argument was successfully *//* retrieved and is of the appropriate type, otherwise returns *//* FALSE. *//*******************************************************************/globle int ArgTypeCheck(functionName,argumentPosition,expectedType,returnValue) char *functionName; int expectedType; int argumentPosition; DATA_OBJECT_PTR returnValue; { /*========================*/ /* Retrieve the argument. */ /*========================*/ RtnUnknown(argumentPosition,returnValue); if (EvaluationError) return(CLIPS_FALSE); /*========================================*/ /* If the argument's type exactly matches */ /* the expected type, then return TRUE. */ /*========================================*/ if (returnValue->type == expectedType) return (CLIPS_TRUE); /*=============================================================*/ /* Some expected types encompass more than one primitive type. */ /* If the argument's type matches one of the primitive types */ /* encompassed by the expected type, then return TRUE. */ /*=============================================================*/ if ((expectedType == INTEGER_OR_FLOAT) && ((returnValue->type == INTEGER) || (returnValue->type == FLOAT))) { return(CLIPS_TRUE); } if ((expectedType == SYMBOL_OR_STRING) && ((returnValue->type == SYMBOL) || (returnValue->type == STRING))) { return(CLIPS_TRUE); }#if OBJECT_SYSTEM if (((expectedType == SYMBOL_OR_STRING) || (expectedType == SYMBOL)) && (returnValue->type == INSTANCE_NAME)) { return(CLIPS_TRUE); } if ((expectedType == INSTANCE_NAME) && ((returnValue->type == INSTANCE_NAME) || (returnValue->type == SYMBOL))) { return(CLIPS_TRUE); } if ((expectedType == INSTANCE_OR_INSTANCE_NAME) && ((returnValue->type == INSTANCE_ADDRESS) || (returnValue->type == INSTANCE_NAME) || (returnValue->type == SYMBOL))) { return(CLIPS_TRUE); }#endif /*===========================================================*/ /* If the expected type is float and the argument's type is */ /* integer (or vice versa), then convert the argument's type */ /* to match the expected type and then return TRUE. */ /*===========================================================*/ if ((returnValue->type == INTEGER) && (expectedType == FLOAT)) { returnValue->type = FLOAT; returnValue->value = (VOID *) AddDouble((double) ValueToLong(returnValue->value)); return(CLIPS_TRUE); } if ((returnValue->type == FLOAT) && (expectedType == INTEGER)) { returnValue->type = INTEGER; returnValue->value = (VOID *) AddLong((long) ValueToDouble(returnValue->value)); return(CLIPS_TRUE); } /*=====================================================*/ /* The argument's type didn't match the expected type. */ /* Print an error message and return FALSE. */ /*=====================================================*/ if (expectedType == FLOAT) ExpectedTypeError1(functionName,argumentPosition,"float"); else if (expectedType == INTEGER) ExpectedTypeError1(functionName,argumentPosition,"integer"); else if (expectedType == SYMBOL) ExpectedTypeError1(functionName,argumentPosition,"symbol"); else if (expectedType == STRING) ExpectedTypeError1(functionName,argumentPosition,"string"); else if (expectedType == MULTIFIELD) ExpectedTypeError1(functionName,argumentPosition,"multifield"); else if (expectedType == INTEGER_OR_FLOAT) ExpectedTypeError1(functionName,argumentPosition,"integer or float"); else if (expectedType == SYMBOL_OR_STRING) ExpectedTypeError1(functionName,argumentPosition,"symbol or string");#if OBJECT_SYSTEM else if (expectedType == INSTANCE_NAME) ExpectedTypeError1(functionName,argumentPosition,"instance name"); else if (expectedType == INSTANCE_ADDRESS) ExpectedTypeError1(functionName,argumentPosition,"instance address"); else if (expectedType == INSTANCE_OR_INSTANCE_NAME) ExpectedTypeError1(functionName,argumentPosition,"instance address or instance name");#endif SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); return(CLIPS_FALSE); }/******************************************************************//* GetNumericArgument: Evaluates an expression to yield a numeric *//* argument. This provides quicker retrieval than using some of *//* the other argument access routines. The numeric argument is *//* returned in a DATA_OBJECT supplied by the calling function. *//* TRUE is returned if a numeric argument was successfully *//* retrieved, otherwise FALSE is returned. *//******************************************************************/globle BOOLEAN GetNumericArgument(theArgument,functionName,result, convertToFloat,whichArgument) struct expr *theArgument; char *functionName; DATA_OBJECT *result; BOOLEAN convertToFloat; int whichArgument; { int theType; VOID *theValue; /*==================================================================*/ /* Evaluate the expression (don't bother calling EvaluateExpression */ /* if the type is float or integer). */ /*==================================================================*/ switch(theArgument->type) { case FLOAT: case INTEGER: theType = theArgument->type; theValue = theArgument->value; break; default: EvaluateExpression(theArgument,result); theType = result->type; theValue = result->value; break; } /*==========================================*/ /* If the argument is not float or integer, */ /* print an error message and return FALSE. */ /*==========================================*/ if ((theType != FLOAT) && (theType != INTEGER)) { ExpectedTypeError1(functionName,whichArgument,"integer or float"); SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); result->type = INTEGER; result->value = (VOID *) AddLong(0L); return(CLIPS_FALSE); } /*==========================================================*/ /* If the argument is an integer and the "convert to float" */ /* flag is TRUE, then convert the integer to a float. */ /*==========================================================*/ if ((convertToFloat) && (theType == INTEGER)) { theType = FLOAT; theValue = (VOID *) AddDouble((double) ValueToLong(theValue)); } /*============================================================*/ /* The numeric argument was successfully retrieved. Store the */ /* argument in the user supplied DATA_OBJECT and return TRUE. */ /*============================================================*/ result->type = theType; result->value = theValue; return(CLIPS_TRUE); } /*********************************************************************//* GetLogicalName: Retrieves the nth argument passed to the function *//* call currently being evaluated and determines if it is a valid *//* logical name. If valid, the logical name is returned, otherwise *//* NULL is returned. *//*********************************************************************/globle char *GetLogicalName(whichArgument,defaultLogicalName) int whichArgument; char *defaultLogicalName; { char *logicalName; DATA_OBJECT result; RtnUnknown(whichArgument,&result); if ((GetType(result) == SYMBOL) || (GetType(result) == STRING) || (GetType(result) == INSTANCE_NAME)) { logicalName = ValueToString(result.value); if ((strcmp(logicalName,"t") == 0) || (strcmp(logicalName,"T") == 0)) { logicalName = defaultLogicalName; } } else if (GetType(result) == FLOAT) { logicalName = ValueToString(AddSymbol(FloatToString(DOToDouble(result)))); } else if (GetType(result) == INTEGER) { logicalName = ValueToString(AddSymbol(LongIntegerToString(DOToLong(result)))); } else { logicalName = NULL; } return(logicalName); }/************************************************************//* GetFileName: Retrieves the nth argument passed to the *//* function call currently being evaluated and determines *//* if it is a valid file name. If valid, the file name is *//* returned, otherwise NULL is returned. *//************************************************************/globle char *GetFileName(functionName,whichArgument) char *functionName; int whichArgument; { DATA_OBJECT result; RtnUnknown(whichArgument,&result); if ((GetType(result) != STRING) && (GetType(result) != SYMBOL)) { ExpectedTypeError1(functionName,whichArgument,"file name"); return(NULL); } return(DOToString(result)); } /******************************************************************//* OpenErrorMessage: Generalized error message for opening files. *//******************************************************************/globle VOID OpenErrorMessage(functionName,fileName) char *functionName, *fileName; { PrintErrorID("ARGACCES",2,CLIPS_FALSE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -