Subversion Repositories wimsdev

Rev

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