Annotation of OpenXM_contrib2/asir2000/io/cpexpr.c, Revision 1.5
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.5 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/cpexpr.c,v 1.4 2000/08/22 05:04:17 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"
53:
1.2 noro 54: extern int hex_output,fortran_output;
55:
56: #define PRINTHAT (fortran_output?PUTS("**"):PUTS("^"))
57:
1.1 noro 58: #define TAIL
59: #define PUTS(s) (total_length+=strlen(s))
60: #define PRINTN length_n
1.2 noro 61: #define PRINTBF length_bf
1.1 noro 62: #define PRINTCPLX length_cplx
1.2 noro 63: #define PRINTLM length_lm
64: #define PRINTUP2 length_up2
1.1 noro 65: #define PRINTV length_v
66: #define PRINTEXPR length_expr
67: #define PRINTNUM length_num
68: #define PRINTP length_p
69: #define PRINTR length_r
70: #define PRINTLIST length_list
71: #define PRINTVECT length_vect
72: #define PRINTMAT length_mat
73: #define PRINTSTR length_str
74: #define PRINTCOMP length_comp
75: #define PRINTDP length_dp
1.2 noro 76: #define PRINTUI length_ui
77: #define PRINTGF2MAT length_gf2mat
78: #define PRINTGFMMAT length_gfmmat
1.5 ! noro 79: #define PRINTBYTEARRAY length_bytearray
1.2 noro 80: #define PRINTERR length_err
81: #define PRINTLF length_lf
82: #define PRINTLOP length_lop
83: #define PRINTFOP length_fop
84: #define PRINTEOP length_eop
85: #define PRINTQOP length_qop
86: #define PRINTUP length_up
1.1 noro 87:
88: void PRINTEXPR();
89: void PRINTNUM();
1.2 noro 90: void PRINTN();
1.1 noro 91: void PRINTV();
92: void PRINTP();
93: void PRINTR();
94: void PRINTLIST();
95: void PRINTVECT();
96: void PRINTMAT();
97: void PRINTSTR();
98: void PRINTCOMP();
99: void PRINTDP();
1.2 noro 100: void PRINTUI();
101: void PRINTGF2MAT();
102: void PRINTGFMMAT();
1.5 ! noro 103: void PRINTBYTEARRAY();
1.2 noro 104: void PRINTERR();
1.1 noro 105: void PRINTCPLX();
1.2 noro 106: void PRINTLM();
107: void PRINTLF();
108: void PRINTUP2();
1.1 noro 109:
110: static int total_length;
111:
112: int estimate_length(vl,p)
113: VL vl;
114: pointer p;
115: {
116: total_length = 0;
117: PRINTEXPR(vl,p);
118: return total_length;
119: }
120:
1.2 noro 121: #if PARI
122: void PRINTBF(a)
123: BF a;
124: {
125: char *str;
126: char *GENtostr();
127:
128: str = GENtostr(a->body);
129: total_length += strlen(str);
130: free(str);
131: }
132: #endif
133:
1.1 noro 134: void PRINTEXPR(vl,p)
135: VL vl;
136: pointer p;
137: {
138: if ( !p ) {
139: total_length++;
140: return;
141: }
142:
143: switch ( OID(p) ) {
144: case O_N:
145: PRINTNUM((Num)p); break;
146: case O_P:
147: PRINTP(vl,(P)p); break;
148: case O_R:
149: PRINTR(vl,(R)p); break;
150: case O_LIST:
151: PRINTLIST(vl,(LIST)p); break;
152: case O_VECT:
153: PRINTVECT(vl,(VECT)p); break;
154: case O_MAT:
155: PRINTMAT(vl,(MAT)p); break;
156: case O_STR:
157: PRINTSTR((STRING)p); break;
158: case O_COMP:
159: PRINTCOMP(vl,(COMP)p); break;
160: case O_DP:
161: PRINTDP(vl,(DP)p); break;
1.2 noro 162: case O_USINT:
163: PRINTUI(vl,(USINT)p); break;
164: case O_GF2MAT:
165: PRINTGF2MAT(vl,(GF2MAT)p); break;
166: case O_ERR:
167: PRINTERR(vl,(ERR)p); break;
168: case O_MATHCAP:
169: PRINTLIST(vl,((MATHCAP)p)->body); break;
170: case O_F:
171: PRINTLF(vl,(F)p); break;
172: case O_GFMMAT:
173: PRINTGFMMAT(vl,(GFMMAT)p); break;
1.5 ! noro 174: case O_BYTEARRAY:
! 175: PRINTBYTEARRAY(vl,(BYTEARRAY)p); break;
1.1 noro 176: default:
177: break;
178: }
179: }
180:
181: void PRINTN(n)
182: N n;
183: {
184: double ceil();
185:
186: if ( !n )
187: PUTS("0");
1.2 noro 188: else if ( hex_output )
189: total_length += 2+(int)(PL(n)*8);
1.1 noro 190: else
191: total_length += (int)(ceil(0.31*((double)(BSH*PL(n))))+1);
192: }
193:
194: void PRINTNUM(q)
195: Num q;
196: {
197: if ( !q ) {
198: PUTS("0");
199: return;
200: }
201: switch ( NID(q) ) {
202: case N_Q:
203: if ( SGN((Q)q) == -1 )
204: PUTS("-");
205: PRINTN(NM((Q)q));
206: if ( !INT((Q)q) ) {
207: PUTS("/"); PRINTN(DN((Q)q));
208: }
209: break;
210: case N_R:
211: total_length += 20; /* XXX */
212: break;
213: case N_A:
214: PUTS("("); PRINTR(ALG,(R)BDY((Alg)q)); PUTS(")");
215: break;
216: #if PARI
217: case N_B:
218: PRINTBF((BF)q); break;
219: #endif
220: case N_C:
221: PRINTCPLX((C)q); break;
222: case N_M:
1.2 noro 223: total_length += 11; /* XXX */
224: break;
225: case N_LM:
226: PRINTN(((LM)q)->body); break;
227: case N_GF2N:
228: if ( hex_output )
229: PRINTN((N)(((GF2N)q)->body));
230: else
231: PRINTUP2(((GF2N)q)->body);
232: break;
233: case N_GFPN:
234: PRINTUP((UP)(((GFPN)q)->body));
235: break;
236: default:
1.1 noro 237: break;
238: }
239: }
240:
241: void PRINTCPLX(a)
242: C a;
243: {
244: PUTS("(");
245: if ( a->r )
246: PRINTNUM(a->r);
247: if ( a->i ) {
248: if ( a->r && (compnum(0,a->i,0) > 0) )
249: PUTS("+");
250: PRINTNUM(a->i); PUTS("*@i");
251: }
252: PUTS(")");
253: }
254:
255: void PRINTP(vl,p)
256: VL vl;
257: P p;
258: {
259: V v;
260: DCP dc;
261: int t;
262:
263: if ( !p )
264: PUTS("0");
265: else if ( NUM(p) )
266: PRINTNUM((Num)p);
267: else
268: for ( dc = DC(p), v = VR(p); dc; dc = NEXT(dc) ) {
269: if ( !DEG(dc) )
270: PRINTP(vl,COEF(dc));
271: else {
272: if ( NUM(COEF(dc)) && UNIQ((Q)COEF(dc)) ) {
273: ;
274: } else if ( NUM(COEF(dc)) && MUNIQ((Q)COEF(dc)) )
275: PUTS("-");
276: else if ( NUM(COEF(dc)) || !NEXT(DC(COEF(dc)))) {
277: PRINTP(vl,COEF(dc)); PUTS("*");
278: } else {
279: PUTS("("); PRINTP(vl,COEF(dc)); PUTS(")*");
280: }
281: PRINTV(vl,v);
282: if ( cmpq(DEG(dc),ONE) ) {
1.2 noro 283: PRINTHAT;
1.1 noro 284: if ( INT(DEG(dc)) && SGN(DEG(dc))>0 )
285: PRINTNUM((Num)DEG(dc));
286: else {
287: PUTS("("); PRINTNUM((Num)DEG(dc)); PUTS(")");
288: }
289: }
290: }
291: if ( NEXT(dc) ) {
292: P t;
293:
294: t = COEF(NEXT(dc));
295: if (!DEG(NEXT(dc))) {
296: if ( NUM(t) ) {
297: if ( !mmono(t) )
298: PUTS("+");
299: } else {
300: if (!mmono(COEF(DC(t))))
301: PUTS("+");
302: }
303: } else {
304: if ( !mmono(t) )
305: PUTS("+");
306: }
307: }
308: }
309: }
310:
1.2 noro 311: extern int hideargs;
312:
1.1 noro 313: void PRINTV(vl,v)
314: VL vl;
315: V v;
316: {
317: PF pf;
318: PFAD ad;
319: int i;
320:
321: if ( NAME(v) )
322: PUTS(NAME(v));
323: else if ( (vid)v->attr == V_PF ) {
324: pf = ((PFINS)v->priv)->pf; ad = ((PFINS)v->priv)->ad;
325: if ( !strcmp(NAME(pf),"pow") ) {
1.2 noro 326: PUTS("("); PRINTR(vl,(R)ad[0].arg); PUTS(")"); PRINTHAT; PUTS("(");
1.1 noro 327: PRINTR(vl,(R)ad[1].arg); PUTS(")");
328: } else if ( !pf->argc )
329: PUTS(NAME(pf));
330: else {
1.2 noro 331: if ( hideargs ) {
332: for ( i = 0; i < pf->argc; i++ )
333: if ( ad[i].d )
334: break;
335: if ( i < pf->argc ) {
336: PUTS(NAME(pf));
1.1 noro 337: total_length += 11; /* XXX */
1.2 noro 338: for ( i = 1; i < pf->argc; i++ ) {
339: total_length += 11; /* XXX */
340: }
341: PUTS("}");
342: } else {
343: PUTS(NAME(pf));
344: total_length += 1; /* XXX */
1.1 noro 345: }
346: } else {
1.2 noro 347: for ( i = 0; i < pf->argc; i++ )
348: if ( ad[i].d )
349: break;
350: if ( i < pf->argc ) {
351: PUTS(NAME(pf));
352: total_length += 11; /* XXX */
353: for ( i = 1; i < pf->argc; i++ ) {
354: total_length += 11; /* XXX */
355: }
356: PUTS(")(");
357: } else {
358: PUTS(NAME(pf));
359: total_length += 1; /* XXX */
360: }
361: PRINTR(vl,(R)ad[0].arg);
362: for ( i = 1; i < pf->argc; i++ ) {
363: PUTS(","); PRINTR(vl,(R)ad[i].arg);
364: }
365: PUTS(")");
1.1 noro 366: }
367: }
368: }
369: }
370:
371: void PRINTR(vl,a)
372: VL vl;
373: R a;
374: {
375: if ( !a )
376: PUTS("0");
377: else
378: switch (OID(a)) {
379: case O_N: case O_P:
380: PRINTP(vl,(P)a); break;
381: default:
382: PUTS("("); PRINTP(vl,NM((R)a)); PUTS(")/("); PRINTP(vl,DN((R)a)); PUTS(")");
383: break;
384: }
385: }
386:
387: void PRINTVECT(vl,vect)
388: VL vl;
389: VECT vect;
390: {
391: int i;
392: pointer *ptr;
393:
394: PUTS("[ ");
395: for ( i = 0, ptr = BDY(vect); i < vect->len; i++ ) {
396: PRINTEXPR(vl,ptr[i]); PUTS(" ");
397: }
398: PUTS("]");
399: }
400:
401: void PRINTMAT(vl,mat)
402: VL vl;
403: MAT mat;
404: {
405: int i,j,r,c;
406: pointer *ptr;
407:
408: for ( i = 0, r = mat->row, c = mat->col; i < r; i++ ) {
409: PUTS("[ ");
410: for ( j = 0, ptr = BDY(mat)[i]; j < c; j++ ) {
411: PRINTEXPR(vl,ptr[j]); PUTS(" ");
412: }
413: PUTS("]");
414: if ( i < r - 1 )
415: PUTS("\n");
416: }
417: }
418:
419: void PRINTLIST(vl,list)
420: VL vl;
421: LIST list;
422: {
423: NODE tnode;
424:
425: PUTS("[");
426: for ( tnode = (NODE)list->body; tnode; tnode = NEXT(tnode) ) {
427: PRINTEXPR(vl,tnode->body);
428: if ( NEXT(tnode) )
429: PUTS(",");
430: }
431: PUTS("]");
432: }
433:
434: void PRINTSTR(str)
435: STRING str;
436: {
437: char *p;
438:
439: for ( p = BDY(str); *p; p++ )
440: if ( *p == '"' )
441: PUTS("\"");
442: else {
443: total_length += 1;
444: }
445: }
446:
447: void PRINTCOMP(vl,c)
448: VL vl;
449: COMP c;
450: {
451: int n,i;
452:
453: n = getcompsize((int)c->type);
454: PUTS("{");
455: for ( i = 0; i < n; i++ ) {
456: PRINTEXPR(vl,(pointer)c->member[i]);
457: if ( i < n-1 )
458: PUTS(",");
459: }
460: PUTS("}");
461: }
462:
463: void PRINTDP(vl,d)
464: VL vl;
465: DP d;
466: {
467: int n,i;
468: MP m;
469: DL dl;
470:
471: for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
472: PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
473: for ( i = 0, dl = m->dl; i < n-1; i++ ) {
474: total_length += 11;
475: }
476: total_length += 10;
477: PUTS(">>");
478: if ( NEXT(m) )
479: PUTS("+");
1.2 noro 480: }
481: }
482:
483: void PRINTUI(vl,u)
484: VL vl;
485: USINT u;
486: {
487: total_length += 10;
488: }
489:
490: void PRINTGF2MAT(vl,mat)
491: VL vl;
492: GF2MAT mat;
493: {
494: int row,col,w,i,j,k,m;
495: unsigned int t;
496: unsigned int **b;
497:
498: row = mat->row;
499: col = mat->col;
500: w = (col+BSH-1)/BSH;
501: b = mat->body;
502: for ( i = 0; i < row; i++ ) {
503: for ( j = 0, m = 0; j < w; j++ ) {
504: t = b[i][j];
505: for ( k = 0; m < col && k < BSH; k++, m++ )
506: if ( t & (1<<k) )
507: PUTS("1");
508: else
509: PUTS("0");
510: }
511: PUTS("\n");
512: }
513: }
514:
515: void PRINTGFMMAT(vl,mat)
516: VL vl;
517: GFMMAT mat;
518: {
519: int row,col,i,j;
520: unsigned int t;
521: unsigned int **b;
522:
523: row = mat->row;
524: col = mat->col;
525: b = mat->body;
526: for ( i = 0; i < row; i++ ) {
527: PUTS("[");
528: for ( j = 0; j < col; j++ ) {
529: total_length += 10; /* XXX */
530: }
531: PUTS("]\n");
532: }
1.5 ! noro 533: }
! 534:
! 535: void PRINTBYTEARRAY(vl,array)
! 536: VL vl;
! 537: BYTEARRAY array;
! 538: {
! 539: /* |xx xx ... xx| */
! 540: return 1+3*array->len;
1.2 noro 541: }
542:
543: void PRINTERR(vl,e)
544: VL vl;
545: ERR e;
546: {
547: PUTS("error("); PRINTEXPR(vl,e->body); PUTS(")");
548: }
549:
550: void PRINTUP2(p)
551: UP2 p;
552: {
553: int d,i;
554:
555: if ( !p ) {
556: PUTS("0");
557: } else {
558: d = degup2(p);
559: PUTS("(");
560: if ( !d ) {
561: PUTS("1");
562: } else if ( d == 1 ) {
563: PUTS("@");
564: } else {
565: PRINTHAT;
566: total_length += 11;
567: }
568: for ( i = d-1; i >= 0; i-- ) {
569: if ( p->b[i/BSH] & (1<<(i%BSH)) )
570: if ( !i ) {
571: PUTS("+1");
572: } else if ( i == 1 ) {
573: PUTS("+@");
574: } else {
575: PRINTHAT;
576: total_length += 12;
577: }
578: }
579: PUTS(")");
580: }
581: }
582:
583: void PRINTLF(vl,f)
584: VL vl;
585: F f;
586: {
587: switch ( FOP(f) ) {
588: case AL_TRUE:
589: PUTS("@true");
590: break;
591: case AL_FALSE:
592: PUTS("@false");
593: break;
594:
595: case AL_OR: case AL_AND:
596: PRINTFOP(vl,f); break;
597: case AL_NOT: case AL_IMPL: case AL_REPL: case AL_EQUIV:
598: PRINTEOP(vl,f); break;
599:
600: case AL_EQUAL: case AL_NEQ: case AL_LESSP:
601: case AL_GREATERP: case AL_LEQ: case AL_GEQ:
602: PRINTLOP(vl,f); break;
603:
604: case AL_EX: case AL_ALL:
605: PRINTQOP(vl,f); break;
606: default:
607: break;
608: }
609: }
610:
611: PRINTFOP(vl,f)
612: VL vl;
613: F f;
614: {
615: char *op;
616: NODE n;
617:
618: op = FOP(f)==AL_OR?" @|| ":" @&& ";
619: n = FJARG(f);
620: PUTS("("); PRINTEXPR(vl,BDY(n)); PUTS(")");
621: for ( n = NEXT(n); n; n = NEXT(n) ) {
622: PUTS(op); PUTS("("); PRINTEXPR(vl,BDY(n)); PUTS(")");
623: }
624: }
625:
626: PRINTEOP(vl,f)
627: VL vl;
628: F f;
629: {
630: oFOP op;
631: char *sop;
632:
633: if ( (op = FOP(f)) == AL_NOT ) {
634: PUTS("(@! "); PRINTEXPR(vl,(Obj)FARG(f)); PUTS(")"); return;
635: }
636: switch ( op ) {
637: case AL_IMPL:
638: sop = " @impl "; break;
639: case AL_REPL:
640: sop = " @repl "; break;
641: case AL_EQUIV:
642: sop = " @equiv "; break;
643: default:
644: break;
645: }
646: PUTS("(");
647: PRINTEXPR(vl,(Obj)FLHS(f));
648: PUTS(sop);
649: PRINTEXPR(vl,(Obj)FRHS(f));
650: PUTS(")");
651: }
652:
653: PRINTLOP(vl,f)
654: VL vl;
655: F f;
656: {
657: char *op;
658:
659: switch ( FOP(f) ) {
660: case AL_EQUAL:
661: op = " @== "; break;
662: case AL_NEQ:
663: op = " @!= "; break;
664: case AL_LESSP:
665: op = " @< "; break;
666: case AL_GREATERP:
667: op = " @> "; break;
668: case AL_LEQ:
669: op = " @<= "; break;
670: case AL_GEQ:
671: op = " @>= "; break;
672: default:
673: error("PRINTLOP : invalid operator");
674: break;
675: }
676: PRINTEXPR(vl,(Obj)FPL(f)); PUTS(op); PUTS("0");
677: }
678:
679: PRINTQOP(vl,f)
680: VL vl;
681: F f;
682: {
683: char *op;
684:
685: op = FOP(f)==AL_EX?"ex":"all";
686: PUTS(op); PUTS(NAME(FQVR(f)));
687: total_length += 2;
688: PRINTEXPR(vl,(Obj)FQMAT(f)); PUTS(")");
689: }
690:
691: PRINTUP(n)
692: UP n;
693: {
694: int i,d;
695:
696: if ( !n )
697: PUTS("0");
698: else if ( !n->d )
699: PRINTNUM(n->c[0]);
700: else {
701: d = n->d;
702: PUTS("(");
703: if ( !d ) {
704: PRINTNUM(n->c[d]);
705: } else if ( d == 1 ) {
706: PRINTNUM(n->c[d]);
707: PUTS("*@p");
708: } else {
709: PRINTNUM(n->c[d]);
710: PRINTHAT;
711: total_length += 13;
712: }
713: for ( i = d-1; i >= 0; i-- ) {
714: if ( n->c[i] ) {
715: PUTS("+("); PRINTNUM(n->c[i]); PUTS(")");
716: if ( i >= 2 ) {
717: PRINTHAT;
718: total_length += 13;
719: } else if ( i == 1 )
720: PUTS("*@p");
721: }
722: }
723: PUTS(")");
1.1 noro 724: }
725: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>