1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include "intersci-n.h"
/* global variables */
extern int indent ; /* incremental counter for code indentation */
extern char target ; /* langage for generation */
/**********************************************************
Function to add decaration during the first pass
and to print them during code generation pass 2
**********************************************************/
static struct Declare {
int type;
char *nameF,*nameC;
char **decls ; /* declaration de logical */
int ndecls;
} Init[] = {
{ DEC_CHAR,"character","char",(char **) 0,0},
{ DEC_INT ,"integer","int",(char **) 0,0},
{ DEC_DOUBLE,"double precision","double",(char **) 0,0},
{ DEC_REAL,"real","float",(char **) 0,0},
{ DEC_LOGICAL,"logical","int",(char **) 0,0},
{ DEC_DATA,"data","static",(char **) 0,0},
{ DEC_UL,"double precision","unsigned long",(char **) 0,0},
{ DEC_IPTR,"double precision","int ",(char **) 0,0},
{ DEC_DPTR,"double precision","double",(char **) 0,0},
{ DEC_RPTR,"double precision","float",(char **) 0,0},
{ DEC_CPTR,"double precision","char",(char **) 0,0},
{ DEC_SPARSE,"double precision","SciSparse",(char **) 0,0},
{ DEC_SPARSEPTR,"double precision","SciSparse",(char **) 0,0},
{ DEC_INIT,"","",(char **) 0,0},
{ DEC_SMAT,"Unimplemented","char ",(char **) 0,0},
{ -1 ,"void","void",(char **) 0,0}
};
void InitDeclare()
{
int i = 0;
while ( Init[i].type != -1)
{
Init[i].decls = (char **) 0;
Init[i].ndecls =0 ;
i++;
}
}
void ResetDeclare()
{
int j = 0;
while ( Init[j].type != -1)
{
if ( Init[j].decls != (char **) 0)
{
int i;
for ( i = 0 ; i < Init[j].ndecls ; i++ )
free((char *) Init[j].decls[i]);
free (( char *) Init[j].decls );
}
Init[j].decls=(char **) 0;
Init[j].ndecls=0;
j++;
}
}
int CheckDeclare(type,declaration)
int type;
char *declaration;
{
int j = 0;
while ( Init[j].type != -1)
{
if ( Init[j].type == type )
{
int i;
for ( i = 0 ; i < Init[j].ndecls ; i++ )
{
if ( strcmp(declaration,Init[j].decls[i])==0)
return(1);
}
return(0);
}
j++;
}
return(0);
}
/***************************
* AddDeclare1(type,format,arg1,...,argn)
***************************/
#define DECLAREBUF 128
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#ifdef __STDC__
void AddDeclare1(int type,char *format,...)
#else
/*VARARGS0*/
void AddDeclare1(va_alist) va_dcl
#endif
{
char decbuf[DECLAREBUF];
va_list ap;
#ifdef __STDC__
va_start(ap,format);
#else
int type;
char *format;
va_start(ap);
type = va_arg(ap, int );
format = va_arg(ap, char *);
#endif
vsprintf(decbuf,format,ap);
AddDeclare(type,decbuf);
va_end(ap);
}
void AddDeclare(type,declaration)
int type;
char *declaration;
{
int j = 0;
if ( declaration[0] == '&' ) return ;
if ( CheckDeclare(type,declaration)== 1) return ;
while ( Init[j].type != -1)
{
if ( Init[j].type == type )
{
if ( Init[j].decls != (char **) 0)
{
(Init[j].ndecls)++;
Init[j].decls = (char **) realloc((char *) Init[j].decls, (unsigned) (Init[j].ndecls ) *sizeof(char *));
}
else
{
(Init[j].ndecls)++;
Init[j].decls = (char **) malloc ( (unsigned) (Init[j].ndecls ) *sizeof(char *));
}
if ( Init[j].decls == ( char **) 0)
{
fprintf(stderr,"No more space\n");
exit(1);
}
Init[j].decls[Init[j].ndecls-1]=(char*) malloc((unsigned) (strlen(declaration)+1)*sizeof(char));
if ( Init[j].decls[Init[j].ndecls-1] == ( char *) 0)
{
fprintf(stderr,"No more space\n");
exit(1);
}
strcpy( Init[j].decls[Init[j].ndecls-1], declaration );
}
j++;
}
}
void WriteInitDeclarations(f)
FILE *f;
{
int j = 0;
int i;
while ( Init[j].type != -1)
{
if ( Init[j].type == DEC_INIT)
{
for (i= 0 ; i < Init[j].ndecls ; i++)
{
Fprintf(f,indent,"%s",Init[j].decls[i]);
Fprintf(f,indent,";\n");
}
}
j++;
}
}
void WriteDeclaration(f)
FILE *f;
{
int j = 0;
int i;
while ( Init[j].type != -1)
{
if ( Init[j].type == DEC_INIT)
{}
else if( Init[j].type == DEC_DATA )
{
for (i= 0 ; i < Init[j].ndecls ; i++)
{
Fprintf(f,indent,"%s ",Init[j].nameC);
Fprintf(f,indent,"%s",Init[j].decls[i]);
Fprintf(f,indent,";\n");
}
}
else
{
if ( Init[j].ndecls != 0)
Fprintf(f,indent,"%s ",Init[j].nameC);
for (i= 0 ; i < Init[j].ndecls ; i++)
{
if ( Init[j].type >= DEC_IPTR && target == 'C')
{
/* pointers declaration */
Fprintf(f,indent,"*");
}
else if ( Init[j].type == DEC_SMAT && target == 'C')
{
Fprintf(f,indent,"**");
}
Fprintf(f,indent,"%s",Init[j].decls[i]);
if ( i != Init[j].ndecls -1 ) Fprintf(f,indent,",");
else
{
Fprintf(f,indent,";\n");
}
}
}
j++;
}
j=0;
WriteInitDeclarations(f);
}
|