declare.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include "intersci-n.h"
00003 #include "declare.h"
00004 /* global variables */
00005 
00006 extern int indent ; /* incremental counter for code indentation */
00007 extern char target ; /* langage for generation */
00008 
00009 /**********************************************************
00010   Function to add decaration during the first pass 
00011   and to print them during code generation pass 2
00012 **********************************************************/
00013 
00014 
00015 static struct Declare {
00016   int type;
00017   char *nameF,*nameC;
00018   char **decls ; /* declaration de logical */
00019   int  ndecls;
00020 } Init[] = {
00021   { DEC_CHAR,"character","char",(char **) 0,0},
00022   { DEC_INT ,"integer","int",(char **) 0,0},
00023   { DEC_DOUBLE,"double precision","double",(char **) 0,0},
00024   { DEC_REAL,"real","float",(char **) 0,0},
00025   { DEC_LOGICAL,"logical","int",(char **) 0,0},
00026   { DEC_DATA,"data","static",(char **) 0,0},
00027   { DEC_UL,"double precision","unsigned long",(char **) 0,0},
00028   { DEC_IPTR,"double precision","int ",(char **) 0,0},
00029   { DEC_DPTR,"double precision","double",(char **) 0,0},
00030   { DEC_RPTR,"double precision","float",(char **) 0,0},
00031   { DEC_CPTR,"double precision","char",(char **) 0,0},
00032   { DEC_SPARSE,"double precision","SciSparse",(char **) 0,0},
00033   { DEC_SPARSEPTR,"double precision","SciSparse",(char **) 0,0},
00034   { DEC_INIT,"","",(char **) 0,0},
00035   { DEC_SMAT,"Unimplemented","char ",(char **) 0,0},
00036   { -1 ,"void","void",(char **) 0,0}
00037 };
00038 
00039 void InitDeclare() 
00040 { 
00041   int i = 0;
00042   while ( Init[i].type != -1) 
00043     {
00044       Init[i].decls = (char **) 0;
00045       Init[i].ndecls =0 ;
00046       i++;
00047     }
00048 }
00049 
00050 void ResetDeclare()
00051 {
00052   int j = 0;
00053   while ( Init[j].type != -1) 
00054     {
00055       if ( Init[j].decls != (char **) 0) 
00056         {
00057           int i;
00058           for ( i = 0 ; i < Init[j].ndecls ; i++ ) 
00059             free((char *) Init[j].decls[i]);
00060           free (( char *) Init[j].decls );
00061         }
00062       Init[j].decls=(char **) 0;
00063       Init[j].ndecls=0;
00064       j++;
00065     }
00066 }
00067 
00068 int  CheckDeclare(int type,char *declaration)
00069 {
00070   int j = 0;
00071   while ( Init[j].type != -1) 
00072     {
00073       if ( Init[j].type == type ) 
00074         {
00075           int i;
00076           for ( i = 0 ; i < Init[j].ndecls ; i++ ) 
00077             {
00078               if ( strcmp(declaration,Init[j].decls[i])==0) 
00079                 return(1);
00080             }
00081           return(0);
00082         }
00083       j++;
00084     }
00085   return(0);
00086 }
00087 
00088 /***************************
00089  * AddDeclare1(type,format,arg1,...,argn) 
00090  ***************************/
00091 
00092 #define DECLAREBUF 128
00093 
00094 #ifdef __STDC__
00095 #include <stdarg.h>
00096 #else
00097 #include <varargs.h>
00098 #endif 
00099 
00100 #ifdef __STDC__ 
00101 void AddDeclare1(int type,char *format,...) 
00102 #else 
00103      /*VARARGS0*/
00104      void AddDeclare1(va_alist) va_dcl
00105 #endif
00106 {
00107   char decbuf[DECLAREBUF];
00108   va_list ap;
00109 #ifdef __STDC__
00110   va_start(ap,format);
00111 #else 
00112   int type;
00113   char *format;
00114   va_start(ap);
00115   type = va_arg(ap, int );
00116   format = va_arg(ap, char *);
00117 #endif 
00118   vsprintf(decbuf,format,ap);
00119   AddDeclare(type,decbuf);
00120   va_end(ap);
00121 }
00122 
00123 void AddDeclare(int type,char *declaration) 
00124 {
00125   int j = 0;
00126   if ( declaration[0] == '&' ) return ;
00127   if ( CheckDeclare(type,declaration)== 1) return ;
00128   while ( Init[j].type != -1) 
00129     {
00130       if ( Init[j].type == type ) 
00131         {
00132           if ( Init[j].decls != (char **) 0) 
00133             {
00134               (Init[j].ndecls)++;
00135               Init[j].decls =  (char **) realloc((char *) Init[j].decls, (unsigned) (Init[j].ndecls ) *sizeof(char *));
00136             }
00137           else 
00138             {
00139               (Init[j].ndecls)++;
00140               Init[j].decls = (char **) malloc ( (unsigned) (Init[j].ndecls ) *sizeof(char *));
00141             }
00142           if ( Init[j].decls == ( char **) 0) 
00143             {
00144               fprintf(stderr,"No more space\n");
00145               exit(1);
00146             }
00147           Init[j].decls[Init[j].ndecls-1]=(char*) malloc((unsigned) (strlen(declaration)+1)*sizeof(char));
00148           if (    Init[j].decls[Init[j].ndecls-1] == ( char *) 0) 
00149             {
00150               fprintf(stderr,"No more space\n");
00151               exit(1);
00152             }
00153           strcpy(   Init[j].decls[Init[j].ndecls-1], declaration );
00154         }
00155       j++;
00156     }
00157 }
00158 
00159 
00160 void WriteInitDeclarations(FILE *f) 
00161 {
00162   int j = 0;
00163   int i;
00164   while ( Init[j].type != -1) 
00165     {
00166       if ( Init[j].type == DEC_INIT) 
00167         {
00168           for (i= 0 ; i < Init[j].ndecls ; i++) 
00169             {
00170               Fprintf(f,indent,"%s",Init[j].decls[i]);
00171               Fprintf(f,indent,";\n");
00172             }
00173         }
00174       j++;
00175     }
00176 }
00177 
00178 
00179 void WriteDeclaration(FILE *f)
00180 {
00181   int j = 0;
00182   int i;
00183   while ( Init[j].type != -1) 
00184     {      
00185       if ( Init[j].type == DEC_INIT) 
00186         {}
00187       else if( Init[j].type == DEC_DATA ) 
00188         {
00189           for (i= 0 ; i < Init[j].ndecls ; i++) 
00190             {
00191               Fprintf(f,indent,"%s ",Init[j].nameC);
00192               Fprintf(f,indent,"%s",Init[j].decls[i]);
00193               Fprintf(f,indent,";\n");
00194             }
00195         }
00196       else 
00197         {
00198           if ( Init[j].ndecls != 0) 
00199             Fprintf(f,indent,"%s ",Init[j].nameC);
00200           for (i= 0 ; i < Init[j].ndecls ; i++) 
00201             {
00202               if ( Init[j].type >= DEC_IPTR && target == 'C')
00203                 {
00204                   /* pointers declaration */
00205                   Fprintf(f,indent,"*");
00206                 }
00207               else if ( Init[j].type == DEC_SMAT && target == 'C')
00208                 {
00209                   Fprintf(f,indent,"**");
00210                 }
00211               Fprintf(f,indent,"%s",Init[j].decls[i]);
00212               if ( i != Init[j].ndecls -1 ) Fprintf(f,indent,",");
00213               else 
00214                 {
00215                   Fprintf(f,indent,";\n");
00216                 }
00217             }
00218         }
00219       j++;
00220     }
00221   j=0;
00222   WriteInitDeclarations(f);
00223 }
00224 

Generated on Sun Mar 4 15:03:57 2007 for Scilab [trunk] by  doxygen 1.5.1