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