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