static void BeforePatternNetworkToCode() { int whichPattern = 0; int whichDeftemplate = 0; struct defmodule *theModule; struct deftemplate *theDeftemplate; struct factPatternNode *thePattern; /*===========================*/ /* Loop through each module. */ /*===========================*/ for (theModule = (struct defmodule *) GetNextDefmodule(NULL); theModule != NULL; theModule = (struct defmodule *) GetNextDefmodule(theModule)) { /*=========================*/ /* Set the current module. */ /*=========================*/ SetCurrentModule((void *) theModule); /*======================================================*/ /* Loop through each deftemplate in the current module. */ /*======================================================*/ for (theDeftemplate = (struct deftemplate *) GetNextDeftemplate(NULL); theDeftemplate != NULL; theDeftemplate = (struct deftemplate *) GetNextDeftemplate(theDeftemplate)) { /*=================================================*/ /* Assign each pattern node in the pattern network */ /* for the deftemplate a unique integer ID. */ /*=================================================*/ theDeftemplate->header.bsaveID = whichDeftemplate++; for (thePattern = theDeftemplate->patternNetwork; thePattern != NULL; thePattern = GetNextPatternNode(thePattern)) { thePattern->bsaveID = whichPattern++; } } } }
void UpdateDeftemplateScope( Environment *theEnv) { Deftemplate *theDeftemplate; unsigned int moduleCount; Defmodule *theModule; struct defmoduleItemHeader *theItem; /*==================================*/ /* Loop through all of the modules. */ /*==================================*/ for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { /*======================================================*/ /* Loop through each of the deftemplates in the module. */ /*======================================================*/ theItem = (struct defmoduleItemHeader *) GetModuleItem(theEnv,theModule,DeftemplateData(theEnv)->DeftemplateModuleIndex); for (theDeftemplate = (Deftemplate *) theItem->firstItem; theDeftemplate != NULL ; theDeftemplate = GetNextDeftemplate(theEnv,theDeftemplate)) { /*=======================================*/ /* If the deftemplate can be seen by the */ /* current module, then it is in scope. */ /*=======================================*/ if (FindImportedConstruct(theEnv,"deftemplate",theModule, theDeftemplate->header.name->contents, &moduleCount,true,NULL) != NULL) { theDeftemplate->inScope = true; } else { theDeftemplate->inScope = false; } } } }
globle void UpdateDeftemplateScope() { struct deftemplate *theDeftemplate; int moduleCount; struct defmodule *theModule; struct defmoduleItemHeader *theItem; /*==================================*/ /* Loop through all of the modules. */ /*==================================*/ for (theModule = (struct defmodule *) GetNextDefmodule(NULL); theModule != NULL; theModule = (struct defmodule *) GetNextDefmodule(theModule)) { /*======================================================*/ /* Loop through each of the deftemplates in the module. */ /*======================================================*/ theItem = (struct defmoduleItemHeader *) GetModuleItem(theModule,DeftemplateModuleIndex); for (theDeftemplate = (struct deftemplate *) theItem->firstItem; theDeftemplate != NULL ; theDeftemplate = (struct deftemplate *) GetNextDeftemplate(theDeftemplate)) { /*=======================================*/ /* If the deftemplate can be seen by the */ /* current module, then it is in scope. */ /*=======================================*/ if (FindImportedConstruct("deftemplate",theModule, ValueToString(theDeftemplate->header.name), &moduleCount,TRUE,NULL) != NULL) { theDeftemplate->inScope = TRUE; } else { theDeftemplate->inScope = FALSE; } } } }
static void BsaveBinaryItem( Environment *theEnv, FILE *fp) { size_t space; Deftemplate *theDeftemplate; struct bsaveDeftemplate tempDeftemplate; struct templateSlot *theSlot; struct bsaveTemplateSlot tempTemplateSlot; struct bsaveDeftemplateModule tempTemplateModule; Defmodule *theModule; struct deftemplateModule *theModuleItem; /*============================================================*/ /* Write out the amount of space taken up by the deftemplate, */ /* deftemplateModule, and templateSlot data structures in the */ /* binary image. */ /*============================================================*/ space = (DeftemplateBinaryData(theEnv)->NumberOfDeftemplates * sizeof(struct bsaveDeftemplate)) + (DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots * sizeof(struct bsaveTemplateSlot)) + (DeftemplateBinaryData(theEnv)->NumberOfTemplateModules * sizeof(struct bsaveDeftemplateModule)); GenWrite(&space,sizeof(size_t),fp); /*===================================================*/ /* Write out each deftemplate module data structure. */ /*===================================================*/ DeftemplateBinaryData(theEnv)->NumberOfDeftemplates = 0; for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { SetCurrentModule(theEnv,theModule); theModuleItem = (struct deftemplateModule *) GetModuleItem(theEnv,NULL,FindModuleItem(theEnv,"deftemplate")->moduleIndex); AssignBsaveDefmdlItemHdrVals(&tempTemplateModule.header, &theModuleItem->header); GenWrite(&tempTemplateModule,sizeof(struct bsaveDeftemplateModule),fp); } /*============================================*/ /* Write out each deftemplate data structure. */ /*============================================*/ DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots = 0; for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { SetCurrentModule(theEnv,theModule); for (theDeftemplate = GetNextDeftemplate(theEnv,NULL); theDeftemplate != NULL; theDeftemplate = GetNextDeftemplate(theEnv,theDeftemplate)) { AssignBsaveConstructHeaderVals(&tempDeftemplate.header, &theDeftemplate->header); tempDeftemplate.implied = theDeftemplate->implied; tempDeftemplate.numberOfSlots = theDeftemplate->numberOfSlots; tempDeftemplate.patternNetwork = BsaveFactPatternIndex(theDeftemplate->patternNetwork); if (theDeftemplate->slotList != NULL) { tempDeftemplate.slotList = DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots; } else { tempDeftemplate.slotList = ULONG_MAX; } GenWrite(&tempDeftemplate,sizeof(struct bsaveDeftemplate),fp); DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots += theDeftemplate->numberOfSlots; } } /*=============================================*/ /* Write out each templateSlot data structure. */ /*=============================================*/ for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { SetCurrentModule(theEnv,theModule); for (theDeftemplate = GetNextDeftemplate(theEnv,NULL); theDeftemplate != NULL; theDeftemplate = GetNextDeftemplate(theEnv,theDeftemplate)) { for (theSlot = theDeftemplate->slotList; theSlot != NULL; theSlot = theSlot->next) { tempTemplateSlot.constraints = ConstraintIndex(theSlot->constraints); tempTemplateSlot.slotName = theSlot->slotName->bucket; tempTemplateSlot.multislot = theSlot->multislot; tempTemplateSlot.noDefault = theSlot->noDefault; tempTemplateSlot.defaultPresent = theSlot->defaultPresent; tempTemplateSlot.defaultDynamic = theSlot->defaultDynamic; tempTemplateSlot.defaultList = HashedExpressionIndex(theEnv,theSlot->defaultList); tempTemplateSlot.facetList = HashedExpressionIndex(theEnv,theSlot->facetList); if (theSlot->next != NULL) tempTemplateSlot.next = 0L; else tempTemplateSlot.next = ULONG_MAX; GenWrite(&tempTemplateSlot,sizeof(struct bsaveTemplateSlot),fp); } } } /*=============================================================*/ /* If a binary image was already loaded when the bsave command */ /* was issued, then restore the counts indicating the number */ /* of deftemplates, deftemplate modules, and deftemplate slots */ /* in the binary image (these were overwritten by the binary */ /* save). */ /*=============================================================*/ RestoreBloadCount(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfDeftemplates); RestoreBloadCount(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots); RestoreBloadCount(theEnv,&DeftemplateBinaryData(theEnv)->NumberOfTemplateModules); }
static void BsaveFind( Environment *theEnv) { Deftemplate *theDeftemplate; struct templateSlot *theSlot; Defmodule *theModule; /*=======================================================*/ /* If a binary image is already loaded, then temporarily */ /* save the count values since these will be overwritten */ /* in the process of saving the binary image. */ /*=======================================================*/ SaveBloadCount(theEnv,DeftemplateBinaryData(theEnv)->NumberOfDeftemplates); SaveBloadCount(theEnv,DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots); SaveBloadCount(theEnv,DeftemplateBinaryData(theEnv)->NumberOfTemplateModules); /*==================================================*/ /* Set the count of deftemplates, deftemplate slots */ /* and deftemplate module data structures to zero. */ /*==================================================*/ DeftemplateBinaryData(theEnv)->NumberOfDeftemplates = 0; DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots = 0; DeftemplateBinaryData(theEnv)->NumberOfTemplateModules = 0; /*===========================*/ /* Loop through each module. */ /*===========================*/ for (theModule = GetNextDefmodule(theEnv,NULL); theModule != NULL; theModule = GetNextDefmodule(theEnv,theModule)) { /*============================================*/ /* Set the current module to the module being */ /* examined and increment the number of */ /* deftemplate modules encountered. */ /*============================================*/ SetCurrentModule(theEnv,theModule); DeftemplateBinaryData(theEnv)->NumberOfTemplateModules++; /*======================================================*/ /* Loop through each deftemplate in the current module. */ /*======================================================*/ for (theDeftemplate = GetNextDeftemplate(theEnv,NULL); theDeftemplate != NULL; theDeftemplate = GetNextDeftemplate(theEnv,theDeftemplate)) { /*======================================================*/ /* Initialize the construct header for the binary save. */ /*======================================================*/ MarkConstructHeaderNeededItems(&theDeftemplate->header, DeftemplateBinaryData(theEnv)->NumberOfDeftemplates++); /*=============================================================*/ /* Loop through each slot in the deftemplate, incrementing the */ /* slot count and marking the slot names as needed symbols. */ /*=============================================================*/ for (theSlot = theDeftemplate->slotList; theSlot != NULL; theSlot = theSlot->next) { DeftemplateBinaryData(theEnv)->NumberOfTemplateSlots++; theSlot->slotName->neededSymbol = true; } } } }
static int PatternNetworkToCode( char *fileName, int fileID, FILE *headerFP, int imageID, int maxIndices) { int fileCount = 1; struct defmodule *theModule; struct deftemplate *theTemplate; struct factPatternNode *thePatternNode; int networkArrayCount = 0, networkArrayVersion = 1; FILE *networkFile = NULL; /*===========================================================*/ /* Include the appropriate fact pattern network header file. */ /*===========================================================*/ fprintf(headerFP,"#include \"factbld.h\"\n"); /*===============================*/ /* Loop through all the modules. */ /*===============================*/ for (theModule = (struct defmodule *) GetNextDefmodule(NULL); theModule != NULL; theModule = (struct defmodule *) GetNextDefmodule(theModule)) { /*=========================*/ /* Set the current module. */ /*=========================*/ SetCurrentModule((void *) theModule); /*======================================*/ /* Loop through all of the deftemplates */ /* in the current module. */ /*======================================*/ for (theTemplate = (struct deftemplate *) GetNextDeftemplate(NULL); theTemplate != NULL; theTemplate = (struct deftemplate *) GetNextDeftemplate(theTemplate)) { /*======================================================*/ /* Loop through each pattern node in the deftemplate's */ /* pattern network writing its C code representation to */ /* the file as it is traversed. */ /*======================================================*/ for (thePatternNode = theTemplate->patternNetwork; thePatternNode != NULL; thePatternNode = GetNextPatternNode(thePatternNode)) { networkFile = OpenFileIfNeeded(networkFile,fileName,fileID,imageID,&fileCount, networkArrayVersion,headerFP, "struct factPatternNode",FactPrefix(),FALSE,NULL); if (networkFile == NULL) { CloseNetworkFiles(networkFile,maxIndices); return(0); } PatternNodeToCode(networkFile,thePatternNode,imageID,maxIndices); networkArrayCount++; networkFile = CloseFileIfNeeded(networkFile,&networkArrayCount, &networkArrayVersion,maxIndices,NULL,NULL); } } } /*==============================*/ /* Close any C files left open. */ /*==============================*/ CloseNetworkFiles(networkFile,maxIndices); /*===============================*/ /* Return TRUE to indicate the C */ /* code was successfully saved. */ /*===============================*/ return(TRUE); }
static int ConstructToCode( char *fileName, int fileID, FILE *headerFP, int imageID, int maxIndices) { int fileCount = 1; struct defmodule *theModule; struct deftemplate *theTemplate; struct templateSlot *slotPtr; int slotCount = 0, slotArrayCount = 0, slotArrayVersion = 1; int moduleCount = 0, moduleArrayCount = 0, moduleArrayVersion = 1; int templateArrayCount = 0, templateArrayVersion = 1; FILE *slotFile = NULL, *moduleFile = NULL, *templateFile = NULL; /*==================================================*/ /* Include the appropriate deftemplate header file. */ /*==================================================*/ fprintf(headerFP,"#include \"tmpltdef.h\"\n"); /*=============================================================*/ /* Loop through all the modules, all the deftemplates, and all */ /* the deftemplate slots writing their C code representation */ /* to the file as they are traversed. */ /*=============================================================*/ theModule = (struct defmodule *) GetNextDefmodule(NULL); while (theModule != NULL) { SetCurrentModule((void *) theModule); moduleFile = OpenFileIfNeeded(moduleFile,fileName,fileID,imageID,&fileCount, moduleArrayVersion,headerFP, "struct deftemplateModule",ModulePrefix(DeftemplateCodeItem), FALSE,NULL); if (moduleFile == NULL) { CloseDeftemplateFiles(moduleFile,templateFile,slotFile,maxIndices); return(0); } DeftemplateModuleToCode(moduleFile,theModule,imageID,maxIndices,moduleCount); moduleFile = CloseFileIfNeeded(moduleFile,&moduleArrayCount,&moduleArrayVersion, maxIndices,NULL,NULL); /*=======================================================*/ /* Loop through each of the deftemplates in this module. */ /*=======================================================*/ theTemplate = (struct deftemplate *) GetNextDeftemplate(NULL); while (theTemplate != NULL) { templateFile = OpenFileIfNeeded(templateFile,fileName,fileID,imageID,&fileCount, templateArrayVersion,headerFP, "struct deftemplate",ConstructPrefix(DeftemplateCodeItem), FALSE,NULL); if (templateFile == NULL) { CloseDeftemplateFiles(moduleFile,templateFile,slotFile,maxIndices); return(0); } DeftemplateToCode(templateFile,theTemplate,imageID,maxIndices, moduleCount,slotCount); templateArrayCount++; templateFile = CloseFileIfNeeded(templateFile,&templateArrayCount,&templateArrayVersion, maxIndices,NULL,NULL); /*======================================================*/ /* Loop through each of the slots for this deftemplate. */ /*======================================================*/ slotPtr = theTemplate->slotList; while (slotPtr != NULL) { slotFile = OpenFileIfNeeded(slotFile,fileName,fileID,imageID,&fileCount, slotArrayVersion,headerFP, "struct templateSlot",SlotPrefix(),FALSE,NULL); if (slotFile == NULL) { CloseDeftemplateFiles(moduleFile,templateFile,slotFile,maxIndices); return(0); } SlotToCode(slotFile,slotPtr,imageID,maxIndices,slotCount); slotCount++; slotArrayCount++; slotFile = CloseFileIfNeeded(slotFile,&slotArrayCount,&slotArrayVersion, maxIndices,NULL,NULL); slotPtr = slotPtr->next; } theTemplate = (struct deftemplate *) GetNextDeftemplate(theTemplate); } theModule = (struct defmodule *) GetNextDefmodule(theModule); moduleCount++; moduleArrayCount++; } CloseDeftemplateFiles(moduleFile,templateFile,slotFile,maxIndices); return(1); }