📄 conscomp.c
字号:
case 'j': case 'k': fprintf(fp,"DATA_OBJECT_PTR_ARG"); break; } fprintf(fp,");\n"); } }/****************************************************//* FunctionsToCode: Generates C code to represent *//* the function declaration data structures (used *//* to declare system and user defined functions). *//****************************************************/static int FunctionsToCode(fileName) char *fileName; { short i = 0; FILE *fp; int version = 1; int newHeader = CLIPS_TRUE; struct FunctionDefinition *fctnPtr; /*=============================*/ /* Assign a reference index to */ /* each of the functions. */ /*=============================*/ for (fctnPtr = GetFunctionList(); fctnPtr != NULL; fctnPtr = fctnPtr->next) { fctnPtr->bsaveIndex = i++; } /*=======================================*/ /* Create the file in which to store the */ /* function definition data structures. */ /*=======================================*/ if ((fp = NewCFile(fileName,2,version,CLIPS_FALSE)) == NULL) { return(0); } /*===============================================*/ /* Construct the definition of the function list */ /* from the definitions of the functions. */ /*===============================================*/ fprintf(fp,"\n\n"); fprintf(fp,"/************************************/\n"); fprintf(fp,"/* FUNCTION LIST DEFINITION */\n"); fprintf(fp,"/************************************/\n\n"); i = 1; fctnPtr = GetFunctionList(); while (fctnPtr != NULL) { if (newHeader) { fprintf(fp,"struct FunctionDefinition P%d_%d[] = {\n",ImageID,version); fprintf(HeaderFP,"extern struct FunctionDefinition P%d_%d[];\n",ImageID,version); newHeader = CLIPS_FALSE; } fprintf(fp,"{"); PrintSymbolReference(fp,fctnPtr->callFunctionName); fprintf(fp,",\"%s\",",fctnPtr->actualFunctionName); fprintf(fp,"'%c',",fctnPtr->returnValueType); fprintf(fp,"PTIF %s,",fctnPtr->actualFunctionName); fprintf(fp,"NULL,"); if (fctnPtr->restrictions != NULL) fprintf(fp,"\"%s\",",fctnPtr->restrictions); else fprintf(fp,"NULL,"); fprintf(fp,"0,0,0,"); PrintFunctionReference(fp,fctnPtr->next); i++; fctnPtr = fctnPtr->next; if ((i > MaxIndices) || (fctnPtr == NULL)) { fprintf(fp,"}};\n"); fclose(fp); i = 1; version++; if (fctnPtr != NULL) { if ((fp = NewCFile(fileName,2,version,CLIPS_FALSE)) == NULL) return(0); newHeader = CLIPS_TRUE; } } else { fprintf(fp,"},\n"); } } return(CLIPS_TRUE); }/************************************************************//* PrintFunctionReference: Writes the C code representation *//* of a pointer to a function definition data structure. *//************************************************************/globle VOID PrintFunctionReference(fp,funcPtr) FILE *fp; struct FunctionDefinition *funcPtr; { if (funcPtr == NULL) fprintf(fp,"NULL"); else fprintf(fp,"&P%d_%d[%d]",ImageID, (funcPtr->bsaveIndex / MaxIndices) + 1, funcPtr->bsaveIndex % MaxIndices); }/******************************************//* WriteInitializationFunction: Generates *//* the C initialization function for *//* this constructs-to-c module. *//******************************************/static int WriteInitializationFunction(fileName) char *fileName; { char fname[FSIZE]; FILE *fp; struct CodeGeneratorItem *cgPtr; /*===============================*/ /* Open the initialization file. */ /*===============================*/ sprintf(fname,"%s.c",fileName); if ((fp = fopen(fname,"w")) == NULL) { OpenErrorMessage("constructs-to-c",fname); return(CLIPS_FALSE); } /*=====================================*/ /* Write out #includes and prototypes. */ /*=====================================*/ fprintf(fp,"#include \"%s.h\"\n",fileName); fprintf(fp,"\n"); fprintf(fp,"#include \"utility.h\"\n"); fprintf(fp,"#include \"generate.h\"\n"); fprintf(fp,"#include \"expressn.h\"\n"); fprintf(fp,"#include \"extnfunc.h\"\n"); fprintf(fp,"#include \"objrtmch.h\"\n"); fprintf(fp,"#include \"rulebld.h\"\n\n"); fprintf(HeaderFP,"#if ANSI_COMPILER\n"); fprintf(HeaderFP," VOID InitCImage_%d(void);\n",ImageID); fprintf(HeaderFP,"#else\n"); fprintf(HeaderFP," VOID InitCImage_%d();\n",ImageID); fprintf(HeaderFP,"#endif\n"); /*============================================*/ /* Begin writing the initialization function. */ /*============================================*/ fprintf(fp,"\n"); fprintf(fp,"/*******************************************/\n"); fprintf(fp,"/* CONSTRUCT IMAGE INITIALIZATION FUNCTION */\n"); fprintf(fp,"/*******************************************/\n"); fprintf(fp,"\nVOID InitCImage_%d()\n",ImageID); fprintf(fp," {\n"); fprintf(fp," Clear();\n"); fprintf(fp," PeriodicCleanup(CLIPS_TRUE,CLIPS_FALSE);\n"); fprintf(fp," SetSymbolTable(sht%d);\n",ImageID); fprintf(fp," SetFloatTable(fht%d);\n",ImageID); fprintf(fp," SetIntegerTable(iht%d);\n",ImageID); fprintf(fp," SetBitMapTable(bmht%d);\n",ImageID); fprintf(fp," RefreshSpecialSymbols();\n"); fprintf(fp," InstallFunctionList(P%d_1);\n\n",ImageID); fprintf(fp," InitExpressionPointers();\n\n"); /*==========================================*/ /* Write construct specific initialization. */ /*==========================================*/ cgPtr = ListOfCodeGeneratorItems; while (cgPtr != NULL) { if (cgPtr->initFunction != NULL) { (*cgPtr->initFunction)(fp,ImageID,MaxIndices); fprintf(fp,"\n"); } cgPtr = cgPtr->next; } /*================================*/ /* Close the initialization file. */ /*================================*/ fprintf(fp," }\n"); fclose(fp); /*========================================*/ /* Return TRUE to indicate initialization */ /* file was successfully written. */ /*========================================*/ return(CLIPS_TRUE); }/**************************************************//* NewCFile: Opens a new file for writing C code. *//**************************************************/globle FILE *NewCFile(fileName,id,version,reopenOldFile) char *fileName; int id, version, reopenOldFile; { char fname[FSIZE]; FILE *newFP; sprintf(fname,"%s%d_%d.c",fileName,id,version); newFP = fopen(fname,reopenOldFile ? "a" : "w"); if (newFP == NULL) { OpenErrorMessage("constructs-to-c",fname); return(NULL); } if (reopenOldFile == CLIPS_FALSE) { fprintf(newFP,"#include \"%s.h\"\n",fileName); fprintf(newFP,"\n"); } return(newFP); } /**********************************************************//* HashedExpressionsToCode: Traverses the expression hash *//* table and calls ExpressionToCode to write the C *//* code representation to a file of every expression in *//* the table. *//**********************************************************/static VOID HashedExpressionsToCode() { unsigned i; EXPRESSION_HN *exphash; for (i = 0; i < EXPRESSION_HASH_SIZE; i++) { for (exphash = ExpressionHashTable[i]; exphash != NULL; exphash = exphash->nxt) { exphash->bsaveID = ExpressionCount + (MaxIndices * ExpressionVersion); ExpressionToCode(NULL,exphash->exp); } } }/*****************************************************//* PrintHashedExpressionReference: Writes the C code *//* representation of a pointer to an expression *//* stored in the expression hash table. *//*****************************************************/globle VOID PrintHashedExpressionReference(theFile,theExpression,imageID,maxIndices) FILE *theFile; struct expr *theExpression; int imageID; int maxIndices; { long theIDValue; if (theExpression == NULL) { fprintf(theFile,"NULL"); } else { theIDValue = HashedExpressionIndex(theExpression); fprintf(theFile,"&E%d_%ld[%ld]", imageID, theIDValue / maxIndices, theIDValue % maxIndices); } } /**************************************************************//* ExpressionToCode: Writes the C code reference of a pointer *//* to an expression and then calls DumpExpression to write *//* the C code for the expression to the expression file. *//**************************************************************/globle int ExpressionToCode(fp,exprPtr) FILE *fp; struct expr *exprPtr; { /*========================================*/ /* Print the reference to the expression. */ /*========================================*/ if (exprPtr == NULL) { if (fp != NULL) fprintf(fp,"NULL"); return(CLIPS_FALSE); } else if (fp != NULL) { fprintf(fp,"&E%d_%d[%ld]",ImageID,ExpressionVersion,ExpressionCount); } /*==================================================*/ /* Create a new expression code file, if necessary. */ /*==================================================*/ if (ExpressionHeader == CLIPS_TRUE) { if ((ExpressionFP = NewCFile(FilePrefix,3,ExpressionVersion,CLIPS_FALSE)) == NULL) { return(-1); } fprintf(ExpressionFP,"struct expr E%d_%d[] = {\n",ImageID,ExpressionVersion); fprintf(HeaderFP,"extern struct expr E%d_%d[];\n",ImageID,ExpressionVersion); ExpressionHeader = CLIPS_FALSE; } else { fprintf(ExpressionFP,",\n"); } /*===========================*/ /* Dump the expression code. */ /*===========================*/ DumpExpression(exprPtr); /*=========================================*/ /* Close the expression file if necessary. */ /*=========================================*/ if (ExpressionCount >= MaxIndices) { ExpressionCount = 0; ExpressionVersion++; fprintf(ExpressionFP,"};\n"); fclose(ExpressionFP); ExpressionFP = NULL; ExpressionHeader = CLIPS_TRUE; } /*==========================================*/ /* Return TRUE to indicate the expression */ /* reference and expression data structures */ /* were succcessfully written to the file. */ /*==========================================*/ return(CLIPS_TRUE); }/**********************************************************//* DumpExpression: Writes the C code representation of an *//* expression data structure to the expression file. *//**********************************************************/static VOID DumpExpression(exprPtr) struct expr *exprPtr; { while (exprPtr != NULL) { fprintf(ExpressionFP,"{"); fprintf(ExpressionFP,"%d,",exprPtr->type); fprintf(ExpressionFP,"VS "); switch (exprPtr->type) { case FCALL: PrintFunctionReference(ExpressionFP,exprPtr->value); break; case INTEGER: PrintIntegerReference(ExpressionFP,exprPtr->value); break; case FLOAT: PrintFloatReference(ExpressionFP,exprPtr->value); break; case PCALL:#if DEFFUNCTION_CONSTRUCT PrintDeffunctionReference(ExpressionFP,(DEFFUNCTION *) exprPtr->value, ImageID,MaxIndices);#else fprintf(ExpressionFP,"NULL");#endif break; case GCALL:#if DEFGENERIC_CONSTRUCT PrintGenericFunctionReference(ExpressionFP,(DEFGENERIC *) exprPtr->value, ImageID,MaxIndices);#else fprintf(ExpressionFP,"NULL");#endif break; case DEFTEMPLATE_PTR:#if DEFTEMPLATE_CONSTRUCT DeftemplateCConstructReference(ExpressionFP,exprPtr->value,ImageID,MaxIndices);#else fprintf(ExpressionFP,"NULL");#endif break; case DEFGLOBAL_PTR:#if DEFGLOBAL_CONSTRUCT DefglobalCConstructReference(ExpressionFP,exprPtr->value,ImageID,MaxIndices);#else fprintf(ExpressionFP,"NULL");#endif break; case DEFCLASS_PTR:#if OBJECT_SYSTEM PrintClassReference(ExpressionFP,(DEFCLASS *) exprPtr->value,ImageID,MaxIndices);#else fprintf(ExpressionFP,"NULL");#endif break; case FACT_ADDRESS:#if DEFTEMPLATE_CONSTRUCT fprintf(ExpressionFP,"&DummyFact");#else fprintf(ExpressionFP,"NULL");#endif break; case INSTANCE_ADDRESS:#if OBJECT_SYSTEM fprintf(ExpressionFP,"&DummyInstance");#else fprintf(ExpressionFP,"NULL");#endif break; case STRING: case SYMBOL: case INSTANCE_NAME: case GBL_VARIABLE: PrintSymbolReference(ExpressionFP,exprPtr->value); break; case RVOID: fprintf(ExpressionFP,"NULL"); break; default: if (PrimitivesArray[exprPtr->type] == NULL) { fprintf(ExpressionFP,"NULL"); } else if (PrimitivesArray[exprPtr->type]->bitMap) { PrintBitMapReference(ExpressionFP,exprPtr->value); } else { fprintf(ExpressionFP,"NULL"); } break; } fprintf(ExpressionFP,","); ExpressionCount++; if (exprPtr->argList == NULL) { fprintf(ExpressionFP,"NULL,"); } else { fprintf(ExpressionFP,"&E%d_%d[%ld],",ImageID,ExpressionVersion, ExpressionCount); } if (exprPtr->nextArg == NULL) { fprintf(ExpressionFP,"NULL}"); } else { fprintf(ExpressionFP,"&E%d_%d[%ld]}",ImageID,ExpressionVersion, ExpressionCount + ExpressionSize(exprPtr->argList)); } if (exprPtr->argList != NULL) { fprintf(ExpressionFP,",\n"); DumpExpression(exprPtr->argList); } exprPtr = exprPtr->nextArg; if (exprPtr != NULL) fprintf(ExpressionFP,",\n"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -