Annotation of OpenXM_contrib2/asir2000/io/cpexpr.c, Revision 1.26
1.3 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.4 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3 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.
1.26 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/cpexpr.c,v 1.25 2005/11/16 23:42:54 noro Exp $
1.3 noro 48: */
1.1 noro 49: #include "ca.h"
50: #include "parse.h"
1.2 noro 51: #include "al.h"
1.1 noro 52: #include "base.h"
1.16 noro 53: #if PARI
54: #include "genpari.h"
55: #endif
1.1 noro 56:
1.21 noro 57: #ifndef CPRINT
58: #define CPRINT
59: #endif
60:
1.8 noro 61: extern int hex_output,fortran_output,double_output,real_digit;
1.21 noro 62: extern int hideargs,outputstyle;
63: static int total_length;
1.2 noro 64:
1.1 noro 65: #define TAIL
66: #define PUTS(s) (total_length+=strlen(s))
67: #define PRINTN length_n
1.2 noro 68: #define PRINTBF length_bf
1.1 noro 69: #define PRINTCPLX length_cplx
1.2 noro 70: #define PRINTLM length_lm
71: #define PRINTUP2 length_up2
1.1 noro 72: #define PRINTV length_v
73: #define PRINTEXPR length_expr
74: #define PRINTNUM length_num
75: #define PRINTP length_p
76: #define PRINTR length_r
77: #define PRINTLIST length_list
78: #define PRINTVECT length_vect
79: #define PRINTMAT length_mat
80: #define PRINTSTR length_str
81: #define PRINTCOMP length_comp
82: #define PRINTDP length_dp
1.2 noro 83: #define PRINTUI length_ui
84: #define PRINTGF2MAT length_gf2mat
85: #define PRINTGFMMAT length_gfmmat
1.5 noro 86: #define PRINTBYTEARRAY length_bytearray
1.11 noro 87: #define PRINTQUOTE length_QUOTE
1.23 noro 88: #define PRINTQUOTEARG length_QUOTEARG
1.17 noro 89: #define PRINTSYMBOL length_SYMBOL
90: #define PRINTRANGE length_RANGE
1.18 noro 91: #define PRINTTB length_TB
1.22 noro 92: #define PRINTDPV length_DPV
1.25 noro 93: #define PRINTNBP length_nbp
1.2 noro 94: #define PRINTERR length_err
95: #define PRINTLF length_lf
96: #define PRINTLOP length_lop
97: #define PRINTFOP length_fop
98: #define PRINTEOP length_eop
99: #define PRINTQOP length_qop
100: #define PRINTUP length_up
1.12 noro 101: #define PRINTUM length_um
102: #define PRINTSF length_sf
1.19 noro 103: #define PRINTFARGS length_fargs
104: #define PRINTFNODENODE length_fnodenode
105: #define PRINTFNODE length_fnode
1.1 noro 106:
1.21 noro 107: #include "pexpr_body.c"
108:
109: /* special functions for estimating length */
1.1 noro 110:
111: static int total_length;
112:
1.13 noro 113: int estimate_length(VL vl,pointer p)
1.1 noro 114: {
115: total_length = 0;
116: PRINTEXPR(vl,p);
117: return total_length;
118: }
119:
1.15 ohara 120: #if defined(PARI)
1.13 noro 121: void PRINTBF(BF a)
1.2 noro 122: {
123: char *str;
1.7 noro 124: char *GENtostr0();
125: void myoutbrute();
1.2 noro 126:
1.7 noro 127: if ( double_output ) {
128: str = GENtostr0(a->body,myoutbrute);
129: } else {
130: str = GENtostr(a->body);
131: }
1.2 noro 132: total_length += strlen(str);
133: free(str);
134: }
135: #endif
136:
1.13 noro 137: void PRINTNUM(Num q)
1.1 noro 138: {
1.24 noro 139: DAlg d;
140: DP nm;
141: Q dn;
142:
1.1 noro 143: if ( !q ) {
144: PUTS("0");
145: return;
146: }
147: switch ( NID(q) ) {
148: case N_Q:
149: if ( SGN((Q)q) == -1 )
150: PUTS("-");
151: PRINTN(NM((Q)q));
152: if ( !INT((Q)q) ) {
153: PUTS("/"); PRINTN(DN((Q)q));
154: }
155: break;
156: case N_R:
1.7 noro 157: if ( double_output )
1.8 noro 158: total_length += 400+real_digit; /* XXX */
1.7 noro 159: else
1.8 noro 160: total_length += 20+real_digit; /* XXX */
1.1 noro 161: break;
162: case N_A:
163: PUTS("("); PRINTR(ALG,(R)BDY((Alg)q)); PUTS(")");
164: break;
1.15 ohara 165: #if defined(PARI)
1.1 noro 166: case N_B:
167: PRINTBF((BF)q); break;
168: #endif
169: case N_C:
170: PRINTCPLX((C)q); break;
171: case N_M:
1.2 noro 172: total_length += 11; /* XXX */
173: break;
174: case N_LM:
175: PRINTN(((LM)q)->body); break;
176: case N_GF2N:
177: if ( hex_output )
178: PRINTN((N)(((GF2N)q)->body));
179: else
180: PRINTUP2(((GF2N)q)->body);
181: break;
182: case N_GFPN:
183: PRINTUP((UP)(((GFPN)q)->body));
1.9 noro 184: break;
185: case N_GFS:
1.10 noro 186: total_length += 13; /* XXX */
1.2 noro 187: break;
1.12 noro 188: case N_GFSN:
189: PRINTUM(BDY((GFSN)q));
1.24 noro 190: break;
191: case N_DA:
192: d = (DAlg)q;
193: nm = d->nm;
194: dn = d->dn;
195: if ( SGN((Q)dn) == -1 ) PUTS("-");
196: PUTS("(");
197: PRINTDP(CO,((DAlg)q)->nm);
198: PUTS(")");
199: if ( !UNIN(NM(dn)) ) {
200: PUTS("/");
201: PRINTN(NM(dn));
202: }
1.12 noro 203: break;
1.2 noro 204: default:
1.1 noro 205: break;
206: }
207: }
208:
1.13 noro 209: void PRINTV(VL vl,V v)
1.1 noro 210: {
211: PF pf;
212: PFAD ad;
213: int i;
214:
215: if ( NAME(v) )
216: PUTS(NAME(v));
217: else if ( (vid)v->attr == V_PF ) {
218: pf = ((PFINS)v->priv)->pf; ad = ((PFINS)v->priv)->ad;
219: if ( !strcmp(NAME(pf),"pow") ) {
1.14 noro 220: PUTS("(("); PRINTR(vl,(R)ad[0].arg); PUTS(")"); PRINTHAT; PUTS("(");
221: PRINTR(vl,(R)ad[1].arg); PUTS("))");
1.1 noro 222: } else if ( !pf->argc )
223: PUTS(NAME(pf));
224: else {
1.2 noro 225: if ( hideargs ) {
226: for ( i = 0; i < pf->argc; i++ )
227: if ( ad[i].d )
228: break;
229: if ( i < pf->argc ) {
230: PUTS(NAME(pf));
1.1 noro 231: total_length += 11; /* XXX */
1.2 noro 232: for ( i = 1; i < pf->argc; i++ ) {
233: total_length += 11; /* XXX */
234: }
235: PUTS("}");
236: } else {
237: PUTS(NAME(pf));
238: total_length += 1; /* XXX */
1.1 noro 239: }
240: } else {
1.2 noro 241: for ( i = 0; i < pf->argc; i++ )
242: if ( ad[i].d )
243: break;
244: if ( i < pf->argc ) {
245: PUTS(NAME(pf));
246: total_length += 11; /* XXX */
247: for ( i = 1; i < pf->argc; i++ ) {
248: total_length += 11; /* XXX */
249: }
250: PUTS(")(");
251: } else {
252: PUTS(NAME(pf));
253: total_length += 1; /* XXX */
254: }
255: PRINTR(vl,(R)ad[0].arg);
256: for ( i = 1; i < pf->argc; i++ ) {
257: PUTS(","); PRINTR(vl,(R)ad[i].arg);
258: }
259: PUTS(")");
1.1 noro 260: }
261: }
262: }
263: }
264:
1.21 noro 265: void PRINTN(N n)
1.1 noro 266: {
1.21 noro 267: double ceil();
268:
269: if ( !n )
1.1 noro 270: PUTS("0");
1.21 noro 271: else if ( hex_output )
272: total_length += 2+(int)(PL(n)*8);
1.1 noro 273: else
1.21 noro 274: total_length += (int)(ceil(0.31*((double)(BSH*PL(n))))+1);
1.1 noro 275: }
276:
1.13 noro 277: void PRINTSTR(STRING str)
1.1 noro 278: {
279: char *p;
280:
281: for ( p = BDY(str); *p; p++ )
282: if ( *p == '"' )
283: PUTS("\"");
284: else {
285: total_length += 1;
286: }
287: }
288:
1.13 noro 289: void PRINTDP(VL vl,DP d)
1.1 noro 290: {
291: int n,i;
292: MP m;
293: DL dl;
294:
295: for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
296: PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
297: for ( i = 0, dl = m->dl; i < n-1; i++ ) {
298: total_length += 11;
299: }
300: total_length += 10;
301: PUTS(">>");
302: if ( NEXT(m) )
303: PUTS("+");
1.2 noro 304: }
305: }
306:
1.13 noro 307: void PRINTUI(VL vl,USINT u)
1.2 noro 308: {
309: total_length += 10;
310: }
311:
1.13 noro 312: void PRINTGFMMAT(VL vl,GFMMAT mat)
1.2 noro 313: {
314: int row,col,i,j;
315: unsigned int **b;
316:
317: row = mat->row;
318: col = mat->col;
319: b = mat->body;
320: for ( i = 0; i < row; i++ ) {
321: PUTS("[");
322: for ( j = 0; j < col; j++ ) {
323: total_length += 10; /* XXX */
324: }
325: PUTS("]\n");
326: }
1.5 noro 327: }
328:
1.13 noro 329: void PRINTBYTEARRAY(VL vl,BYTEARRAY array)
1.5 noro 330: {
331: /* |xx xx ... xx| */
1.6 noro 332: total_length += 1+3*array->len;
1.11 noro 333: }
334:
1.19 noro 335: extern int print_quote;
336:
1.13 noro 337: void PRINTQUOTE(VL vl,QUOTE quote)
1.11 noro 338: {
1.19 noro 339: LIST list;
340:
341: if ( print_quote == 2 ) {
1.21 noro 342: PRINTFNODE(BDY(quote),0);
1.19 noro 343: } else if ( print_quote == 1 ) {
344: /* XXX */
345: fnodetotree(BDY(quote),&list);
346: PRINTEXPR(vl,(Obj)list);
347: } else {
348: /* <...quoted...> */
349: total_length += 20;
350: }
1.23 noro 351: }
352:
353: void PRINTQUOTEARG(VL vl,QUOTEARG quote)
354: {
355: /* XXX */
356: /* <...quoted...> */
357: total_length += 20;
1.17 noro 358: }
359:
360: void PRINTSYMBOL(SYMBOL sym)
361: {
362: total_length += strlen(sym->name);
363: }
364:
1.18 noro 365: void PRINTTB(VL vl,TB p)
366: {
367: int i;
368:
369: for ( i = 0; i < p->next; i++ ) {
370: total_length += strlen(p->body[i]);
371: }
1.2 noro 372: }
373:
1.13 noro 374: void PRINTUP2(UP2 p)
1.2 noro 375: {
376: int d,i;
377:
378: if ( !p ) {
379: PUTS("0");
380: } else {
381: d = degup2(p);
382: PUTS("(");
383: if ( !d ) {
384: PUTS("1");
385: } else if ( d == 1 ) {
386: PUTS("@");
387: } else {
388: PRINTHAT;
389: total_length += 11;
390: }
391: for ( i = d-1; i >= 0; i-- ) {
392: if ( p->b[i/BSH] & (1<<(i%BSH)) )
393: if ( !i ) {
394: PUTS("+1");
395: } else if ( i == 1 ) {
396: PUTS("+@");
397: } else {
398: PRINTHAT;
399: total_length += 12;
400: }
401: }
402: PUTS(")");
403: }
404: }
405:
1.13 noro 406: void PRINTQOP(VL vl,F f)
1.2 noro 407: {
408: char *op;
409:
410: op = FOP(f)==AL_EX?"ex":"all";
411: PUTS(op); PUTS(NAME(FQVR(f)));
412: total_length += 2;
413: PRINTEXPR(vl,(Obj)FQMAT(f)); PUTS(")");
414: }
415:
1.13 noro 416: void PRINTUP(UP n)
1.2 noro 417: {
418: int i,d;
419:
420: if ( !n )
421: PUTS("0");
422: else if ( !n->d )
423: PRINTNUM(n->c[0]);
424: else {
425: d = n->d;
426: PUTS("(");
427: if ( !d ) {
428: PRINTNUM(n->c[d]);
429: } else if ( d == 1 ) {
430: PRINTNUM(n->c[d]);
431: PUTS("*@p");
432: } else {
433: PRINTNUM(n->c[d]);
434: PRINTHAT;
435: total_length += 13;
436: }
437: for ( i = d-1; i >= 0; i-- ) {
438: if ( n->c[i] ) {
439: PUTS("+("); PRINTNUM(n->c[i]); PUTS(")");
440: if ( i >= 2 ) {
441: PRINTHAT;
442: total_length += 13;
443: } else if ( i == 1 )
444: PUTS("*@p");
445: }
446: }
447: PUTS(")");
1.1 noro 448: }
1.12 noro 449: }
450:
1.13 noro 451: void PRINTUM(UM n)
1.12 noro 452: {
453: int i,d;
454:
455: if ( !n )
456: PUTS("0");
457: else if ( !n->d )
458: PRINTSF(n->c[0]);
459: else {
460: d = n->d;
461: PUTS("(");
462: if ( !d ) {
463: PRINTSF(n->c[d]);
464: } else if ( d == 1 ) {
465: PRINTSF(n->c[d]);
466: PUTS("*@s");
467: } else {
468: PRINTSF(n->c[d]);
469: PUTS("*@s"); PRINTHAT; total_length += 13;
470: }
471: for ( i = d-1; i >= 0; i-- ) {
472: if ( n->c[i] ) {
473: PUTS("+("); PRINTSF(n->c[i]); PUTS(")");
474: if ( i >= 2 ) {
475: PUTS("*@s"); PRINTHAT; total_length += 13;
476: } else if ( i == 1 )
477: PUTS("*@s");
478: }
479: }
480: PUTS(")");
1.25 noro 481: }
482: }
483:
484: void PRINTNBP(VL vl,NBP p)
485: {
486: NODE t;
487: NBM m;
488: int d,i;
489: unsigned int *b;
490: if ( !p ) PUTS("0");
491: else {
492: for ( t = BDY(p); t; t = NEXT(t) ) {
493: m = (NBM)BDY(t);
494: PRINTEXPR(vl,(Obj)m->c);
495: d = m->d;
496: b = m->b;
1.26 ! noro 497: if ( d )
! 498: for ( i = 0; i < d; i++ ) {
! 499: if ( NBM_GET(b,i) ) PUTS("x");
! 500: else PUTS("y");
! 501: }
! 502: else PUTS("1");
1.25 noro 503: if ( NEXT(t) ) PUTS("+");
504: }
1.12 noro 505: }
506: }
507:
1.13 noro 508: void PRINTSF(unsigned int i)
1.12 noro 509: {
510: if ( !i ) {
511: PUTS("0");
512: } else
513: total_length += 15;
1.19 noro 514:
1.21 noro 515: #ifndef CPRINT
516: #define CPRINT
517: #endif
1.1 noro 518: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>