Annotation of OpenXM_contrib2/asir2000/io/cpexpr.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/io/cpexpr.c,v 1.1.1.1 1999/11/10 08:12:30 noro Exp $ */
2: #include "ca.h"
3: #include "parse.h"
4: #include "base.h"
5:
6: #define TAIL
7: #define PUTS(s) (total_length+=strlen(s))
8: #define PRINTN length_n
9: /* #define PRINTBF length_bf */
10: #define PRINTBF
11: #define PRINTCPLX length_cplx
12: #define PRINTV length_v
13: #define PRINTEXPR length_expr
14: #define PRINTNUM length_num
15: #define PRINTP length_p
16: #define PRINTR length_r
17: #define PRINTLIST length_list
18: #define PRINTVECT length_vect
19: #define PRINTMAT length_mat
20: #define PRINTSTR length_str
21: #define PRINTCOMP length_comp
22: #define PRINTDP length_dp
23:
24: #if defined(THINK_C)
25: void PRINTEXPR(VL,pointer);
26: void PRINTNUM(Num);
27: void PRINTV(VL,V);
28: void PRINTN(N);
29: void PRINTP(VL,P);
30: void PRINTR(VL,R);
31: void PRINTLIST(VL,LIST);
32: void PRINTVECT(VL,VECT);
33: void PRINTMAT(VL,MAT);
34: void PRINTSTR(STRING);
35: void PRINTCOMP(VL,COMP);
36: void PRINTDP(VL,DP);
37: void PRINTCPLX(C);
38: #else
39: void PRINTEXPR();
40: void PRINTNUM();
41: void PRINTV();
42: void PRINTN();
43: void PRINTP();
44: void PRINTR();
45: void PRINTLIST();
46: void PRINTVECT();
47: void PRINTMAT();
48: void PRINTSTR();
49: void PRINTCOMP();
50: void PRINTDP();
51: void PRINTCPLX();
52: #endif
53:
54: static int total_length;
55:
56: int estimate_length(vl,p)
57: VL vl;
58: pointer p;
59: {
60: total_length = 0;
61: PRINTEXPR(vl,p);
62: return total_length;
63: }
64:
65: void PRINTEXPR(vl,p)
66: VL vl;
67: pointer p;
68: {
69: if ( !p ) {
70: total_length++;
71: return;
72: }
73:
74: switch ( OID(p) ) {
75: case O_N:
76: PRINTNUM((Num)p); break;
77: case O_P:
78: PRINTP(vl,(P)p); break;
79: case O_R:
80: PRINTR(vl,(R)p); break;
81: case O_LIST:
82: PRINTLIST(vl,(LIST)p); break;
83: case O_VECT:
84: PRINTVECT(vl,(VECT)p); break;
85: case O_MAT:
86: PRINTMAT(vl,(MAT)p); break;
87: case O_STR:
88: PRINTSTR((STRING)p); break;
89: case O_COMP:
90: PRINTCOMP(vl,(COMP)p); break;
91: case O_DP:
92: PRINTDP(vl,(DP)p); break;
93: default:
94: break;
95: }
96: }
97:
98: void PRINTN(n)
99: N n;
100: {
101: double ceil();
102:
103: if ( !n )
104: PUTS("0");
105: else
106: total_length += (int)(ceil(0.31*((double)(BSH*PL(n))))+1);
107: }
108:
109: void PRINTNUM(q)
110: Num q;
111: {
112: if ( !q ) {
113: PUTS("0");
114: return;
115: }
116: switch ( NID(q) ) {
117: case N_Q:
118: if ( SGN((Q)q) == -1 )
119: PUTS("-");
120: PRINTN(NM((Q)q));
121: if ( !INT((Q)q) ) {
122: PUTS("/"); PRINTN(DN((Q)q));
123: }
124: break;
125: case N_R:
126: total_length += 20; /* XXX */
127: break;
128: case N_A:
129: PUTS("("); PRINTR(ALG,(R)BDY((Alg)q)); PUTS(")");
130: break;
131: #if PARI
132: case N_B:
133: PRINTBF((BF)q); break;
134: #endif
135: case N_C:
136: PRINTCPLX((C)q); break;
137: case N_M:
138: total_length += 10; /* XXX */
139: break;
140: }
141: }
142:
143: void PRINTCPLX(a)
144: C a;
145: {
146: PUTS("(");
147: if ( a->r )
148: PRINTNUM(a->r);
149: if ( a->i ) {
150: if ( a->r && (compnum(0,a->i,0) > 0) )
151: PUTS("+");
152: PRINTNUM(a->i); PUTS("*@i");
153: }
154: PUTS(")");
155: }
156:
157: void PRINTP(vl,p)
158: VL vl;
159: P p;
160: {
161: V v;
162: DCP dc;
163: int t;
164:
165: if ( !p )
166: PUTS("0");
167: else if ( NUM(p) )
168: PRINTNUM((Num)p);
169: else
170: for ( dc = DC(p), v = VR(p); dc; dc = NEXT(dc) ) {
171: if ( !DEG(dc) )
172: PRINTP(vl,COEF(dc));
173: else {
174: if ( NUM(COEF(dc)) && UNIQ((Q)COEF(dc)) ) {
175: ;
176: } else if ( NUM(COEF(dc)) && MUNIQ((Q)COEF(dc)) )
177: PUTS("-");
178: else if ( NUM(COEF(dc)) || !NEXT(DC(COEF(dc)))) {
179: PRINTP(vl,COEF(dc)); PUTS("*");
180: } else {
181: PUTS("("); PRINTP(vl,COEF(dc)); PUTS(")*");
182: }
183: PRINTV(vl,v);
184: if ( cmpq(DEG(dc),ONE) ) {
185: PUTS("^");
186: if ( INT(DEG(dc)) && SGN(DEG(dc))>0 )
187: PRINTNUM((Num)DEG(dc));
188: else {
189: PUTS("("); PRINTNUM((Num)DEG(dc)); PUTS(")");
190: }
191: }
192: }
193: if ( NEXT(dc) ) {
194: P t;
195:
196: t = COEF(NEXT(dc));
197: if (!DEG(NEXT(dc))) {
198: if ( NUM(t) ) {
199: if ( !mmono(t) )
200: PUTS("+");
201: } else {
202: if (!mmono(COEF(DC(t))))
203: PUTS("+");
204: }
205: } else {
206: if ( !mmono(t) )
207: PUTS("+");
208: }
209: }
210: }
211: }
212:
213: void PRINTV(vl,v)
214: VL vl;
215: V v;
216: {
217: PF pf;
218: PFAD ad;
219: int i;
220:
221: if ( NAME(v) )
222: PUTS(NAME(v));
223: else if ( (vid)v->attr == V_PF ) {
224: pf = ((PFINS)v->priv)->pf; ad = ((PFINS)v->priv)->ad;
225: if ( !strcmp(NAME(pf),"pow") ) {
226: PUTS("("); PRINTR(vl,(R)ad[0].arg); PUTS(")^(");
227: PRINTR(vl,(R)ad[1].arg); PUTS(")");
228: } else if ( !pf->argc )
229: PUTS(NAME(pf));
230: else {
231: for ( i = 0; i < pf->argc; i++ )
232: if ( ad[i].d )
233: break;
234: if ( i < pf->argc ) {
235: PUTS(NAME(pf));
236: total_length += 11; /* XXX */
237: for ( i = 1; i < pf->argc; i++ ) {
238: total_length += 11; /* XXX */
239: }
240: PUTS(")(");
241: } else {
242: PUTS(NAME(pf));
243: total_length += 1; /* XXX */
244: }
245: PRINTR(vl,(R)ad[0].arg);
246: for ( i = 1; i < pf->argc; i++ ) {
247: PUTS(","); PRINTR(vl,(R)ad[i].arg);
248: }
249: PUTS(")");
250: }
251: }
252: }
253:
254: void PRINTR(vl,a)
255: VL vl;
256: R a;
257: {
258: if ( !a )
259: PUTS("0");
260: else
261: switch (OID(a)) {
262: case O_N: case O_P:
263: PRINTP(vl,(P)a); break;
264: default:
265: PUTS("("); PRINTP(vl,NM((R)a)); PUTS(")/("); PRINTP(vl,DN((R)a)); PUTS(")");
266: break;
267: }
268: }
269:
270: void PRINTVECT(vl,vect)
271: VL vl;
272: VECT vect;
273: {
274: int i;
275: pointer *ptr;
276:
277: PUTS("[ ");
278: for ( i = 0, ptr = BDY(vect); i < vect->len; i++ ) {
279: PRINTEXPR(vl,ptr[i]); PUTS(" ");
280: }
281: PUTS("]");
282: }
283:
284: void PRINTMAT(vl,mat)
285: VL vl;
286: MAT mat;
287: {
288: int i,j,r,c;
289: pointer *ptr;
290:
291: for ( i = 0, r = mat->row, c = mat->col; i < r; i++ ) {
292: PUTS("[ ");
293: for ( j = 0, ptr = BDY(mat)[i]; j < c; j++ ) {
294: PRINTEXPR(vl,ptr[j]); PUTS(" ");
295: }
296: PUTS("]");
297: if ( i < r - 1 )
298: PUTS("\n");
299: }
300: }
301:
302: void PRINTLIST(vl,list)
303: VL vl;
304: LIST list;
305: {
306: NODE tnode;
307:
308: PUTS("[");
309: for ( tnode = (NODE)list->body; tnode; tnode = NEXT(tnode) ) {
310: PRINTEXPR(vl,tnode->body);
311: if ( NEXT(tnode) )
312: PUTS(",");
313: }
314: PUTS("]");
315: }
316:
317: void PRINTSTR(str)
318: STRING str;
319: {
320: char *p;
321:
322: for ( p = BDY(str); *p; p++ )
323: if ( *p == '"' )
324: PUTS("\"");
325: else {
326: total_length += 1;
327: }
328: }
329:
330: void PRINTCOMP(vl,c)
331: VL vl;
332: COMP c;
333: {
334: int n,i;
335:
336: n = getcompsize((int)c->type);
337: PUTS("{");
338: for ( i = 0; i < n; i++ ) {
339: PRINTEXPR(vl,(pointer)c->member[i]);
340: if ( i < n-1 )
341: PUTS(",");
342: }
343: PUTS("}");
344: }
345:
346: void PRINTDP(vl,d)
347: VL vl;
348: DP d;
349: {
350: int n,i;
351: MP m;
352: DL dl;
353:
354: for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
355: PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
356: for ( i = 0, dl = m->dl; i < n-1; i++ ) {
357: total_length += 11;
358: }
359: total_length += 10;
360: PUTS(">>");
361: if ( NEXT(m) )
362: PUTS("+");
363: }
364: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>