Subversion Repositories wimsdev

Rev

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