:开始::函数:fortranget:模式:FortranGet[filename_String,debug_Integer:1]:参数:{filename,debug}:参数类型:{String,Integer}:返回类型:手动:结束::Evaluate:FortranGet::nofile=“无法打开`1'。”:Evaluate:FortranGet::nooutput=“警告:未找到输出。”:Evaluate:FortranGet::syntax=“`1`中的语法错误。”:Evaluate:FGeval[lhs_,rhs_]:=检查[ToExpression[lhs]->ToExpression[rhs],消息[FortranGet::syntax,lhs,rhs]]:评估:FGpar[x_]:=x:评估:FGpar[x_,y_]:=x+I y:评估:FGnum[n_]:=n/*FortranGet.tm公司[最后修改日期:2017年1月19日,托马斯·哈恩]此程序提供了一个Mathematica函数FortranGet,它读取Fortran程序进入Mathematica,只选择变量赋值。编译:键入“mcc-O-O FortranGet-FortranGet.tm”,然后如果需要,安装可在一些方便的位置执行FortranGet,如/usr/local/bin。用法:在Mathematica中安装软件包:安装[“FortranGet”]。阅读Fortran程序:FortranGet[“file.f”]。输出:Fortran表达式expr=a+b+c在Mathematica中以“expr->a+b+c”的形式出现。如果有几个表达式如果Fortran程序中存在,则返回此类规则的列表。FGpar环绕所有带括号的对象,包括Fortran-style复数“(re,im)”。FGnum环绕所有(整数和实数)数字。调试标志可以设置为以下值的总和:1在表达式中以字符串形式传输未识别的代码部分,stderr上的2个回波读取行,4回显stderr上传输给Mathematica的表达式的lhs,8个在stderr上传输给Mathematica的表达式的回音rhs。请注意,前端抑制stderr输出。*/#包括“mathlink.h”#包括#包括#包括#定义RHSBUF 1024#定义EXPRBUF 512typedef结构rhs{struct rhs*next;字符表达式[RHSBUF];}RHS;typedef结构表达式{struct-expr*next;char*pre;右侧*右侧;整数nrhs;char lhs[EXPRBUF];}EXPR;typedef常量int cint;typedef字符*字符串;typedef MLCONST char*cstring;静态内联void MLMessage(MLINK mlp,cstring标记,cstring-arg){MLPutFunction(mlp,“复合表达式”,2);MLPutFunction(mlp,“消息”,(arg)?2∶1);MLPutFunction(mlp,“消息名称”,2);MLPutSymbol(mlp,“FortranGet”);MLPutString(mlp,tag);if(arg)MLPutString(mlp,arg);}静态内联void MLFail(MLINK mlp,cstring标记,cstring参数){MLMessage(mlp、tag、arg);MLPutSymbol(mlp,“$Failed”);MLEndPacket(mlp);}静态void sendexpr(EXPR*EXPR,int nexpr,cint debug){EXPR*ep,*epnext;右侧*rp,*rpnext;if(debug&(16+8+4))fprintf(stderr,“nexpr=%d\n”,nexpr);如果(nexpr==0)MLMessage(stdlink,“nooutput”,NULL);MLPutFunction(stdlink,“列表”,nexpr);对于(ep=expr;ep;ep=epnext){if(ep->pre){if(debug&16)fprintf(stderr,“pre=|%s|\n”,ep->pre);MLPutString(stdlink,ep->pre);自由(ep->pre);}如果(ep->nrhs){MLPutFunction(标准链接,“FGeval”,2);if(debug&4)fprintf(stderr,“lhs=|%s|\n”,ep->lhs);MLPutString(stdlink,ep->lhs);如果(ep->nrhs>1)MLPutFunction(stdlink,“StringJoin”,ep->n rhs);对于(rp=ep->rhs;rp;rp=rpnext){if(debug&8)fprintf(stderr,“rhs=|%s|\n”,rp->expr);MLPutString(stdlink,rp->expr);rpnext=rp->next;自由(rp);}}epnext=ep->next;游离(ep);}MLEndPacket(stdlink);}static void fortranget(cstring文件名,cint调试){FILE*文件;char*s,*d,*dmax,lastc,*lastd,*pre=NULL;int nexpr=0,不确定=0,br=0,sp=0;int ntok,word,word;字符行[EXPRBUF];EXPR*cur,*EXPR,**nextexpr=&EXPR;右侧**下一个;file=fopen(文件名,“r”);if(文件==NULL){MLFail(标准链接,“nofile”,文件名);回报;}#定义NEXTLHS()做{\*nextexpr=cur=malloc(sizeof(EXPR))\nextexpr=&cur->next\cur->next=空\cur->pre=NULL\nextrhs=&cur->rhs\cur->rhs=空\cur->nrhs=0\dmax=cur->lhs+cur->l hs的大小\}同时(0)#定义INILHS()做{\lastd=d=cur->lhs\ntok=单词=0\lastc=“+”\}同时(0)#定义NEXTRHS()做{\RHS*RHS=*nextrhs=malloc(sizeof(RHS))\nextrhs=&rhs->next\*nextrhs=空\*d=0\lastd=d=rhs->expr\dmax=rhs->expr+sizeof-rhs->expr\++cur->nrhs\lastc=“+”\}同时(0)NEXTLHS();INILHS();用于(;){if(fgets(line,sizeof line,file)==NULL){如果(单词==1)*d++=']';*d=0;fclose(文件);如果(预){如果(cur->nrhs)NEXTLHS();cur->pre=pre;}sendexpr(expr,nexpr+inexpr,debug);回报;}if(debug&2)fprintf(stderr,“%d%c |%s”,br,不确定?'*':'',行);s=直线;如果(*s=='*'||(s[-1]|0x21)=='c')转到eol;if(strncmp(s,“”,5)==0&s[5]>'')s+=6;其他{if(不确定){不确定=0;如果(单词==1)*d++=']';*d=0;++nexpr;NEXTLHS();}INILHS();}对于(;*s;++s){char c=*s;如果(c<=''){sp=1;继续;}如果(c=='='&&br==0&&ntok<=1){不确定=1;单词=0;cur->pre=pre;pre=空;NEXTRHS();继续;}if(不确定==0&&sp){d=cur->lhs;++ntok;sp=0;}开关(c){案例“!”:转到eol;大小写“(”:if(strchr(“+-*/^,([”,lastc))d=stpcpy(d,“FGpar”);c=“[”;案例“[”:++br;断裂;案例“)”:c=“]”;案例“]”:--br;断裂;案例“_”:c=“$”;断裂;案例'*':if(lastc!='*')break;*lastd=lastc='^';继续;案例“?”:继续;案例“d”:案例“D”:如果(word!=1)break;if(lastc=='.')*lastd='*';else*d++=“*”;*d++=“^”;继续;}wold=单词;如果(c=='.'||(c>='0'&&c<='9'))单词|=1;else如果(c=='$'||((c|0x20)>='a'&&(c|x20)<='z'))单词|=2;else单词=0;如果(wold==0&&word==1)d=stpcpy(d,“FGnum[”);else if(wold==1&&word!=1)*d++=']';如果(不确定&&d+10>=dmax)NEXTRHS();lastd=d;*d++=最后c=c;}如果(不确定)继续;s=线;石油醚:if((调试&1)){if(pre==NULL){如果(s[strspn(s,“\t\n\r”)]!=0)++nextpr,pre=strdup(s);}其他{size_told=strlen(pre);size_t new=字符串+1;pre=重新分配(pre、old+new);memcpy(pre+old,s,new);}}}}int main(int argc,char**argv){返回MLMain(argc,argv);}