Rev 9068 | Rev 9095 | 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 "oef2wims.h" |
||
10 | reyssat | 19 | int prepcnt; |
2656 | bpr | 20 | int ex_statement=0, ex_hint=0, ex_help=0, ex_solution=0, ex_latex=0; |
8135 | bpr | 21 | |
22 | const size_t MAX_KEY_LEN=128; |
||
10 | reyssat | 23 | char vbuf_statement[MAX_LINELEN+1]; |
24 | char vbuf_hint[MAX_LINELEN+1]; |
||
25 | char vbuf_help[MAX_LINELEN+1]; |
||
26 | char vbuf_solution[MAX_LINELEN+1]; |
||
9093 | bpr | 27 | char vbuf_latex[MAX_LINELEN+1]; |
10 | reyssat | 28 | |
7677 | bpr | 29 | /* empty processor, template. */ |
10 | reyssat | 30 | void empty(char *p[MAX_PARM]) {} |
31 | |||
3288 | bpr | 32 | static void p_keyword(char *p[MAX_PARM], const char *key) |
33 | { |
||
34 | p[0]=find_word_start(p[0]); |
||
35 | if(strlen(p[0])>MAX_KEY_LEN) p[0][MAX_KEY_LEN]=0; |
||
36 | fprintf(outf,"%s=%s\n", key, p[0]); |
||
37 | } |
||
38 | |||
9068 | bpr | 39 | void p_author(char *p[MAX_PARM]) { p_keyword(p, "author"); } |
40 | void p_email(char *p[MAX_PARM]) { p_keyword(p, "email"); } |
||
41 | void p_keywords(char *p[MAX_PARM]) { p_keyword(p, "keywords"); } |
||
3288 | bpr | 42 | void p_title_ca(char *p[MAX_PARM]) { p_keyword(p, "title_ca"); } |
43 | void p_title_cn(char *p[MAX_PARM]) { p_keyword(p, "title_cn"); } |
||
44 | void p_title_en(char *p[MAX_PARM]) { p_keyword(p, "title_en"); } |
||
45 | void p_title_es(char *p[MAX_PARM]) { p_keyword(p, "title_es"); } |
||
6828 | bpr | 46 | void p_title_fr(char *p[MAX_PARM]) { p_keyword(p, "title_fr"); } |
3288 | bpr | 47 | void p_title_it(char *p[MAX_PARM]) { p_keyword(p, "title_it"); } |
48 | void p_title_nl(char *p[MAX_PARM]) { p_keyword(p, "title_nl"); } |
||
49 | void p_title_si(char *p[MAX_PARM]) { p_keyword(p, "title_si"); } |
||
50 | |||
10 | reyssat | 51 | void p_computeanswer(char *p[MAX_PARM]) |
7677 | bpr | 52 | { |
10 | reyssat | 53 | p[0]=find_word_start(p[0]); |
54 | *find_word_end(p[0])=0; |
||
55 | if(strcasecmp(p[0],"yes")) |
||
56 | fprintf(outf,"computeanswer=no\n"); |
||
57 | else fprintf(outf,"computeanswer=%s\n",p[0]); |
||
58 | } |
||
59 | |||
60 | void p_precision(char *p[MAX_PARM]) |
||
61 | { |
||
62 | int pr; |
||
63 | pr=atoi(p[0]); |
||
64 | if(pr<0 || pr>100000000) return; |
||
65 | fprintf(outf,"precision=%d\n",pr); |
||
66 | } |
||
67 | |||
1105 | bpr | 68 | void p_css(char *p[MAX_PARM]) |
7677 | bpr | 69 | { |
1105 | bpr | 70 | char vbuf_css[MAX_LINELEN+1]; |
71 | if(p==NULL) return; |
||
72 | snprintf(vbuf_css,sizeof(vbuf_css),"%s",p[0]); subst(vbuf_css); |
||
8113 | bpr | 73 | fprintf(outf,"oefcss=%s\n",vbuf_css); |
1105 | bpr | 74 | } |
4810 | bpr | 75 | |
76 | void p_credits(char *p[MAX_PARM]) |
||
77 | { |
||
78 | char vbuf_credits[MAX_LINELEN+1]; |
||
79 | if(p==NULL) return; |
||
7677 | bpr | 80 | snprintf(vbuf_credits,sizeof(vbuf_credits),"%s",p[0]); |
4810 | bpr | 81 | subst(vbuf_credits); |
7677 | bpr | 82 | singlespace(vbuf_credits); |
83 | fprintf(outf,"credits=%s\n\n", vbuf_credits); |
||
4810 | bpr | 84 | } |
85 | |||
10 | reyssat | 86 | void p_wims(char *p[MAX_PARM]) |
87 | { |
||
88 | char vbuf[MAX_LINELEN+1]; |
||
89 | snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf); |
||
7677 | bpr | 90 | /* the second condition could be removed? |
91 | * To be very careful! */ |
||
10 | reyssat | 92 | if(strchr(vbuf,'=')==NULL || strchr(vbuf,'!')!=NULL) return; |
7677 | bpr | 93 | fprintf(outf,"%s\n",vbuf); |
10 | reyssat | 94 | } |
95 | |||
96 | void p_mdef(char *p[MAX_PARM]) |
||
97 | { |
||
98 | char vbuf[MAX_LINELEN+1]; |
||
99 | if(wordchr(mdef,p[0])==NULL) return; |
||
100 | snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf); |
||
7677 | bpr | 101 | /* the second condition could be removed? |
102 | * To be very careful! */ |
||
10 | reyssat | 103 | if(strchr(vbuf,'!')!=NULL) return; |
104 | fprintf(outf,"m_%s=%s\n",p[0],vbuf); |
||
105 | } |
||
106 | |||
107 | void p_range(char *p[MAX_PARM]) |
||
108 | { |
||
109 | double left, right; |
||
110 | char *pp; |
||
111 | pp=strstr(p[0],".."); |
||
112 | if(pp==NULL) return; |
||
113 | *pp=0;pp+=strlen(".."); |
||
114 | left=atof(p[0]); right=atof(pp); |
||
115 | if(left>=right-1E-50 || left<-1E50 || right>1E50) return; |
||
116 | fprintf(outf,"leftrange=%f\nrightrange=%f\n",left,right); |
||
117 | } |
||
118 | |||
119 | void p_language(char *p[MAX_PARM]) |
||
7677 | bpr | 120 | { |
10 | reyssat | 121 | p[0]=find_word_start(p[0]); |
122 | *find_word_end(p[0])=0; |
||
7677 | bpr | 123 | if(strlen(p[0])==2) fprintf(outf,"language=%s\n",p[0]); |
10 | reyssat | 124 | } |
125 | |||
126 | void p_statement(char *p[MAX_PARM]) |
||
127 | { |
||
128 | if(ex_statement<0) return; |
||
129 | if(ex_statement>0 || p==NULL) { |
||
7677 | bpr | 130 | out_exec(vbuf_statement,"question"); |
131 | ex_statement=-1; return; |
||
10 | reyssat | 132 | } |
133 | if(p==NULL) return; |
||
134 | snprintf(vbuf_statement,sizeof(vbuf_statement),"%s",p[0]); |
||
135 | subst(vbuf_statement); |
||
136 | if(strcmp(format,"html")!=0) { |
||
7677 | bpr | 137 | fprintf(outf,"question=!nosubst %s\n",vbuf_statement); |
138 | ex_statement=-1; |
||
10 | reyssat | 139 | } |
140 | else { |
||
7677 | bpr | 141 | fprintf(outf,"question=%s\n",executed_str); |
142 | ex_statement=1; |
||
10 | reyssat | 143 | } |
144 | } |
||
145 | |||
9093 | bpr | 146 | void p_gen(int *ex_gen, char vbuf_gen[], char *name_gen, char *p[MAX_PARM]) |
10 | reyssat | 147 | { |
9093 | bpr | 148 | if(*ex_gen<0) return; |
149 | if(*ex_gen>0 || p==NULL) { |
||
150 | out_exec(vbuf_gen, name_gen); |
||
151 | *ex_gen=-1; return; |
||
10 | reyssat | 152 | } |
9093 | bpr | 153 | snprintf(vbuf_gen,MAX_LINELEN,"%s",p[0]); subst(vbuf_gen); |
154 | if(strchr(vbuf_gen,'\\')!=NULL) { |
||
155 | fprintf(outf,"%s=%s\n", name_gen, executed_str); |
||
156 | *ex_gen=1; |
||
10 | reyssat | 157 | } |
158 | else { |
||
9093 | bpr | 159 | singlespace(vbuf_gen); |
160 | fprintf(outf,"%s=!nosubst %s\n\n", name_gen, vbuf_gen); |
||
10 | reyssat | 161 | } |
162 | } |
||
163 | |||
9093 | bpr | 164 | void p_hint(char *p[MAX_PARM]) {p_gen(&ex_hint, vbuf_hint, "hint", p);} |
165 | void p_help(char *p[MAX_PARM]) {p_gen(&ex_help, vbuf_help, "help", p);} |
||
166 | void p_solution(char *p[MAX_PARM]) {p_gen(&ex_solution, vbuf_solution, "solution", p);} |
||
10 | reyssat | 167 | |
2656 | bpr | 168 | void p_latex(char *p[MAX_PARM]) |
169 | { |
||
170 | if(ex_latex<0) return; |
||
171 | if(ex_latex>0 || p==NULL) { |
||
7677 | bpr | 172 | out_exec(vbuf_latex,"latex"); |
173 | ex_latex=-1; return; |
||
2656 | bpr | 174 | } |
7677 | bpr | 175 | snprintf(vbuf_latex,sizeof(vbuf_latex),"%s",p[0]); |
2656 | bpr | 176 | subst(vbuf_latex); |
7677 | bpr | 177 | singlespace(vbuf_latex); |
178 | fprintf(outf,"latex=!nosubst %s\n\n", vbuf_latex); |
||
2656 | bpr | 179 | } |
180 | |||
10 | reyssat | 181 | enum {typ_default, typ_num, typ_func, typ_units, typ_text, |
182 | typ_formal,typ_matrix,typ_vector,typ_set,typ_equation, |
||
183 | typ_case, typ_nocase, typ_atext, typ_wlist, typ_comp, |
||
184 | typ_algexp, typ_litexp, typ_menu, typ_coord, typ_fill, |
||
185 | typ_raw, typ_symtext, |
||
186 | typ_java, typ_src, typ_chem |
||
187 | }; |
||
188 | |||
189 | struct { |
||
190 | char *name; |
||
191 | int type; |
||
192 | char *def; |
||
193 | } anstype[]={ |
||
7677 | bpr | 194 | {"algexp", typ_algexp, "algexp"}, |
195 | {"aset", typ_set, "aset"}, |
||
196 | {"atext", typ_atext, "atext"}, |
||
197 | {"case", typ_case, "case"}, |
||
198 | {"checkbox", typ_menu, "checkbox"}, |
||
10 | reyssat | 199 | {"chemeq", typ_chem, "chemeq"}, |
7677 | bpr | 200 | {"chset", typ_atext, "chset"}, |
201 | {"click", typ_menu, "click"}, |
||
202 | {"clickfill", typ_fill, "clickfill"}, |
||
203 | {"code", typ_src, "code"}, |
||
204 | {"compose", typ_comp, "compose"}, |
||
205 | {"coord", typ_coord, "coord"}, |
||
206 | {"coordinates", typ_coord, "coord"}, |
||
207 | {"corresp", typ_comp, "correspond"}, |
||
208 | {"correspond", typ_comp, "correspond"}, |
||
209 | {"default", typ_default, "default"}, |
||
210 | {"dragfill", typ_fill, "dragfill"}, |
||
211 | {"equation", typ_equation, "equation"}, |
||
212 | {"expalg", typ_algexp, "algexp"}, |
||
213 | {"formal", typ_formal, "formal"}, |
||
214 | {"fset", typ_set, "fset"}, |
||
215 | {"function", typ_func, "function"}, |
||
216 | {"imgcomp", typ_comp, "imgcomp"}, |
||
217 | {"javacurve", typ_java, "javacurve"}, |
||
218 | {"link", typ_menu, "click"}, |
||
219 | {"litexp", typ_litexp, "litexp"}, |
||
220 | {"mark", typ_menu, "mark"}, |
||
221 | {"matrix", typ_matrix, "matrix"}, |
||
222 | {"menu", typ_menu, "menu"}, |
||
223 | {"nocase", typ_nocase, "nocase"}, |
||
224 | {"number", typ_num, "numeric"}, |
||
225 | {"numeric", typ_num, "numeric"}, |
||
226 | {"numexp", typ_algexp, "numexp"}, |
||
227 | {"radio", typ_menu, "radio"}, |
||
228 | {"range", typ_func, "range"}, |
||
229 | {"ranges", typ_func, "range"}, |
||
230 | {"raw", typ_raw, "raw"}, |
||
231 | {"reorder", typ_comp, "reorder"}, |
||
232 | {"select", typ_menu, "menu"}, |
||
233 | {"set", typ_set, "set"}, |
||
234 | {"sigunits", typ_units, "sigunits"}, |
||
235 | {"symtext", typ_symtext, "symtext"}, |
||
236 | {"text", typ_text, "case"}, |
||
237 | {"textcomp", typ_comp, "textcomp"}, |
||
238 | {"unit", typ_units, "units"}, |
||
239 | {"units", typ_units, "units"}, |
||
240 | {"vector", typ_vector, "vector"}, |
||
241 | {"wlist", typ_wlist, "wlist"}, |
||
242 | {"wordcomp", typ_comp, "textcomp"} |
||
10 | reyssat | 243 | }; |
244 | |||
245 | #define anstype_no (sizeof(anstype)/sizeof(anstype[0])) |
||
246 | |||
247 | void p_answer(char *p[MAX_PARM]) |
||
248 | { |
||
249 | char *pp, vbuf[MAX_LINELEN+1],nbuf[MAX_LINELEN+1]; |
||
250 | int i,j,k,typ; |
||
7677 | bpr | 251 | |
252 | /* look for type definition */ |
||
10 | reyssat | 253 | typ=typ_default; |
254 | for(i=0;i<5;i++) { |
||
7677 | bpr | 255 | if(p[i]==NULL || p[i][0]==0) continue; |
256 | p[i]=find_word_start(p[i]); |
||
257 | if(strncasecmp(p[i],"type",strlen("type"))==0) { |
||
258 | char *tt; |
||
259 | tt=find_word_start(p[i]+strlen("type")); |
||
260 | if(*tt=='=') { |
||
261 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
262 | tt=find_word_start(tt+1); *find_word_end(tt)=0; |
||
263 | k=search_list(anstype,anstype_no,sizeof(anstype[0]),tt); |
||
264 | /* unknown type is now substituted */ |
||
265 | if(k>=0) { |
||
266 | fprintf(outf,"replytype%d=%s\n", |
||
267 | answercnt,anstype[k].def); |
||
268 | typ=anstype[k].type; |
||
269 | } |
||
270 | else { |
||
271 | snprintf(nbuf,sizeof(nbuf),"%s",tt); subst(nbuf); |
||
272 | fprintf(outf,"replytype%d=%s\n\n",answercnt,nbuf); |
||
273 | } |
||
274 | } |
||
275 | continue; |
||
276 | } |
||
277 | if(strncasecmp(p[i],"option",strlen("option"))==0) { |
||
278 | char *tt, *tv; |
||
279 | tt=p[i]+strlen("option"); |
||
280 | if(*tt=='s' || *tt=='S') tt++; |
||
281 | tt=find_word_start(tt); |
||
282 | if(*tt=='=') { |
||
283 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
284 | snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf); |
||
285 | for(tv=nbuf; *tv; tv++) if(*tv==',' || *tv==';') *tv=' '; |
||
286 | strip_trailing_spaces(nbuf); |
||
287 | fprintf(outf,"replyoption%d=%s \n",answercnt, |
||
288 | find_word_start(nbuf)); |
||
289 | } |
||
290 | continue; |
||
291 | } |
||
292 | if(strncasecmp(p[i],"weight",strlen("weight"))==0) { |
||
293 | char *tt; |
||
294 | tt=p[i]+strlen("weight"); |
||
295 | tt=find_word_start(tt); |
||
296 | if(*tt=='=') { |
||
297 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
298 | snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf); |
||
299 | strip_trailing_spaces(nbuf); |
||
300 | fprintf(outf,"replyweight%d=%s \n",answercnt, |
||
301 | find_word_start(nbuf)); |
||
302 | } |
||
303 | continue; |
||
304 | } |
||
10 | reyssat | 305 | } |
306 | p[0]=find_word_start(p[0]); |
||
307 | strncpy(nbuf,p[0],MAX_LINELEN); nbuf[MAX_LINELEN]=0; subst(nbuf); |
||
308 | nbuf[MAX_PROMPTLEN]=0; |
||
309 | strip_trailing_spaces(nbuf); pp=nbuf+strlen(nbuf)-1; |
||
310 | if(*pp=='=') *pp=0; |
||
311 | p[1]=find_word_start(p[1]); |
||
312 | if(*p[1]=='\\' && (isalnum(*(p[1]+1)) || *(p[1]+1)=='_')) { |
||
7677 | bpr | 313 | /* check for analyzed answers */ |
314 | int i,n; char *pt; |
||
315 | strncpy(vbuf,p[1]+1,MAX_LINELEN); vbuf[MAX_LINELEN]=0; |
||
316 | pt=strchr(vbuf,';'); if(pt!=NULL) *pt=0; |
||
317 | strip_trailing_spaces(vbuf); n=strlen(vbuf); |
||
318 | if(n>=MAX_NAMELEN) goto normal; |
||
319 | for(i=0;i<n && (isalnum(vbuf[i]) || vbuf[i]=='_');i++); |
||
320 | if(i<n) goto normal; |
||
321 | for(i=varcnt-1;i>=1 && strcmp(vbuf,param[i].name)!=0;i--); |
||
322 | if(i<1) { /* unused name; the answer should be analyzed */ |
||
323 | char *pm; |
||
324 | pm=xmalloc(MAX_NAMELEN+2); |
||
325 | ovlstrcpy(pm,vbuf); param[varcnt].name=pm; |
||
326 | if(pt) { |
||
327 | *pt=';'; |
||
328 | ovlstrcpy(vbuf,pt); subst(vbuf); pt=vbuf; |
||
329 | } |
||
330 | else pt=""; |
||
331 | param[varcnt].type=pt_real; |
||
332 | param[varcnt].save=1; |
||
333 | fprintf(outf,"replyname%d=%s\nreplygood%d=?analyze %d%s\n", |
||
334 | answercnt,nbuf,answercnt,varcnt,pt); |
||
335 | condans++; answercnt++; varcnt++; return; |
||
336 | } |
||
10 | reyssat | 337 | } |
338 | normal: |
||
339 | strncpy(vbuf,p[1],MAX_LINELEN); vbuf[MAX_LINELEN]=0; |
||
340 | subst(vbuf); |
||
341 | switch(typ) { |
||
7677 | bpr | 342 | default: |
343 | case typ_default: { |
||
344 | fprintf(outf,"replyname%d=%s\nreplygood%d=%s\n", |
||
345 | answercnt,nbuf,answercnt,vbuf); |
||
346 | break; |
||
347 | } |
||
348 | case typ_num: { |
||
349 | fprintf(outf,"replyname%d=%s\nreplygood%d=$[%s]\n", |
||
350 | answercnt,nbuf,answercnt,vbuf); |
||
351 | break; |
||
352 | } |
||
353 | case typ_equation: |
||
354 | case typ_func: { |
||
355 | fprintf(outf,"replyname%d=%s\nreplygood%d=!rawmath %s\n", |
||
356 | answercnt,nbuf,answercnt,vbuf); |
||
357 | break; |
||
358 | } |
||
359 | case typ_units: { |
||
360 | fprintf(outf,"replyname%d=%s\nreplygood%d=%s\n", |
||
361 | answercnt,nbuf,answercnt,vbuf); |
||
362 | break; |
||
363 | } |
||
10 | reyssat | 364 | } |
365 | answercnt++; |
||
366 | } |
||
367 | |||
368 | void p_choice(char *p[MAX_PARM]) |
||
369 | { |
||
370 | int i,j; |
||
371 | char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1],nbuf[MAX_LINELEN+1]; |
||
372 | for(i=0;i<5;i++) { |
||
7677 | bpr | 373 | if(p[i]==NULL || p[i][0]==0) continue; |
374 | p[i]=find_word_start(p[i]); |
||
375 | if(strncasecmp(p[i],"option",strlen("option"))==0) { |
||
376 | char *tt, *tv; |
||
377 | tt=p[i]+strlen("option"); |
||
378 | if(*tt=='s' || *tt=='S') tt++; |
||
379 | tt=find_word_start(tt); |
||
380 | if(*tt=='=') { |
||
381 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
382 | snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf); |
||
383 | for(tv=nbuf; *tv; tv++) if(*tv==',' || *tv==';') *tv=' '; |
||
384 | strip_trailing_spaces(nbuf); |
||
385 | fprintf(outf,"choiceoption%d=%s \n",choicecnt, |
||
386 | find_word_start(nbuf)); |
||
387 | } |
||
388 | continue; |
||
389 | } |
||
390 | if(strncasecmp(p[i],"weight",strlen("weight"))==0) { |
||
391 | char *tt; |
||
392 | tt=p[i]+strlen("weight"); |
||
393 | tt=find_word_start(tt); |
||
394 | if(*tt=='=') { |
||
395 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
396 | snprintf(nbuf,sizeof(nbuf),"%s",tt+1); subst(nbuf); |
||
397 | strip_trailing_spaces(nbuf); |
||
398 | fprintf(outf,"choiceweight%d=%s \n",choicecnt, |
||
399 | find_word_start(nbuf)); |
||
400 | } |
||
401 | continue; |
||
402 | } |
||
10 | reyssat | 403 | } |
404 | p[0]=find_word_start(p[0]); |
||
405 | snprintf(buf1,sizeof(buf1),"%s",p[1]); subst(buf1); |
||
406 | snprintf(buf2,sizeof(buf2),"%s",p[2]); subst(buf2); |
||
407 | snprintf(nbuf,sizeof(nbuf),"%s",p[0]); subst(nbuf); |
||
408 | nbuf[MAX_PROMPTLEN]=0; |
||
409 | fprintf(outf,"choicename%d=%s\nchoicegood%d=%s\nchoicebad%d=%s\n", |
||
7677 | bpr | 410 | choicecnt,nbuf,choicecnt,buf1,choicecnt,buf2); |
10 | reyssat | 411 | choicecnt++; |
412 | } |
||
413 | |||
414 | void putval(char *p, int n, int ptype) |
||
415 | { |
||
416 | switch(ptype) { |
||
7677 | bpr | 417 | case pt_int: { |
418 | fprintf(outf,"val%d=$[rint(%s)]\n",n,p); |
||
419 | break; |
||
420 | } |
||
421 | case pt_real: { |
||
422 | fprintf(outf,"val%d=$[%s]\n",n,p); |
||
423 | break; |
||
424 | } |
||
425 | case pt_func: { |
||
426 | fprintf(outf,"val%d=!rawmath %s\n",n,p); |
||
427 | break; |
||
428 | } |
||
429 | case pt_complex: { |
||
430 | fprintf(outf,"t_=!rawmath %s\nt_=!exec pari print($t_)\n\ |
||
10 | reyssat | 431 | val%d=!mathsubst I=i in $t_\n",p,n); |
7677 | bpr | 432 | break; |
433 | } |
||
434 | case pt_matrix: { |
||
435 | fprintf(outf,"tmp=!trim %s\n\ |
||
7688 | bpr | 436 | val%d=!translate internal $ \\\n$ to ;; in $tmp\n",p,n); |
7677 | bpr | 437 | break; |
438 | } |
||
439 | case pt_rat: { |
||
440 | fprintf(outf,"t_=!rawmath %s\n\ |
||
10 | reyssat | 441 | val%d=!exec pari print($t_)\n",p,n); |
7677 | bpr | 442 | break; |
443 | } |
||
444 | default: { |
||
445 | fprintf(outf,"val%d=%s\n",n,p); |
||
446 | break; |
||
447 | } |
||
10 | reyssat | 448 | } |
449 | } |
||
450 | |||
451 | void parm(char *p[MAX_PARM], int ptype) |
||
452 | { |
||
453 | char *pp, *p2; |
||
454 | char vbuf[MAX_LINELEN+1]; |
||
455 | int i; |
||
456 | |||
457 | p[0]=find_word_start(p[0]); |
||
458 | if(*p[0]=='\\') p[0]++; |
||
7677 | bpr | 459 | /* bad name */ |
10 | reyssat | 460 | if(!isalpha(*p[0])) return; |
461 | strip_trailing_spaces(p[0]); |
||
462 | for(pp=p[0];*pp;pp++) if(!isalnum(*pp) && *pp!='_') { |
||
7677 | bpr | 463 | /* bad name and security risk */ |
464 | if(!isspace(*pp)) return; |
||
465 | ovlstrcpy(pp,pp+1); pp--; |
||
10 | reyssat | 466 | } |
467 | for(i=1;i<varcnt && strcmp(p[0],param[i].name)!=0;i++); |
||
468 | p[1]=find_word_start(p[1]); |
||
469 | snprintf(vbuf,sizeof(vbuf),"%s",p[1]); subst(vbuf); |
||
470 | if(deftag) repsubst(vbuf); |
||
471 | if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?') { |
||
7677 | bpr | 472 | char buf[MAX_LINELEN+1]; |
473 | if(check_compare(vbuf)==0) goto noif; |
||
474 | p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0; |
||
475 | snprintf(buf,sizeof(buf),"%s",vbuf); |
||
476 | prepcnt=0; parmprep(buf,ptype); |
||
477 | fprintf(outf,"\n!ifval %s\n",buf); |
||
478 | snprintf(buf,sizeof(buf),"%s",pp); |
||
479 | parmprep(buf,ptype); putval(buf,i,ptype); |
||
480 | if(p2!=NULL) { |
||
481 | fprintf(outf,"!else\n"); |
||
482 | snprintf(buf,sizeof(buf),"%s",p2); |
||
483 | parmprep(buf,ptype); putval(buf,i,ptype); |
||
484 | } |
||
485 | fprintf(outf,"!endif\n"); |
||
10 | reyssat | 486 | } |
487 | else { |
||
488 | noif: |
||
7677 | bpr | 489 | prepcnt=0; parmprep(vbuf, ptype); |
490 | putval(vbuf,i,ptype); |
||
10 | reyssat | 491 | } |
492 | if(i>=varcnt && i<MAX_PARAM) { |
||
7677 | bpr | 493 | param[varcnt].name=p[0]; |
494 | param[varcnt].type=ptype; |
||
495 | param[varcnt].save=0; |
||
496 | varcnt++; |
||
10 | reyssat | 497 | } |
498 | } |
||
499 | |||
500 | void p_int(char *p[MAX_PARM]) {parm(p,pt_int);} |
||
501 | void p_rational(char *p[MAX_PARM]) {parm(p,pt_rat);} |
||
502 | void p_real(char *p[MAX_PARM]) {parm(p,pt_real);} |
||
503 | void p_complex(char *p[MAX_PARM]) {parm(p,pt_complex);} |
||
504 | void p_func(char *p[MAX_PARM]) {parm(p,pt_func);} |
||
505 | void p_text(char *p[MAX_PARM]) {parm(p,pt_text);} |
||
506 | void p_matrix(char *p[MAX_PARM]) {parm(p,pt_matrix);} |
||
507 | |||
508 | void p_parm(char *p[MAX_PARM]) |
||
509 | { |
||
510 | parm(p,pt_real); |
||
511 | } |
||
512 | |||
8367 | bpr | 513 | void _p_if(char *p[MAX_PARM], int type) |
10 | reyssat | 514 | { |
515 | char vbuf[MAX_LINELEN+1]; |
||
516 | snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf); |
||
517 | if(deftag) repsubst(vbuf); |
||
518 | prepcnt=0; parmprep(vbuf, pt_real); |
||
8367 | bpr | 519 | switch(type) { |
520 | case 0: fprintf(outf,"!if %s \n",vbuf); break; |
||
521 | case 1: fprintf(outf,"!ifval %s \n",vbuf); |
||
522 | } |
||
10 | reyssat | 523 | } |
8367 | bpr | 524 | void p_if(char *p[MAX_PARM]) { return _p_if(p, 0);} |
525 | void p_ifval(char *p[MAX_PARM]) { return _p_if(p, 1);} |
||
10 | reyssat | 526 | |
527 | void p_else(char *p[MAX_PARM]) |
||
528 | { |
||
529 | fprintf(outf,"!else\n"); |
||
530 | } |
||
531 | |||
532 | void p_endif(char *p[MAX_PARM]) |
||
533 | { |
||
534 | fprintf(outf,"!endif\n"); |
||
535 | } |
||
536 | |||
537 | |||
538 | void p_while(char *p[MAX_PARM]) |
||
539 | { |
||
540 | char vbuf[MAX_LINELEN+1]; |
||
541 | snprintf(vbuf,sizeof(vbuf),"%s",p[0]); subst(vbuf); |
||
542 | if(deftag) repsubst(vbuf); |
||
543 | prepcnt=0; parmprep(vbuf, pt_real); |
||
544 | fprintf(outf,"!while %s \n",vbuf); |
||
545 | } |
||
546 | |||
547 | void p_endwhile(char *p[MAX_PARM]) |
||
548 | { |
||
549 | fprintf(outf,"!endwhile\n"); |
||
550 | } |
||
551 | |||
552 | void p_for(char *p[MAX_PARM]) |
||
553 | { |
||
554 | char *p1, *p2, buf[256]; |
||
555 | char vbuf[MAX_LINELEN+1]; |
||
556 | int i; |
||
557 | |||
558 | p1=find_word_start(p[0]); |
||
559 | if(!isalpha(*p1)) return; |
||
560 | for(p2=p1; isalnum(*p2); p2++); |
||
561 | if(p2-p1>64) return; |
||
562 | memmove(buf,p1,p2-p1); buf[p2-p1]=0; |
||
563 | for(i=1;i<varcnt && strcmp(buf,param[i].name)!=0;i++); |
||
564 | if(i>=varcnt && i<MAX_PARAM) { |
||
7677 | bpr | 565 | param[varcnt].name=p1; |
566 | param[varcnt].type=pt_real; |
||
567 | param[varcnt].save=0; |
||
568 | varcnt++; |
||
10 | reyssat | 569 | } |
570 | snprintf(vbuf,sizeof(vbuf),"%s",p2); subst(vbuf); *p2=0; |
||
571 | if(deftag) repsubst(vbuf); |
||
572 | prepcnt=0; parmprep(vbuf, pt_real); |
||
573 | fprintf(outf,"!for val%d %s \n", i, vbuf); |
||
574 | } |
||
575 | |||
576 | void p_next(char *p[MAX_PARM]) |
||
577 | { |
||
578 | fprintf(outf,"!next\n"); |
||
579 | } |
||
580 | |||
581 | void p_plot(char *p[MAX_PARM]) |
||
582 | { |
||
583 | int i, f, xr, yr; |
||
584 | char *pp, *p2; |
||
585 | char buf[MAX_LINELEN+1]; |
||
7677 | bpr | 586 | f=xr=yr=-1; |
10 | reyssat | 587 | for(i=0;i<3;i++) { |
7677 | bpr | 588 | if(*p[i]==0) continue; |
589 | if((pp=strchr(p[i],'='))==NULL) f=i; |
||
590 | else { |
||
591 | *pp=0; pp++; |
||
592 | p2=find_word_start(p[i]); |
||
593 | if(*p2=='x' || *p2=='X') xr=i; |
||
594 | else if (*p2=='y' || *p2=='Y') yr=i; |
||
595 | ovlstrcpy(p[i],pp); |
||
596 | } |
||
10 | reyssat | 597 | } |
598 | /* if(xr>=0 && (pp=strstr(p[xr],".."))!=NULL) { |
||
7677 | bpr | 599 | |
10 | reyssat | 600 | } |
601 | */ if(f<0) return; |
||
3718 | reyssat | 602 | ovlstrcpy(buf, p[f]); |
10 | reyssat | 603 | prepcnt=0; parmprep(buf,pt_func); |
604 | fprintf(outf,"plot_fn=!rawmath %s\n",buf); |
||
7677 | bpr | 605 | |
10 | reyssat | 606 | } |
607 | |||
608 | void p_condition(char *p[MAX_PARM]) |
||
609 | { |
||
610 | int i,j; |
||
611 | char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1]; |
||
612 | for(i=0;i<5;i++) { |
||
7677 | bpr | 613 | if(p[i]==NULL || p[i][0]==0) continue; |
614 | p[i]=find_word_start(p[i]); |
||
615 | if(strncasecmp(p[i],"option",strlen("option"))==0) { |
||
616 | char *tt, *tv; |
||
617 | tt=p[i]+strlen("option"); |
||
618 | if(*tt=='s' || *tt=='S') tt++; |
||
619 | tt=find_word_start(tt); |
||
620 | if(*tt=='=') { |
||
621 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
622 | snprintf(buf1,sizeof(buf1),"%s",tt+1); subst(buf1); |
||
623 | for(tv=buf1; *tv; tv++) if(*tv==',' || *tv==';') *tv=' '; |
||
624 | strip_trailing_spaces(buf1); |
||
625 | fprintf(outf,"condoption%d=%s \n",conditioncnt, |
||
626 | find_word_start(buf1)); |
||
627 | } |
||
628 | continue; |
||
629 | } |
||
630 | if(strncasecmp(p[i],"weight",strlen("weight"))==0) { |
||
631 | char *tt; |
||
632 | tt=p[i]+strlen("weight"); |
||
633 | tt=find_word_start(tt); |
||
634 | if(*tt=='=') { |
||
635 | for(j=i;j<6;j++) p[j]=p[j+1]; i--; |
||
636 | snprintf(buf1,sizeof(buf1),"%s",tt+1); subst(buf1); |
||
637 | strip_trailing_spaces(buf1); |
||
638 | fprintf(outf,"condweight%d=%s \n",conditioncnt, |
||
639 | find_word_start(buf1)); |
||
640 | } |
||
641 | continue; |
||
642 | } |
||
10 | reyssat | 643 | } |
644 | if(p[1][0]==0) {p[1]=p[0]; p[0]="";} |
||
645 | snprintf(buf1,sizeof(buf1),"%s",p[0]); subst(buf1); |
||
646 | snprintf(buf2,sizeof(buf2),"%s",p[1]); subst(buf2); |
||
647 | prepcnt=0; parmprep(buf2, pt_real); |
||
648 | repsubst(buf2); |
||
649 | fprintf(outf,"\n!ifval %s\n condtest%d=1\n!else\n condtest%d=0\n!endif\n\ |
||
650 | condname%d=%s\n", buf2,conditioncnt,conditioncnt,conditioncnt,buf1); |
||
651 | conditioncnt++; |
||
652 | } |
||
653 | |||
654 | void p_conditions(char *p[MAX_PARM]) |
||
655 | { |
||
656 | char buf[MAX_LINELEN+1]; |
||
657 | snprintf(buf,sizeof(buf),"%s",p[0]); subst(buf); |
||
658 | prepcnt=0; parmprep(buf, pt_real); |
||
659 | repsubst(buf); |
||
660 | fprintf(outf,"\ncondlist=%s\n",buf); |
||
661 | } |
||
662 | |||
663 | void p_feedback(char *p[MAX_PARM]) |
||
664 | { |
||
665 | char buf1[MAX_LINELEN+1],buf2[MAX_LINELEN+1]; |
||
666 | char *cmpstr="ifval"; |
||
7677 | bpr | 667 | |
10 | reyssat | 668 | snprintf(buf1,sizeof(buf1),"%s",p[0]); subst(buf1); |
669 | snprintf(buf2,sizeof(buf2),"%s",p[1]); subst(buf2); |
||
670 | repsubst(buf1); repsubst(buf2); |
||
671 | if(strstr(buf1,"$m_choice")!=NULL) cmpstr="if"; |
||
672 | prepcnt=0; setpre="!set "; parmprep(buf1, pt_real); setpre=""; |
||
6450 | obado | 673 | fprintf(outf,"!%s %s\n <div class='oef_feedbacks'>",cmpstr, buf1); |
10 | reyssat | 674 | out_exec(buf2,NULL); |
6450 | obado | 675 | fprintf(outf,"</div>\n!endif\n"); |
10 | reyssat | 676 | } |
677 | |||
7677 | bpr | 678 | /* definition of steps */ |
10 | reyssat | 679 | void p_steps(char *p[MAX_PARM]) |
680 | { |
||
681 | char vbuf[MAX_LINELEN+1]; |
||
682 | char *pp, *p2; |
||
7677 | bpr | 683 | |
10 | reyssat | 684 | snprintf(vbuf,sizeof(vbuf),"%s",find_word_start(p[0])); subst(vbuf); |
685 | strip_trailing_spaces(vbuf); |
||
686 | if(vbuf[0]==0) return; |
||
687 | if((pp=strparchr(vbuf,'?'))!=NULL && pp[1]!='?') { |
||
7677 | bpr | 688 | char buf[MAX_LINELEN+1]; |
689 | if(check_compare(vbuf)==0) goto noif; |
||
690 | p2=strparchr(pp,':'); *pp++=0; if(p2!=NULL) *p2++=0; |
||
691 | snprintf(buf,sizeof(buf),"%s",vbuf); |
||
692 | prepcnt=0; parmprep(buf,pt_text); |
||
693 | fprintf(outf,"\n!ifval %s \n",buf); |
||
694 | snprintf(buf,sizeof(buf),"%s",pp); |
||
695 | parmprep(buf,pt_text); |
||
696 | fprintf(outf,"oefsteps=%s \n",buf); |
||
697 | if(p2!=NULL) { |
||
698 | fprintf(outf,"!else\n"); |
||
699 | snprintf(buf,sizeof(buf),"%s",p2); |
||
700 | parmprep(buf,pt_text); |
||
701 | fprintf(outf,"oefsteps=%s \n",buf); |
||
702 | } |
||
703 | fprintf(outf,"!endif\n"); |
||
10 | reyssat | 704 | } |
705 | else { |
||
706 | noif: |
||
7677 | bpr | 707 | prepcnt=0; parmprep(vbuf, pt_text); |
708 | fprintf(outf,"oefsteps=%s \nnextstep=!nosubst %s \n",vbuf,vbuf); |
||
10 | reyssat | 709 | } |
710 | fprintf(outf,"!readproc oef/steps.proc\n"); |
||
711 | } |
||
712 | |||
7677 | bpr | 713 | /* dynamic steps */ |
10 | reyssat | 714 | void p_nextstep(char *p[MAX_PARM]) |
715 | { |
||
716 | fprintf(outf,"dynsteps=yes\n"); |
||
717 | p_steps(p); |
||
718 | } |
||
719 |