📄 conscomp.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* CONSTRUCT COMPILER MODULE */ /*******************************************************//*************************************************************//* Purpose: Provides core routines for the constructs-to-c *//* command. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* Brian L. Donnell *//* Barry Cameron *//* *//* Contributing Programmer(s): *//* *//* Revision History: *//* *//*************************************************************/#define _CONSCOMP_SOURCE_#include "setup.h"#if CONSTRUCT_COMPILER && (! RUN_TIME)#include <stdio.h>#define _CLIPS_STDIO_#include <string.h>#include "symbol.h"#include "clipsmem.h"#include "constant.h"#include "exprnpsr.h"#include "cstrccom.h"#include "constrct.h"#include "argacces.h"#include "cstrncmp.h"#include "router.h"#include "utility.h"#include "modulcmp.h"#if DEFRULE_CONSTRUCT#include "network.h"#endif#if DEFFUNCTION_CONSTRUCT#include "dffnxcmp.h"#endif#if DEFTEMPLATE_CONSTRUCT#include "tmpltcmp.h"#endif#if DEFGLOBAL_CONSTRUCT#include "globlcmp.h"#endif#if DEFGENERIC_CONSTRUCT#include "genrccmp.h"#endif#if OBJECT_SYSTEM#include "objcmp.h"#endif#include "conscomp.h"/***************//* DEFINITIONS *//***************/#define FSIZE 80/**********************************************//* CONSTRUCT CODES DEFINITIONS: The codes F, *//* I, B, S, E, P, L, and C are not included *//* because those are already taken. *//* */ /* B: BitMap hash nodes *//* C: Constraint hash nodes *//* E: Expression hash nodes *//* F: Float hash nodes *//* I: Integer hash nodes *//* L: Bitmaps *//* P: Functions *//* S: Symbol hash nodes *//**********************************************/ #define PRIMARY_CODES "ADGHJKMNOQRTUVWXYZ"#define PRIMARY_LEN 18#define SECONDARY_CODES "ABCDEFGHIJKLMNOPQRSTUVWXYZ"#define SECONDARY_LEN 26/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER VOID ConstructsToCCommand(void); static int ConstructsToC(char *,int,int); static VOID WriteFunctionExternDeclarations(FILE *); static int FunctionsToCode(char *); static int WriteInitializationFunction(char *); static VOID DumpExpression(struct expr *); static VOID MarkConstruct(struct constructHeader *,VOID *); static VOID HashedExpressionsToCode(VOID);#else VOID ConstructsToCCommand(); static int ConstructsToC(); static VOID WriteFunctionExternDeclarations(); static int FunctionsToCode(); static int WriteInitializationFunction(); static VOID DumpExpression(); static VOID MarkConstruct(); static VOID HashedExpressionsToCode();#endif/****************************************//* GLOBAL INTERNAL VARIABLE DEFINITIONS *//****************************************/ globle int ImageID; globle FILE *HeaderFP; globle int MaxIndices = 2000; /***************************************//* LOCAL INTERNAL VARIABLE DEFINITIONS *//***************************************/ static FILE *ExpressionFP; static char *FilePrefix; static BOOLEAN ExpressionHeader; static long ExpressionCount; static int ExpressionVersion; static struct CodeGeneratorItem *ListOfCodeGeneratorItems = NULL;/**********************************************//* ConstructsToCCommand: CLIPS access routine *//* for the constructs-to-c command. *//**********************************************/globle VOID ConstructsToCCommand() { char *fileName; DATA_OBJECT theArg; int argCount; int id, max;#if VAX_VMS || IBM_MSC || IBM_TBC || IBM_ICB || IBM_ZTC || IBM_SC int i;#endif /*============================================*/ /* Check for appropriate number of arguments. */ /*============================================*/ if ((argCount = ArgRangeCheck("constructs-to-c",2,3)) == -1) return; /*====================================================*/ /* Get the name of the file in which to place C code. */ /*====================================================*/ if (ArgTypeCheck("constructs-to-c",1,SYMBOL_OR_STRING,&theArg) == CLIPS_FALSE) { return; } fileName = DOToString(theArg); /*================================*/ /* File names for the VAX and IBM */ /* PCs can't contain a period. */ /*================================*/ #if VAX_VMS || IBM_MSC || IBM_TBC || IBM_ICB || IBM_ZTC || IBM_SC for (i = 0 ; *(fileName+i) ; i++) { if (*(fileName+i) == '.') { PrintErrorID("CONSCOMP",1,CLIPS_FALSE); PrintCLIPS(WERROR,"Invalid file name "); PrintCLIPS(WERROR,fileName); PrintCLIPS(WERROR," contains \'.\'\n"); return; } }#endif /*===========================================*/ /* If the base file name is greater than 3 */ /* characters, issue a warning that the file */ /* name lengths may exceed what is allowed */ /* under some operating systems. */ /*===========================================*/ if (((int) strlen(fileName)) > 3) { PrintWarningID("CONSCOMP",1,CLIPS_FALSE); PrintCLIPS(WWARNING,"Base file name exceeds 3 characters.\n"); PrintCLIPS(WWARNING," This may cause files to be overwritten if file name length\n"); PrintCLIPS(WWARNING," is limited on your platform.\n"); } /*====================================*/ /* Get the runtime image ID argument. */ /*====================================*/ if (ArgTypeCheck("constructs-to-c",2,INTEGER,&theArg) == CLIPS_FALSE) { return; } id = DOToInteger(theArg); if (id < 0) { ExpectedTypeError1("constructs-to-c",2,"positive integer"); return; } /*===========================================*/ /* Get the maximum number of data structures */ /* to store per file argument (if supplied). */ /*===========================================*/ if (argCount == 3) { if (ArgTypeCheck("constructs-to-c",3,INTEGER,&theArg) == CLIPS_FALSE) { return; } max = DOToInteger(theArg); if (max < 0) { ExpectedTypeError1("constructs-to-c",3,"positive integer"); return; } } else { max = 10000; } /*============================*/ /* Call the driver routine to */ /* generate the C code. */ /*============================*/ ConstructsToC(fileName,id,max); }/***************************************//* ConstructsToC: C access routine for *//* the constructs-to-c command. *//***************************************/static int ConstructsToC(fileName,theImageID,max) char *fileName; int theImageID, max; { char fname[FSIZE]; int fileVersion; struct CodeGeneratorItem *cgPtr; /*===============================================*/ /* Set the global MaxIndices variable indicating */ /* the maximum number of data structures to save */ /* in each file. */ /*===============================================*/ MaxIndices = max; /*==================================*/ /* Call the list of functions to be */ /* executed before generating code. */ /*==================================*/ for (cgPtr = ListOfCodeGeneratorItems; cgPtr != NULL; cgPtr = cgPtr->next) { if (cgPtr->beforeFunction != NULL) (*cgPtr->beforeFunction)(); } /*=================================================*/ /* Do a periodic cleanup without using heuristics */ /* to get rid of as much garbage as possible so */ /* that it isn't written out as C data structures. */ /*=================================================*/ PeriodicCleanup(CLIPS_FALSE,CLIPS_FALSE); /*=====================================*/ /* Initialize some global information. */ /*=====================================*/ FilePrefix = fileName; ImageID = theImageID; ExpressionFP = NULL; ExpressionVersion = 1; ExpressionHeader = CLIPS_TRUE; ExpressionCount = 0; /*=====================================================*/ /* Open a header file for dumping general information. */ /*=====================================================*/ sprintf(fname,"%s.h",fileName); if ((HeaderFP = fopen(fname,"w")) == NULL) { OpenErrorMessage("constructs-to-c",fname); return(0); } fprintf(HeaderFP,"#ifndef _CONSTRUCT_COMPILER_HEADER_\n"); fprintf(HeaderFP,"#define _CONSTRUCT_COMPILER_HEADER_\n\n"); fprintf(HeaderFP,"#include <stdio.h>\n"); fprintf(HeaderFP,"#include \"setup.h\"\n"); fprintf(HeaderFP,"#include \"expressn.h\"\n"); fprintf(HeaderFP,"#include \"extnfunc.h\"\n"); fprintf(HeaderFP,"#include \"clips.h\"\n"); fprintf(HeaderFP,"\n#define VS (VOID *)\n"); fprintf(HeaderFP,"\n"); /*=========================================================*/ /* Give extern declarations for user and system functions. */ /*=========================================================*/ WriteFunctionExternDeclarations(HeaderFP); fprintf(HeaderFP,"\n#endif\n\n"); fprintf(HeaderFP,"/****************************/\n"); fprintf(HeaderFP,"/* EXTERN ARRAY DEFINITIONS */\n"); fprintf(HeaderFP,"/****************************/\n\n"); /*==================================*/ /* Generate code for atomic values, */ /* function definitions, hashed */ /* expressions, and constructs. */ /*==================================*/ AtomicValuesToCode(fileName); FunctionsToCode(fileName); HashedExpressionsToCode(); ConstraintsToCode(fileName,4,HeaderFP,ImageID,MaxIndices); /*===============================*/ /* Call each code generator item */ /* for the various constructs. */ /*===============================*/ fileVersion = 5; for (cgPtr = ListOfCodeGeneratorItems; cgPtr != NULL; cgPtr = cgPtr->next) { if (cgPtr->generateFunction != NULL) { (*cgPtr->generateFunction)(fileName,fileVersion,HeaderFP,ImageID,MaxIndices); fileVersion++; } } /*=========================================*/ /* Restore the atomic data bucket values */ /* (which were set to an index reference). */ /*=========================================*/ RestoreAtomicValueBuckets(); /*============================*/ /* Close the expression file. */ /*============================*/ if (ExpressionFP != NULL) { fprintf(ExpressionFP,"};\n"); fclose(ExpressionFP); } /*====================================*/ /* Write the initialization function. */ /*====================================*/ WriteInitializationFunction(fileName); /*========================*/ /* Close the header file. */ /*========================*/ fclose(HeaderFP); /*==================================================*/ /* Return TRUE to indicate that the constructs-to-c */ /* command was successfully executed. */ /*==================================================*/ return(CLIPS_TRUE); }/*******************************************************//* WriteFunctionExternDeclarations: Loop through the *//* list of function definitions and generates extern *//* declarations for them in the specified file. *//*******************************************************/static VOID WriteFunctionExternDeclarations(fp) FILE *fp; { struct FunctionDefinition *theFunction; fprintf(fp,"\n"); fprintf(fp,"/************************************/\n"); fprintf(fp,"/* EXTERNAL FUNCTION DEFINITIONS */\n"); fprintf(fp,"/************************************/\n\n"); for (theFunction = GetFunctionList(); theFunction != NULL; theFunction = theFunction->next) { fprintf(fp,"extern "); switch(theFunction->returnValueType) { case 'i': case 'b': fprintf(fp,"int "); break; case 'l': fprintf(fp,"long "); break; case 'f': fprintf(fp,"float "); break; case 'd': fprintf(fp,"double "); break; case 'w': case 's': case 'o': fprintf(fp,"SYMBOL_HN *"); break; case 'c': fprintf(fp,"char "); break; case 'a': case 'x': fprintf(fp,"VOID * "); break; case 'v': case 'm': case 'u': case 'n': case 'j': case 'k': fprintf(fp,"VOID "); break; default: CLIPSSystemError("CONSCOMP",1); break; } fprintf(fp,"%s(",theFunction->actualFunctionName); switch(theFunction->returnValueType) { case 'i': case 'b': case 'l': case 'f': case 'd': case 'w': case 's': case 'o': case 'c': case 'a': case 'x': case 'v': fprintf(fp,"VOID_ARG"); break; case 'm': case 'u': case 'n':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -