Subversion Repositories wimsdev

Rev

Rev 12247 | Go to most recent revision | 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
 
7622 bpr 157
void sp_canvasdraw(char *p, int ptype)
158
{
12247 bpr 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/canvasdraw.phtml %s \\\n%s \n\
7622 bpr 164
%stmp%d=$canvasdraw_out\n", primitive_dir, p, p2, setpre,prepcnt);
165
}
166
 
10 reyssat 167
void sp_shuffle(char *p, int ptype)
168
{
12247 bpr 169
  fprintf(outf,"%stmp%d=!shuffle %s\n\n",setpre,prepcnt,p);
10 reyssat 170
}
171
 
172
void sp_positionof(char *p, int ptype)
173
{
12247 bpr 174
  char *p1;
175
  p1=strparchr(p,','); if(p1==NULL) {
176
    fprintf(outf,"%stmp%d=%s\n\n",setpre,prepcnt,p); return;
177
  }
178
  *p1++=0;
179
  fprintf(outf,"%stmp%d=!positionof item %s in %s\n\n",setpre,prepcnt,p, p1);
10 reyssat 180
}
181
 
182
void sp_random(char *p, int ptype)
183
{
12247 bpr 184
  char *pr, *p2, buf[MAX_LINELEN+1];
185
  snprintf(buf,sizeof(buf),"%s",p);
186
  for(p2=buf; *p2 && p2-buf<MAX_LINELEN; p2++) {
187
    if(*p2=='(') {
188
      p2=find_matching(p2+1,')'); continue;
10 reyssat 189
    }
12247 bpr 190
    if(*p2==',' || (*p2=='.' && *(p2+1)=='.')) break;
191
  }
192
  if(*p2==',') pr="randitem";
193
  else {
194
    if(*p2=='.') {
195
      *p2=','; *(p2+1)=' ';
10 reyssat 196
    }
12247 bpr 197
    if(ptype==pt_int) pr="randint"; else pr="random";
198
  }
199
  fprintf(outf,"%stmp%d=!%s %s\n\n",setpre,prepcnt,pr,buf);
10 reyssat 200
}
201
 
202
void sp_pickone(char *p, int ptype)
203
{
12247 bpr 204
  sp_random(p,pt_int);
10 reyssat 205
}
206
 
207
void sp_item(char *p, int ptype)
208
{
12247 bpr 209
  char *pp, *p2, buf[MAX_LINELEN+1];
210
  pp=strparchr(p,',');
211
  if(pp==NULL) pp=""; else *pp++=0;
212
  strncpy(buf,p,MAX_LINELEN);buf[MAX_LINELEN]=0;
213
  strip_enclosing_par(buf);
214
  if((p2=strstr(buf,".."))!=NULL) string_modify(buf,p2,p2+2," to ");
215
  fprintf(outf,"%stmp%d=!item %s of %s\n\n",setpre,prepcnt,buf,pp);
10 reyssat 216
}
217
 
218
void sp_items(char *p, int ptype)
219
{
12247 bpr 220
  fprintf(outf,"%stmp%d=!itemcnt %s\n\n",setpre,prepcnt,p);
10 reyssat 221
}
222
 
223
void sp_randitem(char *p, int ptype)
224
{
12247 bpr 225
  fprintf(outf,"%stmp=!nonempty items %s\n\n\
10 reyssat 226
%stmp%d=!randitem $tmp\n",setpre,p,setpre,prepcnt);
227
}
228
 
229
void sp_column(char *p, int ptype)
230
{
12247 bpr 231
  char *pp, *p2, buf[MAX_LINELEN+1];
232
  pp=strparchr(p,',');
233
  if(pp==NULL) pp=""; else *pp++=0;
234
  strncpy(buf,p,MAX_LINELEN);buf[MAX_LINELEN]=0;
235
  strip_enclosing_par(buf);
236
  if((p2=strstr(buf,".."))!=NULL) string_modify(buf,p2,p2+2," to ");
237
  fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
10 reyssat 238
%stmp=!column %s of $tmp\n\
239
%stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
7677 bpr 240
          setpre,pp,setpre, buf, setpre, prepcnt);
10 reyssat 241
}
242
 
243
void sp_row(char *p, int ptype)
244
{
12247 bpr 245
  char *pp, *p2, buf[MAX_LINELEN+1];
246
  pp=strparchr(p,',');
247
  if(pp==NULL) pp=""; else *pp++=0;
248
  strncpy(buf,p,MAX_LINELEN);buf[MAX_LINELEN]=0;
249
  strip_enclosing_par(buf);
250
  fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n",
251
        setpre,pp);
252
  if(strstr(buf,"column")!=NULL) {
253
    fprintf(outf,"%stmp%d=!select $tmp where %s\n\n",setpre,prepcnt,buf);
254
    return;
255
  }
256
  while((p2=strstr(buf,".."))!=NULL)
257
    string_modify(buf,p2,p2+2," to ");
258
  fprintf(outf,"%stmp=!row %s of $tmp\n\
10 reyssat 259
%stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
7677 bpr 260
          setpre, buf, setpre, prepcnt);
10 reyssat 261
}
262
 
263
void sp_rows(char *p, int ptype)
264
{
12247 bpr 265
  fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
10 reyssat 266
%stmp%d=!rowcnt $tmp\n",setpre,p,setpre,prepcnt);
267
}
268
 
269
void sp_randrow(char *p, int ptype)
270
{
12247 bpr 271
  fprintf(outf,"%stmp=!translate internal $     $ to ; in %s\n\n\
10 reyssat 272
%stmp=!nonempty rows $tmp\n\
273
%stmp=!randrow $tmp\n\
274
%stmp%d=!translate internal $\\\n$ to ; in $tmp\n",
7677 bpr 275
          setpre,p,setpre,setpre,setpre,prepcnt);
10 reyssat 276
}
277
 
278
void sp_mathexp_cut(char *p, int ptype)
279
{
12247 bpr 280
  char *p2;
281
  p2=find_word_end(find_word_start(p)); if(isspace(*p2)) *p2++=0;
282
  p2=find_word_start(p2);
283
  fprintf(outf,"%stmp%d=!exec mathexp cut %s\\\n%s\n\n",
284
        setpre,prepcnt,p,p2);
10 reyssat 285
}
286
 
287
void sp_wims(char *p, int ptype)
288
{
12247 bpr 289
  p=find_word_start(p);
290
  if(!isalpha(*p) || strncasecmp(p,"ins",3)==0)
291
    fprintf(outf,"%stmp%d=%s\n\n",setpre,prepcnt,p);
292
  else
293
    fprintf(outf,"%stmp%d=!%s\n\n",setpre,prepcnt,p);
10 reyssat 294
}
295
 
8135 bpr 296
struct specialfn specialfn[]={
12247 bpr 297
  {"asis",  sp_asis},
298
  {"canvasdraw", sp_canvasdraw},
299
  {"column", sp_column},
300
  {"deriv",  sp_diff},
301
  {"derivative", sp_diff},
302
  {"det",  sp_det},
303
  {"determinant", sp_det},
304
  {"diff",  sp_diff},
305
  {"draw",  sp_draw},
306
  {"evaluate", sp_evalue},
307
  {"evalue", sp_evalue},
308
  {"htmlmath", sp_htmlmath},
309
  {"int",  sp_int},
310
  {"integral", sp_int},
311
  {"integrate", sp_int},
312
  {"item",  sp_item},
313
  {"items",  sp_items},
314
  {"mathexp_cut", sp_mathexp_cut},
315
  {"maxima", sp_maxima},
316
  {"pari",  sp_pari},
317
  {"pickone", sp_pickone},
318
  {"position", sp_positionof},
319
  {"positionof", sp_positionof},
320
  {"randint", sp_pickone},
321
  {"randitem", sp_randitem},
322
  {"random", sp_random},
323
  {"randomitem", sp_randitem},
324
  {"randomrow", sp_randrow},
325
  {"randrow", sp_randrow},
326
  {"row",  sp_row},
327
  {"rows",  sp_rows},
328
  {"shuffle", sp_shuffle},
329
  {"simplify", sp_simplify},
330
  {"slib",  sp_slib},
331
  {"solve",  sp_solve},
332
  {"teximg", sp_teximg},
333
  {"texmath", sp_texmath},
334
  {"wims",  sp_wims},
335
  {"yacas", sp_yacas}
10 reyssat 336
};
8135 bpr 337
int specialfn_no=(sizeof(specialfn)/sizeof(specialfn[0]));
10 reyssat 338
 
7677 bpr 339
/* This routine treats special functions */
10 reyssat 340
void parmprep(char *p,int ptype)
341
{
12247 bpr 342
  char *pp, *p2, *pn, namebuf[32], buf[MAX_LINELEN+1];
343
  int i;
10 reyssat 344
 
12247 bpr 345
  while((pp=strchr(p,'\n'))!=NULL) *pp='        ';
346
  for(pp=p;*pp && pp-p<MAX_LINELEN;pp++) {
7677 bpr 347
/* function names */
12247 bpr 348
    if(isalpha(*pp)) {
349
      for(i=0;i<30 && (isalnum(pp[i]) || pp[i]=='_');i++) namebuf[i]=pp[i];
350
      namebuf[i]=0; p2=find_word_start(pp+i);
351
      if((pp>p && isalnum(*(pp-1))) || *p2!='(') {
7677 bpr 352
/* if(*p2=='\\' && *(p2+1)=='(') ovlstrcpy(p2,p2+1); */
12247 bpr 353
        pp=p2-1; continue;
7677 bpr 354
      }
12247 bpr 355
      pn=pp; pp=p2+1; p2=find_matching(pp,')');
356
      if(p2==NULL) {
357
        sp_error("unmatched_parentheses");
358
        pp=p2; continue;
359
      }
360
      i=search_list(specialfn,specialfn_no,sizeof(specialfn[0]),namebuf);
361
      if(i<0) {
362
        pp--; continue;
363
      }
364
      *p2=0;
365
      snprintf(buf,sizeof(buf),"%s",pp);
366
      if(specialfn[i].processor!=sp_asis) parmprep(buf,ptype);
367
      specialfn[i].processor(buf,ptype);
368
      string_modify(p, pn, p2+1, "$(tmp%d)",prepcnt); prepcnt++;
369
      pp=pn+6;
10 reyssat 370
    }
12247 bpr 371
  }
10 reyssat 372
}
373