Subversion Repositories wimsdev

Rev

Rev 8135 | Rev 8871 | 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. #include "../Lib/libwims.h"
  19. #include "msg2wims.h"
  20.  
  21. int dollar_subst=1;
  22.  
  23. /* variable substitution. buffer p must have MAX_LINELEN */
  24. void subst(char *p)
  25. {
  26.     char *pp, *pe;
  27.  
  28.     for(pp=p;pp-p<MAX_LINELEN && *pp; pp++) {
  29.       if(*pp==' ') *pp=' ';
  30.       if(*pp=='\n') *pp='       ';
  31.       if(*pp=='$' && dollar_subst) {
  32.           string_modify(p,pp,pp+1,"&#36;");
  33.           pp++; continue;
  34.       }
  35.       if(*pp=='!' && isalnum(*(pp+1)) && dollar_subst) {
  36.           string_modify(p,pp,pp+1,"&#33;");
  37.           pp++; continue;
  38.       }
  39.       if(*pp!='\\') continue;
  40.       if(*(pp+1)=='\\') {
  41.           pp++; continue;
  42.       }
  43.       if(!isalpha(*(pp+1))) continue;
  44.       for(pe=pp+1;isalnum(*pe) || *pe=='_'; pe++);
  45.       if(pe-pp<MAX_NAMELEN && *pe=='[') {
  46.           char *pt;
  47.           pt=find_matching(pe+1,']'); if(pt!=NULL && pt-pe<MAX_LINELEN) {
  48.             string_modify(p,pt+1,pt+1,")");
  49.             string_modify(p,pp,pp+1,"$(m_");
  50.           }
  51.           else goto nobrack;
  52.       }
  53.       else {
  54.           nobrack: string_modify(p,pp,pp+1,"$m_");
  55.       }
  56.     }
  57. }
  58.  
  59. void putval(char *p, char *name, int ptype)
  60. {
  61.     switch(ptype) {
  62.       case pt_int: {
  63.           fprintf(outf,"%sm_%s=$[rint(%s)]\n",setpre,name,p);
  64.           break;
  65.       }
  66.       case pt_real: {
  67.           fprintf(outf,"%sm_%s=$[%s]\n",setpre,name,p);
  68.           break;
  69.       }
  70.       case pt_func: {
  71.           fprintf(outf,"%sm_%s=!rawmath %s \n",setpre,name,p);
  72.           break;
  73.       }
  74.       case pt_complex: {
  75.           fprintf(outf,"%st_=!rawmath %s \n%st_=!exec pari print($t_)\n\
  76. %sm_%s=!mathsubst I=i in $t_\n",
  77.                 setpre,p,setpre,setpre,name);
  78.           break;
  79.       }
  80.       case pt_matrix: {
  81.           fprintf(outf,"%stmp=!trim %s \n\
  82. %sm_%s=!translate internal $    $ to ; in $tmp\n",setpre,p,setpre,name);
  83.           break;
  84.       }
  85.       case pt_rat: {
  86.           fprintf(outf,"%st_=!rawmath %s \n%sm_%s=!exec pari print($t_)\n",
  87.                 setpre,p,setpre,name);
  88.           break;
  89.       }
  90.       default: {
  91.           fprintf(outf,"%sm_%s=%s\n\n",setpre,name,p);
  92.           break;
  93.       }
  94.     }
  95. }
  96.  
  97. void parm(char *p[MAX_PARM], int ptype)
  98. {
  99.     char *pp, *p2;
  100.     char vbuf[MAX_LINELEN+1];
  101.  
  102.     setpre="!set ";
  103.     p[0]=find_word_start(p[0]);
  104.     if(*p[0]=='\\') p[0]++;
  105. /* bad name */
  106.     if(!isalpha(*p[0])) return;
  107.     strip_trailing_spaces(p[0]);
  108.     for(pp=p[0];*pp;pp++) if(!isalnum(*pp) && *pp!='_') {
  109. /* bad name and security risk */
  110.       if(!isspace(*pp)) return;
  111.       ovlstrcpy(pp,pp+1); pp--;
  112.     }
  113.     p[1]=find_word_start(p[1]);
  114.     snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf);
  115.     fprintf(outf,"\n");
  116.     if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?' && check_compare(vbuf)) {
  117.       char buf[MAX_LINELEN+1];
  118.       p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0;
  119.       snprintf(buf,sizeof(buf),"%s",vbuf);
  120.       prepcnt=0; fprintf(outf,"!ifval %s \n",vbuf);
  121.       snprintf(buf,sizeof(buf),"%s",pp);
  122.       parmprep(buf, ptype); putval(buf,p[0],ptype);
  123.       if(p2!=NULL) {
  124.           snprintf(buf,sizeof(buf),"%s",p2);
  125.           fprintf(outf,"!else\n");
  126.           parmprep(buf, ptype); putval(buf,p[0],ptype);
  127.       }
  128.       fprintf(outf,"!endif\n");return;
  129.     }
  130.     prepcnt=0; parmprep(vbuf, ptype);
  131.     putval(vbuf,p[0],ptype);
  132. }
  133.  
  134. void p_int(char *p[MAX_PARM]) {parm(p,pt_int);}
  135. void p_rational(char *p[MAX_PARM]) {parm(p,pt_rat);}
  136. void p_real(char *p[MAX_PARM]) {parm(p,pt_real);}
  137. void p_complex(char *p[MAX_PARM]) {parm(p,pt_complex);}
  138. void p_func(char *p[MAX_PARM]) {parm(p,pt_func);}
  139. void p_text(char *p[MAX_PARM]) {parm(p,pt_text);}
  140. void p_matrix(char *p[MAX_PARM]) {parm(p,pt_matrix);}
  141.  
  142. void p_parm(char *p[MAX_PARM])
  143. {
  144.     parm(p,pt_real);
  145. }
  146.  
  147. struct {
  148.     char *name;
  149.     void (*processor) (char *p[MAX_PARM]);
  150. } ptype[]={
  151.     {"complex",  p_complex},
  152.     {"function", p_func},
  153.     {"int",  p_int},
  154.     {"integer",  p_int},
  155.     {"matrix",  p_matrix},
  156.     {"parameter", p_parm},
  157.     {"rational", p_rational},
  158.     {"real",  p_real},
  159.     {"text",  p_text},
  160.     {"variable", p_parm}
  161. };
  162.  
  163. #define ptypeno (sizeof(ptype)/sizeof(ptype[0]))
  164.  
  165. void def(char *p)
  166. {
  167.     char *p1,*p2,*pp[2];
  168.     int i;
  169.     p1=find_word_start(p);
  170.     pp[1]=strchr(p1,'='); if(*pp[1]==0) return;
  171.     *pp[1]=0; pp[1]++;
  172.     p2=find_word_start(find_word_end(p1));
  173.     if(*p2==0) {pp[0]=p1; p_parm(pp); return;}
  174.     pp[0]=p2; *find_word_end(p1)=0;
  175.     for(p2=p1; *p2; p2++) *p2=tolower(*p2);
  176.     for(i=0;i<ptypeno;i++) if(strcmp(p1,ptype[i].name)==0) break;
  177.     if(i<ptypeno) ptype[i].processor(pp);
  178.     else p_parm(pp);
  179. }
  180.  
  181.