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