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