00001 #include <stdlib.h>
00002
00003 #include "intersci-n.h"
00004 #include "outext.h"
00005
00006
00007
00008
00009
00010
00011
00012 extern int icre ;
00013 extern int indent ;
00014 extern int pass ;
00015
00016 static char str[MAXNAM];
00017 static char str1[MAXNAM];
00018 static char str2[MAXNAM];
00019 static char str3[MAXNAM];
00020
00021 void OutExtCOLUMN(FILE *f,VARPTR var,int insidelist,int nel)
00022 {
00023 strcpy(str2,"1");
00024 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00025 OutExtCommon(f,var,insidelist,nel);
00026 }
00027
00028 void OutExtROW(FILE *f,VARPTR var,int insidelist,int nel)
00029 {
00030 strcpy(str1,"1");
00031 strcpy(str2,Forname2Int(variables[var->el[0]-1],0));
00032 OutExtCommon(f,var,insidelist,nel);
00033 }
00034
00035 void OutExtVECTOR(FILE *f,VARPTR var,int insidelist,int nel)
00036 {
00037 strcpy(str1,"1");
00038 strcpy(str2,Forname2Int(variables[var->el[0]-1],0));
00039 OutExtCommon(f,var,insidelist,nel);
00040 }
00041
00042 void OutExtMATRIX(FILE *f,VARPTR var,int insidelist,int nel)
00043 {
00044 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00045 strcpy(str2,Forname2Int(variables[var->el[1]-1],0));
00046 OutExtCommon(f,var,insidelist,nel);
00047 }
00048
00049 void OutExtSCALAR(FILE *f,VARPTR var,int insidelist,int nel)
00050 {
00051 strcpy(str1,"1");
00052 strcpy(str2,"1");
00053 OutExtCommon(f,var,insidelist,nel);
00054 }
00055
00056 void OutExtCommon(FILE *f,VARPTR var,int insidelist,int nel)
00057 {
00058 if ( insidelist != 0)
00059 {
00060 int iout = GetExistOutVar();
00061 VARPTR vout = variables[iout -1];
00062 AddDeclare(DEC_INT,"lrs");
00063 if ( strncmp(var->fexternal,"cintf",4)==0 ||
00064 strncmp(var->fexternal,"cboolf",5)==0 ||
00065 strncmp(var->fexternal,"cdoublef",7)==0 ||
00066 strncmp(var->fexternal,"cfloatf",6)==0 ||
00067 strncmp(var->fexternal,"ccharf",5)==0)
00068 {
00069 if (isdigit(str1[0]) != 0)
00070 {
00071 if (isdigit(str2[0]) != 0)
00072 {
00073 AddDeclare(DEC_INT,"loc1");
00074 AddDeclare(DEC_INT,"loc2");
00075 Fprintf(f,indent,"CreateListVarFromPtr(%d,%d,\"%s\",(loc1=%s,&loc1),(loc2=%s,&loc2),&le%d);\n",
00076 vout->stack_position,nel,
00077 SGetExtForTypeAbrev(var),
00078 str1,str2,var->stack_position);
00079 }
00080 else
00081 {
00082 AddDeclare(DEC_INT,"loc1");
00083 Fprintf(f,indent,"CreateListVarFromPtr(%d,%d,\"%s\",(loc1=%s,&loc1),%s,&le%d);\n",
00084 vout->stack_position,nel,
00085 SGetExtForTypeAbrev(var),
00086 str1,str2,var->stack_position);
00087 }
00088 }
00089 else
00090 {
00091 if (isdigit(str2[0]) != 0)
00092 {
00093 AddDeclare(DEC_INT,"loc2");
00094 Fprintf(f,indent,"CreateListVarFromPtr(%d,%d,\"%s\",%s,(loc2=%s,&loc2),&le%d);\n",
00095 vout->stack_position,nel,
00096 SGetExtForTypeAbrev(var),
00097 str1,str2,var->stack_position);
00098 }
00099 else
00100 {
00101 Fprintf(f,indent,"CreateListVarFromPtr(%d,%d,\"%s\",%s,%s,&le%d);\n",
00102 vout->stack_position,nel,
00103 SGetExtForTypeAbrev(var),
00104 str1,str2,var->stack_position);
00105 }
00106 }
00107 if ( strcmp(var->fexternal,"cintf")==0 ||
00108 strcmp(var->fexternal,"cboolf")==0 ||
00109 strcmp(var->fexternal,"cdoublef")==0 ||
00110 strcmp(var->fexternal,"cfloatf")==0 ||
00111 strcmp(var->fexternal,"ccharf")==0 )
00112 Fprintf(f,indent,"if (%s*%s != 0) FreePtr(&le%d);\n",
00113 (str1[0]== '&') ? str1+1: str1,
00114 (str2[0]== '&') ? str2+1: str2,
00115 var->stack_position);
00116 }
00117 else
00118 {
00119 Fprintf(f,indent,"CreateListVar(%d,\"%s\",%s,%s,&lrs);\n",icre,
00120 SGetExtForTypeAbrev(var),
00121 str1,str2);
00122 }
00123 }
00124 else
00125 {
00127 if ( strncmp(var->fexternal,"cintf",4)==0 ||
00128 strncmp(var->fexternal,"cboolf",5)==0 ||
00129 strncmp(var->fexternal,"cdoublef",7)==0 ||
00130 strncmp(var->fexternal,"cfloatf",6)==0 ||
00131 strncmp(var->fexternal,"ccharf",5)==0)
00132 {
00133 if (isdigit(str1[0]) != 0)
00134 {
00135 if (isdigit(str2[0]) != 0)
00136 {
00137 AddDeclare(DEC_INT,"loc1");
00138 AddDeclare(DEC_INT,"loc2");
00139 Fprintf(f,indent,"CreateVarFromPtr(%d,\"%s\",(loc1=%s,&loc1),(loc2=%s,&loc2),&le%d);\n",icre,
00140 SGetExtForTypeAbrev(var),
00141 str1,str2,var->stack_position);
00142 }
00143 else
00144 {
00145 AddDeclare(DEC_INT,"loc1");
00146 Fprintf(f,indent,"CreateVarFromPtr(%d,\"%s\",(loc1=%s,&loc1),%s,&le%d);\n",icre,
00147 SGetExtForTypeAbrev(var),
00148 str1,str2,var->stack_position);
00149 }
00150 }
00151 else
00152 {
00153 if (isdigit(str2[0]) != 0)
00154 {
00155 AddDeclare(DEC_INT,"loc2");
00156 Fprintf(f,indent,"CreateVarFromPtr(%d,\"%s\",%s,(loc2=%s,&loc2),&le%d);\n",icre,
00157 SGetExtForTypeAbrev(var),
00158 str1,str2,var->stack_position);
00159 }
00160 else
00161 {
00162 Fprintf(f,indent,"CreateVarFromPtr(%d,\"%s\",%s,%s,&le%d);\n",icre,
00163 SGetExtForTypeAbrev(var),
00164 str1,str2,var->stack_position);
00165 }
00166 }
00167 if ( strcmp(var->fexternal,"cintf")==0 ||
00168 strcmp(var->fexternal,"cboolf")==0 ||
00169 strcmp(var->fexternal,"cdoublef")==0 ||
00170 strcmp(var->fexternal,"cfloatf")==0 ||
00171 strcmp(var->fexternal,"ccharf")==0 )
00172 Fprintf(f,indent,"if (%s*%s != 0) FreePtr(&le%d);\n",
00173 (str1[0]== '&') ? str1+1: str1,
00174 (str2[0]== '&') ? str2+1: str2,
00175 var->stack_position);
00176 }
00177 else
00178 {
00179 Fprintf(f,indent,"CreateVar(%d,\"%s\",%s,%s,&lrs);\n",icre,
00180 SGetExtForTypeAbrev(var),
00181 str1,str2);
00182 }
00183 Fprintf(f,indent,"LhsVar(%d)=%d;\n",var->out_position,icre);
00184 icre++;
00185 }
00186 if ( strcmp(var->fexternal,"cintf")==0 ||
00187 strcmp(var->fexternal,"cboolf")==0 ||
00188 strcmp(var->fexternal,"cdoublef")==0 ||
00189 strcmp(var->fexternal,"cfloatf")==0 ||
00190 strcmp(var->fexternal,"ccharf")==0 )
00191 {
00192 }
00193 else
00194 {
00195 AddDeclare(DEC_INT,"lrs");
00196 AddDeclare(DEC_INT,"mn");
00197 Fprintf(f,indent,"mn=%s*%s;\n",str1+1,str2+1);
00198 Fprintf(f,indent,"C2F(%s)(&mn,&le%d,%s(lrs));\n",var->fexternal,
00199 var->stack_position,SGetExtForTypeStack(var));
00200 }
00201 }
00202
00203
00204 void OutExtSTRING(FILE *f,VARPTR var,int insidelist,int nel)
00205 {
00206 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00207 strcpy(str2,"1");
00208 if ( strcmp(var->fexternal,"ccharf")== 0 || strcmp(var->fexternal,"cchar")== 0 )
00209 {
00210 if ( insidelist != 0)
00211 {
00212 int iout = GetExistOutVar();
00213 VARPTR vout = variables[iout -1];
00214 AddDeclare(DEC_INT,"un=1");
00215 strcpy(str2,"&un");
00216 Fprintf(f,indent,"CreateListVarFromPtr(%d,%d,\"c\",%s,%s,&le%d);\n",
00217 vout->stack_position,nel,str1,str2,var->stack_position);
00218 }
00219 else
00220 {
00221 AddDeclare(DEC_INT,"un=1");
00222 strcpy(str2,"&un");
00223 Fprintf(f,indent,"CreateVarFromPtr(%d,\"c\",%s,%s,&le%d);\n",icre,
00224 str1,str2,var->stack_position);
00225 Fprintf(f,indent,"LhsVar(%d)=%d;\n",var->out_position,icre);
00226 icre++;
00227 }
00228 if ( strcmp(var->fexternal,"ccharf")== 0 )
00229 Fprintf(f,indent,"FreePtr(&le%d);\n",var->stack_position);
00230 }
00231 else
00232 {
00233 if ( insidelist != 0)
00234 {
00235 int iout = GetExistOutVar();
00236 VARPTR vout = variables[iout -1];
00237 AddDeclare(DEC_INT,"lrs");
00239 AddDeclare(DEC_INT,"m_u=-1");
00240 AddDeclare(DEC_INT,"un=1");
00241 strcpy(str2,"&un");
00242 Fprintf(f,indent,"CreateListVar(%d,%d,\"c\",%s,%s,&lrs,m_u);\n",
00243 vout->stack_position,nel,str1,str2);
00244 }
00245 else
00246 {
00248 AddDeclare(DEC_INT,"un=1");
00249 strcpy(str2,"&un");
00250 Fprintf(f,indent,"CreateVar(%d,\"c\",%s,%s,&lrs);\n",icre,
00251 str1,str2);
00252 Fprintf(f,indent,"LhsVar(%d)=%d;\n",var->out_position,icre);
00253 icre++;
00254 }
00255 AddDeclare(DEC_INT,"lrs");
00256 AddDeclare(DEC_INT,"mn");
00257 Fprintf(f,indent,"mn=%s*%s;\n",str1+1,str2+1);
00258 Fprintf(f,indent,"C2F(%s)(&mn,&le%d,cstk(lrs));\n",var->fexternal,
00259 var->stack_position);
00260 }
00261 }
00262
00263
00264 void OutExtSPARSE(FILE *f,VARPTR var,int insidelist,int nel)
00265 {
00266 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00267 strcpy(str2,Forname2Int(variables[var->el[1]-1],0));
00268 sprintf(str3,"%d",var->stack_position);
00269 if ( insidelist != 0)
00270 {
00271 int iout = GetExistOutVar();
00272 VARPTR vout = variables[iout -1];
00273 Fprintf(f,indent,"CreateListVarFromPtr(%d,\"s\",&(S%s->m),&(S%s->n),S%s);\n",
00274 vout->stack_position,nel,str3,str3,str3);
00275 }
00276 else
00277 {
00278 Fprintf(f,indent,"LhsVar(%d)=%d;\n",var->out_position,icre);
00279 Fprintf(f,indent,"CreateVarFromPtr(%d,\"s\",&(S%s->m),&(S%s->n),S%s);\n",
00280 icre,str3,str3,str3);
00281 icre++;
00282 }
00283 if ( strcmp(var->fexternal,"csparsef")==0)
00284 {
00285 Fprintf(f,indent,"FreeSparse(S%s);\n",str3);
00286
00287 }
00288 }
00289
00290
00291
00292 void OutExtIMATRIX(FILE *f,VARPTR var,int insidelist,int nel)
00293 {
00294 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00295 strcpy(str2,Forname2Int(variables[var->el[1]-1],0));
00296 strcpy(str3,Forname2Int(variables[var->el[2]-1],0));
00297 if ( insidelist != 0)
00298 {
00299 int iout = GetExistOutVar();
00300 VARPTR vout = variables[iout -1];
00301 AddDeclare(DEC_INT,"lrs");
00302 AddDeclare(DEC_INT,"lcs");
00304 AddDeclare(DEC_INT,"m_u=-1");
00305 Fprintf(f,indent,"CreateListCVar(%d,%d,\"%s\",%s,%s,%s,&lrs,&lcs,m_u,m_u);\n",
00306 vout->stack_position,nel,
00307 SGetExtForTypeAbrev(var),
00308 str3,str1,str2);
00309 }
00310 else
00311 {
00313 Fprintf(f,indent,"CreateCVar(%d,\"%s\",%s,%s,%s,&lrs,&lcs);\n",icre,
00314 SGetExtForTypeAbrev(var),
00315 str3,str1,str2);
00316 Fprintf(f,indent,"LhsVar(%d)=%d;\n",var->out_position,icre);
00317 icre++;
00318 }
00319 AddDeclare(DEC_INT,"lrs");
00320 AddDeclare(DEC_INT,"lcrs");
00321 AddDeclare(DEC_INT,"mnit");
00322 Fprintf(f,indent,"mnit=%s*%s;\n",str1+1,str2+1);
00323 Fprintf(f,indent,"C2F(%s)(&mnit,&ler%d,%s(lrs));\n",var->fexternal,
00324 var->stack_position,SGetExtForTypeStack(var));
00325 Fprintf(f,indent++,"if ( %s > 0 ) {\n",str3+1);
00326 Fprintf(f,indent,"C2F(%s)(&mnit,&lec%d,%s(lrs));\n",var->fexternal,
00327 var->stack_position,SGetExtForTypeStack(var));
00328 Fprintf(f,--indent,"}\n");
00329 }
00330
00331
00332 void OutExtBMATRIX(FILE *f,VARPTR var,int insidelist,int nel)
00333 {
00334 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00335 strcpy(str2,Forname2Int(variables[var->el[1]-1],0));
00336 OutExtCommon(f,var,insidelist,nel);
00337 }
00338
00339
00340 void OutExtBMATRIX1(FILE *f,VARPTR var,int insidelist,int nel)
00341 {
00342 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00343 strcpy(str2,Forname2Int(variables[var->el[1]-1],0));
00344 if ( insidelist != 0)
00345 {
00346 AddDeclare(DEC_LOGICAL,"listcrebmat");
00347 Fprintf(f,indent,"if(.not.listcrebmat(fname,top,%d,lw,%s,%s,lrs)) return\n",
00348 nel,str1,str2);
00349 }
00350 else
00351 {
00352 AddDeclare(DEC_LOGICAL,"crebmat");
00353 Fprintf(f,indent,"if(.not.crebmat(fname,top,%s,%s,lrs)) return\n",str1,str2);
00354 }
00355 sprintf(str,"lr%d",var->stack_position);
00356 Fprintf(f,indent,"call %s(%s*%s,istk(%s),istk(lrs))\n",var->fexternal,str1,str2,str);
00357 }
00358
00359 void OutExtPOLYNOM(FILE *f,VARPTR var,int insidelist,int nel)
00360 {
00361 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00362 strcpy(str2,variables[var->el[1]-1]->name);
00363 Fprintf(f,indent,"err=sadr(ilw+10)+%s-lstk(bot)\n",str1);
00364 Fprintf(f,indent,"if(err .gt. 0) then\n");
00365 Fprintf(f,indent,"call error(17)\n");
00366 Fprintf(f,indent,"return\n");
00367 Fprintf(f,indent,"endif\n");
00368 Fprintf(f,indent,"istk(ilw)=1\n");
00369 Fprintf(f,indent,"istk(ilw+1)=1\n");
00370 Fprintf(f,indent,"istk(ilw+2)=1\n");
00371 Fprintf(f,indent,"istk(ilw+3)=0\n");
00372 Fprintf(f,indent,"call cvstr(4,'%s ',istk(ilw+4),0)\n",str2);
00373
00374 Fprintf(f,indent,"istk(ilw+8)=1\n");
00375 Fprintf(f,indent,"istk(ilw+9)=1+%s\n",str1);
00376 sprintf(str,"lr%d",var->stack_position);
00377 Fprintf(f,indent,"lw=sadr(ilw+10)\n");
00378 Fprintf(f,indent,"call %s(%s,stk(%s),stk(lw))\n",
00379 var->fexternal,str1,str);
00380 Fprintf(f,indent,"lw=lw+%s\n",str1);
00381 }
00382
00383
00384 void OutExtPOINTER(FILE *f,VARPTR var,int insidelist,int nel)
00385 {
00386 if ( insidelist != 0)
00387 {
00388 printf(" %s in output list : not implemented ;",SGetSciType(var->type));
00389 }
00390 else
00391 {
00392 }
00393 }
00394
00395 void OutExtSTRINGMAT(FILE *f,VARPTR var,int insidelist,int nel)
00396 {
00397 strcpy(str1,Forname2Int(variables[var->el[0]-1],0));
00398 strcpy(str2,Forname2Int(variables[var->el[1]-1],0));
00399 sprintf(str,"lr%d",var->stack_position);
00400 Fprintf(f,indent,"call %s(stk(%s),istk(ilw),%s,%s,lstk(bot)-sadr(ilw),ierr)\n",
00401 var->fexternal,str,str1,str2);
00402 Fprintf(f,indent,"if(ierr .gt. 0) then\n");
00403 Fprintf(f,indent,"buf='not enough memory'\n");
00404 Fprintf(f,indent,"call error(1000)\n");
00405 Fprintf(f,indent,"return\n");
00406 Fprintf(f,indent,"endif\n");
00407 sprintf(str,"istk(ilw+4+%s*%s)-1",str1,str2);
00408 Fprintf(f,indent,"lw=sadr(ilw+5+%s*%s+%s)\n",str1,str2,str);
00409 }
00410
00411
00412 void OutExtANY(FILE *f,VARPTR var,int insidelist,int nel)
00413 {
00414 printf("output variable \"%s\" cannot have type\n",
00415 var->name);
00416 printf(" \"WORK\", \"LIST\", \"TLIST\", \"SEQUENCE\" or \"ANY\"\n");
00417 exit(1);
00418 }
00419
00420 typedef struct {
00421 int type;
00422 void (*fonc) __PARAMS((FILE *f,VARPTR var,int insidelist,int nel));} OutExtRhsTab ;
00423
00424
00425
00426
00427 OutExtRhsTab OutExtRHSTAB[] = {
00428 {DIMFOREXT,OutExtANY},
00429 {COLUMN,OutExtCOLUMN},
00430 {LIST,OutExtANY},
00431 {TLIST,OutExtANY},
00432 {MATRIX,OutExtMATRIX},
00433 {POLYNOM,OutExtPOLYNOM},
00434 {ROW,OutExtROW},
00435 {SCALAR,OutExtSCALAR},
00436 {SEQUENCE,OutExtANY},
00437 {STRING,OutExtSTRING},
00438 {WORK,OutExtVECTOR},
00439 {EMPTY,OutExtANY},
00440 {ANY,OutExtANY},
00441 {VECTOR,OutExtVECTOR},
00442 {STRINGMAT,OutExtSTRINGMAT},
00443 {SCIMPOINTER,OutExtPOINTER},
00444 {IMATRIX,OutExtIMATRIX},
00445 {SCISMPOINTER,OutExtPOINTER},
00446 {SCILPOINTER,OutExtPOINTER},
00447 {BMATRIX,OutExtBMATRIX},
00448 {SCIBPOINTER,OutExtPOINTER},
00449 {SCIOPOINTER,OutExtPOINTER},
00450 {SPARSE,OutExtSPARSE}
00451 };
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 void WriteExternalVariableOutput(FILE *f,VARPTR var,int insidelist,int nel)
00463 {
00464 (*(OutExtRHSTAB[var->type].fonc))(f,var,insidelist,nel);
00465 }
00466
00467