📄 modulcmp.c
字号:
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.24 06/05/06 */
/* */
/* DEFMODULE CONSTRUCTS-TO-C MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Implements the constructs-to-c feature for the */
/* defmodule construct. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.24: Added environment parameter to GenClose. */
/* */
/*************************************************************/
#define _MODULCMP_SOURCE_
#include "setup.h"
#if CONSTRUCT_COMPILER && (! RUN_TIME)
#include <stdio.h>
#define _STDIO_INCLUDED_
#include "conscomp.h"
#include "moduldef.h"
#include "sysdep.h"
#include "envrnmnt.h"
#include "modulcmp.h"
/***************/
/* DEFINITIONS */
/***************/
#define ItemPrefix() ArbitraryPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem,0)
#define DefmodulePrefix() ArbitraryPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem,1)
#define PortPrefix() ArbitraryPrefix(DefmoduleData(theEnv)->DefmoduleCodeItem,2)
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
static int ConstructToCode(void *,char *,int,FILE *,int,int);
static void InitDefmoduleCode(void *,FILE *,int,int);
static struct portItem *GetNextPortItem(void *,struct defmodule **,struct portItem **,
int *,int *);
static int PortItemsToCode(void *,char *,int,FILE *,int,int,int *);
static void BeforeDefmodulesToCode(void *);
/***************************************************************/
/* DefmoduleCompilerSetup: Initializes the defmodule construct */
/* for use with the constructs-to-c command. */
/***************************************************************/
globle void DefmoduleCompilerSetup(
void *theEnv)
{
DefmoduleData(theEnv)->DefmoduleCodeItem =
AddCodeGeneratorItem(theEnv,"defmodule",200,BeforeDefmodulesToCode,
InitDefmoduleCode,ConstructToCode,3);
}
/***********************************************************/
/* BeforeDefmodulesToCode: Assigns each defmodule a unique */
/* ID which will be used for pointer references when the */
/* data structures are written to a file as C code */
/***********************************************************/
static void BeforeDefmodulesToCode(
void *theEnv)
{
int value = 0;
struct defmodule *theModule;
for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
theModule != NULL;
theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
{ theModule->bsaveID = value++; }
}
/*************************************************************/
/* PrintDefmoduleReference: Writes the C code representation */
/* of a reference to a defmodule data structure. */
/*************************************************************/
globle void PrintDefmoduleReference(
void *theEnv,
FILE *theFile,
struct defmodule *theModule)
{
if (theModule == NULL) fprintf(theFile,"NULL");
else fprintf(theFile,"&%s%d_%ld[%ld]",DefmodulePrefix(),ConstructCompilerData(theEnv)->ImageID,
(long) ((theModule->bsaveID / ConstructCompilerData(theEnv)->MaxIndices) + 1),
(long) (theModule->bsaveID % ConstructCompilerData(theEnv)->MaxIndices));
}
/************************************************/
/* InitDefmoduleCode: Writes out initialization */
/* code for defmodules for a run-time module. */
/************************************************/
#if IBM_TBC
#pragma argsused
#endif
static void InitDefmoduleCode(
void *theEnv,
FILE *initFP,
int imageID,
int maxIndices)
{
#if MAC_MCW || IBM_MCW || MAC_XCD
#pragma unused(maxIndices)
#endif
if (EnvGetNextDefmodule(theEnv,NULL) != NULL)
{ fprintf(initFP," SetListOfDefmodules(theEnv,(void *) %s%d_1);\n",DefmodulePrefix(),imageID); }
else
{ fprintf(initFP," SetListOfDefmodules(theEnv,NULL);\n"); }
fprintf(initFP," EnvSetCurrentModule(theEnv,(void *) EnvGetNextDefmodule(theEnv,NULL));\n");
}
/***********************************************************/
/* ConstructToCode: Produces defmodule code for a run-time */
/* module created using the constructs-to-c function. */
/***********************************************************/
static int ConstructToCode(
void *theEnv,
char *fileName,
int fileID,
FILE *headerFP,
int imageID,
int maxIndices)
{
struct defmodule *theConstruct;
FILE *moduleFile = NULL, *itemsFile;
int portItemCount = 0;
struct portItem *portItemPtr;
int mihCount = 0, moduleCount = 0;
int j;
struct moduleItem *theItem;
int moduleArrayVersion = 1;
int fileCount = 2;
/*================================================*/
/* Include the appropriate defmodule header file. */
/*================================================*/
fprintf(headerFP,"#include \"moduldef.h\"\n");
/*============================================*/
/* Open up the items file for the defmodules. */
/* Only one file of this type is created so */
/* the maximum number of indices is ignored. */
/*============================================*/
if ((itemsFile = NewCFile(theEnv,fileName,fileID,1,FALSE)) == NULL)
{ return(FALSE); }
fprintf(itemsFile,"struct defmoduleItemHeader *%s%d_%d[] = {\n",ItemPrefix(),imageID,1);
fprintf(headerFP,"extern struct defmoduleItemHeader *%s%d_%d[];\n",ItemPrefix(),imageID,1);
/*======================================================*/
/* Loop through all the defmodules writing their C code */
/* representation to the file as they are traversed. */
/*======================================================*/
for (theConstruct = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
theConstruct != NULL;
theConstruct = (struct defmodule *) EnvGetNextDefmodule(theEnv,theConstruct))
{
/*===========================================*/
/* Open a new file to write to if necessary. */
/*===========================================*/
moduleFile = OpenFileIfNeeded(theEnv,moduleFile,fileName,fileID,imageID,
&fileCount,moduleArrayVersion,headerFP,
"struct defmodule",DefmodulePrefix(),
FALSE,NULL);
if (moduleFile == NULL)
{
moduleCount = maxIndices;
CloseFileIfNeeded(theEnv,moduleFile,&moduleCount,
&moduleArrayVersion,maxIndices,NULL,NULL);
GenClose(theEnv,itemsFile);
return(FALSE);
}
/*======================================*/
/* Write the construct name and ppform. */
/*======================================*/
fprintf(moduleFile,"{");
PrintSymbolReference(theEnv,moduleFile,theConstruct->name);
fprintf(moduleFile,",NULL,");
/*=====================================================*/
/* Write the items array pointers to other constructs. */
/*=====================================================*/
fprintf(moduleFile,"&%s%d_1[%d],",ItemPrefix(),imageID,mihCount);
for (j = 0, theItem = GetListOfModuleItems(theEnv);
(j < GetNumberOfModuleItems(theEnv)) && (theItem != NULL) ;
j++, theItem = theItem->next)
{
mihCount++;
if (theItem->constructsToCModuleReference == NULL)
{ fprintf(itemsFile,"NULL"); }
else
{ (*theItem->constructsToCModuleReference)(theEnv,itemsFile,(int) theConstruct->bsaveID,imageID,maxIndices); }
if ((j + 1) < GetNumberOfModuleItems(theEnv)) fprintf(itemsFile,",");
else if (theConstruct->next != NULL) fprintf(itemsFile,",\n");
}
/*=================================*/
/* Write the importList reference. */
/*=================================*/
if (theConstruct->importList == NULL)
{ fprintf(moduleFile,"NULL,"); }
else
{
fprintf(moduleFile,"&%s%d_%d[%d],",PortPrefix(),imageID,
(portItemCount / maxIndices) + 1,
portItemCount % maxIndices);
for (portItemPtr = theConstruct->importList;
portItemPtr != NULL;
portItemPtr = portItemPtr->next)
{ portItemCount++; }
}
/*=================================*/
/* Write the exportList reference. */
/*=================================*/
if (theConstruct->exportList == NULL)
{ fprintf(moduleFile,"NULL,"); }
else
{
fprintf(moduleFile,"&%s%d_%d[%d],",PortPrefix(),imageID,
(portItemCount / maxIndices) + 1,
portItemCount % maxIndices);
for (portItemPtr = theConstruct->exportList;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -