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