Subversion Repositories wimsdev

Rev

Rev 14873 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 */
8135 bpr 17
#include "../Lib/libwims.h"
18
#include "sp.h"
14873 georgesk 19
void (*sp_error)(char *s);
10 reyssat 20
 
21
char *setpre="";
22
 
23
void sp_asis(char *p, int ptype)
24
{
12247 bpr 25
  fprintf(outf,"%stmp%d=!nosubst %s\n\n",setpre,prepcnt,p);
10 reyssat 26
}
27
 
28
void sp_evalue(char *p, int ptype)
29
{
12247 bpr 30
  char *p1, *p2, *pe;
7622 bpr 31
 
12247 bpr 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);
10 reyssat 50
}
51
 
7677 bpr 52
/* simple roots.*/
10 reyssat 53
void sp_solve(char *p, int ptype)
54
{
12247 bpr 55
  fprintf(outf,"%stmp=!replace internal .. by , in %s \n\n\
10 reyssat 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",
7677 bpr 60
          setpre,p,setpre,setpre,prepcnt);
10 reyssat 61
}
62
 
7677 bpr 63
/* use maxima to do formal derivation. */
10 reyssat 64
void sp_diff(char *p, int ptype)
65
{
12247 bpr 66
  fprintf(outf,"%stmp=!translate ;\";': to $     $ in %s\n\n\
10 reyssat 67
%stmp=!lower $tmp\n\
68
%stmp%d=!exec maxima diff($tmp);\n",
7677 bpr 69
          setpre,p,setpre,setpre,prepcnt);
10 reyssat 70
}
71
 
12122 bpr 72
/* use pari to compute matrix determinant. */
10 reyssat 73
void sp_det(char *p, int ptype)
74
{
12247 bpr 75
  fprintf(outf,"%stmp=!translate $      $ to ; in %s\n\n\
10 reyssat 76
%stmp%d=!exec pari matdet([$tmp])\n",
7677 bpr 77
          setpre,p,setpre,prepcnt);
10 reyssat 78
}
79
 
7677 bpr 80
/* use maxima to do formal integration,
81
 * but pari for numerical integration. */
10 reyssat 82
void sp_int(char *p, int ptype)
83
{
12247 bpr 84
  char *s;
7622 bpr 85
 
12247 bpr 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\
10 reyssat 89
%stmp=!lower $tmp\n\
90
!readproc slib/function/integrate $tmp\n\
91
%stmp%d=$slib_out\n",
7677 bpr 92
          setpre,p,setpre,setpre,prepcnt);
10 reyssat 93
}
94
 
95
void sp_htmlmath(char *p, int ptype)
96
{
12247 bpr 97
  fprintf(outf,"%stmp%d=!htmlmath %s\n\n",setpre,prepcnt,p);
10 reyssat 98
}
99
 
3346 bpr 100
void sp_teximg(char *p, int ptype)
101
{
12247 bpr 102
  fprintf(outf,"!readproc %s/teximg.phtml %s \n\
3346 bpr 103
%stmp%d=$ins_url\n", primitive_dir, p, setpre,prepcnt);
104
}
105
 
10 reyssat 106
void sp_texmath(char *p, int ptype)
107
{
12247 bpr 108
  fprintf(outf,"%stmp%d=!texmath %s\n\n",setpre,prepcnt,p);
10 reyssat 109
}
110
 
111
void sp_maxima(char *p, int ptype)
112
{
12247 bpr 113
  fprintf(outf,"%st_=!replace internal \\( by ( in %s\n\
10 reyssat 114
%stmp%d=!exec maxima $t_\n\n",setpre,p,setpre,prepcnt);
115
}
116
 
117
void sp_yacas(char *p, int ptype)
118
{
12247 bpr 119
  fprintf(outf,"%st_=!replace internal \\( by ( in %s\n\
10 reyssat 120
%stmp%d=!exec yacas $t_\n\n",setpre,p,setpre,prepcnt);
121
}
122
 
123
void sp_pari(char *p, int ptype)
124
{
12247 bpr 125
  fprintf(outf,"%st_=!replace internal \\( by ( in %s\n\
10 reyssat 126
%stmp%d=!exec pari $t_\n\n",setpre,p,setpre,prepcnt);
127
}
128
 
129
void sp_simplify(char *p, int ptype)
130
{
12247 bpr 131
  fprintf(outf,"%stmp=!translate \";': to $     $ in %s\n\n\
10 reyssat 132
%stmp=!lower $tmp\n\
133
%stmp%d=!exec maxima fullratsimp($tmp);\n",
7677 bpr 134
          setpre,p,setpre,setpre,prepcnt);
10 reyssat 135
}
136
 
137
void sp_slib(char *p, int ptype)
138
{
12247 bpr 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\
10 reyssat 144
%stmp%d=$slib_out\n",p,setpre,prepcnt);
145
}
146
 
147
void sp_draw(char *p, int ptype)
148
{
12247 bpr 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\
10 reyssat 154
%stmp%d=$ins_url\n", primitive_dir, p, p2, setpre,prepcnt);
155
}
156
 
17918 bpr 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
 
7622 bpr 167
void sp_canvasdraw(char *p, int ptype)
168
{
12247 bpr 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\
7622 bpr 174
%stmp%d=$canvasdraw_out\n", primitive_dir, p, p2, setpre,prepcnt);
175
}
176
 
10 reyssat 177
void sp_shuffle(char *p, int ptype)
178
{
12247 bpr 179
  fprintf(outf,"%stmp%d=!shuffle %s\n\n",setpre,prepcnt,p);
10 reyssat 180
}
181
 
182
void sp_positionof(char *p, int ptype)
183
{
12247 bpr 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);
10 reyssat 190
}
191
 
192
void sp_random(char *p, int ptype)
193
{
12247 bpr 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;
10 reyssat 199
    }
12247 bpr 200
    if(*p2==',' || (*p2=='.' && *(p2+1)=='.')) break;
201
  }
202
  if(*p2==',') pr="randitem";
203
  else {
204
    if(*p2=='.') {
205
      *p2=','; *(p2+1)=' ';
10 reyssat 206
    }
12247 bpr 207
    if(ptype==pt_int) pr="randint"; else pr="random";
208
  }
209
  fprintf(outf,"%stmp%d=!%s %s\n\n",setpre,prepcnt,pr,buf);
10 reyssat 210
}
211
 
212
void sp_pickone(char *p, int ptype)
213
{
12247 bpr 214
  sp_random(p,pt_int);
10 reyssat 215
}
216
 
217
void sp_item(char *p, int ptype)
218
{
12247 bpr 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);
10 reyssat 226
}
227
 
228
void sp_items(char *p, int ptype)
229
{
12247 bpr 230
  fprintf(outf,"%stmp%d=!itemcnt %s\n\n",setpre,prepcnt,p);
10 reyssat 231
}
232
 
233
void sp_randitem(char *p, int ptype)
234
{
12247 bpr 235
  fprintf(outf,"%stmp=!nonempty items %s\n\n\
10 reyssat 236
%stmp%d=!randitem $tmp\n",setpre,p,setpre,prepcnt);
237
}
238
 
239
void sp_column(char *p, int ptype)
240
{
12247 bpr 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\
10 reyssat 248
%stmp=!column %s of $tmp\n\
249
%stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
7677 bpr 250
          setpre,pp,setpre, buf, setpre, prepcnt);
10 reyssat 251
}
252
 
253
void sp_row(char *p, int ptype)
254
{
12247 bpr 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\
10 reyssat 269
%stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
7677 bpr 270
          setpre, buf, setpre, prepcnt);
10 reyssat 271
}
272
 
273
void sp_rows(char *p, int ptype)
274
{
12247 bpr 275
  fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
10 reyssat 276
%stmp%d=!rowcnt $tmp\n",setpre,p,setpre,prepcnt);
277
}
278
 
279
void sp_randrow(char *p, int ptype)
280
{
12247 bpr 281
  fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
10 reyssat 282
%stmp=!nonempty rows $tmp\n\
283
%stmp=!randrow $tmp\n\
284
%stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
7677 bpr 285
          setpre,p,setpre,setpre,setpre,prepcnt);
10 reyssat 286
}
287
 
288
void sp_mathexp_cut(char *p, int ptype)
289
{
12247 bpr 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);
10 reyssat 295
}
296
 
297
void sp_wims(char *p, int ptype)
298
{
12247 bpr 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);
10 reyssat 304
}
305
 
8135 bpr 306
struct specialfn specialfn[]={
12247 bpr 307
  {"asis",  sp_asis},
308
  {"canvasdraw", sp_canvasdraw},
309
  {"column", sp_column},
17918 bpr 310
  {"deriv", sp_diff},
12247 bpr 311
  {"derivative", sp_diff},
17918 bpr 312
  {"det", sp_det},
12247 bpr 313
  {"determinant", sp_det},
314
  {"diff",  sp_diff},
315
  {"draw",  sp_draw},
17918 bpr 316
  {"drawtikz", sp_drawtikz},
12247 bpr 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}
10 reyssat 347
};
8135 bpr 348
int specialfn_no=(sizeof(specialfn)/sizeof(specialfn[0]));
10 reyssat 349
 
7677 bpr 350
/* This routine treats special functions */
10 reyssat 351
void parmprep(char *p,int ptype)
352
{
12247 bpr 353
  char *pp, *p2, *pn, namebuf[32], buf[MAX_LINELEN+1];
354
  int i;
10 reyssat 355
 
12247 bpr 356
  while((pp=strchr(p,'\n'))!=NULL) *pp='        ';
357
  for(pp=p;*pp && pp-p<MAX_LINELEN;pp++) {
7677 bpr 358
/* function names */
12247 bpr 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!='(') {
7677 bpr 363
/* if(*p2=='\\' && *(p2+1)=='(') ovlstrcpy(p2,p2+1); */
12247 bpr 364
        pp=p2-1; continue;
7677 bpr 365
      }
12247 bpr 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;
10 reyssat 381
    }
12247 bpr 382
  }
10 reyssat 383
}
384