Annotation of OpenXM_contrib2/asir2000/builtin/strobj.c, Revision 1.62
1.6 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.7 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.6 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.62 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.61 2005/09/27 03:00:21 noro Exp $
1.6 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52: #include "ctype.h"
1.10 ohara 53: #if defined(PARI)
1.1 noro 54: #include "genpari.h"
1.11 saito 55: # if !(PARI_VERSION_CODE > 131588)
1.1 noro 56: extern jmp_buf environnement;
1.11 saito 57: # endif
1.1 noro 58: #endif
1.5 noro 59: #include <string.h>
60:
1.18 noro 61: struct TeXSymbol {
62: char *text;
63: char *symbol;
64: };
65:
1.1 noro 66: extern char *parse_strp;
67:
1.50 ohara 68: void Psprintf();
1.1 noro 69: void Prtostr(), Pstrtov(), Peval_str();
1.3 noro 70: void Pstrtoascii(), Pasciitostr();
1.5 noro 71: void Pstr_len(), Pstr_chr(), Psub_str();
1.14 noro 72: void Pwrite_to_tb();
73: void Ptb_to_string();
74: void Pclear_tb();
75: void Pstring_to_tb();
76: void Pquotetotex_tb();
77: void Pquotetotex();
1.24 noro 78: void Pquotetotex_env();
1.47 noro 79: void Pflatten_quote();
1.52 noro 80: void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name();
1.53 noro 81: void Pquote_unify();
1.58 ohara 82: void do_assign(NODE arg);
1.14 noro 83: void fnodetotex_tb(FNODE f,TB tb);
84: char *symbol_name(char *name);
1.28 noro 85: char *conv_rule(char *name);
1.38 noro 86: char *conv_subs(char *name);
1.28 noro 87: char *call_convfunc(char *name);
1.14 noro 88: void tb_to_string(TB tb,STRING *rp);
89: void fnodenodetotex_tb(NODE n,TB tb);
90: void fargstotex_tb(char *opname,FNODE f,TB tb);
1.35 noro 91: int top_is_minus(FNODE f);
1.56 noro 92: int quote_unify(Obj f,Obj pat,NODE *rp);
1.1 noro 93:
94: struct ftab str_tab[] = {
1.50 ohara 95: {"sprintf",Psprintf,-99999999},
1.1 noro 96: {"rtostr",Prtostr,1},
97: {"strtov",Pstrtov,1},
98: {"eval_str",Peval_str,1},
1.3 noro 99: {"strtoascii",Pstrtoascii,1},
100: {"asciitostr",Pasciitostr,1},
1.5 noro 101: {"str_len",Pstr_len,1},
102: {"str_chr",Pstr_chr,3},
103: {"sub_str",Psub_str,3},
1.14 noro 104: {"write_to_tb",Pwrite_to_tb,2},
105: {"clear_tb",Pclear_tb,1},
106: {"tb_to_string",Ptb_to_string,1},
107: {"string_to_tb",Pstring_to_tb,1},
1.60 noro 108: {"quotetotex_tb",Pquotetotex_tb,2},
109: {"quotetotex",Pquotetotex,1},
110: {"quotetotex_env",Pquotetotex_env,-99999999},
111: {"flatten_quote",Pflatten_quote,2},
112: {"quote_to_funargs",Pquote_to_funargs,1},
1.62 ! noro 113: {"quote_unify",Pquote_unify,2},
1.48 noro 114: {"funargs_to_quote",Pfunargs_to_quote,1},
1.52 noro 115: {"get_function_name",Pget_function_name,1},
1.1 noro 116: {0,0,0},
117: };
1.13 noro 118:
1.14 noro 119: void write_tb(char *s,TB tb)
120: {
121: if ( tb->next == tb->size ) {
122: tb->size *= 2;
123: tb->body = (char **)REALLOC(tb->body,tb->size*sizeof(char *));
124: }
125: tb->body[tb->next] = s;
126: tb->next++;
127: }
1.13 noro 128:
1.18 noro 129: int register_symbol_table(Obj arg);
130: int register_conv_rule(Obj arg);
1.38 noro 131: int register_conv_func(Obj arg);
1.23 noro 132: int register_dp_vars(Obj arg);
1.46 noro 133: int register_dp_vars_origin(Obj arg);
134: int register_dp_dvars_origin(Obj arg);
135: int register_dp_dvars_prefix(Obj arg);
1.25 noro 136: int register_dp_vars_prefix(Obj arg);
1.31 noro 137: int register_dp_vars_hweyl(Obj arg);
1.27 noro 138: int register_show_lt(Obj arg);
1.40 noro 139: char *objtostr(Obj obj);
1.18 noro 140: static struct TeXSymbol *user_texsymbol;
1.23 noro 141: static char **dp_vars;
1.25 noro 142: static int dp_vars_len;
143: static char *dp_vars_prefix;
1.46 noro 144: static char *dp_dvars_prefix;
145: static int dp_vars_origin;
146: static int dp_dvars_origin;
1.27 noro 147: static int show_lt;
1.26 noro 148: static FUNC convfunc;
1.27 noro 149: static int is_lt;
1.28 noro 150: static int conv_flag;
1.31 noro 151: static int dp_vars_hweyl;
1.28 noro 152:
1.38 noro 153: #define CONV_TABLE (1U<<0)
154: #define CONV_SUBS (1U<<1)
155: #define CONV_DMODE (1U<<2)
1.18 noro 156:
157: static struct {
158: char *name;
159: Obj value;
160: int (*reg)();
161: } qtot_env[] = {
162: {"symbol_table",0,register_symbol_table},
163: {"conv_rule",0,register_conv_rule},
1.38 noro 164: {"conv_func",0,register_conv_func},
1.23 noro 165: {"dp_vars",0,register_dp_vars},
1.25 noro 166: {"dp_vars_prefix",0,register_dp_vars_prefix},
1.46 noro 167: {"dp_dvars_prefix",0,register_dp_dvars_prefix},
168: {"dp_vars_origin",0,register_dp_vars_origin},
169: {"dp_dvars_origin",0,register_dp_dvars_origin},
1.31 noro 170: {"dp_vars_hweyl",0,register_dp_vars_hweyl},
1.27 noro 171: {"show_lt",0,register_show_lt},
1.18 noro 172: {0,0,0},
173: };
174:
1.20 noro 175: #define PARTIAL "\\partial"
1.28 noro 176:
177: char *conv_rule(char *name)
178: {
179: char *body,*r;
1.41 noro 180: int len;
1.28 noro 181:
1.38 noro 182: if ( convfunc )
183: name = call_convfunc(name);
1.41 noro 184: if ( conv_flag & CONV_TABLE ) {
185: r = symbol_name(name);
186: if ( r ) return r;
187: }
188: if ( (conv_flag & CONV_DMODE) && *name == 'd' ) {
189: body = conv_rule(name+1);
190: r = MALLOC_ATOMIC((strlen(PARTIAL)+strlen(body)+5)*sizeof(char));
191: if ( !body || !(len=strlen(body)) )
192: strcpy(r,PARTIAL);
193: else if ( len == 1 )
194: sprintf(r,"%s_%s",PARTIAL,body);
195: else
196: sprintf(r,"%s_{%s}",PARTIAL,body);
197: return r;
198: } else
199: return conv_subs(name);
1.28 noro 200: }
201:
1.40 noro 202: int _is_delimiter(char c)
203: {
204: if ( (c == ' ' || c == '_' || c == ',') ) return 1;
205: else return 0;
206: }
207:
208: int _is_alpha(char c)
209: {
210: if ( isdigit(c) || c == '{' || _is_delimiter(c) ) return 0;
211: else return 1;
212: }
213:
1.38 noro 214: char *conv_subs(char *name)
1.19 noro 215: {
1.29 noro 216: int i,j,k,len,clen,slen,start,level;
1.41 noro 217: char *buf,*head,*r,*h,*brace,*buf_conv;
1.28 noro 218: char **subs;
219:
1.41 noro 220: if ( !name || !(len=strlen(name)) ) return "";
221: if ( !(conv_flag&CONV_SUBS) ) return name;
1.28 noro 222: subs = (char **)ALLOCA(len*sizeof(char* ));
1.32 noro 223: for ( i = 0, j = 0, start = i; ; j++ ) {
1.40 noro 224: while ( (i < len) && _is_delimiter(name[i]) ) i++;
1.32 noro 225: start = i;
1.28 noro 226: if ( i == len ) break;
1.29 noro 227: if ( name[i] == '{' ) {
228: for ( level = 1, i++; i < len && level; i++ ) {
229: if ( name[i] == '{' ) level++;
230: else if ( name[i] == '}' ) level--;
231: }
1.32 noro 232: slen = i-start;
1.37 noro 233: if ( slen >= 3 ) {
234: brace = (char *)ALLOCA((slen+1)*sizeof(char));
235: strncpy(brace,name+start+1,slen-2);
236: brace[slen-2] = 0;
1.38 noro 237: buf = conv_subs(brace);
1.37 noro 238: subs[j] = (char *)ALLOCA((strlen(buf)+3)*sizeof(char));
1.38 noro 239: if ( strlen(buf) == 1 )
240: strcpy(subs[j],buf);
241: else
242: sprintf(subs[j],"{%s}",buf);
1.37 noro 243: } else
244: subs[j] = "{}";
1.32 noro 245: } else {
246: if ( isdigit(name[i]) )
247: while ( i < len && isdigit(name[i]) ) i++;
248: else
1.40 noro 249: while ( i < len && _is_alpha(name[i]) ) i++;
1.32 noro 250: slen = i-start;
251: buf = (char *)ALLOCA((slen+1)*sizeof(char));
252: strncpy(buf,name+start,slen); buf[slen] = 0;
1.41 noro 253: buf_conv = symbol_name(buf);
254: subs[j] = buf_conv?buf_conv:buf;
1.32 noro 255: }
1.28 noro 256: }
1.32 noro 257: for ( k = 0, clen = 0; k < j; k++ ) clen += strlen(subs[k]);
258: /* {subs(0)}_{{subs(1)},...,{subs(j-1)}} => {}:j+1 _:1 ,:j-2 */
259: h = r = MALLOC_ATOMIC((clen+(j+1)*2+1+(j-2)+1)*sizeof(char));
260: if ( j == 1 )
261: sprintf(h,"{%s}",subs[0]);
1.28 noro 262: else {
1.38 noro 263: sprintf(h,"{%s}_{%s",subs[0],subs[1]);
1.28 noro 264: h += strlen(h);
1.32 noro 265: for ( k = 2; k < j; k++ ) {
1.38 noro 266: sprintf(h,",%s",subs[k]);
1.28 noro 267: h += strlen(h);
1.19 noro 268: }
1.28 noro 269: strcpy(h,"}");
1.20 noro 270: }
271: return r;
1.19 noro 272: }
273:
1.26 noro 274: char *call_convfunc(char *name)
275: {
276: STRING str,r;
277: NODE arg;
278:
279: MKSTR(str,name);
280: arg = mknode(1,str);
281: r = (STRING)bevalf(convfunc,arg);
282: if ( !r || OID(r) != O_STR )
283: error("call_convfunc : invalid result");
284: return BDY(r);
285: }
286:
1.18 noro 287: int register_symbol_table(Obj arg)
288: {
289: NODE n,t;
290: Obj b;
291: STRING a0,a1;
292: struct TeXSymbol *uts;
293: int i,len;
294:
295: /* check */
296: if ( !arg ) {
297: user_texsymbol = 0;
298: return 1;
299: }
300: if ( OID(arg) != O_LIST ) return 0;
301:
302: n = BDY((LIST)arg);
303: len = length(n);
304: uts = (struct TeXSymbol *)MALLOC((len+1)*sizeof(struct TeXSymbol));
305: for ( i = 0; n; n = NEXT(n), i++ ) {
306: b = (Obj)BDY(n);
307: if ( !b || OID(b) != O_LIST ) return 0;
308: t = BDY((LIST)b);
309: if ( !t || !NEXT(t) ) return 0;
310: a0 = (STRING)BDY(t);
311: a1 = (STRING)BDY(NEXT(t));
1.23 noro 312: if ( !a0 ) return 0;
313: if ( OID(a0) == O_STR )
314: uts[i].text = BDY(a0);
315: else if ( OID(a0) == O_P )
316: uts[i].text = NAME(VR((P)a0));
317: else
318: return 0;
319: if ( !a1 ) return 0;
320: if ( OID(a1) == O_STR )
321: uts[i].symbol = BDY(a1);
322: else if ( OID(a1) == O_P )
323: uts[i].symbol = NAME(VR((P)a1));
324: else
325: return 0;
1.18 noro 326: }
327: uts[i].text = 0;
328: uts[i].symbol = 0;
329: user_texsymbol = uts;
330: return 1;
331: }
332:
1.46 noro 333: int register_dp_vars_origin(Obj arg)
334: {
335: if ( INT(arg) ) {
336: dp_vars_origin = QTOS((Q)arg);
337: return 1;
338: } else return 0;
339: }
340:
341: int register_dp_dvars_origin(Obj arg)
342: {
343: if ( INT(arg) ) {
344: dp_dvars_origin = QTOS((Q)arg);
345: return 1;
346: } else return 0;
347: }
348:
1.31 noro 349: int register_dp_vars_hweyl(Obj arg)
350: {
351: if ( INT(arg) ) {
352: dp_vars_hweyl = QTOS((Q)arg);
353: return 1;
354: } else return 0;
355: }
356:
1.27 noro 357: int register_show_lt(Obj arg)
358: {
359: if ( INT(arg) ) {
360: show_lt = QTOS((Q)arg);
361: return 1;
362: } else return 0;
363: }
1.26 noro 364:
1.18 noro 365: int register_conv_rule(Obj arg)
366: {
1.19 noro 367: if ( INT(arg) ) {
1.28 noro 368: conv_flag = QTOS((Q)arg);
369: convfunc = 0;
370: return 1;
1.38 noro 371: } else return 0;
372: }
373:
374: int register_conv_func(Obj arg)
375: {
1.43 noro 376: if ( !arg ) {
377: convfunc = 0;
378: return 1;
379: } else if ( OID(arg) == O_P && (int)(VR((P)arg))->attr == V_SR ) {
1.26 noro 380: convfunc = (FUNC)(VR((P)arg)->priv);
381: /* f must be a function which takes single argument */
382: return 1;
1.19 noro 383: } else return 0;
1.18 noro 384: }
385:
1.23 noro 386: int register_dp_vars(Obj arg)
387: {
388: int l,i;
389: char **r;
390: NODE n;
391: STRING a;
392:
393: if ( !arg ) {
394: dp_vars = 0;
395: dp_vars_len = 0;
1.25 noro 396: return 1;
1.23 noro 397: } else if ( OID(arg) != O_LIST )
398: return 0;
399: else {
400: n = BDY((LIST)arg);
401: l = length(n);
402: r = (char **)MALLOC_ATOMIC(l*sizeof(char *));
403: for ( i = 0; i < l; i++, n = NEXT(n) ) {
404: a = (STRING)BDY(n);
405: if ( !a ) return 0;
406: if ( OID(a) == O_STR )
407: r[i] = BDY(a);
408: else if ( OID(a) == O_P )
409: r[i] = NAME(VR((P)a));
410: else
411: return 0;
412: }
413: dp_vars = r;
414: dp_vars_len = l;
415: return 1;
416: }
417: }
418:
1.25 noro 419: int register_dp_vars_prefix(Obj arg)
420: {
421: if ( !arg ) {
422: dp_vars_prefix = 0;
423: return 1;
424: } else if ( OID(arg) == O_STR ) {
425: dp_vars_prefix = BDY((STRING)arg);
426: return 1;
427: } else if ( OID(arg) == O_P ) {
428: dp_vars_prefix = NAME(VR((P)arg));
429: return 1;
430: } else return 0;
431: }
432:
1.46 noro 433: int register_dp_dvars_prefix(Obj arg)
434: {
435: if ( !arg ) {
436: dp_dvars_prefix = 0;
437: return 1;
438: } else if ( OID(arg) == O_STR ) {
439: dp_dvars_prefix = BDY((STRING)arg);
440: return 1;
441: } else if ( OID(arg) == O_P ) {
442: dp_dvars_prefix = NAME(VR((P)arg));
443: return 1;
444: } else return 0;
445: }
446:
1.24 noro 447: void Pquotetotex_env(NODE arg,Obj *rp)
1.18 noro 448: {
449: int ac,i;
450: char *name;
451: NODE n,n0;
452: STRING s;
453: LIST l;
454:
455: ac = argc(arg);
456: if ( !ac ) {
457: n0 = 0;
458: for ( i = 0; qtot_env[i].name; i++ ) {
459: NEXTNODE(n0,n); MKSTR(s,qtot_env[i].name); BDY(n) = (pointer)s;
460: NEXTNODE(n0,n); BDY(n) = (Q)qtot_env[i].value;
461: }
462: NEXT(n) = 0;
463: MKLIST(l,n0);
464: *rp = (Obj)l;
1.34 noro 465: } else if ( ac == 1 && !ARG0(arg) ) {
466: /* set to default */
1.43 noro 467: for ( i = 0; qtot_env[i].name; i++ ) {
468: (qtot_env[i].reg)(0);
1.34 noro 469: qtot_env[i].value = 0;
1.43 noro 470: }
1.34 noro 471: *rp = 0;
1.18 noro 472: } else if ( ac == 1 || ac == 2 ) {
1.24 noro 473: asir_assert(ARG0(arg),O_STR,"quotetotex_env");
1.18 noro 474: name = BDY((STRING)ARG0(arg));
475: for ( i = 0; qtot_env[i].name; i++ )
476: if ( !strcmp(qtot_env[i].name,name) ) {
477: if ( ac == 2 ) {
478: if ( (qtot_env[i].reg)((Obj)ARG1(arg)) )
479: qtot_env[i].value = (Obj)ARG1(arg);
480: else
1.24 noro 481: error("quotetotex_env : invalid argument");
1.18 noro 482: }
483: *rp = qtot_env[i].value;
484: return;
485: }
486: *rp = 0;
487: } else
488: *rp = 0;
489: }
490:
1.14 noro 491: void Pwrite_to_tb(NODE arg,Q *rp)
1.13 noro 492: {
493: int i;
1.16 noro 494: Obj obj;
495: TB tb;
1.13 noro 496:
1.14 noro 497: asir_assert(ARG1(arg),O_TB,"write_to_tb");
1.16 noro 498: obj = ARG0(arg);
499: if ( !obj )
500: write_tb("",ARG1(arg));
501: else if ( OID(obj) == O_STR )
502: write_tb(BDY((STRING)obj),ARG1(arg));
503: else if ( OID(obj) == O_TB ) {
504: tb = (TB)obj;
505: for ( i = 0; i < tb->next; i++ )
506: write_tb(tb->body[i],ARG1(arg));
507: }
1.14 noro 508: *rp = 0;
1.53 noro 509: }
510:
1.61 noro 511: FNODE partial_eval(FNODE);
512:
1.57 noro 513: void Pquote_unify(NODE arg,Q *rp)
1.53 noro 514: {
1.61 noro 515: FNODE f,g;
516: QUOTE q;
1.53 noro 517: NODE r;
1.56 noro 518: int ret;
1.53 noro 519:
1.62 ! noro 520: #if 0
1.61 noro 521: g = partial_eval(BDY(((QUOTE)ARG0(arg))));
522: MKQUOTE(q,g);
523: ret = quote_unify((Obj)q,(Obj)ARG1(arg),&r);
1.62 ! noro 524: #else
! 525: ret = quote_unify((Obj)ARG0(arg),(Obj)ARG1(arg),&r);
! 526: #endif
1.57 noro 527: if ( ret ) {
528: do_assign(r);
529: *rp = ONE;
530: } else
1.56 noro 531: *rp = 0;
532: }
533:
534: void do_assign(NODE arg)
535: {
536: NODE t,pair;
537: int pv;
538:
539: QUOTE value;
540:
541: for ( t = arg; t; t = NEXT(t) ) {
542: pair = BDY((LIST)BDY(t));
543: pv = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
544: value = (QUOTE)(BDY(NEXT(pair)));
545: ASSPV(pv,value);
546: }
1.53 noro 547: }
548:
549: /*
1.56 noro 550: /* consistency check and merge
551: */
1.53 noro 552:
1.56 noro 553: int merge_matching_node(NODE n,NODE a,NODE *rp)
1.53 noro 554: {
555: NODE ta,ba,tn,bn;
556: QUOTE pa,va,pn,vn;
557:
1.56 noro 558: if ( !n ) {
559: *rp = a;
560: return 1;
561: }
1.53 noro 562: for ( ta = a; ta; ta = NEXT(ta) ) {
563: ba = BDY((LIST)BDY(ta));
1.55 noro 564: if ( !ba ) continue;
1.53 noro 565: pa = (QUOTE)BDY(ba); va = (QUOTE)BDY(NEXT(ba));
566: for ( tn = n; tn; tn = NEXT(tn) ) {
567: bn = BDY((LIST)BDY(tn));
1.55 noro 568: if ( !bn ) continue;
1.53 noro 569: pn = (QUOTE)BDY(bn); vn = (QUOTE)BDY(NEXT(bn));
1.55 noro 570: if ( !compquote(CO,pa,pn) ) {
571: if ( !compquote(CO,va,vn) ) break;
572: else return 0;
573: }
1.53 noro 574: }
575: if ( !tn ) {
576: MKNODE(tn,(pointer)BDY(ta),n);
577: n = tn;
578: }
579: }
1.56 noro 580: *rp = n;
581: return 1;
1.53 noro 582: }
583:
1.56 noro 584: int quote_unify_node(NODE f,NODE pat,NODE *rp) {
585: NODE r,a,tf,tp,r1;
586: int ret;
1.53 noro 587:
588: if ( length(f) != length(pat) ) return 0;
589: r = 0;
590: for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) {
1.56 noro 591: ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a);
592: if ( !ret ) return 0;
593: ret = merge_matching_node(r,a,&r1);
594: if ( !ret ) return 0;
595: else r = r1;
1.53 noro 596: }
1.56 noro 597: *rp = r;
598: return 1;
1.53 noro 599: }
600:
601: void get_quote_id_arg(QUOTE f,int *id,NODE *r)
602: {
603: LIST fa;
604: NODE arg,fab;
605:
606: arg = mknode(1,f); Pquote_to_funargs(arg,&fa); fab = BDY((LIST)fa);
607: *id = QTOS((Q)BDY(fab)); *r = NEXT(fab);
608: }
609:
1.56 noro 610: /* *rp : [[quote(A),quote(1)],...] */
1.53 noro 611:
1.56 noro 612: int quote_unify(Obj f, Obj pat, NODE *rp)
1.53 noro 613: {
614: NODE tf,tp,head,body;
615: NODE parg,farg,r;
616: LIST fa,l;
617: int pid,id;
1.55 noro 618: FUNC ff,pf;
1.56 noro 619: int ret;
1.53 noro 620:
621: if ( OID(pat) == O_LIST ) {
622: if ( OID(f) == O_LIST )
1.56 noro 623: return quote_unify_node(BDY((LIST)f),BDY((LIST)pat),rp);
1.53 noro 624: else
625: return 0;
626: } else if ( OID(pat) == O_QUOTE ) {
627: if ( OID(f) != O_QUOTE ) return 0;
628: get_quote_id_arg((QUOTE)pat,&pid,&parg);
629: get_quote_id_arg((QUOTE)f,&id,&farg);
630: switch ( pid ) {
1.55 noro 631: case I_FORMULA:
632: if ( compquote(CO,f,pat) )
633: return 0;
634: else {
1.56 noro 635: *rp = 0;
636: return 1;
1.55 noro 637: }
638: break;
639: case I_LIST:
1.56 noro 640: return quote_unify_node(BDY((LIST)BDY(farg)),
641: BDY((LIST)BDY(parg)),rp);
1.53 noro 642: case I_PVAR:
643: /* [[pat,f]] */
644: r = mknode(2,pat,f); MKLIST(l,r);
1.56 noro 645: *rp = mknode(1,l);
646: return 1;
1.53 noro 647: case I_IFUNC:
648: /* F(X,Y,...) = ... */
649: if ( id == I_FUNC ) {
1.54 noro 650: r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r);
651: head = mknode(1,l);
1.56 noro 652: ret = quote_unify(BDY(NEXT(farg)),
653: BDY(NEXT(parg)),&body);
654: if ( !ret ) return 0;
655: else return merge_matching_node(head,body,rp);
1.53 noro 656: } else
657: return 0;
658: case I_BOP:
1.55 noro 659: case I_FUNC:
1.53 noro 660: /* X+Y = ... */
1.55 noro 661: /* f(...) = ... */
1.53 noro 662: if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0;
1.56 noro 663: return quote_unify_node(NEXT(farg),NEXT(parg),rp);
1.55 noro 664: break;
1.53 noro 665: default:
666: if ( pid == id )
1.56 noro 667: return quote_unify_node(farg,parg,rp);
1.53 noro 668: else
669: return 0;
670: }
671: }
1.13 noro 672: }
673:
1.14 noro 674: void Pquotetotex(NODE arg,STRING *rp)
1.13 noro 675: {
1.14 noro 676: TB tb;
1.13 noro 677:
1.14 noro 678: NEWTB(tb);
1.27 noro 679: /* XXX for DP */
680: is_lt = 1;
1.14 noro 681: fnodetotex_tb(BDY((QUOTE)ARG0(arg)),tb);
682: tb_to_string(tb,rp);
1.13 noro 683: }
684:
1.14 noro 685: void Pquotetotex_tb(NODE arg,Q *rp)
1.13 noro 686: {
687: int i;
1.14 noro 688: TB tb;
1.13 noro 689:
1.14 noro 690: asir_assert(ARG1(arg),O_TB,"quotetotex_tb");
1.27 noro 691: /* XXX for DP */
692: is_lt = 1;
1.14 noro 693: fnodetotex_tb(BDY((QUOTE)ARG0(arg)),ARG1(arg));
1.13 noro 694: *rp = 0;
695: }
696:
1.14 noro 697: void Pstring_to_tb(NODE arg,TB *rp)
698: {
699: TB tb;
700:
701: asir_assert(ARG0(arg),O_STR,"string_to_tb");
702: NEWTB(tb);
703: tb->body[0] = BDY((STRING)ARG0(arg));
704: tb->next++;
705: *rp = tb;
706: }
707:
708: void Ptb_to_string(NODE arg,STRING *rp)
709: {
710: TB tb;
711:
712: asir_assert(ARG0(arg),O_TB,"tb_to_string");
713: tb = (TB)ARG0(arg);
714: tb_to_string(tb,rp);
715: }
716:
717: void tb_to_string(TB tb,STRING *rp)
1.13 noro 718: {
1.14 noro 719: int j,len;
1.13 noro 720: char *all,*p,*q;
721:
1.14 noro 722: for ( j = 0, len = 0; j < tb->next; j++ )
723: len += strlen(tb->body[j]);
724: all = (char *)MALLOC_ATOMIC((len+1)*sizeof(char));
725: for ( j = 0, p = all; j < tb->next; j++ )
726: for ( q = tb->body[j]; *q; *p++ = *q++ );
727: *p = 0;
728: MKSTR(*rp,all);
729: }
730:
731: void Pclear_tb(NODE arg,Q *rp)
732: {
733: TB tb;
734: int j;
735:
736: asir_assert(ARG0(arg),O_TB,"clear_tb");
737: tb = (TB)ARG0(arg);
738: for ( j = 0; j < tb->next; j++ )
739: tb->body[j] = 0;
740: tb->next = 0;
741: *rp = 0;
1.13 noro 742: }
1.5 noro 743:
744: void Pstr_len(arg,rp)
745: NODE arg;
746: Q *rp;
747: {
1.16 noro 748: Obj obj;
749: TB tb;
750: int r,i;
1.5 noro 751:
1.16 noro 752: obj = (Obj)ARG0(arg);
753: if ( !obj || (OID(obj) != O_STR && OID(obj) != O_TB) )
754: error("str_len : invalid argument");
755: if ( OID(obj) == O_STR)
756: r = strlen(BDY((STRING)obj));
757: else if ( OID(obj) == O_TB ) {
758: tb = (TB)obj;
759: for ( r = i = 0; i < tb->next; i++ )
760: r += strlen(tb->body[i]);
761: }
1.5 noro 762: STOQ(r,*rp);
763: }
764:
765: void Pstr_chr(arg,rp)
766: NODE arg;
767: Q *rp;
768: {
769: STRING str,terminator;
770: Q start;
771: char *p,*ind;
772: int chr,spos,r;
773:
774: str = (STRING)ARG0(arg);
775: start = (Q)ARG1(arg);
776: terminator = (STRING)ARG2(arg);
777: asir_assert(str,O_STR,"str_chr");
778: asir_assert(start,O_N,"str_chr");
779: asir_assert(terminator,O_STR,"str_chr");
780: p = BDY(str);
781: spos = QTOS(start);
782: chr = BDY(terminator)[0];
1.8 noro 783: if ( spos > (int)strlen(p) )
1.5 noro 784: r = -1;
785: else {
786: ind = strchr(p+spos,chr);
787: if ( ind )
788: r = ind-p;
789: else
790: r = -1;
791: }
792: STOQ(r,*rp);
793: }
794:
795: void Psub_str(arg,rp)
796: NODE arg;
797: STRING *rp;
798: {
799: STRING str;
800: Q head,tail;
801: char *p,*r;
802: int spos,epos,len;
803:
804: str = (STRING)ARG0(arg);
805: head = (Q)ARG1(arg);
806: tail = (Q)ARG2(arg);
807: asir_assert(str,O_STR,"sub_str");
808: asir_assert(head,O_N,"sub_str");
809: asir_assert(tail,O_N,"sub_str");
810: p = BDY(str);
811: spos = QTOS(head);
812: epos = QTOS(tail);
813: len = strlen(p);
814: if ( (spos >= len) || (epos < spos) ) {
815: *rp = 0; return;
816: }
817: if ( epos >= len )
818: epos = len-1;
819: len = epos-spos+1;
820: r = (char *)MALLOC(len+1);
821: strncpy(r,p+spos,len);
822: r[len] = 0;
823: MKSTR(*rp,r);
824: }
1.3 noro 825:
826: void Pstrtoascii(arg,rp)
827: NODE arg;
828: LIST *rp;
829: {
830: STRING str;
831: unsigned char *p;
832: int len,i;
833: NODE n,n1;
834: Q q;
835:
836: str = (STRING)ARG0(arg);
837: asir_assert(str,O_STR,"strtoascii");
838: p = BDY(str);
839: len = strlen(p);
840: for ( i = len-1, n = 0; i >= 0; i-- ) {
841: UTOQ((unsigned int)p[i],q);
842: MKNODE(n1,q,n);
843: n = n1;
844: }
845: MKLIST(*rp,n);
846: }
847:
848: void Pasciitostr(arg,rp)
849: NODE arg;
850: STRING *rp;
851: {
852: LIST list;
853: unsigned char *p;
854: int len,i,j;
855: NODE n;
856: Q q;
857:
858: list = (LIST)ARG0(arg);
859: asir_assert(list,O_LIST,"asciitostr");
860: n = BDY(list);
861: len = length(n);
862: p = MALLOC_ATOMIC(len+1);
863: for ( i = 0; i < len; i++, n = NEXT(n) ) {
864: q = (Q)BDY(n);
865: asir_assert(q,O_N,"asciitostr");
866: j = QTOS(q);
1.4 noro 867: if ( j >= 256 || j <= 0 )
1.3 noro 868: error("asciitostr : argument out of range");
869: p[i] = j;
870: }
871: p[i] = 0;
872: MKSTR(*rp,(char *)p);
873: }
1.1 noro 874:
875: void Peval_str(arg,rp)
876: NODE arg;
877: Obj *rp;
878: {
879: FNODE fnode;
880: char *cmd;
1.10 ohara 881: #if defined(PARI)
1.8 noro 882: void recover(int);
883:
1.1 noro 884: recover(0);
1.11 saito 885: # if !(PARI_VERSION_CODE > 131588)
1.1 noro 886: if ( setjmp(environnement) ) {
887: avma = top; recover(1);
888: resetenv("");
889: }
1.11 saito 890: # endif
1.1 noro 891: #endif
892: cmd = BDY((STRING)ARG0(arg));
1.9 noro 893: exprparse_create_var(0,cmd,&fnode);
1.1 noro 894: *rp = eval(fnode);
895: }
896:
897: void Prtostr(arg,rp)
898: NODE arg;
899: STRING *rp;
900: {
901: char *b;
902: int len;
903:
1.2 noro 904: len = estimate_length(CO,ARG0(arg));
1.12 noro 905: b = (char *)MALLOC_ATOMIC(len+1);
1.1 noro 906: soutput_init(b);
907: sprintexpr(CO,ARG0(arg));
908: MKSTR(*rp,b);
909: }
910:
911: void Pstrtov(arg,rp)
912: NODE arg;
913: P *rp;
914: {
1.8 noro 915: char *p;
1.1 noro 916:
917: p = BDY((STRING)ARG0(arg));
918: #if 0
919: if ( !islower(*p) )
920: *rp = 0;
921: else {
922: for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
923: if ( *t )
924: *rp = 0;
925: else
926: makevar(p,rp);
927: }
928: #else
929: makevar(p,rp);
930: #endif
1.14 noro 931: }
932:
1.15 noro 933: static struct TeXSymbol texsymbol[] = {
934: {"sin","\\sin"},
935: {"cos","\\cos"},
936: {"tan","\\tan"},
937: {"sinh","\\sinh"},
938: {"cosh","\\cosh"},
939: {"tanh","\\tanh"},
940: {"exp","\\exp"},
941: {"log","\\log"},
942:
943: /* Greek Letters (lower case) */
944: {"alpha","\\alpha"},
945: {"beta","\\beta"},
946: {"gamma","\\gamma"},
947: {"delta","\\delta"},
948: {"epsilon","\\epsilon"},
949: {"varepsilon","\\varepsilon"},
950: {"zeta","\\zeta"},
951: {"eta","\\eta"},
952: {"theta","\\theta"},
953: {"vartheta","\\vartheta"},
954: {"iota","\\iota"},
955: {"kappa","\\kappa"},
956: {"lambda","\\lambda"},
957: {"mu","\\mu"},
958: {"nu","\\nu"},
959: {"xi","\\xi"},
960: {"pi","\\pi"},
961: {"varpi","\\varpi"},
962: {"rho","\\rho"},
963: {"sigma","\\sigma"},
964: {"varsigma","\\varsigma"},
965: {"tau","\\tau"},
966: {"upsilon","\\upsilon"},
967: {"phi","\\phi"},
968: {"varphi","\\varphi"},
969: {"chi","\\chi"},
970: {"omega","\\omega"},
971:
972: /* Greek Letters, (upper case) */
973: {"ggamma","\\Gamma"},
974: {"ddelta","\\Delta"},
975: {"ttheta","\\Theta"},
976: {"llambda","\\Lambda"},
977: {"xxi","\\Xi"},
978: {"ppi","\\Pi"},
979: {"ssigma","\\Sigma"},
980: {"uupsilon","\\Upsilon"},
981: {"pphi","\\Phi"},
982: {"ppsi","\\Psi"},
983: {"oomega","\\Omega"},
984:
985: /* Our own mathematical functions */
986: {"algebra_tensor","\\otimes"},
987: {"base_where","{\\rm \\ where \\ }"},
988: /* Mathematical constants */
989: {"c_pi","\\pi"},
990: {"c_i","\\sqrt{-1}"},
991:
992: /* Temporary */
993: {0,0}
994: };
995:
1.14 noro 996: char *symbol_name(char *name)
997: {
1.15 noro 998: int i;
999:
1.41 noro 1000: if ( !name || strlen(name) == 0 )
1001: return "";
1.38 noro 1002: if ( !(conv_flag & CONV_TABLE) )
1003: return name;
1004:
1.18 noro 1005: if ( user_texsymbol )
1006: for ( i = 0; user_texsymbol[i].text; i++ )
1007: if ( !strcmp(user_texsymbol[i].text,name) )
1008: return user_texsymbol[i].symbol;
1.15 noro 1009: for ( i = 0; texsymbol[i].text; i++ )
1010: if ( !strcmp(texsymbol[i].text,name) )
1011: return texsymbol[i].symbol;
1.41 noro 1012: return 0;
1.52 noro 1013: }
1014:
1015: void Pget_function_name(NODE arg,STRING *rp)
1016: {
1017: QUOTEARG qa;
1018: ARF f;
1019: char *opname;
1020:
1021: qa = (QUOTEARG)BDY(arg);
1022: if ( !qa || OID(qa) != O_QUOTEARG || qa->type != A_arf )
1023: *rp = 0;
1024: else {
1025: f = (ARF)BDY(qa);
1026: opname = f->name;
1027: MKSTR(*rp,opname);
1028: }
1.14 noro 1029: }
1030:
1.51 noro 1031: FNODE strip_paren(FNODE);
1032:
1.14 noro 1033: void fnodetotex_tb(FNODE f,TB tb)
1034: {
1035: NODE n,t,t0;
1.38 noro 1036: char vname[BUFSIZ],prefix[BUFSIZ];
1037: char *opname,*vname_conv,*prefix_conv;
1.14 noro 1038: Obj obj;
1.46 noro 1039: int i,len,allzero,elen,elen2,si;
1.40 noro 1040: C cplx;
1041: char *r;
1.17 noro 1042: FNODE fi,f2;
1.14 noro 1043:
1044: write_tb(" ",tb);
1045: if ( !f ) {
1046: write_tb("0",tb);
1047: return;
1048: }
1049: switch ( f->id ) {
1050: /* unary operators */
1.23 noro 1051: case I_NOT:
1052: write_tb("\\neg (",tb);
1053: fnodetotex_tb((FNODE)FA0(f),tb);
1054: write_tb(")",tb);
1055: break;
1056: case I_PAREN:
1057: write_tb("(",tb);
1058: fnodetotex_tb((FNODE)FA0(f),tb);
1059: write_tb(")",tb);
1060: break;
1061: case I_MINUS:
1062: write_tb("-",tb);
1063: fnodetotex_tb((FNODE)FA0(f),tb);
1064: break;
1065:
1066: /* binary operators */
1067: /* arg list */
1068: /* I_AND, I_OR => FA0(f), FA1(f) */
1069: /* otherwise => FA1(f), FA2(f) */
1070: case I_BOP:
1071: opname = ((ARF)FA0(f))->name;
1072: if ( !strcmp(opname,"+") ) {
1073: fnodetotex_tb((FNODE)FA1(f),tb);
1.35 noro 1074: if ( !top_is_minus((FNODE)FA2(f)) ) write_tb(opname,tb);
1.23 noro 1075: fnodetotex_tb((FNODE)FA2(f),tb);
1076: } else if ( !strcmp(opname,"-") ) {
1077: if ( FA1(f) ) fnodetotex_tb((FNODE)FA1(f),tb);
1078: write_tb(opname,tb);
1079: fnodetotex_tb((FNODE)FA2(f),tb);
1080: } else if ( !strcmp(opname,"*") ) {
1081: fnodetotex_tb((FNODE)FA1(f),tb);
1082: write_tb(" ",tb);
1083: /* XXX special care for DP */
1084: f2 = (FNODE)FA2(f);
1085: if ( f2->id == I_EV ) {
1086: n = (NODE)FA0(f2);
1087: for ( i = 0; n; n = NEXT(n), i++ ) {
1088: fi = (FNODE)BDY(n);
1089: if ( fi->id != I_FORMULA || FA0(fi) )
1090: break;
1091: }
1092: if ( n )
1093: fnodetotex_tb((FNODE)FA2(f),tb);
1094: } else
1095: fnodetotex_tb((FNODE)FA2(f),tb);
1096: } else if ( !strcmp(opname,"/") ) {
1097: write_tb("\\frac{",tb);
1098: fnodetotex_tb((FNODE)FA1(f),tb);
1099: write_tb("} {",tb);
1100: fnodetotex_tb((FNODE)FA2(f),tb);
1101: write_tb("}",tb);
1102: } else if ( !strcmp(opname,"^") ) {
1103: fnodetotex_tb((FNODE)FA1(f),tb);
1104: write_tb("^{",tb);
1.51 noro 1105: fnodetotex_tb(strip_paren((FNODE)FA2(f)),tb);
1.23 noro 1106: write_tb("} ",tb);
1107: } else if ( !strcmp(opname,"%") ) {
1108: fnodetotex_tb((FNODE)FA1(f),tb);
1109: write_tb(" {\\rm mod}\\, ",tb);
1110: fnodetotex_tb((FNODE)FA2(f),tb);
1111: } else
1112: error("invalid binary operator");
1113: break;
1114:
1115: case I_COP:
1116: switch( (cid)FA0(f) ) {
1117: case C_EQ:
1118: fnodetotex_tb((FNODE)FA1(f),tb);
1119: write_tb(" = ",tb);
1120: fnodetotex_tb((FNODE)FA2(f),tb);
1121: break;
1122: case C_NE:
1123: fnodetotex_tb((FNODE)FA1(f),tb);
1124: write_tb(" \\neq ",tb);
1125: fnodetotex_tb((FNODE)FA2(f),tb);
1126: break;
1127: case C_GT:
1128: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1129: write_tb(" > ",tb);
1.23 noro 1130: fnodetotex_tb((FNODE)FA2(f),tb);
1131: break;
1132: case C_LT:
1133: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1134: write_tb(" < ",tb);
1.23 noro 1135: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1136: break;
1.23 noro 1137: case C_GE:
1138: fnodetotex_tb((FNODE)FA1(f),tb);
1139: write_tb(" \\geq ",tb);
1140: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1141: break;
1.23 noro 1142: case C_LE:
1143: fnodetotex_tb((FNODE)FA1(f),tb);
1144: write_tb(" \\leq ",tb);
1145: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1146: break;
1147: }
1148: break;
1149:
1.23 noro 1150: case I_LOP:
1151: switch( (lid)FA0(f) ) {
1152: case L_EQ:
1153: fnodetotex_tb((FNODE)FA1(f),tb);
1154: write_tb(" = ",tb);
1155: fnodetotex_tb((FNODE)FA2(f),tb);
1156: break;
1157: case L_NE:
1158: fnodetotex_tb((FNODE)FA1(f),tb);
1159: write_tb(" \\neq ",tb);
1160: fnodetotex_tb((FNODE)FA2(f),tb);
1161: break;
1162: case L_GT:
1163: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1164: write_tb(" > ",tb);
1.23 noro 1165: fnodetotex_tb((FNODE)FA2(f),tb);
1166: break;
1167: case L_LT:
1168: fnodetotex_tb((FNODE)FA1(f),tb);
1.45 noro 1169: write_tb(" < ",tb);
1.23 noro 1170: fnodetotex_tb((FNODE)FA2(f),tb);
1171: break;
1172: case L_GE:
1173: fnodetotex_tb((FNODE)FA1(f),tb);
1174: write_tb(" \\geq ",tb);
1175: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1176: break;
1.23 noro 1177: case L_LE:
1178: fnodetotex_tb((FNODE)FA1(f),tb);
1179: write_tb(" \\leq ",tb);
1180: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1181: break;
1.23 noro 1182: case L_AND:
1183: fnodetotex_tb((FNODE)FA1(f),tb);
1.14 noro 1184: write_tb(" {\\rm \\ and\\ } ",tb);
1.23 noro 1185: fnodetotex_tb((FNODE)FA2(f),tb);
1186: break;
1187: case L_OR:
1.14 noro 1188: fnodetotex_tb((FNODE)FA1(f),tb);
1.23 noro 1189: write_tb(" {\\rm \\ or\\ } ",tb);
1190: fnodetotex_tb((FNODE)FA2(f),tb);
1.14 noro 1191: break;
1.23 noro 1192: case L_NOT:
1193: /* XXX : L_NOT is a unary operator */
1194: write_tb("\\neg (",tb);
1.14 noro 1195: fnodetotex_tb((FNODE)FA1(f),tb);
1.23 noro 1196: write_tb(")",tb);
1197: return;
1.14 noro 1198: }
1199: break;
1200:
1.23 noro 1201: case I_AND:
1202: fnodetotex_tb((FNODE)FA0(f),tb);
1203: write_tb(" {\\rm \\ and\\ } ",tb);
1204: fnodetotex_tb((FNODE)FA1(f),tb);
1205: break;
1206:
1207: case I_OR:
1208: fnodetotex_tb((FNODE)FA0(f),tb);
1209: write_tb(" {\\rm \\ or\\ } ",tb);
1210: fnodetotex_tb((FNODE)FA1(f),tb);
1211: break;
1212:
1.14 noro 1213: /* ternary operators */
1214: case I_CE:
1215: error("fnodetotex_tb : not implemented yet");
1216: break;
1217:
1218: /* lists */
1219: case I_LIST:
1220: write_tb(" [ ",tb);
1221: n = (NODE)FA0(f);
1222: fnodenodetotex_tb(n,tb);
1223: write_tb("]",tb);
1224: break;
1225:
1226: /* function */
1.23 noro 1227: case I_FUNC:
1.40 noro 1228: if ( !strcmp(((FUNC)FA0(f))->name,"@pi") )
1229: write_tb("\\pi",tb);
1230: else if ( !strcmp(((FUNC)FA0(f))->name,"@e") )
1231: write_tb("e",tb);
1232: else {
1233: opname = conv_rule(((FUNC)FA0(f))->name);
1234: write_tb(opname,tb);
1235: write_tb("(",tb);
1236: fargstotex_tb(opname,FA1(f),tb);
1237: write_tb(")",tb);
1238: }
1.23 noro 1239: break;
1240:
1241: /* XXX */
1242: case I_CAR:
1.28 noro 1243: opname = conv_rule("car");
1.23 noro 1244: write_tb(opname,tb);
1245: write_tb("(",tb);
1246: fargstotex_tb(opname,FA0(f),tb);
1247: write_tb(")",tb);
1248: break;
1249:
1250: case I_CDR:
1.28 noro 1251: opname = conv_rule("cdr");
1.23 noro 1252: write_tb(opname,tb);
1253: write_tb("(",tb);
1254: fargstotex_tb(opname,FA0(f),tb);
1255: write_tb(")",tb);
1256: break;
1257:
1258: /* exponent vector */
1259: case I_EV:
1260: n = (NODE)FA0(f);
1.31 noro 1261: if ( dp_vars_hweyl ) {
1262: elen = length(n);
1263: elen2 = elen>>1;
1264: elen = elen2<<1;
1265: }
1.23 noro 1266: allzero = 1;
1.27 noro 1267: if ( show_lt && is_lt )
1268: write_tb("\\underline{",tb);
1.23 noro 1269: for ( t0 = 0, i = 0; n; n = NEXT(n), i++ ) {
1270: fi = (FNODE)BDY(n);
1271: if ( fi->id == I_FORMULA && !FA0(fi) ) continue;
1272: allzero = 0;
1.38 noro 1273: if ( dp_vars && i < dp_vars_len ) {
1274: strcpy(vname,dp_vars[i]);
1275: vname_conv = conv_rule(vname);
1276: } else {
1277: if ( dp_vars_hweyl ) {
1.44 noro 1278: if ( i < elen2 ) {
1.38 noro 1279: strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44 noro 1280: prefix_conv = conv_rule(prefix);
1281: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1282: si = i+dp_vars_origin;
1283: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1284: prefix_conv,si);
1.44 noro 1285: } else if ( i < elen ) {
1.46 noro 1286: strcpy(prefix,
1287: dp_dvars_prefix?dp_dvars_prefix:"\\partial");
1.44 noro 1288: prefix_conv = conv_rule(prefix);
1289: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1290: si = i+dp_dvars_origin-elen2;
1291: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1292: prefix_conv,si);
1.44 noro 1293: } else {
1.38 noro 1294: strcpy(prefix,"h");
1.44 noro 1295: vname_conv = conv_rule(prefix);
1296: }
1297: } else {
1.38 noro 1298: strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44 noro 1299: prefix_conv = conv_rule(prefix);
1300: vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46 noro 1301: si = i+dp_vars_origin;
1302: sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
1303: prefix_conv,si);
1.44 noro 1304: }
1.38 noro 1305: }
1.23 noro 1306: if ( fi->id == I_FORMULA && UNIQ(FA0(fi)) ) {
1307: len = strlen(vname_conv);
1308: opname = MALLOC_ATOMIC(len+2);
1309: sprintf(opname,"%s ",vname_conv);
1.14 noro 1310: write_tb(opname,tb);
1.23 noro 1311: } else {
1312: len = strlen(vname_conv);
1313: /* 2: ^{ */
1314: opname = MALLOC_ATOMIC(len+1+2);
1315: sprintf(opname,"%s^{",vname_conv);
1.14 noro 1316: write_tb(opname,tb);
1.23 noro 1317: fnodetotex_tb((FNODE)BDY(n),tb);
1318: write_tb("} ",tb);
1319: }
1.14 noro 1320: }
1.23 noro 1321: /* XXX */
1322: if ( allzero )
1323: write_tb(" 1 ",tb);
1.27 noro 1324: if ( show_lt && is_lt ) {
1325: write_tb("}",tb);
1326: is_lt = 0;
1327: }
1.14 noro 1328: break;
1329:
1.23 noro 1330: /* string */
1.14 noro 1331: case I_STR:
1332: write_tb((char *)FA0(f),tb);
1333: break;
1334:
1.23 noro 1335: /* internal object */
1.14 noro 1336: case I_FORMULA:
1337: obj = (Obj)FA0(f);
1.40 noro 1338: if ( !obj )
1.42 noro 1339: write_tb("0",tb);
1.40 noro 1340: else if ( OID(obj) == O_N && NID(obj) == N_C ) {
1341: cplx = (C)obj;
1342: write_tb("(",tb);
1343: if ( cplx->r ) {
1344: r = objtostr((Obj)cplx->r); write_tb(r,tb);
1345: }
1346: if ( cplx->i ) {
1347: if ( cplx->r && compnum(0,cplx->i,0) > 0 ) {
1348: write_tb("+",tb);
1349: if ( !UNIQ(cplx->i) ) {
1350: r = objtostr((Obj)cplx->i); write_tb(r,tb);
1351: }
1352: } else if ( MUNIQ(cplx->i) )
1353: write_tb("-",tb);
1354: else if ( !UNIQ(cplx->i) ) {
1355: r = objtostr((Obj)cplx->i); write_tb(r,tb);
1356: }
1357: write_tb("\\sqrt{-1}",tb);
1358: }
1359: write_tb(")",tb);
1360: } else if ( OID(obj) == O_P )
1361: write_tb(conv_rule(VR((P)obj)->name),tb);
1362: else
1363: write_tb(objtostr(obj),tb);
1.14 noro 1364: break;
1365:
1.23 noro 1366: /* program variable */
1.14 noro 1367: case I_PVAR:
1368: if ( FA1(f) )
1369: error("fnodetotex_tb : not implemented yet");
1370: GETPVNAME(FA0(f),opname);
1371: write_tb(opname,tb);
1372: break;
1373:
1374: default:
1375: error("fnodetotex_tb : not implemented yet");
1376: }
1.40 noro 1377: }
1378:
1379: char *objtostr(Obj obj)
1380: {
1381: int len;
1382: char *r;
1383:
1384: len = estimate_length(CO,obj);
1385: r = (char *)MALLOC_ATOMIC(len+1);
1386: soutput_init(r);
1387: sprintexpr(CO,obj);
1388: return r;
1.50 ohara 1389: }
1390:
1391: void Psprintf(NODE arg,STRING *rp)
1392: {
1393: STRING string;
1394: char *s,*t,*r;
1395: int argc,n,len;
1396: NODE node;
1397:
1398: string = (STRING)ARG0(arg);
1399: asir_assert(string,O_STR,"sprintf");
1400: s = BDY(string);
1401: for(n = 0, t = s; *t; t++) {
1402: if (*t=='%' && *(t+1)=='a') {
1403: n++;
1404: }
1405: }
1406: for(node = NEXT(arg), argc = 0, len = strlen(s); node; node = NEXT(node), argc++) {
1407: len += estimate_length(CO,BDY(node));
1408: }
1409: if (argc < n) {
1410: error("sprintf: invalid argument");
1411: }
1412: r = (char *)MALLOC_ATOMIC(len);
1413: for(node = NEXT(arg), t = r; *s; s++) {
1414: if (*s=='%' && *(s+1)=='a') {
1415: strcpy(t,objtostr(BDY(node)));
1416: node = NEXT(node);
1417: t = strchr(t,0);
1418: s++;
1419: }else {
1420: *t++ = *s;
1421: }
1422: }
1423: *t = 0;
1424: MKSTR(*rp,r);
1.14 noro 1425: }
1426:
1427: void fnodenodetotex_tb(NODE n,TB tb)
1428: {
1429: for ( ; n; n = NEXT(n) ) {
1.27 noro 1430: is_lt = 1;
1.14 noro 1431: fnodetotex_tb((FNODE)BDY(n),tb);
1432: if ( NEXT(n) ) write_tb(", ",tb);
1433: }
1434: }
1435:
1436: void fargstotex_tb(char *name,FNODE f,TB tb)
1437: {
1438: NODE n;
1439:
1440: if ( !strcmp(name,"matrix") ) {
1441: error("fargstotex_tb : not implemented yet");
1442: } else if ( !strcmp(name,"vector") ) {
1443: error("fargstotex_tb : not implemented yet");
1444: } else {
1445: if ( f->id == I_LIST ) {
1446: n = (NODE)FA0(f);
1447: fnodenodetotex_tb(n,tb);
1448: } else
1449: fnodetotex_tb(f,tb);
1.35 noro 1450: }
1451: }
1452:
1453: int top_is_minus(FNODE f)
1454: {
1455: char *opname;
1456: int len;
1457: Obj obj;
1458:
1459: if ( !f )
1460: return 0;
1461: switch ( f->id ) {
1462: case I_MINUS:
1463: return 1;
1464: case I_BOP:
1465: opname = ((ARF)FA0(f))->name;
1466: switch ( opname[0] ) {
1467: case '+': case '*': case '/': case '^': case '%':
1468: return top_is_minus((FNODE)FA1(f));
1469: case '-':
1470: if ( FA1(f) )
1471: return top_is_minus((FNODE)FA1(f));
1472: else
1473: return 1;
1474: default:
1475: return 0;
1476: }
1477: break;
1478: case I_COP:
1479: return top_is_minus((FNODE)FA1(f));
1480: case I_LOP:
1481: if ( (lid)FA0(f) == L_NOT ) return 0;
1482: else return top_is_minus((FNODE)FA1(f));
1483: case I_AND: case I_OR:
1484: return top_is_minus((FNODE)FA0(f));
1485: case I_FORMULA:
1486: obj = (Obj)FA0(f);
1.36 noro 1487: if ( !obj )
1488: return 0;
1489: else {
1490: switch ( OID(obj) ) {
1491: case O_N:
1492: return mmono((P)obj);
1493: case O_P:
1494: /* must be a variable */
1495: opname = conv_rule(VR((P)obj)->name);
1496: return opname[0]=='-';
1497: default:
1498: /* ??? */
1499: len = estimate_length(CO,obj);
1500: opname = (char *)MALLOC_ATOMIC(len+1);
1501: soutput_init(opname);
1502: sprintexpr(CO,obj);
1503: return opname[0]=='-';
1504: }
1.35 noro 1505: }
1506: default:
1507: return 0;
1.14 noro 1508: }
1.47 noro 1509: }
1510:
1511: FNODE flatten_fnode(FNODE,char *);
1512:
1.48 noro 1513: void Pflatten_quote(NODE arg,Obj *rp)
1.47 noro 1514: {
1515: FNODE f;
1516: QUOTE q;
1517:
1.48 noro 1518: if ( !ARG0(arg) || OID((Obj)ARG0(arg)) != O_QUOTE )
1519: *rp = (Obj)ARG0(arg);
1520: else {
1521: f = flatten_fnode(BDY((QUOTE)ARG0(arg)),BDY((STRING)ARG1(arg)));
1522: MKQUOTE(q,f);
1523: *rp = (Obj)q;
1524: }
1525: }
1526:
1527: void Pquote_to_funargs(NODE arg,LIST *rp)
1528: {
1529: fid_spec_p spec;
1530: QUOTE q;
1531: QUOTEARG qa;
1532: FNODE f;
1533: STRING s;
1534: QUOTE r;
1535: int i;
1536: Q id,a;
1.49 noro 1537: LIST l;
1538: NODE t0,t,w,u,u0;
1.48 noro 1539:
1540: q = (QUOTE)ARG0(arg);
1541: if ( !q || OID(q) != O_QUOTE )
1542: error("quote_to_funargs : invalid argument");
1543: f = BDY(q);
1544: if ( !f ) {
1545: MKLIST(*rp,0);
1546: return;
1547: }
1548: get_fid_spec(f->id,&spec);
1549: if ( !spec )
1550: error("quote_to_funargs : not supported yet");
1551: t0 = 0;
1552: STOQ((int)f->id,id);
1553: NEXTNODE(t0,t);
1554: BDY(t) = (pointer)id;
1555: for ( i = 0; spec->type[i] != A_end; i++ ) {
1556: NEXTNODE(t0,t);
1557: switch ( spec->type[i] ) {
1558: case A_fnode:
1559: MKQUOTE(r,(FNODE)f->arg[i]);
1560: BDY(t) = (pointer)r;
1561: break;
1562: case A_int:
1563: STOQ((int)f->arg[i],a);
1564: BDY(t) = (pointer)a;
1565: break;
1566: case A_str:
1567: MKSTR(s,(char *)f->arg[i]);
1568: BDY(t) = (pointer)s;
1569: break;
1570: case A_internal:
1571: BDY(t) = (pointer)f->arg[i];
1572: break;
1.49 noro 1573: case A_node:
1574: w = (NODE)f->arg[i];
1575: for ( u0 = 0; w; w = NEXT(w) ){
1576: NEXTNODE(u0,u);
1577: MKQUOTE(r,(FNODE)BDY(w));
1578: BDY(u) = (pointer)r;
1579: }
1580: if ( u0 ) NEXT(u) = 0;
1581: MKLIST(l,u0);
1582: BDY(t) = (pointer)l;
1583: break;
1.48 noro 1584: default:
1585: MKQUOTEARG(qa,spec->type[i],f->arg[i]);
1586: BDY(t) = (pointer)qa;
1587: break;
1588: }
1589: }
1590: if ( t0 ) NEXT(t) = 0;
1591: MKLIST(*rp,t0);
1592: }
1593:
1594: void Pfunargs_to_quote(NODE arg,QUOTE *rp)
1595: {
1596: fid_spec_p spec;
1597: QUOTE q;
1598: QUOTEARG qa;
1599: FNODE f;
1600: STRING s;
1.49 noro 1601: QUOTE r,b;
1.48 noro 1602: int i;
1603: LIST l;
1604: fid id;
1605: Obj a;
1.49 noro 1606: NODE t0,t,u0,u,w;
1.48 noro 1607:
1608: l = (LIST)ARG0(arg);
1609: if ( !l || OID(l) != O_LIST || !(t=BDY(l)) )
1610: error("funargs_to_quote : invalid argument");
1611: t = BDY(l);
1612: id = (fid)QTOS((Q)BDY(t)); t = NEXT(t);
1613: get_fid_spec(id,&spec);
1614: if ( !spec )
1615: error("funargs_to_quote : not supported yet");
1616: for ( i = 0; spec->type[i] != A_end; i++ );
1617: NEWFNODE(f,i);
1618: f->id = id;
1619: for ( i = 0; spec->type[i] != A_end; i++, t = NEXT(t) ) {
1620: if ( !t )
1621: error("funargs_to_quote : argument mismatch");
1622: a = (Obj)BDY(t);
1623: switch ( spec->type[i] ) {
1624: case A_fnode:
1625: if ( !a || OID(a) != O_QUOTE )
1626: error("funargs_to_quote : invalid argument");
1627: f->arg[i] = BDY((QUOTE)a);
1628: break;
1629: case A_int:
1630: if ( !INT(a) )
1631: error("funargs_to_quote : invalid argument");
1632: f->arg[i] = (pointer)QTOS((Q)a);
1633: break;
1634: case A_str:
1635: if ( !a || OID(a) != O_STR )
1636: error("funargs_to_quote : invalid argument");
1637: f->arg[i] = (pointer)BDY((STRING)a);
1638: break;
1639: case A_internal:
1640: f->arg[i] = (pointer)a;
1.49 noro 1641: break;
1642: case A_node:
1643: if ( !a || OID(a) != O_LIST )
1644: error("funargs_to_quote : invalid argument");
1645: u0 = 0;
1646: for ( w = BDY((LIST)a); w; w = NEXT(w) ) {
1647: NEXTNODE(u0,u);
1648: b = (QUOTE)BDY(w);
1649: if ( !b || OID(b) != O_QUOTE )
1650: error("funargs_to_quote : invalid argument");
1651: BDY(u) = BDY(b);
1652: }
1653: if ( u0 ) NEXT(u) = 0;
1654: f->arg[i] = (pointer)u0;
1.48 noro 1655: break;
1656: default:
1657: if ( !a || OID(a) != O_QUOTEARG ||
1658: ((QUOTEARG)a)->type != spec->type[i] )
1659: error("funargs_to_quote : invalid argument");
1660: f->arg[i] = BDY((QUOTEARG)a);
1661: break;
1662: }
1663: }
1664: MKQUOTE(*rp,f);
1.1 noro 1665: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>