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