Subversion Repositories wimsdev

Rev

Rev 12122 | 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. #include "../Lib/libwims.h"
  18. #include "sp.h"
  19.  
  20. char *setpre="";
  21.  
  22. void sp_asis(char *p, int ptype)
  23. {
  24.   fprintf(outf,"%stmp%d=!nosubst %s\n\n",setpre,prepcnt,p);
  25. }
  26.  
  27. void sp_evalue(char *p, int ptype)
  28. {
  29.   char *p1, *p2, *pe;
  30.  
  31.   p1=strparchr(p,',');
  32.   if(p1==NULL) {
  33.     fprintf(outf,"%stmp%d=%s\n\n",setpre,prepcnt,p);
  34.     return;
  35.   }
  36.   *p1++=0; fprintf(outf,"%sevaltmp=%s\n\n", setpre,p);
  37.   for(; p1!=NULL; p1=p2){
  38.     p1=find_word_start(p1);
  39.     p2=strparchr(p1,',');
  40.     if(p2!=NULL) *p2++=0;
  41.     pe=strchr(p1,'=');
  42.     if(pe==NULL) continue;
  43.     *pe++=0; *find_word_end(p1)=0;
  44.     if(*p1==0) continue;
  45.     fprintf(outf,"%sevaltmp=!mathsubst %s=(%s) in $evaltmp\n",
  46.           setpre,p1,pe);
  47.   }
  48.   fprintf(outf,"%stmp%d=($evaltmp)\n",setpre,prepcnt);
  49. }
  50.  
  51. /* simple roots.*/
  52. void sp_solve(char *p, int ptype)
  53. {
  54.   fprintf(outf,"%stmp=!replace internal .. by , in %s \n\n\
  55. !distribute items $tmp into tmp1,tmp2,tmp3\n\
  56. %stmp2=!replace internal = by , in $tmp2\n\
  57. !distribute items $tmp2 into tmp4,tmp5\n\
  58. %stmp%d=!solve $tmp1 for $tmp4 = $tmp5 to $tmp3 \n",
  59.           setpre,p,setpre,setpre,prepcnt);
  60. }
  61.  
  62. /* use maxima to do formal derivation. */
  63. void sp_diff(char *p, int ptype)
  64. {
  65.   fprintf(outf,"%stmp=!translate ;\";': to $     $ in %s\n\n\
  66. %stmp=!lower $tmp\n\
  67. %stmp%d=!exec maxima diff($tmp);\n",
  68.           setpre,p,setpre,setpre,prepcnt);
  69. }
  70.  
  71. /* use pari to compute matrix determinant. */
  72. void sp_det(char *p, int ptype)
  73. {
  74.   fprintf(outf,"%stmp=!translate $      $ to ; in %s\n\n\
  75. %stmp%d=!exec pari matdet([$tmp])\n",
  76.           setpre,p,setpre,prepcnt);
  77. }
  78.  
  79. /* use maxima to do formal integration,
  80.  * but pari for numerical integration. */
  81. void sp_int(char *p, int ptype)
  82. {
  83.   char *s;
  84.  
  85.   if((s=strchr(p,'='))!=NULL) *s=',';
  86.   if((s=strstr(p,".."))!=NULL) {*s=','; *(s+1)=' ';}
  87.   fprintf(outf,"%stmp=!translate ;\";': to $     $ in %s \n\
  88. %stmp=!lower $tmp\n\
  89. !readproc slib/function/integrate $tmp\n\
  90. %stmp%d=$slib_out\n",
  91.           setpre,p,setpre,setpre,prepcnt);
  92. }
  93.  
  94. void sp_htmlmath(char *p, int ptype)
  95. {
  96.   fprintf(outf,"%stmp%d=!htmlmath %s\n\n",setpre,prepcnt,p);
  97. }
  98.  
  99. void sp_teximg(char *p, int ptype)
  100. {
  101.   fprintf(outf,"!readproc %s/teximg.phtml %s \n\
  102. %stmp%d=$ins_url\n", primitive_dir, p, setpre,prepcnt);
  103. }
  104.  
  105. void sp_texmath(char *p, int ptype)
  106. {
  107.   fprintf(outf,"%stmp%d=!texmath %s\n\n",setpre,prepcnt,p);
  108. }
  109.  
  110. void sp_maxima(char *p, int ptype)
  111. {
  112.   fprintf(outf,"%st_=!replace internal \\( by ( in %s\n\
  113. %stmp%d=!exec maxima $t_\n\n",setpre,p,setpre,prepcnt);
  114. }
  115.  
  116. void sp_yacas(char *p, int ptype)
  117. {
  118.   fprintf(outf,"%st_=!replace internal \\( by ( in %s\n\
  119. %stmp%d=!exec yacas $t_\n\n",setpre,p,setpre,prepcnt);
  120. }
  121.  
  122. void sp_pari(char *p, int ptype)
  123. {
  124.   fprintf(outf,"%st_=!replace internal \\( by ( in %s\n\
  125. %stmp%d=!exec pari $t_\n\n",setpre,p,setpre,prepcnt);
  126. }
  127.  
  128. void sp_simplify(char *p, int ptype)
  129. {
  130.   fprintf(outf,"%stmp=!translate \";': to $     $ in %s\n\n\
  131. %stmp=!lower $tmp\n\
  132. %stmp%d=!exec maxima fullratsimp($tmp);\n",
  133.           setpre,p,setpre,setpre,prepcnt);
  134. }
  135.  
  136. void sp_slib(char *p, int ptype)
  137. {
  138.   char *p2;
  139.   p=find_word_start(p);
  140.   for(p2=p;*p2!=0 && !isspace(*p2) && *p2!=',' && *p2!=';';p2++);
  141.   if(*p2!=0 && !isspace(*p2)) *p2=' ';
  142.   fprintf(outf,"!readproc slib/%s \n\
  143. %stmp%d=$slib_out\n",p,setpre,prepcnt);
  144. }
  145.  
  146. void sp_draw(char *p, int ptype)
  147. {
  148.   char *p2;
  149.   p2=strchr(p,' '); if(p2==NULL) p2=strchr(p,'\n');
  150.   if(p2==NULL) return;
  151.   *p2++=0;
  152.   fprintf(outf,"!readproc %s/draw.phtml %s \\\n%s \n\
  153. %stmp%d=$ins_url\n", primitive_dir, p, p2, setpre,prepcnt);
  154. }
  155.  
  156. void sp_canvasdraw(char *p, int ptype)
  157. {
  158.   char *p2;
  159.   p2=strchr(p,' '); if(p2==NULL) p2=strchr(p,'\n');
  160.   if(p2==NULL) return;
  161.   *p2++=0;
  162.   fprintf(outf,"!readproc %s/canvasdraw.phtml %s \\\n%s \n\
  163. %stmp%d=$canvasdraw_out\n", primitive_dir, p, p2, setpre,prepcnt);
  164. }
  165.  
  166. void sp_shuffle(char *p, int ptype)
  167. {
  168.   fprintf(outf,"%stmp%d=!shuffle %s\n\n",setpre,prepcnt,p);
  169. }
  170.  
  171. void sp_positionof(char *p, int ptype)
  172. {
  173.   char *p1;
  174.   p1=strparchr(p,','); if(p1==NULL) {
  175.     fprintf(outf,"%stmp%d=%s\n\n",setpre,prepcnt,p); return;
  176.   }
  177.   *p1++=0;
  178.   fprintf(outf,"%stmp%d=!positionof item %s in %s\n\n",setpre,prepcnt,p, p1);
  179. }
  180.  
  181. void sp_random(char *p, int ptype)
  182. {
  183.   char *pr, *p2, buf[MAX_LINELEN+1];
  184.   snprintf(buf,sizeof(buf),"%s",p);
  185.   for(p2=buf; *p2 && p2-buf<MAX_LINELEN; p2++) {
  186.     if(*p2=='(') {
  187.       p2=find_matching(p2+1,')'); continue;
  188.     }
  189.     if(*p2==',' || (*p2=='.' && *(p2+1)=='.')) break;
  190.   }
  191.   if(*p2==',') pr="randitem";
  192.   else {
  193.     if(*p2=='.') {
  194.       *p2=','; *(p2+1)=' ';
  195.     }
  196.     if(ptype==pt_int) pr="randint"; else pr="random";
  197.   }
  198.   fprintf(outf,"%stmp%d=!%s %s\n\n",setpre,prepcnt,pr,buf);
  199. }
  200.  
  201. void sp_pickone(char *p, int ptype)
  202. {
  203.   sp_random(p,pt_int);
  204. }
  205.  
  206. void sp_item(char *p, int ptype)
  207. {
  208.   char *pp, *p2, buf[MAX_LINELEN+1];
  209.   pp=strparchr(p,',');
  210.   if(pp==NULL) pp=""; else *pp++=0;
  211.   strncpy(buf,p,MAX_LINELEN);buf[MAX_LINELEN]=0;
  212.   strip_enclosing_par(buf);
  213.   if((p2=strstr(buf,".."))!=NULL) string_modify(buf,p2,p2+2," to ");
  214.   fprintf(outf,"%stmp%d=!item %s of %s\n\n",setpre,prepcnt,buf,pp);
  215. }
  216.  
  217. void sp_items(char *p, int ptype)
  218. {
  219.   fprintf(outf,"%stmp%d=!itemcnt %s\n\n",setpre,prepcnt,p);
  220. }
  221.  
  222. void sp_randitem(char *p, int ptype)
  223. {
  224.   fprintf(outf,"%stmp=!nonempty items %s\n\n\
  225. %stmp%d=!randitem $tmp\n",setpre,p,setpre,prepcnt);
  226. }
  227.  
  228. void sp_column(char *p, int ptype)
  229. {
  230.   char *pp, *p2, buf[MAX_LINELEN+1];
  231.   pp=strparchr(p,',');
  232.   if(pp==NULL) pp=""; else *pp++=0;
  233.   strncpy(buf,p,MAX_LINELEN);buf[MAX_LINELEN]=0;
  234.   strip_enclosing_par(buf);
  235.   if((p2=strstr(buf,".."))!=NULL) string_modify(buf,p2,p2+2," to ");
  236.   fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
  237. %stmp=!column %s of $tmp\n\
  238. %stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
  239.           setpre,pp,setpre, buf, setpre, prepcnt);
  240. }
  241.  
  242. void sp_row(char *p, int ptype)
  243. {
  244.   char *pp, *p2, buf[MAX_LINELEN+1];
  245.   pp=strparchr(p,',');
  246.   if(pp==NULL) pp=""; else *pp++=0;
  247.   strncpy(buf,p,MAX_LINELEN);buf[MAX_LINELEN]=0;
  248.   strip_enclosing_par(buf);
  249.   fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n",
  250.         setpre,pp);
  251.   if(strstr(buf,"column")!=NULL) {
  252.     fprintf(outf,"%stmp%d=!select $tmp where %s\n\n",setpre,prepcnt,buf);
  253.     return;
  254.   }
  255.   while((p2=strstr(buf,".."))!=NULL)
  256.     string_modify(buf,p2,p2+2," to ");
  257.   fprintf(outf,"%stmp=!row %s of $tmp\n\
  258. %stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
  259.           setpre, buf, setpre, prepcnt);
  260. }
  261.  
  262. void sp_rows(char *p, int ptype)
  263. {
  264.   fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
  265. %stmp%d=!rowcnt $tmp\n",setpre,p,setpre,prepcnt);
  266. }
  267.  
  268. void sp_randrow(char *p, int ptype)
  269. {
  270.   fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
  271. %stmp=!nonempty rows $tmp\n\
  272. %stmp=!randrow $tmp\n\
  273. %stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
  274.           setpre,p,setpre,setpre,setpre,prepcnt);
  275. }
  276.  
  277. void sp_mathexp_cut(char *p, int ptype)
  278. {
  279.   char *p2;
  280.   p2=find_word_end(find_word_start(p)); if(isspace(*p2)) *p2++=0;
  281.   p2=find_word_start(p2);
  282.   fprintf(outf,"%stmp%d=!exec mathexp cut %s\\\n%s\n\n",
  283.         setpre,prepcnt,p,p2);
  284. }
  285.  
  286. void sp_wims(char *p, int ptype)
  287. {
  288.   p=find_word_start(p);
  289.   if(!isalpha(*p) || strncasecmp(p,"ins",3)==0)
  290.     fprintf(outf,"%stmp%d=%s\n\n",setpre,prepcnt,p);
  291.   else
  292.     fprintf(outf,"%stmp%d=!%s\n\n",setpre,prepcnt,p);
  293. }
  294.  
  295. struct specialfn specialfn[]={
  296.   {"asis",  sp_asis},
  297.   {"canvasdraw", sp_canvasdraw},
  298.   {"column", sp_column},
  299.   {"deriv",  sp_diff},
  300.   {"derivative", sp_diff},
  301.   {"det",  sp_det},
  302.   {"determinant", sp_det},
  303.   {"diff",  sp_diff},
  304.   {"draw",  sp_draw},
  305.   {"evaluate", sp_evalue},
  306.   {"evalue", sp_evalue},
  307.   {"htmlmath", sp_htmlmath},
  308.   {"int",  sp_int},
  309.   {"integral", sp_int},
  310.   {"integrate", sp_int},
  311.   {"item",  sp_item},
  312.   {"items",  sp_items},
  313.   {"mathexp_cut", sp_mathexp_cut},
  314.   {"maxima", sp_maxima},
  315.   {"pari",  sp_pari},
  316.   {"pickone", sp_pickone},
  317.   {"position", sp_positionof},
  318.   {"positionof", sp_positionof},
  319.   {"randint", sp_pickone},
  320.   {"randitem", sp_randitem},
  321.   {"random", sp_random},
  322.   {"randomitem", sp_randitem},
  323.   {"randomrow", sp_randrow},
  324.   {"randrow", sp_randrow},
  325.   {"row",  sp_row},
  326.   {"rows",  sp_rows},
  327.   {"shuffle", sp_shuffle},
  328.   {"simplify", sp_simplify},
  329.   {"slib",  sp_slib},
  330.   {"solve",  sp_solve},
  331.   {"teximg", sp_teximg},
  332.   {"texmath", sp_texmath},
  333.   {"wims",  sp_wims},
  334.   {"yacas", sp_yacas}
  335. };
  336. int specialfn_no=(sizeof(specialfn)/sizeof(specialfn[0]));
  337.  
  338. /* This routine treats special functions */
  339. void parmprep(char *p,int ptype)
  340. {
  341.   char *pp, *p2, *pn, namebuf[32], buf[MAX_LINELEN+1];
  342.   int i;
  343.  
  344.   while((pp=strchr(p,'\n'))!=NULL) *pp='        ';
  345.   for(pp=p;*pp && pp-p<MAX_LINELEN;pp++) {
  346. /* function names */
  347.     if(isalpha(*pp)) {
  348.       for(i=0;i<30 && (isalnum(pp[i]) || pp[i]=='_');i++) namebuf[i]=pp[i];
  349.       namebuf[i]=0; p2=find_word_start(pp+i);
  350.       if((pp>p && isalnum(*(pp-1))) || *p2!='(') {
  351. /* if(*p2=='\\' && *(p2+1)=='(') ovlstrcpy(p2,p2+1); */
  352.         pp=p2-1; continue;
  353.       }
  354.       pn=pp; pp=p2+1; p2=find_matching(pp,')');
  355.       if(p2==NULL) {
  356.         sp_error("unmatched_parentheses");
  357.         pp=p2; continue;
  358.       }
  359.       i=search_list(specialfn,specialfn_no,sizeof(specialfn[0]),namebuf);
  360.       if(i<0) {
  361.         pp--; continue;
  362.       }
  363.       *p2=0;
  364.       snprintf(buf,sizeof(buf),"%s",pp);
  365.       if(specialfn[i].processor!=sp_asis) parmprep(buf,ptype);
  366.       specialfn[i].processor(buf,ptype);
  367.       string_modify(p, pn, p2+1, "$(tmp%d)",prepcnt); prepcnt++;
  368.       pp=pn+6;
  369.     }
  370.   }
  371. }
  372.  
  373.