Back to index

wims  3.65+svn20090927
def.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 int dollar_subst=1;
00019 
00020        /* variable substitution. buffer p must have MAX_LINELEN */
00021 void subst(char *p)
00022 {
00023     char *pp, *pe;
00024 
00025     for(pp=p;pp-p<MAX_LINELEN && *pp; pp++) {
00026        if(*pp=='     ') *pp=' ';
00027        if(*pp=='\n') *pp='  ';
00028        if(*pp=='$' && dollar_subst) {
00029            string_modify(p,pp,pp+1,"&#36;");
00030            pp++; continue;
00031        }
00032        if(*pp=='!' && isalnum(*(pp+1)) && dollar_subst) {
00033            string_modify(p,pp,pp+1,"&#33;");
00034            pp++; continue;
00035        }
00036        if(*pp!='\\') continue;
00037        if(*(pp+1)=='\\') {
00038            pp++; continue;
00039        }
00040        if(!isalpha(*(pp+1))) continue;
00041        for(pe=pp+1;isalnum(*pe) || *pe=='_'; pe++);
00042        if(pe-pp<MAX_NAMELEN && *pe=='[') {
00043            char *pt;
00044            pt=find_matching(pe+1,']'); if(pt!=NULL && pt-pe<MAX_LINELEN) {
00045               string_modify(p,pt+1,pt+1,")");
00046               string_modify(p,pp,pp+1,"$(m_");
00047            }
00048            else goto nobrack;
00049        }
00050        else {
00051            nobrack: string_modify(p,pp,pp+1,"$m_");
00052        }
00053     }
00054 }
00055 
00056 enum {pt_int, pt_rat, pt_real, pt_complex, pt_func, pt_text, pt_matrix};
00057 int prepcnt;
00058 
00059 #include "sp.c"
00060 
00061 void putval(char *p, char *name, int ptype)
00062 {
00063     switch(ptype) {
00064        case pt_int: {
00065            fprintf(outf,"%sm_%s=$[rint(%s)]\n",setpre,name,p);
00066            break;
00067        }
00068        case pt_real: {
00069            fprintf(outf,"%sm_%s=$[%s]\n",setpre,name,p);
00070            break;
00071        }
00072        case pt_func: {
00073            fprintf(outf,"%sm_%s=!rawmath %s \n",setpre,name,p);
00074            break;
00075        }
00076        case pt_complex: {
00077            fprintf(outf,"%st_=!rawmath %s \n%st_=!exec pari print($t_)\n\
00078 %sm_%s=!mathsubst I=i in $t_\n",
00079                   setpre,p,setpre,setpre,name);
00080            break;
00081        }
00082        case pt_matrix: {
00083            fprintf(outf,"%stmp=!trim %s \n\
00084 %sm_%s=!translate internal $       $ to ; in $tmp\n",setpre,p,setpre,name);
00085            break;
00086        }
00087        case pt_rat: {
00088            fprintf(outf,"%st_=!rawmath %s \n%sm_%s=!exec pari print($t_)\n",
00089                   setpre,p,setpre,name);
00090            break;
00091        }
00092        default: {
00093            fprintf(outf,"%sm_%s=%s\n\n",setpre,name,p);
00094            break;
00095        }
00096     }
00097 }
00098 
00099 void parm(char *p[MAX_PARM], int ptype)
00100 {
00101     char *pp, *p2;
00102     char vbuf[MAX_LINELEN+1];
00103 
00104     setpre="!set ";
00105     p[0]=find_word_start(p[0]);
00106     if(*p[0]=='\\') p[0]++;
00107        /* bad name */
00108     if(!isalpha(*p[0])) return;
00109     strip_trailing_spaces(p[0]);
00110     for(pp=p[0];*pp;pp++) if(!isalnum(*pp) && *pp!='_') {
00111        /* bad name and security risk */
00112        if(!isspace(*pp)) return;
00113        strcpy(pp,pp+1); pp--;
00114     }
00115     p[1]=find_word_start(p[1]);
00116     snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf);
00117     fprintf(outf,"\n");
00118     if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?' && check_compare(vbuf)) {
00119        char buf[MAX_LINELEN+1];
00120        p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
00121        snprintf(buf,sizeof(buf),"%s",vbuf);
00122        prepcnt=0; fprintf(outf,"!ifval %s \n",vbuf);
00123        snprintf(buf,sizeof(buf),"%s",pp);
00124        parmprep(buf, ptype); putval(buf,p[0],ptype);
00125        if(p2!=NULL) {
00126            snprintf(buf,sizeof(buf),"%s",p2);
00127            fprintf(outf,"!else\n");
00128            parmprep(buf, ptype); putval(buf,p[0],ptype);
00129        }
00130        fprintf(outf,"!endif\n");return;
00131     }
00132     prepcnt=0; parmprep(vbuf, ptype);
00133     putval(vbuf,p[0],ptype);
00134 }
00135 
00136 void p_int(char *p[MAX_PARM]) {parm(p,pt_int);}
00137 void p_rational(char *p[MAX_PARM]) {parm(p,pt_rat);}
00138 void p_real(char *p[MAX_PARM]) {parm(p,pt_real);}
00139 void p_complex(char *p[MAX_PARM]) {parm(p,pt_complex);}
00140 void p_func(char *p[MAX_PARM]) {parm(p,pt_func);}
00141 void p_text(char *p[MAX_PARM]) {parm(p,pt_text);}
00142 void p_matrix(char *p[MAX_PARM]) {parm(p,pt_matrix);}
00143 
00144 void p_parm(char *p[MAX_PARM])
00145 {
00146     parm(p,pt_real);
00147 }
00148 
00149 struct {
00150     char *name;
00151     void (*processor) (char *p[MAX_PARM]);
00152 } ptype[]={
00153     {"complex",             p_complex},
00154     {"function",     p_func},
00155     {"int",          p_int},
00156     {"integer",             p_int},
00157     {"matrix",              p_matrix},
00158     {"parameter",    p_parm},
00159     {"rational",     p_rational},
00160     {"real",         p_real},
00161     {"text",         p_text},
00162     {"variable",     p_parm}
00163 };
00164 
00165 #define ptypeno (sizeof(ptype)/sizeof(ptype[0]))
00166 
00167 void def(char *p)
00168 {
00169     char *p1,*p2,*pp[2];
00170     int i;
00171     p1=find_word_start(p);
00172     pp[1]=strchr(p1,'='); if(*pp[1]==0) return;
00173     *pp[1]=0; pp[1]++;
00174     p2=find_word_start(find_word_end(p1));
00175     if(*p2==0) {pp[0]=p1; p_parm(pp); return;}
00176     pp[0]=p2; *find_word_end(p1)=0;
00177     for(p2=p1; *p2; p2++) *p2=tolower(*p2);
00178     for(i=0;i<ptypeno;i++) if(strcmp(p1,ptype[i].name)==0) break;
00179     if(i<ptypeno) ptype[i].processor(pp);
00180     else p_parm(pp);
00181 }
00182