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