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