Annotation of OpenXM_contrib2/asir2000/io/cpexpr.c, Revision 1.21
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.21 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/cpexpr.c,v 1.20 2004/03/17 02:23:45 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.17 noro 88: #define PRINTSYMBOL length_SYMBOL
89: #define PRINTRANGE length_RANGE
1.18 noro 90: #define PRINTTB length_TB
1.2 noro 91: #define PRINTERR length_err
92: #define PRINTLF length_lf
93: #define PRINTLOP length_lop
94: #define PRINTFOP length_fop
95: #define PRINTEOP length_eop
96: #define PRINTQOP length_qop
97: #define PRINTUP length_up
1.12 noro 98: #define PRINTUM length_um
99: #define PRINTSF length_sf
1.19 noro 100: #define PRINTFARGS length_fargs
101: #define PRINTFNODENODE length_fnodenode
102: #define PRINTFNODE length_fnode
1.1 noro 103:
1.21 ! noro 104: #include "pexpr_body.c"
! 105:
! 106: /* special functions for estimating length */
1.1 noro 107:
108: static int total_length;
109:
1.13 noro 110: int estimate_length(VL vl,pointer p)
1.1 noro 111: {
112: total_length = 0;
113: PRINTEXPR(vl,p);
114: return total_length;
115: }
116:
1.15 ohara 117: #if defined(PARI)
1.13 noro 118: void PRINTBF(BF a)
1.2 noro 119: {
120: char *str;
1.7 noro 121: char *GENtostr0();
122: void myoutbrute();
1.2 noro 123:
1.7 noro 124: if ( double_output ) {
125: str = GENtostr0(a->body,myoutbrute);
126: } else {
127: str = GENtostr(a->body);
128: }
1.2 noro 129: total_length += strlen(str);
130: free(str);
131: }
132: #endif
133:
1.13 noro 134: void PRINTNUM(Num q)
1.1 noro 135: {
136: if ( !q ) {
137: PUTS("0");
138: return;
139: }
140: switch ( NID(q) ) {
141: case N_Q:
142: if ( SGN((Q)q) == -1 )
143: PUTS("-");
144: PRINTN(NM((Q)q));
145: if ( !INT((Q)q) ) {
146: PUTS("/"); PRINTN(DN((Q)q));
147: }
148: break;
149: case N_R:
1.7 noro 150: if ( double_output )
1.8 noro 151: total_length += 400+real_digit; /* XXX */
1.7 noro 152: else
1.8 noro 153: total_length += 20+real_digit; /* XXX */
1.1 noro 154: break;
155: case N_A:
156: PUTS("("); PRINTR(ALG,(R)BDY((Alg)q)); PUTS(")");
157: break;
1.15 ohara 158: #if defined(PARI)
1.1 noro 159: case N_B:
160: PRINTBF((BF)q); break;
161: #endif
162: case N_C:
163: PRINTCPLX((C)q); break;
164: case N_M:
1.2 noro 165: total_length += 11; /* XXX */
166: break;
167: case N_LM:
168: PRINTN(((LM)q)->body); break;
169: case N_GF2N:
170: if ( hex_output )
171: PRINTN((N)(((GF2N)q)->body));
172: else
173: PRINTUP2(((GF2N)q)->body);
174: break;
175: case N_GFPN:
176: PRINTUP((UP)(((GFPN)q)->body));
1.9 noro 177: break;
178: case N_GFS:
1.10 noro 179: total_length += 13; /* XXX */
1.2 noro 180: break;
1.12 noro 181: case N_GFSN:
182: PRINTUM(BDY((GFSN)q));
183: break;
1.2 noro 184: default:
1.1 noro 185: break;
186: }
187: }
188:
1.13 noro 189: void PRINTV(VL vl,V v)
1.1 noro 190: {
191: PF pf;
192: PFAD ad;
193: int i;
194:
195: if ( NAME(v) )
196: PUTS(NAME(v));
197: else if ( (vid)v->attr == V_PF ) {
198: pf = ((PFINS)v->priv)->pf; ad = ((PFINS)v->priv)->ad;
199: if ( !strcmp(NAME(pf),"pow") ) {
1.14 noro 200: PUTS("(("); PRINTR(vl,(R)ad[0].arg); PUTS(")"); PRINTHAT; PUTS("(");
201: PRINTR(vl,(R)ad[1].arg); PUTS("))");
1.1 noro 202: } else if ( !pf->argc )
203: PUTS(NAME(pf));
204: else {
1.2 noro 205: if ( hideargs ) {
206: for ( i = 0; i < pf->argc; i++ )
207: if ( ad[i].d )
208: break;
209: if ( i < pf->argc ) {
210: PUTS(NAME(pf));
1.1 noro 211: total_length += 11; /* XXX */
1.2 noro 212: for ( i = 1; i < pf->argc; i++ ) {
213: total_length += 11; /* XXX */
214: }
215: PUTS("}");
216: } else {
217: PUTS(NAME(pf));
218: total_length += 1; /* XXX */
1.1 noro 219: }
220: } else {
1.2 noro 221: for ( i = 0; i < pf->argc; i++ )
222: if ( ad[i].d )
223: break;
224: if ( i < pf->argc ) {
225: PUTS(NAME(pf));
226: total_length += 11; /* XXX */
227: for ( i = 1; i < pf->argc; i++ ) {
228: total_length += 11; /* XXX */
229: }
230: PUTS(")(");
231: } else {
232: PUTS(NAME(pf));
233: total_length += 1; /* XXX */
234: }
235: PRINTR(vl,(R)ad[0].arg);
236: for ( i = 1; i < pf->argc; i++ ) {
237: PUTS(","); PRINTR(vl,(R)ad[i].arg);
238: }
239: PUTS(")");
1.1 noro 240: }
241: }
242: }
243: }
244:
1.21 ! noro 245: void PRINTN(N n)
1.1 noro 246: {
1.21 ! noro 247: double ceil();
! 248:
! 249: if ( !n )
1.1 noro 250: PUTS("0");
1.21 ! noro 251: else if ( hex_output )
! 252: total_length += 2+(int)(PL(n)*8);
1.1 noro 253: else
1.21 ! noro 254: total_length += (int)(ceil(0.31*((double)(BSH*PL(n))))+1);
1.1 noro 255: }
256:
1.13 noro 257: void PRINTSTR(STRING str)
1.1 noro 258: {
259: char *p;
260:
261: for ( p = BDY(str); *p; p++ )
262: if ( *p == '"' )
263: PUTS("\"");
264: else {
265: total_length += 1;
266: }
267: }
268:
1.13 noro 269: void PRINTDP(VL vl,DP d)
1.1 noro 270: {
271: int n,i;
272: MP m;
273: DL dl;
274:
275: for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
276: PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
277: for ( i = 0, dl = m->dl; i < n-1; i++ ) {
278: total_length += 11;
279: }
280: total_length += 10;
281: PUTS(">>");
282: if ( NEXT(m) )
283: PUTS("+");
1.2 noro 284: }
285: }
286:
1.13 noro 287: void PRINTUI(VL vl,USINT u)
1.2 noro 288: {
289: total_length += 10;
290: }
291:
1.13 noro 292: void PRINTGFMMAT(VL vl,GFMMAT mat)
1.2 noro 293: {
294: int row,col,i,j;
295: unsigned int **b;
296:
297: row = mat->row;
298: col = mat->col;
299: b = mat->body;
300: for ( i = 0; i < row; i++ ) {
301: PUTS("[");
302: for ( j = 0; j < col; j++ ) {
303: total_length += 10; /* XXX */
304: }
305: PUTS("]\n");
306: }
1.5 noro 307: }
308:
1.13 noro 309: void PRINTBYTEARRAY(VL vl,BYTEARRAY array)
1.5 noro 310: {
311: /* |xx xx ... xx| */
1.6 noro 312: total_length += 1+3*array->len;
1.11 noro 313: }
314:
1.19 noro 315: extern int print_quote;
316:
1.13 noro 317: void PRINTQUOTE(VL vl,QUOTE quote)
1.11 noro 318: {
1.19 noro 319: LIST list;
320:
321: if ( print_quote == 2 ) {
1.21 ! noro 322: PRINTFNODE(BDY(quote),0);
1.19 noro 323: } else if ( print_quote == 1 ) {
324: /* XXX */
325: fnodetotree(BDY(quote),&list);
326: PRINTEXPR(vl,(Obj)list);
327: } else {
328: /* <...quoted...> */
329: total_length += 20;
330: }
1.17 noro 331: }
332:
333: void PRINTSYMBOL(SYMBOL sym)
334: {
335: total_length += strlen(sym->name);
336: }
337:
1.18 noro 338: void PRINTTB(VL vl,TB p)
339: {
340: int i;
341:
342: for ( i = 0; i < p->next; i++ ) {
343: total_length += strlen(p->body[i]);
344: }
1.2 noro 345: }
346:
1.13 noro 347: void PRINTUP2(UP2 p)
1.2 noro 348: {
349: int d,i;
350:
351: if ( !p ) {
352: PUTS("0");
353: } else {
354: d = degup2(p);
355: PUTS("(");
356: if ( !d ) {
357: PUTS("1");
358: } else if ( d == 1 ) {
359: PUTS("@");
360: } else {
361: PRINTHAT;
362: total_length += 11;
363: }
364: for ( i = d-1; i >= 0; i-- ) {
365: if ( p->b[i/BSH] & (1<<(i%BSH)) )
366: if ( !i ) {
367: PUTS("+1");
368: } else if ( i == 1 ) {
369: PUTS("+@");
370: } else {
371: PRINTHAT;
372: total_length += 12;
373: }
374: }
375: PUTS(")");
376: }
377: }
378:
1.13 noro 379: void PRINTQOP(VL vl,F f)
1.2 noro 380: {
381: char *op;
382:
383: op = FOP(f)==AL_EX?"ex":"all";
384: PUTS(op); PUTS(NAME(FQVR(f)));
385: total_length += 2;
386: PRINTEXPR(vl,(Obj)FQMAT(f)); PUTS(")");
387: }
388:
1.13 noro 389: void PRINTUP(UP n)
1.2 noro 390: {
391: int i,d;
392:
393: if ( !n )
394: PUTS("0");
395: else if ( !n->d )
396: PRINTNUM(n->c[0]);
397: else {
398: d = n->d;
399: PUTS("(");
400: if ( !d ) {
401: PRINTNUM(n->c[d]);
402: } else if ( d == 1 ) {
403: PRINTNUM(n->c[d]);
404: PUTS("*@p");
405: } else {
406: PRINTNUM(n->c[d]);
407: PRINTHAT;
408: total_length += 13;
409: }
410: for ( i = d-1; i >= 0; i-- ) {
411: if ( n->c[i] ) {
412: PUTS("+("); PRINTNUM(n->c[i]); PUTS(")");
413: if ( i >= 2 ) {
414: PRINTHAT;
415: total_length += 13;
416: } else if ( i == 1 )
417: PUTS("*@p");
418: }
419: }
420: PUTS(")");
1.1 noro 421: }
1.12 noro 422: }
423:
1.13 noro 424: void PRINTUM(UM n)
1.12 noro 425: {
426: int i,d;
427:
428: if ( !n )
429: PUTS("0");
430: else if ( !n->d )
431: PRINTSF(n->c[0]);
432: else {
433: d = n->d;
434: PUTS("(");
435: if ( !d ) {
436: PRINTSF(n->c[d]);
437: } else if ( d == 1 ) {
438: PRINTSF(n->c[d]);
439: PUTS("*@s");
440: } else {
441: PRINTSF(n->c[d]);
442: PUTS("*@s"); PRINTHAT; total_length += 13;
443: }
444: for ( i = d-1; i >= 0; i-- ) {
445: if ( n->c[i] ) {
446: PUTS("+("); PRINTSF(n->c[i]); PUTS(")");
447: if ( i >= 2 ) {
448: PUTS("*@s"); PRINTHAT; total_length += 13;
449: } else if ( i == 1 )
450: PUTS("*@s");
451: }
452: }
453: PUTS(")");
454: }
455: }
456:
1.13 noro 457: void PRINTSF(unsigned int i)
1.12 noro 458: {
459: if ( !i ) {
460: PUTS("0");
461: } else
462: total_length += 15;
1.19 noro 463:
1.21 ! noro 464: #ifndef CPRINT
! 465: #define CPRINT
! 466: #endif
1.1 noro 467: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>