Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 8135 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. int dollar_subst=1;
  19.  
  20. /* variable substitution. buffer p must have MAX_LINELEN */
  21. void subst(char *p)
  22. {
  23.     char *pp, *pe;
  24.  
  25.     for(pp=p;pp-p<MAX_LINELEN && *pp; pp++) {
  26.       if(*pp==' ') *pp=' ';
  27.       if(*pp=='\n') *pp='       ';
  28.       if(*pp=='$' && dollar_subst) {
  29.           string_modify(p,pp,pp+1,"&#36;");
  30.           pp++; continue;
  31.       }
  32.       if(*pp=='!' && isalnum(*(pp+1)) && dollar_subst) {
  33.           string_modify(p,pp,pp+1,"&#33;");
  34.           pp++; continue;
  35.       }
  36.       if(*pp!='\\') continue;
  37.       if(*(pp+1)=='\\') {
  38.           pp++; continue;
  39.       }
  40.       if(!isalpha(*(pp+1))) continue;
  41.       for(pe=pp+1;isalnum(*pe) || *pe=='_'; pe++);
  42.       if(pe-pp<MAX_NAMELEN && *pe=='[') {
  43.           char *pt;
  44.           pt=find_matching(pe+1,']'); if(pt!=NULL && pt-pe<MAX_LINELEN) {
  45.             string_modify(p,pt+1,pt+1,")");
  46.             string_modify(p,pp,pp+1,"$(m_");
  47.           }
  48.           else goto nobrack;
  49.       }
  50.       else {
  51.           nobrack: string_modify(p,pp,pp+1,"$m_");
  52.       }
  53.     }
  54. }
  55.  
  56. enum {pt_int, pt_rat, pt_real, pt_complex, pt_func, pt_text, pt_matrix};
  57. int prepcnt;
  58.  
  59. #include "sp.c"
  60.  
  61. void putval(char *p, char *name, int ptype)
  62. {
  63.     switch(ptype) {
  64.       case pt_int: {
  65.           fprintf(outf,"%sm_%s=$[rint(%s)]\n",setpre,name,p);
  66.           break;
  67.       }
  68.       case pt_real: {
  69.           fprintf(outf,"%sm_%s=$[%s]\n",setpre,name,p);
  70.           break;
  71.       }
  72.       case pt_func: {
  73.           fprintf(outf,"%sm_%s=!rawmath %s \n",setpre,name,p);
  74.           break;
  75.       }
  76.       case pt_complex: {
  77.           fprintf(outf,"%st_=!rawmath %s \n%st_=!exec pari print($t_)\n\
  78. %sm_%s=!mathsubst I=i in $t_\n",
  79.                 setpre,p,setpre,setpre,name);
  80.           break;
  81.       }
  82.       case pt_matrix: {
  83.           fprintf(outf,"%stmp=!trim %s \n\
  84. %sm_%s=!translate internal $    $ to ; in $tmp\n",setpre,p,setpre,name);
  85.           break;
  86.       }
  87.       case pt_rat: {
  88.           fprintf(outf,"%st_=!rawmath %s \n%sm_%s=!exec pari print($t_)\n",
  89.                 setpre,p,setpre,name);
  90.           break;
  91.       }
  92.       default: {
  93.           fprintf(outf,"%sm_%s=%s\n\n",setpre,name,p);
  94.           break;
  95.       }
  96.     }
  97. }
  98.  
  99. void parm(char *p[MAX_PARM], int ptype)
  100. {
  101.     char *pp, *p2;
  102.     char vbuf[MAX_LINELEN+1];
  103.  
  104.     setpre="!set ";
  105.     p[0]=find_word_start(p[0]);
  106.     if(*p[0]=='\\') p[0]++;
  107. /* bad name */
  108.     if(!isalpha(*p[0])) return;
  109.     strip_trailing_spaces(p[0]);
  110.     for(pp=p[0];*pp;pp++) if(!isalnum(*pp) && *pp!='_') {
  111. /* bad name and security risk */
  112.       if(!isspace(*pp)) return;
  113.       ovlstrcpy(pp,pp+1); pp--;
  114.     }
  115.     p[1]=find_word_start(p[1]);
  116.     snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf);
  117.     fprintf(outf,"\n");
  118.     if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?' && check_compare(vbuf)) {
  119.       char buf[MAX_LINELEN+1];
  120.       p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
  121.       snprintf(buf,sizeof(buf),"%s",vbuf);
  122.       prepcnt=0; fprintf(outf,"!ifval %s \n",vbuf);
  123.       snprintf(buf,sizeof(buf),"%s",pp);
  124.       parmprep(buf, ptype); putval(buf,p[0],ptype);
  125.       if(p2!=NULL) {
  126.           snprintf(buf,sizeof(buf),"%s",p2);
  127.           fprintf(outf,"!else\n");
  128.           parmprep(buf, ptype); putval(buf,p[0],ptype);
  129.       }
  130.       fprintf(outf,"!endif\n");return;
  131.     }
  132.     prepcnt=0; parmprep(vbuf, ptype);
  133.     putval(vbuf,p[0],ptype);
  134. }
  135.  
  136. void p_int(char *p[MAX_PARM]) {parm(p,pt_int);}
  137. void p_rational(char *p[MAX_PARM]) {parm(p,pt_rat);}
  138. void p_real(char *p[MAX_PARM]) {parm(p,pt_real);}
  139. void p_complex(char *p[MAX_PARM]) {parm(p,pt_complex);}
  140. void p_func(char *p[MAX_PARM]) {parm(p,pt_func);}
  141. void p_text(char *p[MAX_PARM]) {parm(p,pt_text);}
  142. void p_matrix(char *p[MAX_PARM]) {parm(p,pt_matrix);}
  143.  
  144. void p_parm(char *p[MAX_PARM])
  145. {
  146.     parm(p,pt_real);
  147. }
  148.  
  149. struct {
  150.     char *name;
  151.     void (*processor) (char *p[MAX_PARM]);
  152. } ptype[]={
  153.     {"complex",  p_complex},
  154.     {"function", p_func},
  155.     {"int",  p_int},
  156.     {"integer",  p_int},
  157.     {"matrix",  p_matrix},
  158.     {"parameter", p_parm},
  159.     {"rational", p_rational},
  160.     {"real",  p_real},
  161.     {"text",  p_text},
  162.     {"variable", p_parm}
  163. };
  164.  
  165. #define ptypeno (sizeof(ptype)/sizeof(ptype[0]))
  166.  
  167. void def(char *p)
  168. {
  169.     char *p1,*p2,*pp[2];
  170.     int i;
  171.     p1=find_word_start(p);
  172.     pp[1]=strchr(p1,'='); if(*pp[1]==0) return;
  173.     *pp[1]=0; pp[1]++;
  174.     p2=find_word_start(find_word_end(p1));
  175.     if(*p2==0) {pp[0]=p1; p_parm(pp); return;}
  176.     pp[0]=p2; *find_word_end(p1)=0;
  177.     for(p2=p1; *p2; p2++) *p2=tolower(*p2);
  178.     for(i=0;i<ptypeno;i++) if(strcmp(p1,ptype[i].name)==0) break;
  179.     if(i<ptypeno) ptype[i].processor(pp);
  180.     else p_parm(pp);
  181. }
  182.  
  183.